File size: 4,143 Bytes
f7907e5
 
 
 
 
 
7c88179
f7907e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b129147
 
 
 
 
f7907e5
 
 
 
 
 
 
 
 
7c88179
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
import numpy as np
from PIL import Image
from tensorflow.keras.models import load_model

# Load the model
model = load_model('model1.h5')

# Define class indices
class_indices = {0: 'Apple___Apple_scab',
 1: 'Apple___Black_rot',
 2: 'Apple___Cedar_apple_rust',
 3: 'Apple___healthy',
 4: 'Blueberry___healthy',
 5: 'Cherry_(including_sour)___Powdery_mildew',
 6: 'Cherry_(including_sour)___healthy',
 7: 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',
 8: 'Corn_(maize)___Common_rust_',
 9: 'Corn_(maize)___Northern_Leaf_Blight',
 10: 'Corn_(maize)___healthy',
 11: 'Grape___Black_rot',
 12: 'Grape___Esca_(Black_Measles)',
 13: 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
 14: 'Grape___healthy',
 15: 'Orange___Haunglongbing_(Citrus_greening)',
 16: 'Peach___Bacterial_spot',
 17: 'Peach___healthy',
 18: 'Pepper,_bell___Bacterial_spot',
 19: 'Pepper,_bell___healthy',
 20: 'Potato___Early_blight',
 21: 'Potato___Late_blight',
 22: 'Potato___healthy',
 23: 'Raspberry___healthy',
 24: 'Soybean___healthy',
 25: 'Squash___Powdery_mildew',
 26: 'Strawberry___Leaf_scorch',
 27: 'Strawberry___healthy',
 28: 'Tomato___Bacterial_spot',
 29: 'Tomato___Early_blight',
 30: 'Tomato___Late_blight',
 31: 'Tomato___Leaf_Mold',
 32: 'Tomato___Septoria_leaf_spot',
 33: 'Tomato___Spider_mites Two-spotted_spider_mite',
 34: 'Tomato___Target_Spot',
 35: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
 36: 'Tomato___Tomato_mosaic_virus',
 37: 'Tomato___healthy'}

# Preprocess the image
def preprocess_image(image):
    # Resize the image
    image = Image.fromarray(image).resize((224, 224))
    # Convert to numpy array and scale the values
    image = np.array(image).astype('float32') / 255.0
    # Add batch dimension
    image = np.expand_dims(image, axis=0)
    return image

# Predict the class of the image
def predict_image(image):
    preprocessed_image = preprocess_image(image)
    predictions = model.predict(preprocessed_image)
    predicted_class_index = np.argmax(predictions)
    predicted_class_name = class_indices[predicted_class_index]
    confidence = round(predictions[0][predicted_class_index], 2)
    return predicted_class_name, confidence


def build_gui():

        description = """
            <center><strong>
                <font size='10'>Plant Disease Detection</font>
            </strong></center>
            <br>
            <p>Welcome to the <a href='https://github.com/meet244/Plantex' target='_blank'>Plantex</a> demo!</p>
            <p>
                PLantex is a plant disease detection model that can predict the disease of a plant based on an image of its leaf.
                To use the model, simply upload an image of a plant's leaf and click on the "Predict" button.
                The model will then predict the disease of the plant and display the predicted class name along with the confidence score.
            </p>
            <p>Great thanks to <a href='https://huggingface.co/meet244' target='_blank'>Meet Patel</a>, the major contributor of this
                demo!
            </p>
        """  # noqa

        article = """
            <p style='text-align: center'>
                Model is trained on public dataset, and we are persisting in refining and iterating upon it.<br/>
                <a href='https://github.com/meet244/Plantex' target='_blank'>Plantex - Plant disease detection & organic waste management</a>
            </p>
        """  # noqa
        with gr.Blocks(title="Plantex - Disease detetion model") as demo:
          gr.HTML(description)
          gr.Interface(
              fn=predict_image, 
              inputs=gr.Image(label="Plant's leaf Image"), 
              outputs=[gr.Textbox(label="Predicted"),gr.Textbox(label="Confidence")],
              # examples=[
              #     ["test_apple_black_rot.jpg"],
              #     ["test_blueberry_healthy.jpg"],
              #     ["test_potato_early_blight.jpg"]
              # ],
              cache_examples=True,
              allow_flagging='never'
              )
          gr.HTML(article)
          return demo



if __name__ == "__main__":
    build_gui().launch()