cropDiseasePred / app.py
onlyshrey98's picture
Update app.py
4066d2a verified
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from PIL import Image
# Load your model
model = load_model("Model.hdf5")
# Class mapping (from your screenshot)
class_indices = {
'Apple___Apple_scab': 0,
'Apple___Black_rot': 1,
'Apple___Cedar_apple_rust': 2,
'Apple___healthy': 3,
'Blueberry___healthy': 4,
'Cherry_(including_sour)___Powdery_mildew': 5,
'Cherry_(including_sour)___healthy': 6,
'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot': 7,
'Corn_(maize)___Common_rust': 8,
'Corn_(maize)___Northern_Leaf_Blight': 9,
'Corn_(maize)___healthy': 10,
'Grape___Black_rot': 11,
'Grape___Esca_(Black_Measles)': 12,
'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)': 13,
'Grape___healthy': 14,
'Orange___Haunglongbing_(Citrus_greening)': 15,
'Peach___Bacterial_spot': 16,
'Peach___healthy': 17,
'Pepper,_bell___Bacterial_spot': 18,
'Pepper,_bell___healthy': 19,
'Potato___Early_blight': 20,
'Potato___Late_blight': 21,
'Potato___healthy': 22,
'Raspberry___healthy': 23,
'Soybean___healthy': 24,
'Squash___Powdery_mildew': 25,
'Strawberry___Leaf_scorch': 26,
'Strawberry___healthy': 27,
'Tomato___Bacterial_spot': 28,
'Tomato___Early_blight': 29,
'Tomato___Late_blight': 30,
'Tomato___Leaf_Mold': 31,
'Tomato___Septoria_leaf_spot': 32,
'Tomato___Spider_mites Two-spotted_spider_mite': 33,
'Tomato___Target_Spot': 34,
'Tomato___Tomato_Yellow_Leaf_Curl_Virus': 35,
'Tomato___Tomato_mosaic_virus': 36,
'Tomato___healthy': 37
}
# Reverse mapping
idx_to_class = {v: k for k, v in class_indices.items()}
# Prediction function
def predict(img):
img = img.resize((224, 224)) # adjust if your model uses different input size
img_array = image.img_to_array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
predictions = model.predict(img_array)
predicted_class = np.argmax(predictions[0])
confidence = np.max(predictions[0]) * 100
return f"{idx_to_class[predicted_class]} ({confidence:.2f}% confidence)"
# Gradio interface
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="Upload a leaf image"),
outputs=gr.Textbox(label="Prediction"),
title="Crop Disease Prediction",
description="Upload an image of a crop leaf to predict its disease."
)
if __name__ == "__main__":
demo.launch()