File size: 1,716 Bytes
990b5d5
d1f76c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
990b5d5
 
d1f76c1
990b5d5
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
from PIL import Image
from keras.models import load_model

# Load the pre-trained model for banana ripeness detection
banana_model = load_model("trained model/best_model.h5")

# Define class names for the banana disease detection
class_names_disease = {
    0: 'BUNCHY_TOP',
    1: 'CORDANA',
    2: 'PANAMA',
    3: 'SIGATOKA'
}

# Define class names for the banana ripeness detection
class_names_ripeness = ["Banana_G1", "Banana_G2", "Rotten"]
model = load_model("trained model/best_model.h5")

def preprocess_image(image):
    img = Image.open(image)
    img = img.resize((256, 256))  # Resize the image to the input size of the model
    img_array = np.array(img)
    img_array = img_array / 255.0  # Normalize the pixel values
    img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension
    return img_array


def predict(image):
    img_array = preprocess_image(image)
    predictions = model.predict(img_array)
    predicted_class = np.argmax(predictions)
    predicted_label = class_names_disease[predicted_class]
    return predicted_label

def predict_disease(uploaded_file):
    if uploaded_file is not None:
        predicted_label = predict(uploaded_file)
        return predicted_label


def predict_ripeness(image):
    img_array = preprocess_image(image)
    predictions = banana_model.predict(img_array)
    predicted_class = np.argmax(predictions)
    predicted_label = class_names_ripeness[predicted_class]
    return predicted_label

inputs = gr.inputs.File(label="Upload an image...")
outputs = gr.outputs.Textbox(label="Prediction")

gr.Interface(fn=predict_disease, inputs=inputs, outputs=outputs, title="Banana Disease Detection").launch()