File size: 1,257 Bytes
7b2de4a 39cf849 7b2de4a |
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 45 46 47 48 49 |
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import load_model
# Load your model with the new path
model_path = 'best_model.keras'
model = load_model(model_path)
# Define the image size your model expects
IMG_SIZE = (224, 224)
# Define class names
class_names = [
'Corn___Common_Rust',
'Corn___Gray_Leaf_Spot',
'Corn___Healthy',
'Corn___Northern_Leaf_Blight',
'Corn___Northern_Leaf_Spot',
'Corn___Phaeosphaeria_Leaf_Spot'
]
# Define prediction function
def predict(image):
img_array = tf.image.resize(image, IMG_SIZE)
img_array = tf.expand_dims(img_array, axis=0) / 255.0
predictions = model.predict(img_array)
predicted_class = np.argmax(predictions[0])
confidence = np.max(predictions[0])
if confidence <= 0.5:
return "Unknown Object"
else:
return {class_names[predicted_class]: float(confidence)}
# Create Gradio interface
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=6),
title="Maize Leaf Disease Detection",
description="Upload an image of a maize leaf to classify its disease."
)
# Launch the interface
if __name__ == "__main__":
interface.launch()
|