TomatoSentry8 / app.py
Jeff28's picture
Update app.py
ebf92a8 verified
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
# Load the trained model
model = tf.keras.models.load_model("Tomato_Leaf_Disease_Model.h5")
# Updated disease categories based on training data
class_labels = [
"Tomato Bacterial Spot",
"Tomato Early Blight",
"Tomato Late Blight",
"Tomato Mosaic Virus",
"Tomato Yellow Leaf Curl Virus"
]
# Image preprocessing function
def preprocess(img):
img = img.resize((224, 224)) # Resize to match model input size
img = image.img_to_array(img) / 255.0 # Normalize
img = np.expand_dims(img, axis=0) # Add batch dimension
return img
# Prediction function
def predict(img):
img = preprocess(img)
prediction = model.predict(img)
class_idx = np.argmax(prediction) # Get class with highest probability
confidence = np.max(prediction) * 100 # Get confidence score
return f"{class_labels[class_idx]} (Confidence: {confidence:.2f}%)"
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# 🍅 Tomato Leaf Disease Detector")
gr.Markdown("Upload a leaf image to detect its disease.")
image_input = gr.Image(type="pil", label="Upload a Tomato Leaf Image")
result_output = gr.Textbox(label="Detected Disease & Confidence")
submit_btn = gr.Button("Detect Disease")
submit_btn.click(predict, inputs=image_input, outputs=result_output)
demo.launch()