Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,20 +55,35 @@ POTATO_MODEL_PATH = "pot_ato_model.tflite"
|
|
| 55 |
POTATO_CLASS_NAMES = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
|
| 56 |
|
| 57 |
|
| 58 |
-
# ---
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
TOMATO_CLASS_NAMES = [
|
| 62 |
-
'
|
| 63 |
-
'
|
| 64 |
-
'
|
| 65 |
-
'
|
| 66 |
-
'
|
| 67 |
-
'
|
| 68 |
-
'
|
| 69 |
-
'
|
| 70 |
-
'
|
| 71 |
-
'Tomato___healthy'
|
| 72 |
]
|
| 73 |
|
| 74 |
|
|
@@ -107,6 +122,12 @@ tomato_interpreter.allocate_tensors()
|
|
| 107 |
tomato_input_details = tomato_interpreter.get_input_details()
|
| 108 |
tomato_output_details = tomato_interpreter.get_output_details()
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# --- LOAD RICE TFLITE MODEL ---
|
| 111 |
rice_interpreter = tf.lite.Interpreter(model_path=RICE_MODEL_PATH)
|
| 112 |
rice_interpreter.allocate_tensors()
|
|
@@ -254,6 +275,46 @@ async def predict_potato(file: UploadFile = File(...)):
|
|
| 254 |
"confidence": confidence
|
| 255 |
}
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
# --- Tomato's Code ---
|
| 258 |
@app.post("/predict_tomato")
|
| 259 |
async def predict_tomato(file: UploadFile = File(...)):
|
|
|
|
| 55 |
POTATO_CLASS_NAMES = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
|
| 56 |
|
| 57 |
|
| 58 |
+
# --- LEMON CONFIGURATION ---
|
| 59 |
+
LEMON_MODEL_PATH = "lemon_disease_model.tflite"
|
| 60 |
+
|
| 61 |
+
# Replace these with the exact 9 lines from your lemon_labels.txt file
|
| 62 |
+
LEMON_CLASS_NAMES = [
|
| 63 |
+
"Anthracnose",
|
| 64 |
+
"Bacterial Blight",
|
| 65 |
+
"Citrus Canker",
|
| 66 |
+
"Curl Virus",
|
| 67 |
+
"Deficiency Leaf",
|
| 68 |
+
"Dry Leaf",
|
| 69 |
+
"Healthy Leaf",
|
| 70 |
+
"Sooty Mould",
|
| 71 |
+
"Spider Mites"
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
# --- Tomato CONFIGURATION ---
|
| 75 |
+
TOMATO_MODEL_PATH = "lemon_disease_model.tflite"
|
| 76 |
|
| 77 |
TOMATO_CLASS_NAMES = [
|
| 78 |
+
'Anthracnose',
|
| 79 |
+
'Bacterial Blight',
|
| 80 |
+
'Citrus Canker',
|
| 81 |
+
'Curl Virus',
|
| 82 |
+
'Deficiency Leaf',
|
| 83 |
+
'Dry Leaf',
|
| 84 |
+
'Healthy Leaf',
|
| 85 |
+
'Sooty Mould',
|
| 86 |
+
'Spider Mites'
|
|
|
|
| 87 |
]
|
| 88 |
|
| 89 |
|
|
|
|
| 122 |
tomato_input_details = tomato_interpreter.get_input_details()
|
| 123 |
tomato_output_details = tomato_interpreter.get_output_details()
|
| 124 |
|
| 125 |
+
# --- LOAD LEMON MODEL ---
|
| 126 |
+
lemon_interpreter = tf.lite.Interpreter(model_path=LEMON_MODEL_PATH)
|
| 127 |
+
lemon_interpreter.allocate_tensors()
|
| 128 |
+
lemon_input_details = lemon_interpreter.get_input_details()
|
| 129 |
+
lemon_output_details = lemon_interpreter.get_output_details()
|
| 130 |
+
|
| 131 |
# --- LOAD RICE TFLITE MODEL ---
|
| 132 |
rice_interpreter = tf.lite.Interpreter(model_path=RICE_MODEL_PATH)
|
| 133 |
rice_interpreter.allocate_tensors()
|
|
|
|
| 275 |
"confidence": confidence
|
| 276 |
}
|
| 277 |
|
| 278 |
+
@app.post("/predict_lemon")
|
| 279 |
+
async def predict_lemon(file: UploadFile = File(...)):
|
| 280 |
+
try:
|
| 281 |
+
# --- PREPROCESS IMAGE ---
|
| 282 |
+
image_data = await file.read()
|
| 283 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 284 |
+
|
| 285 |
+
if not is_leaf(image):
|
| 286 |
+
return JSONResponse(status_code=400, content={
|
| 287 |
+
"error": "Not a leaf",
|
| 288 |
+
"message": "Please upload a clear image of a PLANT leaf."
|
| 289 |
+
})
|
| 290 |
+
|
| 291 |
+
image = image.resize((224, 224))
|
| 292 |
+
|
| 293 |
+
# EfficientNetB0 Normalization
|
| 294 |
+
input_data = np.array(image).astype(np.float32)
|
| 295 |
+
input_data = np.expand_dims(input_data, axis=0)
|
| 296 |
+
|
| 297 |
+
# --- RUN INFERENCE ---
|
| 298 |
+
lemon_interpreter.set_tensor(lemon_input_details[0]['index'], input_data)
|
| 299 |
+
lemon_interpreter.invoke()
|
| 300 |
+
output_data = lemon_interpreter.get_tensor(lemon_output_details[0]['index'])
|
| 301 |
+
|
| 302 |
+
# --- PROCESS RESULTS ---
|
| 303 |
+
prediction = np.argmax(output_data[0])
|
| 304 |
+
confidence = float(np.max(output_data[0]))
|
| 305 |
+
predicted_class = LEMON_CLASS_NAMES[prediction]
|
| 306 |
+
|
| 307 |
+
display_name = predicted_class
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
return {
|
| 311 |
+
"class": display_name,
|
| 312 |
+
"confidence": confidence
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
except Exception as e:
|
| 316 |
+
return {"error": str(e), "message": "The code crashed before finishing the prediction."}
|
| 317 |
+
|
| 318 |
# --- Tomato's Code ---
|
| 319 |
@app.post("/predict_tomato")
|
| 320 |
async def predict_tomato(file: UploadFile = File(...)):
|