Spaces:
Sleeping
Sleeping
import gradio as gr | |
import numpy as np | |
import cv2 | |
import tensorflow as tf | |
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
# Load the model | |
model = tf.keras.models.load_model('model6.h5') | |
# Preprocessing | |
test_datagen = ImageDataGenerator(rescale=1./255) | |
# Class labels | |
class_names = ['0.Healthy', '1.Anthracnose', '2.Phytophthora Blight', | |
'3.Brown Spot', '4.Black Spot', '5.Others'] | |
# Prediction function | |
def classify_image(image): | |
""" | |
Process and classify the input image. | |
Args: | |
image: Input image in PIL format. | |
Returns: | |
Predicted class label. | |
""" | |
# Convert to numpy array | |
opencv_image = np.array(image) | |
# Resize and preprocess the image | |
img = cv2.resize(opencv_image, (150, 150)) | |
img = np.expand_dims(img, axis=0).astype('float32') # Expand dimensions | |
img = test_datagen.standardize(img) # Normalize the image | |
# Predict using the model | |
predictions = model.predict(img) | |
predicted_class = class_names[np.argmax(predictions)] | |
return predicted_class | |
# Gradio Interface | |
interface = gr.Interface( | |
fn=classify_image, | |
inputs=gr.Image(type="pil", label="Upload an image"), | |
outputs=gr.Textbox(label="Predicted Disease"), | |
title="Papaya Fruit Disease Classification", | |
description=( | |
"This app classifies diseases in papaya fruits using deep learning. " | |
"Upload an image of a papaya to get started." | |
), | |
examples=[ | |
["example_images/healthy.jpg"], | |
["example_images/anthracnose.jpg"], | |
["example_images/brown_spot.jpg"], | |
], | |
allow_flagging="never", | |
) | |
# Launch the app | |
interface.launch() | |