Commit
·
0da4931
1
Parent(s):
9a8ae6b
Add model and app
Browse files- app.py +84 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import requests
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
|
| 5 |
+
# Load your model (you'll need to upload trained_modela.keras to your space)
|
| 6 |
+
model = tf.keras.models.load_model('trained_modela.keras')
|
| 7 |
+
|
| 8 |
+
class_name = ['Apple___Apple_scab',
|
| 9 |
+
'Apple___Black_rot',
|
| 10 |
+
'Apple___Cedar_apple_rust',
|
| 11 |
+
'Apple___healthy',
|
| 12 |
+
'Blueberry___healthy',
|
| 13 |
+
'Cherry_(including_sour)___Powdery_mildew',
|
| 14 |
+
'Cherry_(including_sour)___healthy',
|
| 15 |
+
'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',
|
| 16 |
+
'Corn_(maize)___Common_rust_',
|
| 17 |
+
'Corn_(maize)___Northern_Leaf_Blight',
|
| 18 |
+
'Corn_(maize)___healthy',
|
| 19 |
+
'Grape___Black_rot',
|
| 20 |
+
'Grape___Esca_(Black_Measles)',
|
| 21 |
+
'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
|
| 22 |
+
'Grape___healthy',
|
| 23 |
+
'Orange___Haunglongbing_(Citrus_greening)',
|
| 24 |
+
'Peach___Bacterial_spot',
|
| 25 |
+
'Peach___healthy',
|
| 26 |
+
'Pepper,_bell___Bacterial_spot',
|
| 27 |
+
'Pepper,_bell___healthy',
|
| 28 |
+
'Potato___Early_blight',
|
| 29 |
+
'Potato___Late_blight',
|
| 30 |
+
'Potato___healthy',
|
| 31 |
+
'Raspberry___healthy',
|
| 32 |
+
'Soybean___healthy',
|
| 33 |
+
'Squash___Powdery_mildew',
|
| 34 |
+
'Strawberry___Leaf_scorch',
|
| 35 |
+
'Strawberry___healthy',
|
| 36 |
+
'Tomato___Bacterial_spot',
|
| 37 |
+
'Tomato___Early_blight',
|
| 38 |
+
'Tomato___Late_blight',
|
| 39 |
+
'Tomato___Leaf_Mold',
|
| 40 |
+
'Tomato___Septoria_leaf_spot',
|
| 41 |
+
'Tomato___Spider_mites Two-spotted_spider_mite',
|
| 42 |
+
'Tomato___Target_Spot',
|
| 43 |
+
'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
|
| 44 |
+
'Tomato___Tomato_mosaic_virus',
|
| 45 |
+
'Tomato___healthy']
|
| 46 |
+
|
| 47 |
+
def predict_disease(image):
|
| 48 |
+
"""
|
| 49 |
+
Predict plant disease from uploaded image
|
| 50 |
+
"""
|
| 51 |
+
try:
|
| 52 |
+
# Preprocess the image
|
| 53 |
+
image = image.resize((128, 128))
|
| 54 |
+
input_arr = tf.keras.preprocessing.image.img_to_array(image)
|
| 55 |
+
input_arr = np.array([input_arr]) # Convert single image to a batch
|
| 56 |
+
input_arr = input_arr / 255.0 # Normalize if your model expects it
|
| 57 |
+
|
| 58 |
+
# Make prediction
|
| 59 |
+
prediction = model.predict(input_arr)
|
| 60 |
+
result_index = np.argmax(prediction)
|
| 61 |
+
confidence = prediction[0][result_index]
|
| 62 |
+
|
| 63 |
+
# Get disease name
|
| 64 |
+
disease_name = class_name[result_index]
|
| 65 |
+
|
| 66 |
+
return f"Disease: {disease_name}\nConfidence: {confidence:.2%}"
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Error: {str(e)}"
|
| 70 |
+
|
| 71 |
+
# Create Gradio interface
|
| 72 |
+
iface = gr.Interface(
|
| 73 |
+
fn=predict_disease,
|
| 74 |
+
inputs=gr.Image(type="pil", label="Upload Plant Image"),
|
| 75 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
| 76 |
+
title="Plant Disease Detection API",
|
| 77 |
+
description="Upload an image of a plant leaf to detect diseases",
|
| 78 |
+
examples=[
|
| 79 |
+
# You can add example images here
|
| 80 |
+
]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorFlow: 2.19.0
|
| 2 |
+
numpy: 2.2.6
|
| 3 |
+
gradio: 5.44.1
|
| 4 |
+
pillow: 10.4.0
|