project21 commited on
Commit
73fce5b
·
verified ·
1 Parent(s): 02b5730

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -296
app.py CHANGED
@@ -1,205 +1,96 @@
 
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 = 'Mango.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=='Mango Anthracrose':
29
- predicted_label = """<style>
30
- li{
31
- font-size: 15px;
32
- margin-left: 90px;
33
- margin-top: 15px;
34
- margin-bottom: 15px;
35
- }
36
- h4{
37
- font-size: 17px;
38
- margin-top: 15px;
39
- }
40
- h4:hover{
41
- cursor: pointer;
42
- }
43
-
44
- h3:hover{
45
- cursor: pointer;
46
- color: blue;
47
- transform: scale(1.3);
48
- }
49
- .note{
50
- text-align: center;
51
- font-size: 16px;
52
- }
53
- p{
54
- font-size: 13px;
55
- text-align: center;
56
- }
57
-
58
- </style>
59
- <h3><center><b>Mango Anthracrose</b></center></h3>
60
- <h4>PESTICIDES TO BE USED:</h4>
61
- <ul>
62
  <li>1. Mancozeb</li>
63
  <li>2. Azoxystrobin</li>
64
- <li>3. carbendazim</li>
65
  <li>4. Propiconazole</li>
66
  <li>5. Thiophanate-methyl</li>
67
  <li>6. Copper Sulfate</li>
68
  </ul><br>
69
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
70
- <p>Be sure to follow local regulations and guidelines for application</p>
71
-
72
  """
73
- elif predicted_label=='Mango Bacterial Canker':
74
- predicted_label = """
75
- <style>
76
- li{
77
- font-size: 15px;
78
- margin-left: 90px;
79
- margin-top: 15px;
80
- margin-bottom: 15px;
81
- }
82
- h4{
83
- font-size: 17px;
84
- margin-top: 15px;
85
- }
86
- h4:hover{
87
- cursor: pointer;
88
- }
89
-
90
- h3:hover{
91
- cursor: pointer;
92
- color: blue;
93
- transform: scale(1.3);
94
- }
95
- .note{
96
- text-align: center;
97
- font-size: 16px;
98
- }
99
- p{
100
- font-size: 13px;
101
- text-align: center;
102
- }
103
-
104
- </style>
105
- <h3><center><b>Mango Bacterial Canker</b></center></h3>
106
- <h4>PESTICIDES TO BE USED:</h4>
107
- <ul>
108
  <li>1. Copper Hydroxide</li>
109
  <li>2. Copper Oxychloride</li>
110
  <li>3. Streptomycin</li>
111
  <li>4. oxytetracycline</li>
112
  <li>5. Neem oil</li>
113
  <li>6. Garlic oil</li>
114
- </ul>
115
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
116
- <p>Be sure to follow local regulations and guidelines for application</p>
117
-
118
-
119
  """
120
- elif predicted_label=='Mango Cutting Weevil':
121
- predicted_label = """
122
- <style>
123
- li{
124
- font-size: 15px;
125
- margin-left: 90px;
126
- margin-top: 15px;
127
- margin-bottom: 15px;
128
- }
129
- h4{
130
- font-size: 17px;
131
- margin-top: 15px;
132
- }
133
- h4:hover{
134
- cursor: pointer;
135
- }
136
-
137
- h3:hover{
138
- cursor: pointer;
139
- color: blue;
140
- transform: scale(1.3);
141
- }
142
- .note{
143
- text-align: center;
144
- font-size: 16px;
145
- }
146
- p{
147
- font-size: 13px;
148
- text-align: center;
149
- }
150
-
151
- </style>
152
- <h3><center><b>Mango Cutting Weevil</b></center></h3>
153
- <h4>PESTICIDES TO BE USED:</h4>
154
- <ul>
155
  <li>1. Imidacloprid</li>
156
  <li>2. Thiamethoxam</li>
157
  <li>3. Chlorpyrifos</li>
158
  <li>4. Lambda-cyhalothrin</li>
159
  <li>5. Fipronil</li>
160
  <li>6. Neem oil</li>
161
- </ul>
162
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
163
- <p>Be sure to follow local regulations and guidelines for application</p>
164
 
165
 
166
  """
