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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = '/content/tomato.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=='Tomato Yellow Leaf Curl Virus':
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>Tomato Yellow Leaf Curl Virus</b></center></h3>
60
+ <h4>PESTICIDES TO BE USED:</h4>
61
+ <ul>
62
+ <li>1. imidacloprid</li>
63
+ <li>2. thiamethoxam</li>
64
+ <li>3. Spinosad</li>
65
+ <li>4. Acetamiprid</li>
66
+
67
+ </ul><br>
68
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
69
+ <p>Be sure to follow local regulations and guidelines for application</p>
70
+
71
+ """
72
+ elif predicted_label=='Tomato Target Spot':
73
+ predicted_label = """
74
+ <style>
75
+ li{
76
+ font-size: 15px;
77
+ margin-left: 90px;
78
+ margin-top: 15px;
79
+ margin-bottom: 15px;
80
+ }
81
+ h4{
82
+ font-size: 17px;
83
+ margin-top: 15px;
84
+ }
85
+ h4:hover{
86
+ cursor: pointer;
87
+ }
88
+
89
+ h3:hover{
90
+ cursor: pointer;
91
+ color: blue;
92
+ transform: scale(1.3);
93
+ }
94
+ .note{
95
+ text-align: center;
96
+ font-size: 16px;
97
+ }
98
+ p{
99
+ font-size: 13px;
100
+ text-align: center;
101
+ }
102
+
103
+ </style>
104
+ <h3><center><b>Tomato Target Spot</b></center></h3>
105
+ <h4>PESTICIDES TO BE USED:</h4>
106
+ <ul>
107
+ <li>1. Azoxystrobin</li>
108
+ <li>2. Boscalid</li>
109
+ <li>3. Mancozeb</li>
110
+ <li>4. Chlorothalonil</li>
111
+ <li>5. Propiconazole</li>
112
+
113
+ </ul>
114
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
115
+ <p>Be sure to follow local regulations and guidelines for application</p>
116
+
117
+
118
+ """
119
+ elif predicted_label=='Tomato Spider mites':
120
+ predicted_label = """
121
+ <style>
122
+ li{
123
+ font-size: 15px;
124
+ margin-left: 90px;
125
+ margin-top: 15px;
126
+ margin-bottom: 15px;
127
+ }
128
+ h4{
129
+ font-size: 17px;
130
+ margin-top: 15px;
131
+ }
132
+ h4:hover{
133
+ cursor: pointer;
134
+ }
135
+
136
+ h3:hover{
137
+ cursor: pointer;
138
+ color: blue;
139
+ transform: scale(1.3);
140
+ }
141
+ .note{
142
+ text-align: center;
143
+ font-size: 16px;
144
+ }
145
+ p{
146
+ font-size: 13px;
147
+ text-align: center;
148
+ }
149
+
150
+ </style>
151
+ <h3><center><b>Tomato Spider mites</b></center></h3>
152
+ <h4>PESTICIDES TO BE USED:</h4>
153
+ <ul>
154
+ <li>1. Abamectin</li>
155
+ <li>2. Spiromesifen</li>
156
+ <li>3. Miticides</li>
157
+ <li>4. insecticidal soap</li>
158
+
159
+ <li>5. Neem oil</li>
160
+ </ul>
161
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
162
+ <p>Be sure to follow local regulations and guidelines for application</p>
163
+
164
+
165
+ """
166
+
167
+ elif predicted_label=='Tomato Septoria leaf spot':
168
+ predicted_label = """
169
+ <style>
170
+ li{
171
+ font-size: 15px;
172
+ margin-left: 90px;
173
+ margin-top: 15px;
174
+ margin-bottom: 15px;
175
+ }
176
+ h4{
177
+ font-size: 17px;
178
+ margin-top: 15px;
179
+ }
180
+ h4:hover{
181
+ cursor: pointer;
182
+ }
183
+
184
+ h3:hover{
185
+ cursor: pointer;
186
+ color: blue;
187
+ transform: scale(1.3);
188
+ }
189
+ .note{
190
+ text-align: center;
191
+ font-size: 16px;
192
+ }
193
+ p{
194
+ font-size: 13px;
195
+ text-align: center;
196
+ }
197
+
198
+ </style>
199
+ <h3><center><b>Tomato Septoria leaf spot</b></center></h3>
200
+ <h4>PESTICIDES TO BE USED:</h4>
201
+ <ul>
202
+ <li>1. Azoxystrobin</li>
203
+ <li>2. Boscalid</li>
204
+ <li>3. Mancozeb</li>
205
+ <li>4. Chlorothalonil</li>
206
+ <li>5. Propiconazole</li>
207
+ </ul>
208
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
209
+ <p>Be sure to follow local regulations and guidelines for application</p>
210
+
211
+
212
+ """
213
+ elif predicted_label=='Tomato Mosaic virus':
214
+ predicted_label = """
215
+ <style>
216
+ li{
217
+ font-size: 15px;
218
+ margin-left: 90px;
219
+ margin-top: 15px;
220
+ margin-bottom: 15px;
221
+ }
222
+ h4{
223
+ font-size: 17px;
224
+ margin-top: 15px;
225
+ }
226
+ h4:hover{
227
+ cursor: pointer;
228
+ }
229
+
230
+ h3:hover{
231
+ cursor: pointer;
232
+ color: blue;
233
+ transform: scale(1.3);
234
+ }
235
+ .note{
236
+ text-align: center;
237
+ font-size: 16px;
238
+ }
239
+ p{
240
+ font-size: 13px;
241
+ text-align: center;
242
+ }
243
+
244
+ </style>
245
+ <h3><center><b>Tomato Mosaic virus</b></center></h3>
246
+ <h4>PESTICIDES TO BE USED:</h4>
247
+ <ul>
248
+ <li>1. Imidacloprid</li>
249
+ <li>2. Thiamethoxam</li>
250
+ <li>3. Acetamiprid</li>
251
+ <li>4. Dinotefuran</li>
252
+ <li>5. Pyrethrin</li>
253
+
254
+ </ul>
255
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
256
+ <p>Be sure to follow local regulations and guidelines for application</p>
257
+
258
+
259
+ """
260
+ elif predicted_label=='Tomato Leaf Mold':
261
+ predicted_label = """
262
+ <style>
263
+ li{
264
+ font-size: 15px;
265
+ margin-left: 90px;
266
+ margin-top: 15px;
267
+ margin-bottom: 15px;
268
+ }
269
+ h4{
270
+ font-size: 17px;
271
+ margin-top: 15px;
272
+ }
273
+ h4:hover{
274
+ cursor: pointer;
275
+ }
276
+
277
+ h3:hover{
278
+ cursor: pointer;
279
+ color: blue;
280
+ transform: scale(1.3);
281
+ }
282
+ .note{
283
+ text-align: center;
284
+ font-size: 16px;
285
+ }
286
+ p{
287
+ font-size: 13px;
288
+ text-align: center;
289
+ }
290
+
291
+ </style>
292
+ <h3><center><b>Tomato Leaf Mold</b></center></h3>
293
+ <h4>PESTICIDES TO BE USED:</h4>
294
+ <ul>
295
+ <li>1. Azoxystrobin</li>
296
+ <li>2. Boscalid</li>
297
+ <li>3. Mancozeb</li>
298
+ <li>4. Chlorothalonil</li>
299
+ <li>5. Propiconazole</li>
300
+ </ul>
301
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
302
+ <p>Be sure to follow local regulations and guidelines for application</p>
303
+
304
+
305
+ """
306
+
307
+ elif predicted_label=='Tomato Late blight':
308
+ predicted_label = """
309
+ <style>
310
+ li{
311
+ font-size: 15px;
312
+ margin-left: 90px;
313
+ margin-top: 15px;
314
+ margin-bottom: 15px;
315
+ }
316
+ h4{
317
+ font-size: 17px;
318
+ margin-top: 15px;
319
+ }
320
+ h4:hover{
321
+ cursor: pointer;
322
+ }
323
+
324
+ h3:hover{
325
+ cursor: pointer;
326
+ color: blue;
327
+ transform: scale(1.3);
328
+ }
329
+ .note{
330
+ text-align: center;
331
+ font-size: 16px;
332
+ }
333
+ p{
334
+ font-size: 13px;
335
+ text-align: center;
336
+ }
337
+
338
+ </style>
339
+ <h3><center><b>Tomato blight</b></center></h3>
340
+ <h4>PESTICIDES TO BE USED:</h4>
341
+ <ul>
342
+ <li>1. metalaxl</li>
343
+ <li>2. Chlorothalonil</li>
344
+ <li>3. Mancozeb</li>
345
+ <li>4. Copper oxychloride</li>
346
+ <li>5. Azoxystrobin</li>
347
+
348
+ </ul>
349
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
350
+ <p>Be sure to follow local regulations and guidelines for application</p>
351
+
352
+
353
+ """
354
+ elif predicted_label=='Tomato Early blight':
355
+ predicted_label = """
356
+ <style>
357
+ li{
358
+ font-size: 15px;
359
+ margin-left: 90px;
360
+ margin-top: 15px;
361
+ margin-bottom: 15px;
362
+ }
363
+ h4{
364
+ font-size: 17px;
365
+ margin-top: 15px;
366
+ }
367
+ h4:hover{
368
+ cursor: pointer;
369
+ }
370
+
371
+ h3:hover{
372
+ cursor: pointer;
373
+ color: blue;
374
+ transform: scale(1.3);
375
+ }
376
+ .note{
377
+ text-align: center;
378
+ font-size: 16px;
379
+ }
380
+ p{
381
+ font-size: 13px;
382
+ text-align: center;
383
+ }
384
+
385
+ </style>
386
+ <h3><center><b>Tomato blight</b></center></h3>
387
+ <h4>PESTICIDES TO BE USED:</h4>
388
+ <ul>
389
+ <li>1. Azoxystrobin</li>
390
+ <li>2. Boscalid</li>
391
+ <li>3. Mancozeb</li>
392
+ <li>4. Chlorothalonil</li>
393
+ <li>5. Propiconazole</li>
394
+ </ul>
395
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
396
+ <p>Be sure to follow local regulations and guidelines for application</p>
397
+
398
+
399
+ """
400
+ elif predicted_label=='Tomato Bacterial spot':
401
+ predicted_label = """
402
+ <style>
403
+ li{
404
+ font-size: 15px;
405
+ margin-left: 90px;
406
+ margin-top: 15px;
407
+ margin-bottom: 15px;
408
+ }
409
+ h4{
410
+ font-size: 17px;
411
+ margin-top: 15px;
412
+ }
413
+ h4:hover{
414
+ cursor: pointer;
415
+ }
416
+
417
+ h3:hover{
418
+ cursor: pointer;
419
+ color: blue;
420
+ transform: scale(1.3);
421
+ }
422
+ .note{
423
+ text-align: center;
424
+ font-size: 16px;
425
+ }
426
+ p{
427
+ font-size: 13px;
428
+ text-align: center;
429
+ }
430
+
431
+ </style>
432
+ <h3><center><b>Tomato Bacterial spot</b></center></h3>
433
+ <h4>PESTICIDES TO BE USED:</h4>
434
+ <ul>
435
+ <li>1. Copper oxychloride</li>
436
+ <li>2. Streptomycin</li>
437
+ <li>3. tetracycline</li>
438
+ <li>4. Oxytetracline(Terramycin)</li>
439
+ <li>5. Insecticidal soap</li>
440
+ <li>6. Horticultural oil</li>
441
+ </ul>
442
+ <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
443
+ <p>Be sure to follow local regulations and guidelines for application</p>
444
+
445
+
446
+ """
447
+
448
+ elif predicted_label=='Tomato Healthy':
449
+
450
+ predicted_label = """<h3 align="center">Tomato Healthy</h3><br><br>
451
+ <center>No need use Pesticides</center>"""
452
+ else:
453
+ predict_label="choose correct image"
454
+
455
+ return predicted_label
456
+
457
+
458
+ except Exception as e:
459
+ print(f"Error: {e}")
460
+ return None
461
+
462
+ # List of class labels
463
+ all_labels = [
464
+ 'Tomato Yellow Leaf Curl Virus',
465
+ 'Tomato Target Spot',
466
+ 'Tomato Spider mites',
467
+ 'Tomato Septoria leaf spot',
468
+ 'Tomato Mosaic virus',
469
+ 'Tomato Leaf Mold',
470
+ 'Tomato Late blight',
471
+ 'Tomato Healthy',
472
+ 'Tomato Early blight',
473
+ 'Tomato Bacterial spot'
474
+ ]
475
+
476
+ # Define the Gradio interface
477
+ def gradio_predict(image_file):
478
+ return predict_disease(image_file, model, all_labels) # Pass the model to the function
479
+
480
+ # Create a Gradio interface
481
+ gr_interface = gr.Interface(
482
+ fn=gradio_predict, # Function to call for predictions
483
+ inputs=gr.Image(type="filepath"), # Upload image as file path
484
+ outputs="html", # Output will be the class label as text
485
+ title="Tomato Disease Predictor",
486
+ description="Upload an image of a plant to predict the disease.",
487
+ )
488
+
489
+ # Launch the Gradio app
490
+
491
+ gr_interface.launch(share=True)