Spaces:
Running
Running
Upload 4 files
Browse files- app-2.py +72 -0
- requirements.txt +1 -1
app-2.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from gradio_webrtc import WebRTC
|
5 |
+
from twilio.rest import Client
|
6 |
+
import os
|
7 |
+
from inference import YOLOv10
|
8 |
+
|
9 |
+
model_file = hf_hub_download(
|
10 |
+
repo_id="onnx-community/yolov10n", filename="onnx/model.onnx"
|
11 |
+
)
|
12 |
+
|
13 |
+
model = YOLOv10(model_file)
|
14 |
+
|
15 |
+
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
|
16 |
+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
|
17 |
+
|
18 |
+
if account_sid and auth_token:
|
19 |
+
client = Client(account_sid, auth_token)
|
20 |
+
|
21 |
+
token = client.tokens.create()
|
22 |
+
|
23 |
+
rtc_configuration = {
|
24 |
+
"iceServers": token.ice_servers,
|
25 |
+
"iceTransportPolicy": "relay",
|
26 |
+
}
|
27 |
+
else:
|
28 |
+
rtc_configuration = None
|
29 |
+
|
30 |
+
|
31 |
+
def detection(image, conf_threshold=0.3):
|
32 |
+
image = cv2.resize(image, (model.input_width, model.input_height))
|
33 |
+
new_image = model.detect_objects(image, conf_threshold)
|
34 |
+
return cv2.resize(new_image, (500, 500))
|
35 |
+
|
36 |
+
|
37 |
+
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
38 |
+
.my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
|
39 |
+
|
40 |
+
|
41 |
+
with gr.Blocks(css=css) as demo:
|
42 |
+
gr.HTML(
|
43 |
+
"""
|
44 |
+
<h1 style='text-align: center'>
|
45 |
+
YOLOv10 Webcam Stream (Powered by WebRTC ⚡️)
|
46 |
+
</h1>
|
47 |
+
"""
|
48 |
+
)
|
49 |
+
gr.HTML(
|
50 |
+
"""
|
51 |
+
<h3 style='text-align: center'>
|
52 |
+
<a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
|
53 |
+
</h3>
|
54 |
+
"""
|
55 |
+
)
|
56 |
+
with gr.Column(elem_classes=["my-column"]):
|
57 |
+
with gr.Group(elem_classes=["my-group"]):
|
58 |
+
image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
59 |
+
conf_threshold = gr.Slider(
|
60 |
+
label="Confidence Threshold",
|
61 |
+
minimum=0.0,
|
62 |
+
maximum=1.0,
|
63 |
+
step=0.05,
|
64 |
+
value=0.30,
|
65 |
+
)
|
66 |
+
|
67 |
+
image.stream(
|
68 |
+
fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
|
69 |
+
)
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
opencv-python
|
2 |
twilio
|
3 |
gradio-webrtc==0.0.4
|
4 |
-
onnxruntime
|
|
|
1 |
opencv-python
|
2 |
twilio
|
3 |
gradio-webrtc==0.0.4
|
4 |
+
onnxruntime-gpu
|