167
-
168
- elif predicted_label=='Mango Die Back':
169
- predicted_label = """
170
- <style>
171
- li{
172
- font-size: 15px;
173
- margin-left: 90px;
174
- margin-top: 15px;
175
- margin-bottom: 15px;
176
- }
177
- h4{
178
- font-size: 17px;
179
- margin-top: 15px;
180
- }
181
- h4:hover{
182
- cursor: pointer;
183
- }
184
-
185
- h3:hover{
186
- cursor: pointer;
187
- color: blue;
188
- transform: scale(1.3);
189
- }
190
- .note{
191
- text-align: center;
192
- font-size: 16px;
193
- }
194
- p{
195
- font-size: 13px;
196
- text-align: center;
197
- }
198
-
199
- </style>
200
- <h3><center><b>Mango Die Back</b></center></h3>
201
- <h4>PESTICIDES TO BE USED:</h4>
202
- <ul>
203
  <li>1. Carbendazim</li>
204
  <li>2. Mancozeb</li>
205
  <li>3. Azoxystrobin</li>
@@ -207,46 +98,14 @@ def predict_disease(image_file, model, all_labels):
207
  <li>5. Potassium bicarbonate</li>
208
  <li>6. Sodium bicarbonate</li>
209
  </ul>
210
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
211
- <p>Be sure to follow local regulations and guidelines for application</p>
212
-
213
-
214
  """
215
- elif predicted_label=='Mango Gall Midge':
216
- predicted_label = """
217
- <style>
218
- li{
219
- font-size: 15px;
220
- margin-left: 90px;
221
- margin-top: 15px;
222
- margin-bottom: 15px;
223
- }
224
- h4{
225
- font-size: 17px;
226
- margin-top: 15px;
227
- }
228
- h4:hover{
229
- cursor: pointer;
230
- }
231
-
232
- h3:hover{
233
- cursor: pointer;
234
- color: blue;
235
- transform: scale(1.3);
236
- }
237
- .note{
238
- text-align: center;
239
- font-size: 16px;
240
- }
241
- p{
242
- font-size: 13px;
243
- text-align: center;
244
- }
245
-
246
- </style>
247
- <h3><center><b>Mango Gall Midge</b></center></h3>
248
- <h4>PESTICIDES TO BE USED:</h4>
249
- <ul>
250
  <li>1. Imidacloprid</li>
251
  <li>2. Thiamethoxam</li>
252
  <li>3. Chlorpyrifos</li>
@@ -254,46 +113,19 @@ def predict_disease(image_file, model, all_labels):
254
  <li>5. Spinosad</li>
255
  <li>6. Pyrethrin</li>
256
  </ul>
257
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
258
- <p>Be sure to follow local regulations and guidelines for application</p>
 
259
 
260
 
261
  """
262
- elif predicted_label=='Mango Powdery Mildew':
263
- predicted_label = """
264
- <style>
265
- li{
266
- font-size: 15px;
267
- margin-left: 90px;
268
- margin-top: 15px;
269
- margin-bottom: 15px;
270
- }
271
- h4{
272
- font-size: 17px;
273
- margin-top: 15px;
274
- }
275
- h4:hover{
276
- cursor: pointer;
277
- }
278
-
279
- h3:hover{
280
- cursor: pointer;
281
- color: blue;
282
- transform: scale(1.3);
283
- }
284
- .note{
285
- text-align: center;
286
- font-size: 16px;
287
- }
288
- p{
289
- font-size: 13px;
290
- text-align: center;
291
- }
292
-
293
- </style>
294
- <h3><center><b>Mango Powdery Mildew</b></center></h3>
295
- <h4>PESTICIDES TO BE USED:</h4>
296
- <ul>
297
  <li>1. Sulfur</li>
298
  <li>2. Bicarbonates</li>
299
  <li>3. Myclobutanil</li>
@@ -301,47 +133,14 @@ def predict_disease(image_file, model, all_labels):
301
  <li>5. Propiconazole</li>
302
  <li>6. Azoxystrobin</li>
303
  </ul>
304
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
305
- <p>Be sure to follow local regulations and guidelines for application</p>
306
-
307
-
308
  """
