|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from tensorflow.keras.models import load_model |
|
|
|
|
|
model_path = 'best_model.keras' |
|
model = load_model(model_path) |
|
|
|
|
|
IMG_SIZE = (224, 224) |
|
|
|
|
|
class_names = [ |
|
'Corn___Common_Rust', |
|
'Corn___Gray_Leaf_Spot', |
|
'Corn___Healthy', |
|
'Corn___Northern_Leaf_Blight', |
|
'Corn___Northern_Leaf_Spot', |
|
'Corn___Phaeosphaeria_Leaf_Spot' |
|
] |
|
|
|
|
|
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.8: |
|
return "Unknown Object" |
|
else: |
|
return {class_names[predicted_class]: float(confidence)} |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|