File size: 1,435 Bytes
6467f63
 
 
 
 
 
ebf92a8
6467f63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()