309
-
310
- elif predicted_label=='Mango Sooty Mould':
311
- predicted_label = """
312
- <style>
313
- li{
314
- font-size: 15px;
315
- margin-left: 90px;
316
- margin-top: 15px;
317
- margin-bottom: 15px;
318
- }
319
- h4{
320
- font-size: 17px;
321
- margin-top: 15px;
322
- }
323
- h4:hover{
324
- cursor: pointer;
325
- }
326
-
327
- h3:hover{
328
- cursor: pointer;
329
- color: blue;
330
- transform: scale(1.3);
331
- }
332
- .note{
333
- text-align: center;
334
- font-size: 16px;
335
- }
336
- p{
337
- font-size: 13px;
338
- text-align: center;
339
- }
340
-
341
- </style>
342
- <h3><center><b>Mango Sooty Mould</b></center></h3>
343
- <h4>PESTICIDES TO BE USED:</h4>
344
- <ul>
345
  <li>1. Imidacloprid (Neonicotinoid)</li>
346
  <li>2. Thiamethoxam (Neonicotinoid)</li>
347
  <li>3. Bifenthrin (Pyrethroid)</li>
@@ -349,21 +148,28 @@ def predict_disease(image_file, model, all_labels):
349
  <li>5. Insecticidal soap</li>
350
  <li>6. Horticultural oil</li>
351
  </ul>
352
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
353
- <p>Be sure to follow local regulations and guidelines for application</p>
354
-
355
-
356
- """
357
  else:
358
- predicted_label = """<h3 align="center">Mango Healthy</h3><br><br>
359
- <center>No need use Pesticides</center>"""
 
360
 
361
- return predicted_label
 
 
362
 
 
 
 
 
 
 
363
 
364
  except Exception as e:
365
- print(f"Error: {e}")
366
- return None
367
 
368
  # List of class labels
