File size: 1,363 Bytes
065a527
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
from PIL import Image
from tensorflow.keras import models
from tensorflow.keras.applications.efficientnet import preprocess_input

# Step 1: Load the entire model (no need to manually load weights)
model = models.load_model("plant_disease_classifier.keras")  # Load the saved model directly

# Step 2: Define class names in the correct order
class_names = ['Early Blight', 'Late Blight', 'Healthy']  # Ensure this matches the training order

# Step 3: Define prediction function
def predict(image: Image.Image):
    image = image.convert("RGB")
    image = image.resize((256, 256))
    image_array = np.array(image, dtype=np.float32)
    image_array = np.expand_dims(image_array, axis=0)
    image_array = preprocess_input(image_array)

    predictions = model.predict(image_array)
    predicted_index = np.argmax(predictions)
    confidence = float(predictions[0][predicted_index]) * 100
    predicted_class = class_names[predicted_index]

    return {predicted_class: confidence}

# Step 4: Define Gradio interface
interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    title="Plant Disease Classifier",
    description="Upload an image of a plant leaf to identify the disease."
)

# Step 5: Launch the app
if __name__ == "__main__":
    interface.launch()