File size: 1,469 Bytes
bdadfba 27ec386 bdadfba |
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 50 51 52 53 54 |
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('guava_disease_cnn.h5')
# Preprocessing
test_datagen = ImageDataGenerator(rescale=1./255)
# Class labels
class_names = ['0.Anthracnose', '1.Fruit Fly', '2.Healthy Guava']
# 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="Guava Fruit Disease Classification",
description=(
"This app classifies diseases in Guava fruits using deep learning. "
"Upload an image of a papaya to get started."
),
allow_flagging="never",
)
# Launch the app
interface.launch()
|