isana25 commited on
Commit
12d776a
Β·
verified Β·
1 Parent(s): 4f88f01

Upload 4 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. alert-1.mp3 +3 -0
  3. app.py +660 -39
  4. requirements.txt +4 -1
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ alert-1.mp3 filter=lfs diff=lfs merge=lfs -text
alert-1.mp3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bcc5a64db2908be4676e69a58a773925b3b36b9f7ea841fced19a8e4e4d05baf
3
+ size 911040
app.py CHANGED
@@ -1,46 +1,667 @@
1
  from ultralytics import YOLO
2
  import gradio as gr
 
3
  from PIL import Image
4
 
5
- # Load YOLOv5 model
6
- model = YOLO("best.pt") # make sure best.pt is in the same directory
7
 
8
- # You can define a simple disease severity mapping like this:
9
- disease_severity = {
10
- "Late_blight": "High",
11
- "Early_blight": "Medium",
12
- "Powdery_mildew": "Low",
13
- "Healthy": "None",
14
- # Add your disease names and severity levels here accordingly
15
- }
16
 
17
- # Prediction function
18
  def predict(image):
19
- results = model(image)
20
- boxes = results[0].boxes
21
- names = model.names
22
- output = ""
23
-
24
- if len(boxes) == 0:
25
- output = "βœ… Healthy: No disease detected!"
26
- else:
27
- for box in boxes:
28
- class_id = int(box.cls[0])
29
- disease_name = names[class_id]
30
- confidence = float(box.conf[0])
31
-
32
- severity = disease_severity.get(disease_name, "Unknown")
33
-
34
- output += f"⚠️ Disease: {disease_name}\n"
35
- output += f"❗ Seriousness Level: {severity}\n"
36
- output += f"πŸ” Confidence: {confidence * 100:.2f}%\n\n"
37
-
38
- return output.strip()
39
-
40
- gr.Interface(
41
- fn=predict,
42
- inputs=gr.Image(type="pil"),
43
- outputs="text",
44
- title="🌿 AgroScan: Plant Disease Detector",
45
- description="Upload a plant leaf image to detect disease and get disease seriousness and confidence.",
46
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from ultralytics import YOLO
2
  import gradio as gr
3
+ import numpy as np
4
  from PIL import Image
5
 
6
+ # Load YOLO model
7
+ model = YOLO("best.pt")
8
 
9
+ # Define what class names indicate a human
10
+ HUMAN_CLASSES = ["person", "human"]
 
 
 
 
 
 
11
 
12
+ # Predict Function
13
  def predict(image):
14
+ image_status = "βœ… Real Image"
15
+ disease_status = "βœ… No Disease Detected"
16
+ details = ""
17
+ audio_html = "alert-1.mp3" # for autoplay sound
18
+
19
+ try:
20
+ results = model(image)
21
+ boxes = results[0].boxes
22
+ class_names = model.names
23
+
24
+ found_human = False
25
+ found_disease = False
26
+
27
+ if len(boxes) > 0:
28
+ for box in boxes:
29
+ class_id = int(box.cls[0])
30
+ class_name = class_names[class_id].lower()
31
+ confidence = float(box.conf[0])
32
+ details += f"πŸ”Ž {class_name} - {confidence*100:.2f}%\n"
33
+
34
+ if class_name in HUMAN_CLASSES:
35
+ found_human = True
36
+ else:
37
+ found_disease = True
38
+
39
+ if found_human:
40
+ disease_status = "🚨 Human Detected (Invalid Input)"
41
+ audio_html = """<audio autoplay><source src="file/disease_alert.mp3" type="audio/mpeg"></audio>"""
42
+ elif found_disease:
43
+ disease_status = "🚨 Disease Detected"
44
+ audio_html = """<audio autoplay><source src="file/disease_alert.mp3" type="audio/mpeg"></audio>"""
45
+ else:
46
+ disease_status = "βœ… No Disease Detected"
47
+
48
+ except Exception as e:
49
+ image_status = "❌ Error"
50
+ disease_status = "⚠️ Detection Failed"
51
+ details = str(e)
52
+
53
+ return image_status, disease_status, details, audio_html
54
+
55
+
56
+ # Gradio Interface
57
+ with gr.Blocks(title="🚨 AgroScan - Human Alert + Disease Detection") as demo:
58
+ gr.Markdown("""
59
+ <h1 style="text-align:center; color:#2e7d32;">🌿 AgroScan</h1>
60
+ <p style="text-align:center;">
61
+ Upload an image of a plant leaf. If a human or disease is detected, an alert will be triggered with sound 🚨.
62
+ </p>
63
+ """)
64
+
65
+ with gr.Row():
66
+ with gr.Column(scale=1):
67
+ image_input = gr.Image(type="pil", label="πŸ“· Upload Image")
68
+ submit_btn = gr.Button("πŸ” Analyze")
69
+ with gr.Column(scale=1):
70
+ result1 = gr.Textbox(label="πŸ–ΌοΈ Image Status")
71
+ result2 = gr.Textbox(label="🌱 Disease Status")
72
+ result3 = gr.Textbox(label="πŸ“‹ Detection Details", lines=5)
73
+ audio_out = gr.HTML(label="🚨 Auto Alert") # autoplay sound (no controls)
74
+
75
+ submit_btn.click(fn=predict, inputs=image_input,
76
+ outputs=[result1, result2, result3, audio_out])
77
+
78
+ demo.launch()
79
+
80
+
81
+
82
+
83
+
84
+ # from ultralytics import YOLO
85
+ # import gradio as gr
86
+ # import numpy as np
87
+ # from PIL import Image
88
+
89
+ # # Load YOLO model
90
+ # model = YOLO("best.pt")
91
+
92
+ # # Define human classes
93
+ # HUMAN_CLASSES = ["person", "human"]
94
+
95
+ # # Prediction function
96
+ # def predict(image):
97
+ # image_status = "βœ… Real Image"
98
+ # disease_status = "βœ… No Disease Detected"
99
+ # details = ""
100
+ # alert_sound = "alert-1.mp3" # Will hold sound file path
101
+
102
+ # try:
103
+ # results = model(image)
104
+ # boxes = results[0].boxes
105
+ # class_names = model.names
106
+
107
+ # found_human = False
108
+ # found_disease = False
109
+
110
+ # if len(boxes) > 0:
111
+ # for box in boxes:
112
+ # class_id = int(box.cls[0])
113
+ # class_name = class_names[class_id].lower()
114
+ # confidence = float(box.conf[0])
115
+ # details += f"πŸ”Ž {class_name} - {confidence*100:.2f}%\n"
116
+
117
+ # if class_name in HUMAN_CLASSES:
118
+ # found_human = True
119
+ # else:
120
+ # found_disease = True
121
+
122
+ # if found_human:
123
+ # disease_status = "🚨 Human Detected (Invalid Input)"
124
+ # alert_sound = "disease_alert.mp3"
125
+ # elif found_disease:
126
+ # disease_status = "🚨 Disease Detected"
127
+ # alert_sound = "disease_alert.mp3"
128
+ # else:
129
+ # disease_status = "βœ… No Disease Detected"
130
+
131
+ # except Exception as e:
132
+ # image_status = "❌ Error"
133
+ # disease_status = "⚠️ Detection Failed"
134
+ # details = str(e)
135
+
136
+ # return image_status, disease_status, details, alert_sound
137
+
138
+ # # Gradio Interface
139
+ # with gr.Blocks(title="🚨 AgroScan - Human Alert + Disease Detection") as demo:
140
+ # gr.Markdown("""
141
+ # <h1 style="text-align:center; color:#2e7d32;">🌿 AgroScan</h1>
142
+ # <p style="text-align:center;">
143
+ # Upload an image of a plant leaf. If a human or disease is detected, an alert will be triggered with sound 🚨.
144
+ # </p>
145
+ # """)
146
+
147
+ # with gr.Row():
148
+ # with gr.Column(scale=1):
149
+ # image_input = gr.Image(type="pil", label="πŸ“· Upload Image")
150
+ # submit_btn = gr.Button("πŸ” Analyze")
151
+ # with gr.Column(scale=1):
152
+ # result1 = gr.Textbox(label="πŸ–ΌοΈ Image Status")
153
+ # result2 = gr.Textbox(label="🌱 Disease Status")
154
+ # result3 = gr.Textbox(label="πŸ“‹ Detection Details", lines=5)
155
+ # audio_out = gr.Audio(label="🚨 Alert Sound", autoplay=True)
156
+
157
+ # submit_btn.click(fn=predict, inputs=image_input,
158
+ # outputs=[result1, result2, result3, audio_out])
159
+
160
+ # demo.launch()
161
+
162
+
163
+
164
+
165
+ # from ultralytics import YOLO
166
+ # import gradio as gr
167
+ # import numpy as np
168
+ # from PIL import Image
169
+
170
+ # # Load YOLO model
171
+ # model = YOLO("best.pt")
172
+
173
+ # # Define what class names indicate a human
174
+ # HUMAN_CLASSES = ["person", "human"]
175
+
176
+ # # Predict Function
177
+ # def predict(image):
178
+ # image_status = "βœ… Real Image"
179
+ # disease_status = "βœ… No Disease Detected"
180
+ # details = ""
181
+ # alert_sound = None # path to MP3 file
182
+
183
+ # try:
184
+ # results = model(image)
185
+ # boxes = results[0].boxes
186
+ # class_names = model.names
187
+
188
+ # found_human = False
189
+ # found_disease = False
190
+
191
+ # if len(boxes) > 0:
192
+ # for box in boxes:
193
+ # class_id = int(box.cls[0])
194
+ # class_name = class_names[class_id].lower()
195
+ # confidence = float(box.conf[0])
196
+ # details += f"πŸ”Ž {class_name} - {confidence*100:.2f}%\n"
197
+
198
+ # if class_name in HUMAN_CLASSES:
199
+ # found_human = True
200
+ # else:
201
+ # found_disease = True
202
+
203
+ # if found_human:
204
+ # disease_status = "🚨 Human Detected (Invalid Input)"
205
+ # alert_sound = "disease_alert.mp3"
206
+ # elif found_disease:
207
+ # disease_status = "🚨 Disease Detected"
208
+ # alert_sound = "disease_alert.mp3"
209
+ # else:
210
+ # disease_status = "βœ… No Disease Detected"
211
+
212
+ # except Exception as e:
213
+ # image_status = "❌ Error"
214
+ # disease_status = "⚠️ Detection Failed"
215
+ # details = str(e)
216
+
217
+ # return image_status, disease_status, details, alert_sound
218
+
219
+ # # Gradio Interface
220
+ # with gr.Blocks(title="🚨 AgroScan - Human Alert + Disease Detection") as demo:
221
+ # gr.Markdown("""
222
+ # <h1 style="text-align:center; color:#2e7d32;">🌿 AgroScan</h1>
223
+ # <p style="text-align:center;">
224
+ # Upload an image of a plant leaf. If a human or disease is detected, an alert will be triggered with sound 🚨.
225
+ # </p>
226
+ # """)
227
+
228
+ # with gr.Row():
229
+ # with gr.Column(scale=1):
230
+ # image_input = gr.Image(type="pil", label="πŸ“· Upload Image")
231
+ # submit_btn = gr.Button("πŸ” Analyze")
232
+ # with gr.Column(scale=1):
233
+ # result1 = gr.Textbox(label="πŸ–ΌοΈ Image Status")
234
+ # result2 = gr.Textbox(label="🌱 Disease Status")
235
+ # result3 = gr.Textbox(label="πŸ“‹ Detection Details", lines=5)
236
+ # audio_out = gr.Audio(label="🚨 Alert Sound", autoplay=True)
237
+
238
+ # submit_btn.click(fn=predict, inputs=image_input,
239
+ # outputs=[result1, result2, result3, audio_out])
240
+
241
+ # demo.launch()
242
+
243
+
244
+
245
+
246
+
247
+ # from ultralytics import YOLO
248
+ # import gradio as gr
249
+ # import numpy as np
250
+ # from PIL import Image
251
+
252
+ # # Load the trained YOLOv8 model
253
+ # model = YOLO("best.pt")
254
+
255
+ # # Class name that represents "human" in your YOLO model
256
+ # HUMAN_CLASS_NAMES = ["person", "human"]
257
+
258
+ # # Prediction function with sound logic
259
+ # def predict(image):
260
+ # image_status = "βœ… Real Image"
261
+ # disease_status = "βœ… No Disease Detected"
262
+ # disease_details = ""
263
+ # play_alert = None # MP3 to be played
264
+
265
+ # try:
266
+ # results = model(image)
267
+ # boxes = results[0].boxes
268
+ # names = model.names
269
+
270
+ # found_human = False
271
+ # if len(boxes) > 0:
272
+ # for box in boxes:
273
+ # class_id = int(box.cls[0])
274
+ # class_name = names[class_id]
275
+ # confidence = float(box.conf[0])
276
+ # disease_details += f"πŸ” {class_name} - {confidence*100:.2f}%\n"
277
+
278
+ # if class_name.lower() in HUMAN_CLASS_NAMES:
279
+ # found_human = True
280
+
281
+ # if found_human:
282
+ # disease_status = "🚨 Disease Detected (Human Image)"
283
+ # play_alert = "disease_alert.mp3" # make sure this file exists
284
+ # elif len(boxes) == 0:
285
+ # disease_status = "βœ… No Disease Detected"
286
+ # else:
287
+ # disease_status = "🚨 Disease Detected"
288
+ # play_alert = "disease_alert.mp3"
289
+
290
+ # except Exception as e:
291
+ # disease_status = "❌ Error during detection"
292
+ # disease_details = str(e)
293
+
294
+ # return image_status, disease_status, disease_details, play_alert
295
+
296
+ # # Gradio UI
297
+ # with gr.Blocks(title="AgroScan - Human Alert + Disease Detection") as demo:
298
+ # gr.Markdown("""
299
+ # <div style="text-align: center;">
300
+ # <h1 style="color: #2e7d32;">🌿 AgroScan: Plant Disease Detector + Human Alert</h1>
301
+ # <p style="font-size: 16px; color: #555;">
302
+ # Upload an image of a leaf or a human. If human is detected, an alert will be triggered.
303
+ # </p>
304
+ # </div>
305
+ # """)
306
+
307
+ # with gr.Row():
308
+ # with gr.Column(scale=1):
309
+ # image_input = gr.Image(type="pil", label="πŸ“· Upload Image")
310
+ # submit_btn = gr.Button("πŸ” Analyze")
311
+ # with gr.Column(scale=1):
312
+ # image_result = gr.Textbox(label="πŸ–ΌοΈ Image Authenticity", interactive=False)
313
+ # disease_result = gr.Textbox(label="🌱 Disease Status", interactive=False)
314
+ # detail = gr.Textbox(label="πŸ“‹ Details", lines=5, interactive=False)
315
+ # sound_output = gr.Audio(label="🚨 Alert Sound", autoplay=True, interactive=False)
316
+
317
+ # submit_btn.click(fn=predict, inputs=image_input,
318
+ # outputs=[image_result, disease_result, detail, sound_output])
319
+
320
+ # demo.launch()
321
+
322
+
323
+
324
+
325
+ # from ultralytics import YOLO
326
+ # import gradio as gr
327
+ # import numpy as np
328
+ # from PIL import Image
329
+
330
+ # # Load the trained YOLOv8 model
331
+ # model = YOLO("best.pt") # Make sure 'best.pt' is in the same directory
332
+
333
+ # # Check if image is real (simple check using NumPy array and format)
334
+ # def is_real_image(image):
335
+ # try:
336
+ # if image is None:
337
+ # return False
338
+ # arr = np.array(image)
339
+ # if arr.ndim == 3 and arr.shape[2] in [3, 4]: # RGB or RGBA
340
+ # return True
341
+ # return False
342
+ # except Exception as e:
343
+ # print(f"Image check error: {e}")
344
+ # return False
345
+
346
+ # # Prediction function
347
+ # def predict(image):
348
+ # image_status = "❌ Fake Image"
349
+ # disease_status = "⚠️ Unknown"
350
+ # disease_details = ""
351
+
352
+ # # Step 1: Image authenticity check
353
+ # if is_real_image(image):
354
+ # image_status = "βœ… Real Image"
355
+ # else:
356
+ # return image_status, "⚠️ Cannot detect disease in a fake image.", ""
357
+
358
+ # # Step 2: YOLOv8 disease detection
359
+ # try:
360
+ # results = model(image)
361
+ # boxes = results[0].boxes
362
+ # names = model.names
363
+
364
+ # if len(boxes) == 0:
365
+ # disease_status = "βœ… No Disease Detected"
366
+ # else:
367
+ # disease_status = "🚨 Disease Detected"
368
+ # for box in boxes:
369
+ # class_id = int(box.cls[0])
370
+ # class_name = names[class_id]
371
+ # confidence = float(box.conf[0])
372
+ # disease_details += f"πŸ”¬ {class_name} - Confidence: {confidence*100:.2f}%\n"
373
+ # except Exception as e:
374
+ # disease_status = "❌ Error during detection"
375
+ # disease_details = str(e)
376
+
377
+ # return image_status, disease_status, disease_details
378
+
379
+ # # Gradio UI
380
+ # with gr.Blocks(title="AgroScan - Plant Disease & Image Verifier") as demo:
381
+ # gr.Markdown("""
382
+ # <div style="text-align: center;">
383
+ # <h1 style="color: #2e7d32;">🌿 AgroScan: Plant Disease Detector + Image Validator</h1>
384
+ # <p style="font-size: 16px; color: #555;">
385
+ # Upload a plant leaf image πŸƒ to check if it's real or fake and detect any diseases using YOLOv8.
386
+ # </p>
387
+ # </div>
388
+ # """)
389
+
390
+ # with gr.Row():
391
+ # with gr.Column(scale=1):
392
+ # image_input = gr.Image(type="pil", label="πŸ“· Upload Leaf Image")
393
+ # submit_btn = gr.Button("πŸ” Analyze")
394
+ # with gr.Column(scale=1):
395
+ # image_result = gr.Textbox(label="πŸ–ΌοΈ Image Authenticity", interactive=False)
396
+ # disease_result = gr.Textbox(label="🌱 Disease Status", interactive=False)
397
+ # detail = gr.Textbox(label="πŸ“‹ Detailed Diagnosis", lines=5, interactive=False)
398
+
399
+ # with gr.Accordion("ℹ️ What is AgroScan?", open=False):
400
+ # gr.Markdown("""
401
+ # AgroScan is an AI-powered detector for identifying plant diseases from leaf images.
402
+ # It also verifies whether the image is genuine (real leaf image) or fake to avoid misdiagnosis.
403
+ # Designed to support farmers, agronomists, and researchers for better crop care.
404
+ # """)
405
+
406
+ # submit_btn.click(fn=predict, inputs=image_input,
407
+ # outputs=[image_result, disease_result, detail])
408
+
409
+ # demo.launch()
410
+
411
+
412
+
413
+
414
+
415
+ # from ultralytics import YOLO
416
+ # import gradio as gr
417
+ # from PIL import Image
418
+
419
+ # # Load YOLO model
420
+ # model = YOLO("best.pt") # Make sure 'best.pt' is in your directory
421
+
422
+ # # Function to check if image is real
423
+ # def is_real_image(img):
424
+ # try:
425
+ # return isinstance(img, Image.Image) and img.mode in ['RGB', 'RGBA'] and img.size[0] > 50 and img.size[1] > 50
426
+ # except:
427
+ # return False
428
+
429
+ # # Main prediction function with error handling
430
+ # def predict(image):
431
+ # try:
432
+ # if image is None:
433
+ # return "❌ No image provided", "⚠️ Cannot check disease", "Upload a valid image first."
434
+
435
+ # # Step 1: Check image authenticity
436
+ # if is_real_image(image):
437
+ # image_status = "βœ… Real Image"
438
+ # else:
439
+ # return "❌ Fake Image", "⚠️ Cannot check disease", "Image doesn't appear real. Try another image."
440
+
441
+ # # Step 2: Predict with YOLO model
442
+ # results = model(image)
443
+ # names = model.names
444
+ # boxes = results[0].boxes
445
+
446
+ # if len(boxes) == 0:
447
+ # return image_status, "βœ… No Disease Detected", "No signs of disease found."
448
+
449
+ # diagnosis = ""
450
+ # disease_found = False
451
+ # for box in boxes:
452
+ # conf = float(box.conf[0])
453
+ # if conf > 0.5:
454
+ # cls_id = int(box.cls[0])
455
+ # disease_name = names[cls_id]
456
+ # diagnosis += f"πŸ”¬ {disease_name} - Confidence: {conf*100:.2f}%\n"
457
+ # disease_found = True
458
+
459
+ # if disease_found:
460
+ # return image_status, "🚨 Disease Detected", diagnosis
461
+ # else:
462
+ # return image_status, "βœ… No Disease Detected", "No confident detections."
463
+
464
+ # except Exception as e:
465
+ # # Handle unexpected errors gracefully
466
+ # return "❌ Error", "❌ Error", f"An error occurred: {str(e)}"
467
+
468
+ # # Gradio Interface
469
+ # with gr.Blocks(title="AgroScan - Plant Disease & Image Verifier") as demo:
470
+ # gr.Markdown("## 🌿 AgroScan: Plant Disease Detector + Image Validator")
471
+
472
+ # with gr.Row():
473
+ # with gr.Column():
474
+ # image_input = gr.Image(type="pil", label="πŸ“· Upload Plant Leaf Image")
475
+ # submit_btn = gr.Button("πŸ” Analyze")
476
+ # with gr.Column():
477
+ # image_result = gr.Textbox(label="πŸ–ΌοΈ Image Authenticity", interactive=False)
478
+ # disease_result = gr.Textbox(label="🌱 Disease Status", interactive=False)
479
+ # detail = gr.Textbox(label="πŸ“‹ Detailed Diagnosis", lines=5, interactive=False)
480
+
481
+ # submit_btn.click(fn=predict, inputs=image_input, outputs=[image_result, disease_result, detail])
482
+
483
+ # demo.launch()
484
+
485
+
486
+
487
+
488
+
489
+
490
+
491
+ # from ultralytics import YOLO
492
+ # import gradio as gr
493
+ # from PIL import Image
494
+ # import imghdr
495
+ # import os
496
+
497
+ # # Load YOLOv5 model
498
+ # model = YOLO("best.pt") # Ensure your model file 'best.pt' is in the same directory
499
+
500
+ # # Check if image is real or fake (basic format check)
501
+ # def is_real_image(image):
502
+ # try:
503
+ # if image is None:
504
+ # return False
505
+ # format_check = imghdr.what(image.fp.name)
506
+ # return format_check in ['jpeg', 'png']
507
+ # except:
508
+ # return False
509
+
510
+ # # Prediction function
511
+ # def predict(image):
512
+ # image_status = "❌ Fake Image"
513
+ # disease_status = "⚠️ Unknown"
514
+ # disease_details = ""
515
+
516
+ # # Step 1: Check if image is real
517
+ # if is_real_image(image):
518
+ # image_status = "βœ… Real Image"
519
+ # else:
520
+ # return image_status, "⚠️ Cannot detect disease in a fake image.", ""
521
+
522
+ # # Step 2: Run disease detection
523
+ # results = model(image)
524
+ # boxes = results[0].boxes
525
+ # names = model.names
526
+
527
+ # if len(boxes) == 0:
528
+ # disease_status = "βœ… No Disease Detected"
529
+ # else:
530
+ # disease_status = "🚨 Disease Detected"
531
+ # for box in boxes:
532
+ # class_id = int(box.cls[0])
533
+ # class_name = names[class_id]
534
+ # confidence = float(box.conf[0])
535
+ # disease_details += f"πŸ”¬ {class_name} - Confidence: {confidence*100:.2f}%\n"
536
+
537
+ # return image_status, disease_status, disease_details
538
+
539
+ # # UI using Gradio Blocks
540
+ # with gr.Blocks(title="AgroScan - Plant Disease & Image Verifier") as demo:
541
+ # gr.Markdown("""
542
+ # <div style="text-align: center;">
543
+ # <h1 style="color: #2e7d32;">🌿 AgroScan: Plant Disease Detector + Image Validator</h1>
544
+ # <p style="font-size: 16px; color: #555;">
545
+ # Upload a plant leaf image πŸƒ to check if it's real or fake and detect any diseases using YOLOv5.
546
+ # </p>
547
+ # </div>
548
+ # """)
549
+
550
+ # with gr.Row():
551
+ # with gr.Column(scale=1):
552
+ # image_input = gr.Image(type="pil", label="πŸ“· Upload Leaf Image")
553
+ # submit_btn = gr.Button("πŸ” Analyze")
554
+ # with gr.Column(scale=1):
555
+ # image_result = gr.Textbox(label="πŸ–ΌοΈ Image Authenticity", interactive=False)
556
+ # disease_result = gr.Textbox(label="🌱 Disease Status", interactive=False)
557
+ # detail = gr.Textbox(label="πŸ“‹ Detailed Diagnosis", lines=5, interactive=False)
558
+
559
+ # with gr.Accordion("ℹ️ What is AgroScan?", open=False):
560
+ # gr.Markdown("""
561
+ # AgroScan is an AI-powered detector for identifying plant diseases from leaf images.
562
+ # It also verifies whether the image is genuine (real leaf image) or fake to avoid misdiagnosis.
563
+ # Designed to support farmers, agronomists, and researchers for better crop care.
564
+ # """)
565
+
566
+ # submit_btn.click(fn=predict, inputs=image_input,
567
+ # outputs=[image_result, disease_result, detail])
568
+
569
+ # demo.launch()
570
+
571
+
572
+
573
+
574
+
575
+
576
+
577
+ # from ultralytics import YOLO
578
+ # import gradio as gr
579
+ # from PIL import Image
580
+
581
+ # # Load YOLOv5 model
582
+ # model = YOLO("best.pt") # Make sure this file exists in your Hugging Face Space
583
+
584
+ # # Prediction function
585
+ # def predict(image):
586
+ # results = model(image)
587
+ # boxes = results[0].boxes
588
+ # names = model.names
589
+ # output = ""
590
+
591
+ # if len(boxes) == 0:
592
+ # output = "βœ… Healthy: No disease detected!"
593
+ # else:
594
+ # for box in boxes:
595
+ # class_id = int(box.cls[0])
596
+ # class_name = names[class_id]
597
+ # confidence = float(box.conf[0])
598
+ # output += f"🚨 Detected: {class_name} ({confidence*100:.2f}%)\n"
599
+ # return output
600
+
601
+ # # Gradio UI using Blocks
602
+ # with gr.Blocks(title="AgroScan - Plant Disease Detector") as demo:
603
+ # gr.Markdown("""
604
+ # <div style="text-align: center;">
605
+ # <h1 style="color: #2e7d32;">🌿 AgroScan: Plant Disease Detector</h1>
606
+ # <p style="font-size: 16px; color: #555;">
607
+ # Upload a high-quality image of a plant leaf πŸƒ and detect possible diseases using AI-powered YOLOv5.
608
+ # </p>
609
+ # </div>
610
+ # """)
611
+
612
+ # with gr.Row():
613
+ # with gr.Column(scale=1):
614
+ # image_input = gr.Image(type="pil", label="πŸ“· Upload Leaf Image")
615
+ # predict_button = gr.Button("πŸ” Analyze Leaf", variant="primary")
616
+ # with gr.Column(scale=1):
617
+ # result_output = gr.Textbox(label="🩺 Diagnosis Result", lines=6)
618
+
619
+ # with gr.Accordion("ℹ️ About AgroScan", open=False):
620
+ # gr.Markdown("""
621
+ # **AgroScan** is an intelligent plant disease detector built with **YOLOv5** and trained on custom agricultural datasets.
622
+ # It helps farmers, researchers, and agriculturists in early detection of plant diseases to ensure timely treatment and improved crop yield. 🌱
623
+ # """)
624
+
625
+ # predict_button.click(fn=predict, inputs=image_input, outputs=result_output)
626
+
627
+ # demo.launch()
628
+
629
+
630
+
631
+
632
+
633
+
634
+ # from ultralytics import YOLO
635
+ # import gradio as gr
636
+ # from PIL import Image
637
+ # import torch
638
+
639
+ # # Load YOLOv5 model
640
+ # model = YOLO("best.pt") # assumes model is in the same directory
641
+
642
+ # # Prediction function
643
+ # def predict(image):
644
+ # results = model(image)
645
+ # boxes = results[0].boxes
646
+ # names = model.names
647
+ # output = ""
648
+
649
+ # if len(boxes) == 0:
650
+ # output = "βœ… Healthy: No disease detected!"
651
+ # else:
652
+ # for box in boxes:
653
+ # class_id = int(box.cls[0])
654
+ # class_name = names[class_id]
655
+ # confidence = float(box.conf[0])
656
+ # output += f"🚨 Detected: {class_name} ({confidence*100:.2f}%)\n"
657
+
658
+ # return output
659
+
660
+ # # Gradio UI
661
+ # gr.Interface(
662
+ # fn=predict,
663
+ # inputs=gr.Image(type="pil"),
664
+ # outputs="text",
665
+ # title="🌿 AgroScan: Plant Disease Detector",
666
+ # description="Upload a plant leaf image to detect disease using a YOLOv5 model.",
667
+ # ).launch()
requirements.txt CHANGED
@@ -1,4 +1,7 @@
1
  ultralytics
2
  gradio
3
- torch
4
  Pillow
 
 
 
 
1
  ultralytics
2
  gradio
3
+ numpy
4
  Pillow
5
+
6
+
7
+