Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import os
|
4 |
+
|
5 |
+
from ultralytics import YOLO
|
6 |
+
|
7 |
+
|
8 |
+
model = YOLO('yolov8x-seg.pt')
|
9 |
+
path = [['3891186464_00d76e10a2_z.jpg'], ['images (1).jpeg']]
|
10 |
+
video_path = [['sheep.mp4']]
|
11 |
+
|
12 |
+
|
13 |
+
def show_preds_image(image_path):
|
14 |
+
image = cv2.imread(image_path)
|
15 |
+
image_copy=image.copy()
|
16 |
+
threshold = 0.1
|
17 |
+
results = model(image)[0]
|
18 |
+
for result in results.boxes.data.tolist():
|
19 |
+
x1, y1, x2, y2, score, class_id = result
|
20 |
+
|
21 |
+
if score > threshold:
|
22 |
+
cv2.rectangle(image_copy, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
|
23 |
+
cv2.putText(image_copy, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
|
24 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
|
25 |
+
cv2.putText(image_copy, str(score), (int(x1), int(y2 + 10)),
|
26 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 255), 3, cv2.LINE_AA)
|
27 |
+
return cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)
|
28 |
+
|
29 |
+
inputs_image = [
|
30 |
+
gr.components.Image(type="filepath", label="Input Image"),
|
31 |
+
]
|
32 |
+
outputs_image = [
|
33 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
34 |
+
]
|
35 |
+
interface_image = gr.Interface(
|
36 |
+
fn=show_preds_image,
|
37 |
+
inputs=inputs_image,
|
38 |
+
outputs=outputs_image,
|
39 |
+
title="Animal detector using YOLOV8 NANO",
|
40 |
+
examples=path,
|
41 |
+
cache_examples=False,
|
42 |
+
)
|
43 |
+
|
44 |
+
def show_preds_video(video_path):
|
45 |
+
cap = cv2.VideoCapture(video_path)
|
46 |
+
while(cap.isOpened()):
|
47 |
+
ret, frame = cap.read()
|
48 |
+
if ret:
|
49 |
+
threshold = 0.1
|
50 |
+
frame_copy = frame.copy()
|
51 |
+
results = model(frame)[0]
|
52 |
+
for result in results.boxes.data.tolist():
|
53 |
+
x1, y1, x2, y2, score, class_id = result
|
54 |
+
|
55 |
+
if score > threshold:
|
56 |
+
cv2.rectangle(frame_copy, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
|
57 |
+
cv2.putText(frame_copy, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
|
58 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
|
59 |
+
cv2.putText(frame_copy, str(score), (int(x1), int(y2 + 10)),
|
60 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 255), 3, cv2.LINE_AA)
|
61 |
+
|
62 |
+
yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
|
63 |
+
|
64 |
+
inputs_video = [
|
65 |
+
gr.components.Video(type="filepath", label="Input Video"),
|
66 |
+
|
67 |
+
]
|
68 |
+
outputs_video = [
|
69 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
70 |
+
]
|
71 |
+
interface_video = gr.Interface(
|
72 |
+
fn=show_preds_video,
|
73 |
+
inputs=inputs_video,
|
74 |
+
outputs=outputs_video,
|
75 |
+
title="Cattle detector using YOLOV8 NANO",
|
76 |
+
examples=video_path,
|
77 |
+
cache_examples=False,
|
78 |
+
)
|
79 |
+
|
80 |
+
gr.TabbedInterface(
|
81 |
+
[interface_image, interface_video],
|
82 |
+
tab_names=['Image inference', 'Video inference']
|
83 |
+
).launch()
|