Spaces:
Sleeping
Sleeping
StevenChen16
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import tempfile
|
4 |
+
from ultralytics import YOLOv10
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
|
8 |
+
@spaces.GPU
|
9 |
+
def yolov10_inference(image, video, model_id, image_size, conf_threshold):
|
10 |
+
# model = YOLOv10.from_pretrained(f'jameslahm/{model_id}')
|
11 |
+
model = YOLOv10.from_pretrained('weights/yolov10s.pt')
|
12 |
+
if image:
|
13 |
+
results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
|
14 |
+
annotated_image = results[0].plot()
|
15 |
+
return annotated_image[:, :, ::-1], None
|
16 |
+
else:
|
17 |
+
video_path = tempfile.mktemp(suffix=".webm")
|
18 |
+
with open(video_path, "wb") as f:
|
19 |
+
with open(video, "rb") as g:
|
20 |
+
f.write(g.read())
|
21 |
+
|
22 |
+
cap = cv2.VideoCapture(video_path)
|
23 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
24 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
25 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
26 |
+
|
27 |
+
output_video_path = tempfile.mktemp(suffix=".webm")
|
28 |
+
out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'vp80'), fps, (frame_width, frame_height))
|
29 |
+
|
30 |
+
while cap.isOpened():
|
31 |
+
ret, frame = cap.read()
|
32 |
+
if not ret:
|
33 |
+
break
|
34 |
+
|
35 |
+
results = model.predict(source=frame, imgsz=image_size, conf=conf_threshold)
|
36 |
+
annotated_frame = results[0].plot()
|
37 |
+
out.write(annotated_frame)
|
38 |
+
|
39 |
+
cap.release()
|
40 |
+
out.release()
|
41 |
+
|
42 |
+
return None, output_video_path
|
43 |
+
|
44 |
+
|
45 |
+
def yolov10_inference_for_examples(image, model_path, image_size, conf_threshold):
|
46 |
+
annotated_image, _ = yolov10_inference(image, None, model_path, image_size, conf_threshold)
|
47 |
+
return annotated_image
|
48 |
+
|
49 |
+
|
50 |
+
def app():
|
51 |
+
with gr.Blocks():
|
52 |
+
with gr.Row():
|
53 |
+
with gr.Column():
|
54 |
+
image = gr.Image(type="pil", label="Image", visible=True)
|
55 |
+
video = gr.Video(label="Video", visible=False)
|
56 |
+
input_type = gr.Radio(
|
57 |
+
choices=["Image", "Video"],
|
58 |
+
value="Image",
|
59 |
+
label="Input Type",
|
60 |
+
)
|
61 |
+
model_id = gr.Dropdown(
|
62 |
+
label="Model",
|
63 |
+
choices=[
|
64 |
+
"yolov10n",
|
65 |
+
"yolov10s",
|
66 |
+
"yolov10m",
|
67 |
+
"yolov10b",
|
68 |
+
"yolov10l",
|
69 |
+
"yolov10x",
|
70 |
+
],
|
71 |
+
value="yolov10m",
|
72 |
+
)
|
73 |
+
image_size = gr.Slider(
|
74 |
+
label="Image Size",
|
75 |
+
minimum=320,
|
76 |
+
maximum=1280,
|
77 |
+
step=32,
|
78 |
+
value=640,
|
79 |
+
)
|
80 |
+
conf_threshold = gr.Slider(
|
81 |
+
label="Confidence Threshold",
|
82 |
+
minimum=0.0,
|
83 |
+
maximum=1.0,
|
84 |
+
step=0.05,
|
85 |
+
value=0.25,
|
86 |
+
)
|
87 |
+
yolov10_infer = gr.Button(value="Detect Objects")
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
output_image = gr.Image(type="numpy", label="Annotated Image", visible=True)
|
91 |
+
output_video = gr.Video(label="Annotated Video", visible=False)
|
92 |
+
|
93 |
+
def update_visibility(input_type):
|
94 |
+
image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
|
95 |
+
video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
|
96 |
+
output_image = gr.update(visible=True) if input_type == "Image" else gr.update(visible=False)
|
97 |
+
output_video = gr.update(visible=False) if input_type == "Image" else gr.update(visible=True)
|
98 |
+
|
99 |
+
return image, video, output_image, output_video
|
100 |
+
|
101 |
+
input_type.change(
|
102 |
+
fn=update_visibility,
|
103 |
+
inputs=[input_type],
|
104 |
+
outputs=[image, video, output_image, output_video],
|
105 |
+
)
|
106 |
+
|
107 |
+
def run_inference(image, video, model_id, image_size, conf_threshold, input_type):
|
108 |
+
if input_type == "Image":
|
109 |
+
return yolov10_inference(image, None, model_id, image_size, conf_threshold)
|
110 |
+
else:
|
111 |
+
return yolov10_inference(None, video, model_id, image_size, conf_threshold)
|
112 |
+
|
113 |
+
|
114 |
+
yolov10_infer.click(
|
115 |
+
fn=run_inference,
|
116 |
+
inputs=[image, video, model_id, image_size, conf_threshold, input_type],
|
117 |
+
outputs=[output_image, output_video],
|
118 |
+
)
|
119 |
+
|
120 |
+
gr.Examples(
|
121 |
+
examples=[
|
122 |
+
[
|
123 |
+
"ultralytics/assets/bus.jpg",
|
124 |
+
"yolov10s",
|
125 |
+
640,
|
126 |
+
0.25,
|
127 |
+
],
|
128 |
+
[
|
129 |
+
"ultralytics/assets/zidane.jpg",
|
130 |
+
"yolov10s",
|
131 |
+
640,
|
132 |
+
0.25,
|
133 |
+
],
|
134 |
+
],
|
135 |
+
fn=yolov10_inference_for_examples,
|
136 |
+
inputs=[
|
137 |
+
image,
|
138 |
+
model_id,
|
139 |
+
image_size,
|
140 |
+
conf_threshold,
|
141 |
+
],
|
142 |
+
outputs=[output_image],
|
143 |
+
cache_examples='lazy',
|
144 |
+
)
|
145 |
+
|
146 |
+
gradio_app = gr.Blocks()
|
147 |
+
with gradio_app:
|
148 |
+
gr.HTML(
|
149 |
+
"""
|
150 |
+
<h1 style='text-align: center'>
|
151 |
+
YOLOv10: Real-Time End-to-End Object Detection
|
152 |
+
</h1>
|
153 |
+
""")
|
154 |
+
gr.HTML(
|
155 |
+
"""
|
156 |
+
<h3 style='text-align: center'>
|
157 |
+
<a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
|
158 |
+
</h3>
|
159 |
+
""")
|
160 |
+
with gr.Row():
|
161 |
+
with gr.Column():
|
162 |
+
app()
|
163 |
+
if __name__ == '__main__':
|
164 |
+
gradio_app.launch()
|