Add new file
Browse files- .gitignore +7 -0
- app.py +95 -0
- requirements.txt +5 -0
.gitignore
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flagged/
|
2 |
+
*.pt
|
3 |
+
*.png
|
4 |
+
*.jpg
|
5 |
+
*.mp4
|
6 |
+
*.mkv
|
7 |
+
gradio_cached_examples/
|
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import os
|
4 |
+
from ultralyticsplus import YOLO, render_result
|
5 |
+
|
6 |
+
model_path= 'best.pt'
|
7 |
+
|
8 |
+
def preds_image(image, conf_thres, iou_thres):
|
9 |
+
|
10 |
+
model = YOLO(model_path)
|
11 |
+
|
12 |
+
result = model.predict(image,
|
13 |
+
conf= conf_thres,
|
14 |
+
iou= iou_thres)
|
15 |
+
|
16 |
+
box = result[0].boxes
|
17 |
+
print("Object type: ", box.cls)
|
18 |
+
print("Coordinates: ", box.xyxy)
|
19 |
+
print("Probability: ", box.conf)
|
20 |
+
|
21 |
+
render = render_result(model=model, image=image, result=result[0])
|
22 |
+
return render
|
23 |
+
|
24 |
+
inputs_image = [
|
25 |
+
gr.Image(label="Input Image"),
|
26 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.40, step=0.05, label="Confidence threshold"),
|
27 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU threshold")
|
28 |
+
]
|
29 |
+
outputs_image = [
|
30 |
+
gr.Image(label="Output Image"),
|
31 |
+
]
|
32 |
+
|
33 |
+
interface_image = gr.Interface(
|
34 |
+
fn=preds_image,
|
35 |
+
inputs=inputs_image,
|
36 |
+
outputs=outputs_image,
|
37 |
+
title="Chili Leaf Disease Detector (Image)"
|
38 |
+
)
|
39 |
+
|
40 |
+
def preds_video(video):
|
41 |
+
|
42 |
+
video_path = video
|
43 |
+
video_path_out = '{}_out.mp4'.format(video_path)
|
44 |
+
|
45 |
+
cap = cv2.VideoCapture(video_path)
|
46 |
+
ret, frame = cap.read()
|
47 |
+
H, W, _ = frame.shape
|
48 |
+
out = cv2.VideoWriter(video_path_out, cv2.VideoWriter_fourcc(*'MP4V'), int(cap.get(cv2.CAP_PROP_FPS)), (W, H))
|
49 |
+
|
50 |
+
model= YOLO(model_path)
|
51 |
+
|
52 |
+
threshold = 0.4
|
53 |
+
|
54 |
+
while ret:
|
55 |
+
|
56 |
+
results = model(frame)[0]
|
57 |
+
|
58 |
+
for result in results.boxes.data.tolist():
|
59 |
+
x1, y1, x2, y2, score, class_id = result
|
60 |
+
|
61 |
+
if score > threshold:
|
62 |
+
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 4)
|
63 |
+
|
64 |
+
#object details
|
65 |
+
org = (int(x1), int(y1 - 10))
|
66 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
67 |
+
fontscale = 0.5
|
68 |
+
color = (0, 0, 0)
|
69 |
+
|
70 |
+
cv2.putText(frame, results.names[int(class_id)].lower(), org,
|
71 |
+
font, fontscale, color, 1, cv2.LINE_AA)
|
72 |
+
|
73 |
+
out.write(frame)
|
74 |
+
ret, frame = cap.read()
|
75 |
+
|
76 |
+
cap.release()
|
77 |
+
out.release()
|
78 |
+
cv2.destroyAllWindows()
|
79 |
+
|
80 |
+
return video_path_out
|
81 |
+
|
82 |
+
inputs_video = gr.Video(label= 'Original chili leaf video')
|
83 |
+
outputs_video = gr.Video(label= 'Predicted leaf')
|
84 |
+
|
85 |
+
interface_video = gr.Interface(
|
86 |
+
fn=preds_video,
|
87 |
+
inputs=inputs_video,
|
88 |
+
outputs=outputs_video,
|
89 |
+
title="Chili Leaf Disease Detector (Video)"
|
90 |
+
)
|
91 |
+
|
92 |
+
gr.TabbedInterface(
|
93 |
+
[interface_image, interface_video],
|
94 |
+
tab_names=['Image inference', 'Video inference']
|
95 |
+
).queue().launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.22.0
|
2 |
+
opencv_python==4.7.0.72
|
3 |
+
opencv_python==4.9.0.80
|
4 |
+
opencv_python_headless==4.8.0.74
|
5 |
+
ultralyticsplus==0.1.0
|