Commit
·
dadd261
1
Parent(s):
7e0d592
predict fxn
Browse files
app.py
CHANGED
|
@@ -48,27 +48,27 @@ class_name = ['Apple___Apple_scab',
|
|
| 48 |
'Tomato___Tomato_mosaic_virus',
|
| 49 |
'Tomato___healthy']
|
| 50 |
|
|
|
|
|
|
|
| 51 |
def predict_disease(image):
|
| 52 |
"""
|
| 53 |
-
Predict plant disease from uploaded image
|
| 54 |
"""
|
| 55 |
try:
|
| 56 |
-
#
|
| 57 |
-
image = image.
|
|
|
|
| 58 |
input_arr = tf.keras.preprocessing.image.img_to_array(image)
|
| 59 |
-
input_arr = np.array([input_arr]) # Convert single image to
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Make prediction
|
| 63 |
prediction = model.predict(input_arr)
|
| 64 |
result_index = np.argmax(prediction)
|
| 65 |
confidence = prediction[0][result_index]
|
| 66 |
-
|
| 67 |
-
# Get disease name
|
| 68 |
disease_name = class_name[result_index]
|
| 69 |
-
|
| 70 |
return f"Disease: {disease_name}\nConfidence: {confidence:.2%}"
|
| 71 |
-
|
| 72 |
except Exception as e:
|
| 73 |
return f"Error: {str(e)}"
|
| 74 |
|
|
|
|
| 48 |
'Tomato___Tomato_mosaic_virus',
|
| 49 |
'Tomato___healthy']
|
| 50 |
|
| 51 |
+
|
| 52 |
+
|
| 53 |
def predict_disease(image):
|
| 54 |
"""
|
| 55 |
+
Predict plant disease from uploaded image using same preprocessing as your working cv2 method
|
| 56 |
"""
|
| 57 |
try:
|
| 58 |
+
# Convert PIL image to array like load_img method
|
| 59 |
+
image = image.convert("RGB") # Ensure 3 channels
|
| 60 |
+
image = image.resize((128, 128)) # Resize
|
| 61 |
input_arr = tf.keras.preprocessing.image.img_to_array(image)
|
| 62 |
+
input_arr = np.array([input_arr]) # Convert single image to batch
|
| 63 |
+
|
| 64 |
+
# Predict
|
|
|
|
| 65 |
prediction = model.predict(input_arr)
|
| 66 |
result_index = np.argmax(prediction)
|
| 67 |
confidence = prediction[0][result_index]
|
|
|
|
|
|
|
| 68 |
disease_name = class_name[result_index]
|
| 69 |
+
|
| 70 |
return f"Disease: {disease_name}\nConfidence: {confidence:.2%}"
|
| 71 |
+
|
| 72 |
except Exception as e:
|
| 73 |
return f"Error: {str(e)}"
|
| 74 |
|