Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# Load your
|
| 7 |
-
model =
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
'
|
| 13 |
-
'
|
| 14 |
-
'
|
| 15 |
-
'
|
| 16 |
-
'
|
| 17 |
-
'
|
| 18 |
-
'
|
| 19 |
-
'
|
| 20 |
-
'
|
| 21 |
-
'
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Prediction function
|
| 26 |
-
def predict(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# --- PREPROCESSING STEP ---
|
| 31 |
-
# The input 'image' from Gradio is a NumPy array.
|
| 32 |
-
# IMPORTANT: You might need to change the resize dimensions to match your model's input shape.
|
| 33 |
-
# Common shapes are (224, 224) or (256, 256). I'll use (256, 256) as a default.
|
| 34 |
-
img = Image.fromarray(image).resize((256, 256))
|
| 35 |
-
img_array = np.array(img)
|
| 36 |
-
|
| 37 |
-
# Normalize the image (if you did this during training)
|
| 38 |
-
# Common normalization is to scale pixel values to [0, 1]
|
| 39 |
-
img_array = img_array / 255.0
|
| 40 |
-
|
| 41 |
-
# Add a batch dimension
|
| 42 |
-
img_batch = np.expand_dims(img_array, axis=0)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Get the index of the highest probability
|
| 48 |
-
predicted_class_index = np.argmax(prediction[0])
|
| 49 |
-
|
| 50 |
-
# Get the predicted class name
|
| 51 |
-
predicted_class_name = class_names[predicted_class_index]
|
| 52 |
-
|
| 53 |
-
# Get the confidence score
|
| 54 |
-
confidence = float(np.max(prediction[0]))
|
| 55 |
-
|
| 56 |
-
# Return a dictionary of labels and their confidences
|
| 57 |
-
return {predicted_class_name: confidence}
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
fn=predict,
|
| 62 |
-
inputs=gr.Image(label="Upload a
|
| 63 |
-
outputs=gr.
|
| 64 |
title="Crop Disease Prediction",
|
| 65 |
-
description="Upload an image of a crop leaf to predict its disease.
|
| 66 |
-
examples=[
|
| 67 |
-
# You can add paths to example images here if you have any
|
| 68 |
-
# ["path/to/your/example1.jpg"],
|
| 69 |
-
# ["path/to/your/example2.jpg"]
|
| 70 |
-
]
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Load your model
|
| 8 |
+
model = load_model("Model.hdf5")
|
| 9 |
|
| 10 |
+
# Class mapping (from your screenshot)
|
| 11 |
+
class_indices = {
|
| 12 |
+
'Apple___Apple_scab': 0,
|
| 13 |
+
'Apple___Black_rot': 1,
|
| 14 |
+
'Apple___Cedar_apple_rust': 2,
|
| 15 |
+
'Apple___healthy': 3,
|
| 16 |
+
'Blueberry___healthy': 4,
|
| 17 |
+
'Cherry_(including_sour)___Powdery_mildew': 5,
|
| 18 |
+
'Cherry_(including_sour)___healthy': 6,
|
| 19 |
+
'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot': 7,
|
| 20 |
+
'Corn_(maize)___Common_rust': 8,
|
| 21 |
+
'Corn_(maize)___Northern_Leaf_Blight': 9,
|
| 22 |
+
'Corn_(maize)___healthy': 10,
|
| 23 |
+
'Grape___Black_rot': 11,
|
| 24 |
+
'Grape___Esca_(Black_Measles)': 12,
|
| 25 |
+
'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)': 13,
|
| 26 |
+
'Grape___healthy': 14,
|
| 27 |
+
'Orange___Haunglongbing_(Citrus_greening)': 15,
|
| 28 |
+
'Peach___Bacterial_spot': 16,
|
| 29 |
+
'Peach___healthy': 17,
|
| 30 |
+
'Pepper,_bell___Bacterial_spot': 18,
|
| 31 |
+
'Pepper,_bell___healthy': 19,
|
| 32 |
+
'Potato___Early_blight': 20,
|
| 33 |
+
'Potato___Late_blight': 21,
|
| 34 |
+
'Potato___healthy': 22,
|
| 35 |
+
'Raspberry___healthy': 23,
|
| 36 |
+
'Soybean___healthy': 24,
|
| 37 |
+
'Squash___Powdery_mildew': 25,
|
| 38 |
+
'Strawberry___Leaf_scorch': 26,
|
| 39 |
+
'Strawberry___healthy': 27,
|
| 40 |
+
'Tomato___Bacterial_spot': 28,
|
| 41 |
+
'Tomato___Early_blight': 29,
|
| 42 |
+
'Tomato___Late_blight': 30,
|
| 43 |
+
'Tomato___Leaf_Mold': 31,
|
| 44 |
+
'Tomato___Septoria_leaf_spot': 32,
|
| 45 |
+
'Tomato___Spider_mites Two-spotted_spider_mite': 33,
|
| 46 |
+
'Tomato___Target_Spot': 34,
|
| 47 |
+
'Tomato___Tomato_Yellow_Leaf_Curl_Virus': 35,
|
| 48 |
+
'Tomato___Tomato_mosaic_virus': 36,
|
| 49 |
+
'Tomato___healthy': 37
|
| 50 |
+
}
|
| 51 |
|
| 52 |
+
# Reverse mapping
|
| 53 |
+
idx_to_class = {v: k for k, v in class_indices.items()}
|
| 54 |
|
| 55 |
# Prediction function
|
| 56 |
+
def predict(img):
|
| 57 |
+
img = img.resize((224, 224)) # adjust if your model uses different input size
|
| 58 |
+
img_array = image.img_to_array(img) / 255.0
|
| 59 |
+
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
predictions = model.predict(img_array)
|
| 62 |
+
predicted_class = np.argmax(predictions[0])
|
| 63 |
+
confidence = np.max(predictions[0]) * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
return f"{idx_to_class[predicted_class]} ({confidence:.2f}% confidence)"
|
| 66 |
+
|
| 67 |
+
# Gradio interface
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
fn=predict,
|
| 70 |
+
inputs=gr.Image(type="pil", label="Upload a leaf image"),
|
| 71 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 72 |
title="Crop Disease Prediction",
|
| 73 |
+
description="Upload an image of a crop leaf to predict its disease."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
demo.launch()
|