File size: 2,285 Bytes
8111363
 
 
 
 
 
 
 
 
 
 
 
 
eaab480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8111363
4165048
8111363
 
 
 
 
 
 
 
6f1c0ed
8111363
efaf45d
6f1c0ed
efaf45d
6f1c0ed
efaf45d
6f1c0ed
 
efaf45d
8111363
 
 
 
ae0a93e
efaf45d
6f1c0ed
 
8111363
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import tensorflow as tf

# Load the saved model
model = load_model('acres-ppdc-01.keras')

# Define the classes the model was trained on
class_labels = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']

def classify_potato_plant(img):
    # Convert the image to a NumPy array for manipulation
    img = np.array(img)

    # Get the current width and height of the image
    h, w, _ = img.shape

    # Calculate the cropping coordinates to keep the center of the image
    if h > w:
        # If height is greater than width, crop the top and bottom
        start = (h - w) // 2
        img = img[start:start + w, :, :]  # Crop to width
    else:
        # If width is greater than height, crop the left and right
        start = (w - h) // 2
        img = img[:, start:start + h, :]  # Crop to height

    # Convert back to PIL image after cropping
    img = image.array_to_img(img)
    
    # Preprocess the image for the model
    img = img.resize((128, 128))  # Resize to the same size the model was trained on
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = img / 255.0  # Normalize the image

    # Make the prediction
    predictions = model.predict(img)
    predicted_class = np.argmax(predictions[0])
    confidence = predictions[0][predicted_class]
    model_output = "None"

    if class_labels[predicted_class] == "Potato___Early_blight":
        model_output = "Early blight"
    elif class_labels[predicted_class] == "Potato___Late_blight":
        model_output = "Late blight"
    elif class_labels[predicted_class] == "Potato___healthy":
        model_output = "Healthy"

    return model_output, confidence

# Create the Gradio interface
interface = gr.Interface(
    fn=classify_potato_plant,
    inputs=gr.Image(type="pil"),
    outputs=[gr.Textbox(label="Predicted Output"), gr.Textbox(label="Confidence Score")],
    title="Acres - PPDC",
    description="Acres PPDC, is our Potato Plant Disease Classification vision model, capable of accurately classifying potato plant disease, based on a single image."
)

# Launch the app
if __name__ == "__main__":
    interface.launch()