|
|
import gradio as gr |
|
|
from keras.preprocessing import image |
|
|
from keras.preprocessing.image import img_to_array |
|
|
from keras.models import load_model |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
model_path = 'Mango.h5' |
|
|
model = load_model(model_path) |
|
|
|
|
|
def predict_disease(image_file, model, all_labels): |
|
|
""" |
|
|
Predict the disease from an image using the trained model. |
|
|
|
|
|
Parameters: |
|
|
- image_file: image, input image file |
|
|
- model: Keras model, trained convolutional neural network |
|
|
- all_labels: list, list of class labels |
|
|
|
|
|
Returns: |
|
|
- str, predicted class label |
|
|
""" |
|
|
try: |
|
|
|
|
|
img = image.load_img(image_file, target_size=(256, 256)) |
|
|
img_array = img_to_array(img) |
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
img_array = img_array / 255.0 |
|
|
|
|
|
|
|
|
predictions = model.predict(img_array) |
|
|
predicted_class = np.argmax(predictions[0]) |
|
|
|
|
|
|
|
|
return all_labels[predicted_class] |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error: {e}") |
|
|
return None |
|
|
|
|
|
|
|
|
all_labels = ['Mango Anthracrose','Mango Bacterial Cancker','Mango Cutting weevil','Mango Die Back','Mango Gall Midge ','Mango Healthy','Mango powdery mildew','Mango Sooty Mould'] |
|
|
|
|
|
|
|
|
def gradio_predict(image_file): |
|
|
return predict_disease(image_file, model, all_labels) |
|
|
|
|
|
|
|
|
gr_interface = gr.Interface( |
|
|
fn=gradio_predict, |
|
|
inputs=gr.Image(type="filepath"), |
|
|
outputs="text", |
|
|
title="Plant Disease Predictor", |
|
|
description="Upload an image of a plant to predict the disease.", |
|
|
) |
|
|
|
|
|
|
|
|
gr_interface.launch() |
|
|
|