Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from keras.preprocessing import image
|
| 3 |
+
from keras.preprocessing.image import img_to_array
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
# Load the pre-trained model from the local path
|
| 8 |
+
model_path = 'Leaf_name.h5'
|
| 9 |
+
model = load_model(model_path)
|
| 10 |
+
|
| 11 |
+
def predict_disease(image_file, model, all_labels):
|
| 12 |
+
"""
|
| 13 |
+
Predict the disease from an image using the trained model.
|
| 14 |
+
|
| 15 |
+
Parameters:
|
| 16 |
+
- image_file: image, input image file
|
| 17 |
+
- model: Keras model, trained convolutional neural network
|
| 18 |
+
- all_labels: list, list of class labels
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
- str, predicted class label
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
# Load and preprocess the image
|
| 25 |
+
img = image.load_img(image_file, target_size=(256, 256))
|
| 26 |
+
img_array = img_to_array(img)
|
| 27 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 28 |
+
img_array = img_array / 255.0 # Normalize the image
|
| 29 |
+
|
| 30 |
+
# Predict the class
|
| 31 |
+
predictions = model.predict(img_array)
|
| 32 |
+
predicted_class = np.argmax(predictions[0])
|
| 33 |
+
|
| 34 |
+
# Return the class label
|
| 35 |
+
return all_labels[predicted_class]
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error: {e}")
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
# List of class labels
|
| 42 |
+
all_labels = ['Corn-Common_rust', 'Potato-Early_blight', 'Tomato-Bacterial_spot']
|
| 43 |
+
|
| 44 |
+
# Define the Gradio interface
|
| 45 |
+
def gradio_predict(image_file):
|
| 46 |
+
return predict_disease(image_file, model, all_labels)
|
| 47 |
+
|
| 48 |
+
# Create a Gradio interface
|
| 49 |
+
gr_interface = gr.Interface(
|
| 50 |
+
fn=gradio_predict, # Function to call for predictions
|
| 51 |
+
inputs=gr.Image(type="filepath"), # Upload image as file path
|
| 52 |
+
outputs="text", # Output will be the class label as text
|
| 53 |
+
title="Plant Disease Predictor",
|
| 54 |
+
description="Upload an image of a plant to predict the disease.",
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Launch the Gradio app
|
| 58 |
+
gr_interface.launch()
|