File size: 1,127 Bytes
9650572
 
 
 
 
 
 
347d632
9650572
 
4a5969d
9650572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import json

# Load the TensorFlow model
model = tf.keras.models.load_model('plant_disease_detection_compatible.h5')

# Load categories
with open('categories.json') as f:
    categories = json.load(f)

def preprocess_image(image):
    image = image.resize((224, 224))  # Adjust size as needed
    image_array = np.array(image) / 255.0  # Normalize to [0, 1]
    image_array = np.expand_dims(image_array, axis=0)  # Add batch dimension
    return image_array

def predict(image):
    image_array = preprocess_image(image)
    predictions = model.predict(image_array)
    predicted_class = np.argmax(predictions, axis=1)[0]
    predicted_label = categories.get(str(predicted_class), 'Unknown')
    return predicted_label, float(predictions[0][predicted_class])

iface = gr.Interface(
    fn=predict, 
    inputs=gr.Image(type="pil"), 
    outputs=[gr.Label(), gr.Number()],
    title="Plant Disease Detection",
    description="Upload an image of a plant leaf to detect if it has any diseases."
)

if __name__ == "__main__":
    iface.launch()