import gradio as gr import numpy as np from PIL import Image from tensorflow.keras.models import load_model # Load the model model = load_model('model1.h5') # Define class indices class_indices = {0: 'Apple___Apple_scab', 1: 'Apple___Black_rot', 2: 'Apple___Cedar_apple_rust', 3: 'Apple___healthy', 4: 'Blueberry___healthy', 5: 'Cherry_(including_sour)___Powdery_mildew', 6: 'Cherry_(including_sour)___healthy', 7: 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot', 8: 'Corn_(maize)___Common_rust_', 9: 'Corn_(maize)___Northern_Leaf_Blight', 10: 'Corn_(maize)___healthy', 11: 'Grape___Black_rot', 12: 'Grape___Esca_(Black_Measles)', 13: 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', 14: 'Grape___healthy', 15: 'Orange___Haunglongbing_(Citrus_greening)', 16: 'Peach___Bacterial_spot', 17: 'Peach___healthy', 18: 'Pepper,_bell___Bacterial_spot', 19: 'Pepper,_bell___healthy', 20: 'Potato___Early_blight', 21: 'Potato___Late_blight', 22: 'Potato___healthy', 23: 'Raspberry___healthy', 24: 'Soybean___healthy', 25: 'Squash___Powdery_mildew', 26: 'Strawberry___Leaf_scorch', 27: 'Strawberry___healthy', 28: 'Tomato___Bacterial_spot', 29: 'Tomato___Early_blight', 30: 'Tomato___Late_blight', 31: 'Tomato___Leaf_Mold', 32: 'Tomato___Septoria_leaf_spot', 33: 'Tomato___Spider_mites Two-spotted_spider_mite', 34: 'Tomato___Target_Spot', 35: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 36: 'Tomato___Tomato_mosaic_virus', 37: 'Tomato___healthy'} # Preprocess the image def preprocess_image(image): # Resize the image image = Image.fromarray(image).resize((224, 224)) # Convert to numpy array and scale the values image = np.array(image).astype('float32') / 255.0 # Add batch dimension image = np.expand_dims(image, axis=0) return image # Predict the class of the image def predict_image(image): preprocessed_image = preprocess_image(image) predictions = model.predict(preprocessed_image) predicted_class_index = np.argmax(predictions) predicted_class_name = class_indices[predicted_class_index] confidence = round(predictions[0][predicted_class_index], 2) return predicted_class_name, confidence def build_gui(): description = """
Plant Disease Detection

Welcome to the Plantex demo!

PLantex is a plant disease detection model that can predict the disease of a plant based on an image of its leaf. To use the model, simply upload an image of a plant's leaf and click on the "Predict" button. The model will then predict the disease of the plant and display the predicted class name along with the confidence score.

Great thanks to Meet Patel, the major contributor of this demo!

""" # noqa article = """

Model is trained on public dataset, and we are persisting in refining and iterating upon it.
Plantex - Plant disease detection & organic waste management

""" # noqa with gr.Blocks(title="Plantex - Disease detetion model") as demo: gr.HTML(description) gr.Interface( fn=predict_image, inputs=gr.Image(label="Plant's leaf Image"), outputs=[gr.Textbox(label="Predicted"),gr.Textbox(label="Confidence")], # examples=[ # ["test_apple_black_rot.jpg"], # ["test_blueberry_healthy.jpg"], # ["test_potato_early_blight.jpg"] # ], cache_examples=True, allow_flagging='never' ) gr.HTML(article) return demo if __name__ == "__main__": build_gui().launch()