Demo-grade on-device classifier. Not a plant-pathologist substitute. Retake if confidence is low or the leaf is not centred.
1import json
2from pathlib import Path
3import numpy as np
4from PIL import Image
5import tensorflow as tf
6
7def preprocess_centercrop(path: str, imgsz: int = 640) -> np.ndarray:
8 im = Image.open(path).convert("RGB")
9 w, h = im.size
10 scale = imgsz / min(w, h)
11 nw, nh = int(round(w * scale)), int(round(h * scale))
12 im = im.resize((nw, nh), Image.BILINEAR)
13 left, top = (nw - imgsz) // 2, (nh - imgsz) // 2
14 im = im.crop((left, top, left + imgsz, top + imgsz))
15 return (np.asarray(im, dtype=np.float32) / 255.0)[None, ...]
16
17labels = json.loads(Path("labels.json").read_text(encoding="utf-8"))
18it = tf.lite.Interpreter(model_path="model.tflite")
19it.allocate_tensors()
20inp, out = it.get_input_details()[0], it.get_output_details()[0]
21it.set_tensor(inp["index"], preprocess_centercrop("leaf.jpg", labels["imgsz"]))
22it.invoke()
23p = it.get_tensor(out["index"])[0]
24i = int(p.argmax())
25print(labels["names"][i], float(p[i]))
KrishokChat Bengali advisory LLM / RAG is
not this classifier. See
RaiyanKhaan/KrishokChat-Advisory-System and arXiv:2606.29243 (Reza & Shahid).
1@software{ahmed2026chashibhai_rice_cls,
2 author = {Ahmed, Shakil},
3 title = {ChashiBhAI Rice Disease Classifier},
4 year = {2026},
5 url = {https://huggingface.co/Shaq2/chashibhai-rice-disease-cls}
6}