Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import tensorflow as tf
|
5 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
6 |
+
|
7 |
+
# Load the model
|
8 |
+
model = tf.keras.models.load_model('guava_disease_cnn.h5')
|
9 |
+
|
10 |
+
|
11 |
+
# Preprocessing
|
12 |
+
test_datagen = ImageDataGenerator(rescale=1./255)
|
13 |
+
|
14 |
+
# Class labels
|
15 |
+
class_names = ['0.Anthracnose', '1.Fruit Fly', '2.Healthy Guava']
|
16 |
+
|
17 |
+
# Prediction function
|
18 |
+
def classify_image(image):
|
19 |
+
"""
|
20 |
+
Process and classify the input image.
|
21 |
+
Args:
|
22 |
+
image: Input image in PIL format.
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
Predicted class label.
|
26 |
+
"""
|
27 |
+
# Convert to numpy array
|
28 |
+
opencv_image = np.array(image)
|
29 |
+
# Resize and preprocess the image
|
30 |
+
img = cv2.resize(opencv_image, (150, 150))
|
31 |
+
img = np.expand_dims(img, axis=0).astype('float32') # Expand dimensions
|
32 |
+
img = test_datagen.standardize(img) # Normalize the image
|
33 |
+
# Predict using the model
|
34 |
+
predictions = model.predict(img)
|
35 |
+
predicted_class = class_names[np.argmax(predictions)]
|
36 |
+
return predicted_class
|
37 |
+
|
38 |
+
# Gradio Interface
|
39 |
+
interface = gr.Interface(
|
40 |
+
fn=classify_image,
|
41 |
+
inputs=gr.Image(type="pil", label="Upload an image"),
|
42 |
+
outputs=gr.Textbox(label="Predicted Disease"),
|
43 |
+
title="Guava Fruit Disease Classification",
|
44 |
+
description=(
|
45 |
+
"This app classifies diseases in Guava fruits using deep learning. "
|
46 |
+
"Upload an image of a papaya to get started."
|
47 |
+
),
|
48 |
+
examples=[
|
49 |
+
["example_images/healthy.jpg"],
|
50 |
+
["example_images/anthracnose.jpg"],
|
51 |
+
["example_images/brown_spot.jpg"],
|
52 |
+
],
|
53 |
+
allow_flagging="never",
|
54 |
+
)
|
55 |
+
|
56 |
+
# Launch the app
|
57 |
+
interface.launch()
|