moazx's picture
Update app.py
c748527
import numpy as np
import cv2
import gradio as gr
from tensorflow.keras.utils import img_to_array
from tensorflow.keras.models import load_model
import os
# Load your pre-trained model
model = load_model(r"plant_leaf_diseases_model.h5")
classes = ['Pepper_bell_Bacterial_spot',
'Pepper_bell_healthy',
'Potato_Early_blight',
'Potato_Late_blight',
'Potato_healthy',
'Tomato_Bacterial_spot',
'Tomato_Early_blight',
'Tomato_Late_blight',
'Tomato_Leaf_Mold',
'Tomato_Septoria_leaf_spot',
'Tomato_Spider_mites_Two_spotted_spider_mite',
'Tomato_Target_Spot',
'Tomato_Tomato_YellowLeaf_Curl_Virus',
'Tomato_Tomato_mosaic_virus',
'Tomato_healthy']
# Define the prediction function that takes an image as input and returns the predicted label
def predict_image(img):
x = img_to_array(img)
x = cv2.resize(x, (256, 256), interpolation=cv2.INTER_AREA)
x /= 255
x = np.expand_dims(x, axis=0)
image = np.vstack([x])
prediction = model.predict(image)[0] # Get the predictions for the first (and only) image
class_probabilities = {class_name: prob for class_name, prob in zip(classes, prediction)}
formatted_class_probabilities = {class_name: "{:.2f}".format(prob) for class_name, prob in class_probabilities.items()}
return formatted_class_probabilities
# Define the Gradio Interface with the desired title and description
description_html = """
<p>This model was trained by Moaz Eldsouky. You can find more about me here:</p>
<p>GitHub: <a href="https://github.com/MoazEldsouky">GitHub Profile</a></p>
<p>LinkedIn: <a href="https://www.linkedin.com/in/moaz-eldesouky-762288251/">LinkedIn Profile</a></p>
<p>Kaggle: <a href="https://www.kaggle.com/moazeldsokyx">Kaggle Profile</a></p>
<p>This model was trained to classify plant diseases using the Plant Villages dataset.</p>
<p>You can see how this model was trained on the following Kaggle Notebook:</p>
<p><a href="https://www.kaggle.com/code/moazeldsokyx/plant-leaf-diseases-detection-using-cnn">Kaggle Notebook</a></p>
<p>The model achieved an accuracy of <strong>96%</strong> on the test set.</p>
<p>Upload a photo to see how the model predicts!</p>
"""
# Define example images and their true labels for users to choose from
example_data = [
r"0a0dbf1f-1131-496f-b337-169ec6693e6f___NREC_B.Spot 9241.JPG",
r"0e69c47d-72c6-4fc6-9437-910c95b183dc___JR_HL 8113.JPG",
r"0c4f6f72-c7a2-42e1-9671-41ab3bf37fe7___RS_Early.B 6752.JPG",
r"1f9870b3-899e-46fb-98c9-cfc2ce92895b___RS_HL 1816.JPG",
r"3f0fd699-1ce7-437e-a899-662a51d59974___RS_LB 2904.JPG",
r"01b2013e-4030-4cd0-843c-2dbacf5f3758___Com.G_TgS_FL 8398.JPG",
r"0ce66ec5-0bb7-4fde-9c61-750a1a150f75___UF.GRC_YLCV_Lab 02219.JPG",
r"0afe3bbd-b18b-4c70-8fbd-072844e742a2___GCREC_Bact.Sp 3434.JPG",
r"0ba3d536-8732-4ea1-b3e1-a1be86e5dc6a___RS_Erly.B 9499.JPG",
r"2e96661f-a313-4deb-bf32-1eacbc10c48d___GH_HL Leaf 424.JPG"
# Add more example images and labels as needed
]
gr.Interface(
fn=predict_image,
inputs="image",
outputs=gr.Label(num_top_classes=15,min_width=360),
title="Plant Diseases Classification 🌱🦠",
description=description_html,
allow_flagging='never',
examples=example_data
).launch()