369
  all_labels = [
@@ -377,18 +183,43 @@ all_labels = [
377
  'Mango Sooty Mould'
378
  ]
379
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380
  # Define the Gradio interface
381
- def gradio_predict(image_file):
382
- return predict_disease(image_file, model, all_labels) # Pass the model to the function
 
 
383
 
384
- # Create a Gradio interface
385
  gr_interface = gr.Interface(
386
- fn=gradio_predict, # Function to call for predictions
387
- inputs=gr.Image(type="filepath"), # Upload image as file path
388
- outputs="html", # Output will be the class label as text
389
- title="""<center>Mango Disease Predictor</center>""",
390
- description="Upload an image of a plant to predict the disease.",
 
 
 
391
  )
392
 
393
  # Launch the Gradio app
394
- gr_interface.launch()
 
1
+ import h5py
2
  import gradio as gr
3
+ from tensorflow.keras.utils import img_to_array, load_img
4
  from keras.models import load_model
5
  import numpy as np
6
+ from deep_translator import GoogleTranslator
7
 
8
  # Load the pre-trained model from the local path
9
  model_path = 'Mango.h5'
 
10
 
11
+ # Check if the model is loading correctly
12
+ try:
13
+ with h5py.File(model_path, 'r+') as f:
14
+ if 'groups' in f.attrs['model_config']:
15
+ model_config_string = f.attrs['model_config']
16
+ model_config_string = model_config_string.replace('"groups": 1,', '')
17
+ model_config_string = model_config_string.replace('"groups": 1}', '}')
18
+ f.attrs['model_config'] = model_config_string.encode('utf-8')
19
+
20
+ model = load_model(model_path)
21
+ print("Model loaded successfully.")
22
+ except Exception as e:
23
+ print(f"Error loading model: {e}")
24
+
25
+ def predict_disease(image_file, model, all_labels, target_language):
26
  try:
27
  # Load and preprocess the image
28
+ print(f"Received image file: {image_file}")
29
+ img = load_img(image_file, target_size=(224, 224)) # Ensure image size matches model input
30
  img_array = img_to_array(img)
31
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
32
  img_array = img_array / 255.0 # Normalize the image
33
 
34
  # Predict the class
35
+ predictions = model.predict(img_array)
36
  predicted_class = np.argmax(predictions[0])
37
+
38
  # Get the predicted class label
39
  predicted_label = all_labels[predicted_class]
 
 
40
 
41
+ # Translate the predicted label to the selected language
42
+ translated_label = GoogleTranslator(source='en', target=target_language).translate(predicted_label)
43
+
44
+ # Provide pesticide information based on the predicted label
45
+ if predicted_label == 'Mango Anthracrose':
46
+ pesticide_info = """
47
+ <h2><center><b>Mango Anthracrose</b></center></h2>
48
+ <h4>PESTICIDES TO BE USED:</h4><br>
49
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  <li>1. Mancozeb</li>
51
  <li>2. Azoxystrobin</li>
52
+ <li>3. Carbendazim</li>
53
  <li>4. Propiconazole</li>
54
  <li>5. Thiophanate-methyl</li>
55
  <li>6. Copper Sulfate</li>
56
  </ul><br>
57
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
58
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
59
  """
60
+ elif predicted_label == 'Mango Bacterial Canker':
61
+ pesticide_info = """<h2><center><b>Mango Bacterial Canker</b></center></h2>
62
+ <h4>PESTICIDES TO BE USED:</h4><br>
63
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  <li>1. Copper Hydroxide</li>
65
  <li>2. Copper Oxychloride</li>
66
  <li>3. Streptomycin</li>
67
  <li>4. oxytetracycline</li>
68
  <li>5. Neem oil</li>
69
  <li>6. Garlic oil</li>
70
+ </ul><br>
71
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
72
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
 
73
  """
74
+ elif predicted_label == 'Mango Cutting Weevil':
75
+ pesticide_info = """<h2><center><b>Mango Cutting Weevil</b></center></h2>
76
+ <h4>PESTICIDES TO BE USED:</h4><br>
77
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  <li>1. Imidacloprid</li>
79
  <li>2. Thiamethoxam</li>
80
  <li>3. Chlorpyrifos</li>
81
  <li>4. Lambda-cyhalothrin</li>
82
  <li>5. Fipronil</li>
83
  <li>6. Neem oil</li>
84
+ </ul><br>
85
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
86
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
87
 
88
 
89
  """
90
+ elif predicted_label == 'Mango Die Back':
91
+ pesticide_info = """<h2><center><b>Mango Die Back</b></center></h2>
92
+ <h4>PESTICIDES TO BE USED:</h4><br>
93
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  <li>1. Carbendazim</li>
95
  <li>2. Mancozeb</li>
96
  <li>3. Azoxystrobin</li>
 
98
  <li>5. Potassium bicarbonate</li>
99
  <li>6. Sodium bicarbonate</li>
100
  </ul>
101
+ <br>
102
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
103
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
104
  """
105
+ elif predicted_label == 'Mango Gall Midge':
106
+ pesticide_info = """<h2><center><b>Mango Gall Midge</b></center></h2>
107
+ <h4>PESTICIDES TO BE USED:</h4><br>
108
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  <li>1. Imidacloprid</li>
110
  <li>2. Thiamethoxam</li>
111
  <li>3. Chlorpyrifos</li>
 
113
  <li>5. Spinosad</li>
114
  <li>6. Pyrethrin</li>
115
  </ul>
116
+ <br>
117
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
118
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
119
 
120
 
121
  """
122
+
123
+ elif predicted_label == 'Mango Healthy':
124
+ pesticide_info = 'No pesticides needed'
125
+ elif predicted_label == 'Mango Powdery Mildew':
126
+ pesticide_info = """<h2><center><b>Mango Powdery Mildew</b></center></h2>
127
+ <h4>PESTICIDES TO BE USED:</h4><br>
128
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  <li>1. Sulfur</li>
130
  <li>2. Bicarbonates</li>
131
  <li>3. Myclobutanil</li>
 
133
  <li>5. Propiconazole</li>
134
  <li>6. Azoxystrobin</li>
135
  </ul>
136
+ <br>
137
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
138
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
139
  """
140
+ elif predicted_label == 'Mango Sooty Mould':
141
+ pesticide_info = """<h2><center><b>Mango Sooty Mould</b></center></h2>
142
+ <h4>PESTICIDES TO BE USED:</h4><br>
143
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  <li>1. Imidacloprid (Neonicotinoid)</li>
145
  <li>2. Thiamethoxam (Neonicotinoid)</li>
146
  <li>3. Bifenthrin (Pyrethroid)</li>
 
148
  <li>5. Insecticidal soap</li>
149
  <li>6. Horticultural oil</li>
150
  </ul>
151
+ <br>
152
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
153
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>"""
 
 
154
  else:
155
+ pesticide_info = 'No pesticide information available.'
156
+
157
+ print(f"Pesticide Info (Before Translation): {pesticide_info}")
158
 
159
+ # Translate the pesticide information to the selected language
160
+ translated_pesticide_info = GoogleTranslator(source='en', target=target_language).translate(pesticide_info)
161
+ print(f"Translated Pesticide Info: {translated_pesticide_info}")
162
 
163
+ # Return translated label and pesticide information with associated styling
164
+ predicted_label_html = f"""
165
+
166
+ {translated_pesticide_info}
167
+ """
168
+ return predicted_label_html
169
 
170
  except Exception as e:
171
+ print(f"Error during prediction: {e}")
172
+ return f"<h3>Error: {e}</h3>"
173
 
174
  # List of class labels
175
  all_labels = [
 
183
  'Mango Sooty Mould'
184
  ]
185
 
186
+ # Language codes and their full names (display full names in dropdown)
187
+ language_choices = {
188
+ 'hi': 'Hindi',
189
+ 'te': 'Telugu',
190
+ 'en': 'English',
191
+ 'ml': 'Malayalam',
192
+ 'ta': 'Tamil',
193
+ 'or': 'Odia/Odisi',
194
+ 'bn': 'Bengali',
195
+ 'gu': 'Gujarati',
196
+ 'kn': 'Kannada',
197
+ 'mr': 'Marathi'
198
+ }
199
+
200
+ # Mapping full names back to their corresponding language code
201
+ full_to_code = {value: key for key, value in language_choices.items()}
202
+
203
+ # Create a dropdown of full language names, using the full name in the UI
204
+ languages = list(language_choices.values()) # List of full language names
205
+
206
  # Define the Gradio interface
207
+ def gradio_predict(image_file, target_language):
208
+ # Map full name back to language code for translation
209
+ language_code = full_to_code.get(target_language, 'en')
210
+ return predict_disease(image_file, model, all_labels, language_code)
211
 
212
+ # Create the Gradio interface
213
  gr_interface = gr.Interface(
214
+ fn=gradio_predict,
215
+ inputs=[
216
+ gr.Image(type="filepath"), # Image input for disease prediction
217
+ gr.Dropdown(label="Select language", choices=languages, value='Choose language') # Language selection dropdown with full names
218
+ ],
219
+ outputs="html", # Output will be in HTML (translated text)
220
+ title="Plant Disease Predictor",
221
+ description="Upload an image of a plant to predict the disease and get the translated label and pesticide information in the selected language."
222
  )
223
 
224
  # Launch the Gradio app
225
+ gr_interface.launch()