Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tensorflow.keras.utils import img_to_array,load_img
|
3 |
+
from keras.models import load_model
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load the pre-trained model from the local path
|
7 |
+
model_path = 'citrus.h5'
|
8 |
+
model = load_model(model_path) # Load the model here
|
9 |
+
|
10 |
+
def predict_disease(image_file, model, all_labels):
|
11 |
+
|
12 |
+
try:
|
13 |
+
# Load and preprocess the image
|
14 |
+
img = load_img(image_file, target_size=(224, 224)) # Use load_img from tensorflow.keras.utils
|
15 |
+
img_array = img_to_array(img)
|
16 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
17 |
+
img_array = img_array / 255.0 # Normalize the image
|
18 |
+
|
19 |
+
# Predict the class
|
20 |
+
predictions = model.predict(img_array) # Use the loaded model here
|
21 |
+
predicted_class = np.argmax(predictions[0])
|
22 |
+
|
23 |
+
# Get the predicted class label
|
24 |
+
predicted_label = all_labels[predicted_class]
|
25 |
+
|
26 |
+
# Print the predicted label to the console
|
27 |
+
|
28 |
+
if predicted_label=='Citrus Healthy':
|
29 |
+
predicted_label = predicted_label = """<h3 align="center">Citrus Healthy</h3><br><br>
|
30 |
+
<center>No need use Pesticides</center>"""
|
31 |
+
elif predicted_label=='Citrus Greening':
|
32 |
+
predicted_label = """
|
33 |
+
<style>
|
34 |
+
li{
|
35 |
+
font-size: 15px;
|
36 |
+
margin-left: 90px;
|
37 |
+
margin-top: 15px;
|
38 |
+
margin-bottom: 15px;
|
39 |
+
}
|
40 |
+
h4{
|
41 |
+
font-size: 17px;
|
42 |
+
margin-top: 15px;
|
43 |
+
}
|
44 |
+
h4:hover{
|
45 |
+
cursor: pointer;
|
46 |
+
}
|
47 |
+
|
48 |
+
h3:hover{
|
49 |
+
cursor: pointer;
|
50 |
+
color: blue;
|
51 |
+
transform: scale(1.3);
|
52 |
+
}
|
53 |
+
.note{
|
54 |
+
text-align: center;
|
55 |
+
font-size: 16px;
|
56 |
+
}
|
57 |
+
p{
|
58 |
+
font-size: 13px;
|
59 |
+
text-align: center;
|
60 |
+
}
|
61 |
+
|
62 |
+
</style>
|
63 |
+
<h3><center><b>Citrus Greening</b></center></h3>
|
64 |
+
<h4>PESTICIDES TO BE USED:</h4>
|
65 |
+
<ul>
|
66 |
+
<li>1. Oxytetracycline (Terramycin)</li>
|
67 |
+
<li>2. Streptomycin (Streptomycin sulfate)</li>
|
68 |
+
<li>3. Mancozeb (Dithane)</li>
|
69 |
+
<li>4. Copper oxychloride (Kocide)</li>
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
</ul>
|
74 |
+
<p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
|
75 |
+
<p>Be sure to follow local regulations and guidelines for application</p>
|
76 |
+
|
77 |
+
|
78 |
+
"""
|
79 |
+
elif predicted_label=='Citrus Canker':
|
80 |
+
predicted_label = """
|
81 |
+
<style>
|
82 |
+
li{
|
83 |
+
font-size: 15px;
|
84 |
+
margin-left: 90px;
|
85 |
+
margin-top: 15px;
|
86 |
+
margin-bottom: 15px;
|
87 |
+
}
|
88 |
+
h4{
|
89 |
+
font-size: 17px;
|
90 |
+
margin-top: 15px;
|
91 |
+
}
|
92 |
+
h4:hover{
|
93 |
+
cursor: pointer;
|
94 |
+
}
|
95 |
+
|
96 |
+
h3:hover{
|
97 |
+
cursor: pointer;
|
98 |
+
color: blue;
|
99 |
+
transform: scale(1.3);
|
100 |
+
}
|
101 |
+
.note{
|
102 |
+
text-align: center;
|
103 |
+
font-size: 16px;
|
104 |
+
}
|
105 |
+
p{
|
106 |
+
font-size: 13px;
|
107 |
+
text-align: center;
|
108 |
+
}
|
109 |
+
|
110 |
+
</style>
|
111 |
+
<h3><center><b>Citrus Canker</b></center></h3>
|
112 |
+
<h4>PESTICIDES TO BE USED:</h4>
|
113 |
+
<ul>
|
114 |
+
<li>1. Oxytetracycline (Terramycin)</li>
|
115 |
+
<li>2. Streptomycin (Streptomycin sulfate)</li>
|
116 |
+
<li>3. Mancozeb (Dithane)</li>
|
117 |
+
<li>4. Copper oxychloride (Kocide)</li>
|
118 |
+
<li>5. Azoxystrobin (Heritage)</li>
|
119 |
+
|
120 |
+
|
121 |
+
</ul>
|
122 |
+
<p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
|
123 |
+
<p>Be sure to follow local regulations and guidelines for application</p>
|
124 |
+
|
125 |
+
|
126 |
+
"""
|
127 |
+
elif predicted_label=='Citrus Black Spot':
|
128 |
+
predicted_label = """
|
129 |
+
<style>
|
130 |
+
li{
|
131 |
+
font-size: 15px;
|
132 |
+
margin-left: 90px;
|
133 |
+
margin-top: 15px;
|
134 |
+
margin-bottom: 15px;
|
135 |
+
}
|
136 |
+
h4{
|
137 |
+
font-size: 17px;
|
138 |
+
margin-top: 15px;
|
139 |
+
}
|
140 |
+
h4:hover{
|
141 |
+
cursor: pointer;
|
142 |
+
}
|
143 |
+
|
144 |
+
h3:hover{
|
145 |
+
cursor: pointer;
|
146 |
+
color: blue;
|
147 |
+
transform: scale(1.3);
|
148 |
+
}
|
149 |
+
.note{
|
150 |
+
text-align: center;
|
151 |
+
font-size: 16px;
|
152 |
+
}
|
153 |
+
p{
|
154 |
+
font-size: 13px;
|
155 |
+
text-align: center;
|
156 |
+
}
|
157 |
+
|
158 |
+
</style>
|
159 |
+
<h3><center><b>Citrus Black Spot</b></center></h3>
|
160 |
+
<h4>PESTICIDES TO BE USED:</h4>
|
161 |
+
<ul>
|
162 |
+
<li>1. Propiconazole (Tilt)</li>
|
163 |
+
<li>2. Chlorothalonil (Daconil)</li>
|
164 |
+
<li>3. Mancozeb (Dithane)</li>
|
165 |
+
<li>4. Azoxystrobin (Heritage)</li>
|
166 |
+
<li>5. Pyraclostrobin (Cabrio)</li>
|
167 |
+
|
168 |
+
|
169 |
+
</ul>
|
170 |
+
<p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
|
171 |
+
<p>Be sure to follow local regulations and guidelines for application</p>
|
172 |
+
|
173 |
+
|
174 |
+
"""
|
175 |
+
|
176 |
+
|
177 |
+
|
178 |
+
else:
|
179 |
+
predicted_label = """<h3 align="center">Choose Correct image</h3><br><br>
|
180 |
+
"""
|
181 |
+
|
182 |
+
return predicted_label
|
183 |
+
|
184 |
+
|
185 |
+
except Exception as e:
|
186 |
+
print(f"Error: {e}")
|
187 |
+
return None
|
188 |
+
|
189 |
+
# List of class labels
|
190 |
+
all_labels = [
|
191 |
+
'Citrus Greening',
|
192 |
+
'Citrus Canker',
|
193 |
+
'Citrus Healthy','Citrus Black Spot'
|
194 |
+
]
|
195 |
+
|
196 |
+
# Define the Gradio interface
|
197 |
+
def gradio_predict(image_file):
|
198 |
+
return predict_disease(image_file, model, all_labels) # Pass the model to the function
|
199 |
+
|
200 |
+
# Create a Gradio interface
|
201 |
+
gr_interface = gr.Interface(
|
202 |
+
fn=gradio_predict, # Function to call for predictions
|
203 |
+
inputs=gr.Image(type="filepath"), # Upload image as file path
|
204 |
+
outputs="html", # Output will be the class label as text
|
205 |
+
title="Citrus Disease Predictor",
|
206 |
+
description="Upload an image of a plant to predict the disease.",
|
207 |
+
)
|
208 |
+
|
209 |
+
# Launch the Gradio app
|
210 |
+
gr_interface.launch(share=True)
|