Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,133 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import cv2
|
3 |
-
import numpy as np
|
4 |
-
import onnxruntime as ort
|
5 |
|
6 |
-
# Load the ONNX model using onnxruntime
|
7 |
-
onnx_model_path = "Model_IV.onnx" # Update with your ONNX model path
|
8 |
-
session = ort.InferenceSession(onnx_model_path)
|
9 |
|
10 |
-
# Function to perform object detection with the ONNX model
|
11 |
-
def detect_objects(frame, confidence_threshold=0.5):
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
# Gradio interface to use the webcam for real-time object detection
|
51 |
-
# Added a slider for the confidence threshold
|
52 |
-
iface = gr.Interface(fn=detect_objects,
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import cv2
|
3 |
+
# import numpy as np
|
4 |
+
# import onnxruntime as ort
|
5 |
|
6 |
+
# # Load the ONNX model using onnxruntime
|
7 |
+
# onnx_model_path = "Model_IV.onnx" # Update with your ONNX model path
|
8 |
+
# session = ort.InferenceSession(onnx_model_path)
|
9 |
|
10 |
+
# # Function to perform object detection with the ONNX model
|
11 |
+
# def detect_objects(frame, confidence_threshold=0.5):
|
12 |
+
# # Convert the frame from BGR (OpenCV) to RGB
|
13 |
+
# image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
14 |
|
15 |
+
# # Preprocessing: Resize and normalize the image
|
16 |
+
# # Assuming YOLO model input is 640x640, update according to your model's input size
|
17 |
+
# input_size = (640, 640)
|
18 |
+
# image_resized = cv2.resize(image, input_size)
|
19 |
+
# image_normalized = image_resized / 255.0 # Normalize to [0, 1]
|
20 |
+
# image_input = np.transpose(image_normalized, (2, 0, 1)) # Change to CHW format
|
21 |
+
# image_input = np.expand_dims(image_input, axis=0).astype(np.float32) # Add batch dimension
|
22 |
+
|
23 |
+
# # Perform inference
|
24 |
+
# inputs = {session.get_inputs()[0].name: image_input}
|
25 |
+
# outputs = session.run(None, inputs)
|
26 |
|
27 |
+
# # # Assuming YOLO model outputs are in the form of [boxes, confidences, class_probs]
|
28 |
+
# # boxes, confidences, class_probs = outputs
|
29 |
+
|
30 |
+
# # # Post-processing: Filter boxes by confidence threshold
|
31 |
+
# # detections = []
|
32 |
+
# # for i, confidence in enumerate(confidences[0]):
|
33 |
+
# # if confidence >= confidence_threshold:
|
34 |
+
# # x1, y1, x2, y2 = boxes[0][i]
|
35 |
+
# # class_id = np.argmax(class_probs[0][i]) # Get class with highest probability
|
36 |
+
# # detections.append((x1, y1, x2, y2, confidence, class_id))
|
37 |
|
38 |
+
# # # Draw bounding boxes and labels on the image
|
39 |
+
# # for (x1, y1, x2, y2, confidence, class_id) in detections:
|
40 |
+
# # color = (0, 255, 0) # Green color for bounding boxes
|
41 |
+
# # cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
|
42 |
+
# # label = f"Class {class_id}: {confidence:.2f}"
|
43 |
+
# # cv2.putText(image, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
44 |
|
45 |
+
# # # Convert the image back to BGR for displaying in Gradio
|
46 |
+
# # image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
47 |
|
48 |
+
# return outputs
|
49 |
+
|
50 |
+
# # Gradio interface to use the webcam for real-time object detection
|
51 |
+
# # Added a slider for the confidence threshold
|
52 |
+
# iface = gr.Interface(fn=detect_objects,
|
53 |
+
# #inputs=[
|
54 |
+
# # gr.Video(sources="webcam", type="numpy"), # Webcam input
|
55 |
+
# inputs = gr.Image(sources=["webcam"], type="numpy"),
|
56 |
+
# # gr.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Confidence Threshold") # Confidence slider
|
57 |
+
# # ],
|
58 |
+
# outputs="image") # Show output image with bounding boxes
|
59 |
+
|
60 |
+
# iface.launch()
|
61 |
+
|
62 |
+
import gradio as gr
|
63 |
+
import cv2
|
64 |
+
from huggingface_hub import hf_hub_download
|
65 |
+
from gradio_webrtc import WebRTC
|
66 |
+
from twilio.rest import Client
|
67 |
+
import os
|
68 |
+
from inference import YOLOv10
|
69 |
+
|
70 |
+
model_file = hf_hub_download(
|
71 |
+
repo_id="onnx-community/yolov10n", filename="onnx/model.onnx"
|
72 |
+
)
|
73 |
+
|
74 |
+
model = YOLOv10(model_file)
|
75 |
+
|
76 |
+
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
|
77 |
+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
|
78 |
+
|
79 |
+
if account_sid and auth_token:
|
80 |
+
client = Client(account_sid, auth_token)
|
81 |
+
|
82 |
+
token = client.tokens.create()
|
83 |
+
|
84 |
+
rtc_configuration = {
|
85 |
+
"iceServers": token.ice_servers,
|
86 |
+
"iceTransportPolicy": "relay",
|
87 |
+
}
|
88 |
+
else:
|
89 |
+
rtc_configuration = None
|
90 |
+
|
91 |
+
|
92 |
+
def detection(image, conf_threshold=0.3):
|
93 |
+
image = cv2.resize(image, (model.input_width, model.input_height))
|
94 |
+
new_image = model.detect_objects(image, conf_threshold)
|
95 |
+
return cv2.resize(new_image, (500, 500))
|
96 |
+
|
97 |
+
|
98 |
+
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
99 |
+
.my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
|
100 |
+
|
101 |
+
|
102 |
+
with gr.Blocks(css=css) as demo:
|
103 |
+
gr.HTML(
|
104 |
+
"""
|
105 |
+
<h1 style='text-align: center'>
|
106 |
+
YOLOv10 Webcam Stream (Powered by WebRTC ⚡️)
|
107 |
+
</h1>
|
108 |
+
"""
|
109 |
+
)
|
110 |
+
gr.HTML(
|
111 |
+
"""
|
112 |
+
<h3 style='text-align: center'>
|
113 |
+
<a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
|
114 |
+
</h3>
|
115 |
+
"""
|
116 |
+
)
|
117 |
+
with gr.Column(elem_classes=["my-column"]):
|
118 |
+
with gr.Group(elem_classes=["my-group"]):
|
119 |
+
image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
120 |
+
conf_threshold = gr.Slider(
|
121 |
+
label="Confidence Threshold",
|
122 |
+
minimum=0.0,
|
123 |
+
maximum=1.0,
|
124 |
+
step=0.05,
|
125 |
+
value=0.30,
|
126 |
+
)
|
127 |
+
|
128 |
+
image.stream(
|
129 |
+
fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
|
130 |
+
)
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
demo.launch()
|