Spaces:
Sleeping
Sleeping
Create app.py (#1)
Browse files- Create app.py (2bc270afa1baf158b6fe898121ccb36294638e9e)
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import gradio as gr # Gradio for creating the web interface
|
3 |
+
import cv2 # OpenCV for image processing
|
4 |
+
from huggingface_hub import hf_hub_download # Download models from Hugging Face Hub
|
5 |
+
from gradio_webrtc import WebRTC # WebRTC integration for streaming webcam feeds in Gradio
|
6 |
+
from twilio.rest import Client # Twilio client for managing ICE servers for WebRTC
|
7 |
+
import os # OS module for environment variable access
|
8 |
+
from inference import YOLOv10 # Custom YOLOv10 inference class
|
9 |
+
|
10 |
+
# Download YOLOv10 model file from Hugging Face Hub
|
11 |
+
model_file = hf_hub_download(
|
12 |
+
repo_id="onnx-community/yolov10n", # Repository containing the YOLOv10 model
|
13 |
+
filename="onnx/model.onnx" # Model file to download
|
14 |
+
)
|
15 |
+
|
16 |
+
# Initialize the YOLOv10 model
|
17 |
+
model = YOLOv10(model_file)
|
18 |
+
|
19 |
+
# Retrieve Twilio account credentials from environment variables
|
20 |
+
account_sid = os.environ.get("TWILIO_ACCOUNT_SID") # Twilio Account SID
|
21 |
+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN") # Twilio Auth Token
|
22 |
+
|
23 |
+
# Check if Twilio credentials are available
|
24 |
+
if account_sid and auth_token:
|
25 |
+
# Initialize Twilio client with credentials
|
26 |
+
client = Client(account_sid, auth_token)
|
27 |
+
|
28 |
+
# Create a Twilio token for ICE server configuration
|
29 |
+
token = client.tokens.create()
|
30 |
+
|
31 |
+
# Configure WebRTC to use Twilio ICE servers for better connection reliability
|
32 |
+
rtc_configuration = {
|
33 |
+
"iceServers": token.ice_servers, # Use Twilio ICE servers
|
34 |
+
"iceTransportPolicy": "relay", # Relay policy to improve NAT traversal
|
35 |
+
}
|
36 |
+
else:
|
37 |
+
# Use default WebRTC configuration if Twilio credentials are not available
|
38 |
+
rtc_configuration = None
|
39 |
+
|
40 |
+
# Function to perform object detection
|
41 |
+
def detection(image, conf_threshold=0.3):
|
42 |
+
# Resize the input image to match the model's expected dimensions
|
43 |
+
image = cv2.resize(image, (model.input_width, model.input_height))
|
44 |
+
# Perform object detection and return the processed image
|
45 |
+
new_image = model.detect_objects(image, conf_threshold)
|
46 |
+
return cv2.resize(new_image, (500, 500)) # Resize output image for display
|
47 |
+
|
48 |
+
# Define custom CSS for Gradio interface layout
|
49 |
+
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
50 |
+
.my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
|
51 |
+
|
52 |
+
# Create a Gradio interface with custom blocks
|
53 |
+
with gr.Blocks(css=css) as demo:
|
54 |
+
# Add a title to the Gradio interface
|
55 |
+
gr.HTML(
|
56 |
+
"""
|
57 |
+
<h1 style='text-align: center'>
|
58 |
+
YOLOv10 Webcam Stream
|
59 |
+
</h1>
|
60 |
+
"""
|
61 |
+
)
|
62 |
+
# Add links to the arXiv paper and GitHub repository for YOLOv10
|
63 |
+
gr.HTML(
|
64 |
+
"""
|
65 |
+
<h3 style='text-align: center'>
|
66 |
+
<a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
|
67 |
+
</h3>
|
68 |
+
"""
|
69 |
+
)
|
70 |
+
# Define a column layout for the interface
|
71 |
+
with gr.Column(elem_classes=["my-column"]):
|
72 |
+
with gr.Group(elem_classes=["my-group"]):
|
73 |
+
# Add a WebRTC component for webcam streaming
|
74 |
+
image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
75 |
+
# Add a slider to adjust the confidence threshold for object detection
|
76 |
+
conf_threshold = gr.Slider(
|
77 |
+
label="Confidence Threshold", # Label for the slider
|
78 |
+
minimum=0.0, # Minimum slider value
|
79 |
+
maximum=1.0, # Maximum slider value
|
80 |
+
step=0.05, # Step size for slider
|
81 |
+
value=0.30, # Default slider value
|
82 |
+
)
|
83 |
+
|
84 |
+
# Stream webcam frames through the detection function
|
85 |
+
image.stream(
|
86 |
+
fn=detection, # Detection function to process frames
|
87 |
+
inputs=[image, conf_threshold], # Inputs: webcam stream and confidence threshold
|
88 |
+
outputs=[image], # Outputs: processed frames
|
89 |
+
time_limit=10 # Limit each detection to 10 seconds
|
90 |
+
)
|
91 |
+
|
92 |
+
# Launch the Gradio app when the script is run directly
|
93 |
+
if __name__ == "__main__":
|
94 |
+
demo.launch()
|
95 |
+
|
96 |
+
|