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()