|
import gradio as gr |
|
import numpy as np |
|
from tensorflow.keras.preprocessing import image |
|
from tensorflow.keras.models import load_model |
|
from PIL import Image |
|
|
|
|
|
model = load_model("plant_disease_model.h5") |
|
IMG_SIZE = (224, 224) |
|
class_names = ['Apple___Black_rot', 'Tomato___Early_blight', 'Potato___Late_blight'] |
|
|
|
|
|
def predict_plant_disease(img): |
|
img = img.resize(IMG_SIZE) |
|
img_array = image.img_to_array(img) / 255.0 |
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
predictions = model.predict(img_array) |
|
index = np.argmax(predictions) |
|
confidence = float(predictions[0][index]) |
|
|
|
disease_name = class_names[index] |
|
confidence_text = f"{confidence:.2%}" |
|
confidence_value = round(confidence, 2) |
|
|
|
return disease_name, confidence_value, confidence_text |
|
|
|
|
|
with gr.Blocks(css=".green-btn button {background-color: #2e7d32 !important; color: white;}") as demo: |
|
gr.Markdown("<h1 style='text-align:center;'>πΏ Smart Plant Disease Detector</h1>") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
image_input = gr.Image(type="pil", label="π· Upload Leaf Image") |
|
predict_btn = gr.Button("π Detect Disease", elem_classes="green-btn") |
|
|
|
with gr.Column(scale=1): |
|
disease_output = gr.Textbox(label="πͺ΄ Detected Disease") |
|
confidence_bar = gr.Slider(label="π Confidence Level", minimum=0, maximum=1, step=0.01, interactive=False) |
|
confidence_text = gr.Textbox(label="π’ Confidence (Text)") |
|
|
|
predict_btn.click(fn=predict_plant_disease, |
|
inputs=image_input, |
|
outputs=[disease_output, confidence_bar, confidence_text]) |
|
|
|
demo.launch() |
|
|