Spaces:
Sleeping
Sleeping
Commit
·
f8727f7
1
Parent(s):
7341dff
use streaming
Browse files
app.py
CHANGED
@@ -4,45 +4,14 @@ import cv2
|
|
4 |
import tempfile
|
5 |
from ultralytics import YOLOv10
|
6 |
|
7 |
-
|
8 |
-
def yolov10_inference(image, video, model_id, image_size, conf_threshold):
|
9 |
-
model = YOLOv10.from_pretrained(f'jameslahm/{model_id}')
|
10 |
-
if image:
|
11 |
-
results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
|
12 |
-
annotated_image = results[0].plot()
|
13 |
-
return annotated_image[:, :, ::-1], None
|
14 |
-
else:
|
15 |
-
video_path = tempfile.mktemp(suffix=".webm")
|
16 |
-
with open(video_path, "wb") as f:
|
17 |
-
with open(video, "rb") as g:
|
18 |
-
f.write(g.read())
|
19 |
-
|
20 |
-
cap = cv2.VideoCapture(video_path)
|
21 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
22 |
-
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
23 |
-
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
24 |
-
|
25 |
-
output_video_path = tempfile.mktemp(suffix=".webm")
|
26 |
-
out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'vp80'), fps, (frame_width, frame_height))
|
27 |
-
|
28 |
-
while cap.isOpened():
|
29 |
-
ret, frame = cap.read()
|
30 |
-
if not ret:
|
31 |
-
break
|
32 |
-
|
33 |
-
results = model.predict(source=frame, imgsz=image_size, conf=conf_threshold)
|
34 |
-
annotated_frame = results[0].plot()
|
35 |
-
out.write(annotated_frame)
|
36 |
-
|
37 |
-
cap.release()
|
38 |
-
out.release()
|
39 |
-
|
40 |
-
return None, output_video_path
|
41 |
|
42 |
@spaces.GPU
|
43 |
-
def
|
44 |
-
|
45 |
-
|
|
|
|
|
46 |
|
47 |
|
48 |
def app():
|
@@ -50,31 +19,6 @@ def app():
|
|
50 |
with gr.Row():
|
51 |
with gr.Column():
|
52 |
image = gr.Image(type="pil", label="Image", visible=True)
|
53 |
-
video = gr.Video(label="Video", visible=False)
|
54 |
-
input_type = gr.Radio(
|
55 |
-
choices=["Image", "Video"],
|
56 |
-
value="Image",
|
57 |
-
label="Input Type",
|
58 |
-
)
|
59 |
-
model_id = gr.Dropdown(
|
60 |
-
label="Model",
|
61 |
-
choices=[
|
62 |
-
"yolov10n",
|
63 |
-
"yolov10s",
|
64 |
-
"yolov10m",
|
65 |
-
"yolov10b",
|
66 |
-
"yolov10l",
|
67 |
-
"yolov10x",
|
68 |
-
],
|
69 |
-
value="yolov10m",
|
70 |
-
)
|
71 |
-
image_size = gr.Slider(
|
72 |
-
label="Image Size",
|
73 |
-
minimum=320,
|
74 |
-
maximum=1280,
|
75 |
-
step=32,
|
76 |
-
value=640,
|
77 |
-
)
|
78 |
conf_threshold = gr.Slider(
|
79 |
label="Confidence Threshold",
|
80 |
minimum=0.0,
|
@@ -82,64 +26,18 @@ def app():
|
|
82 |
step=0.05,
|
83 |
value=0.25,
|
84 |
)
|
85 |
-
yolov10_infer = gr.Button(value="Detect Objects")
|
86 |
|
87 |
with gr.Column():
|
88 |
output_image = gr.Image(type="numpy", label="Annotated Image", visible=True)
|
89 |
-
output_video = gr.Video(label="Annotated Video", visible=False)
|
90 |
-
|
91 |
-
def update_visibility(input_type):
|
92 |
-
image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
|
93 |
-
video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
|
94 |
-
output_image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
|
95 |
-
output_video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
|
96 |
-
|
97 |
-
return image, video, output_image, output_video
|
98 |
-
|
99 |
-
input_type.change(
|
100 |
-
fn=update_visibility,
|
101 |
-
inputs=[input_type],
|
102 |
-
outputs=[image, video, output_image, output_video],
|
103 |
-
)
|
104 |
-
|
105 |
-
def run_inference(image, video, model_id, image_size, conf_threshold, input_type):
|
106 |
-
if input_type == "Image":
|
107 |
-
return yolov10_inference(image, None, model_id, image_size, conf_threshold)
|
108 |
-
else:
|
109 |
-
return yolov10_inference(None, video, model_id, image_size, conf_threshold)
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
116 |
)
|
117 |
|
118 |
-
gr.Examples(
|
119 |
-
examples=[
|
120 |
-
[
|
121 |
-
"bus.jpg",
|
122 |
-
"yolov10s",
|
123 |
-
640,
|
124 |
-
0.25,
|
125 |
-
],
|
126 |
-
[
|
127 |
-
"zidane.jpg",
|
128 |
-
"yolov10s",
|
129 |
-
640,
|
130 |
-
0.25,
|
131 |
-
],
|
132 |
-
],
|
133 |
-
fn=yolov10_inference_for_examples,
|
134 |
-
inputs=[
|
135 |
-
image,
|
136 |
-
model_id,
|
137 |
-
image_size,
|
138 |
-
conf_threshold,
|
139 |
-
],
|
140 |
-
outputs=[output_image],
|
141 |
-
cache_examples='lazy',
|
142 |
-
)
|
143 |
|
144 |
gradio_app = gr.Blocks()
|
145 |
with gradio_app:
|
|
|
4 |
import tempfile
|
5 |
from ultralytics import YOLOv10
|
6 |
|
7 |
+
model = YOLOv10.from_pretrained(f'jameslahm/yolov10s')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
@spaces.GPU
|
10 |
+
def yolov10_inference(image, conf_threshold):
|
11 |
+
width, _ = image.size
|
12 |
+
results = model.predict(source=image, imgsz=width, conf=conf_threshold)
|
13 |
+
annotated_image = results[0].plot()
|
14 |
+
return annotated_image[:, :, ::-1]
|
15 |
|
16 |
|
17 |
def app():
|
|
|
19 |
with gr.Row():
|
20 |
with gr.Column():
|
21 |
image = gr.Image(type="pil", label="Image", visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
conf_threshold = gr.Slider(
|
23 |
label="Confidence Threshold",
|
24 |
minimum=0.0,
|
|
|
26 |
step=0.05,
|
27 |
value=0.25,
|
28 |
)
|
|
|
29 |
|
30 |
with gr.Column():
|
31 |
output_image = gr.Image(type="numpy", label="Annotated Image", visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
image.stream(
|
34 |
+
fn=yolov10_inference,
|
35 |
+
inputs=[image, conf_threshold],
|
36 |
+
outputs=[image],
|
37 |
+
stream_every=0.1,
|
38 |
+
time_limit=30
|
39 |
)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
gradio_app = gr.Blocks()
|
43 |
with gradio_app:
|