Update app.py
Browse files
app.py
CHANGED
@@ -190,14 +190,18 @@
|
|
190 |
#)
|
191 |
|
192 |
import logging
|
|
|
|
|
|
|
|
|
|
|
193 |
import cv2
|
194 |
import numpy as np
|
195 |
-
import mediapipe as mp
|
196 |
import streamlit as st
|
197 |
-
from streamlit_webrtc import webrtc_streamer
|
198 |
-
|
199 |
-
import
|
200 |
-
from
|
201 |
|
202 |
# Logging setup
|
203 |
logger = logging.getLogger(__name__)
|
@@ -216,6 +220,13 @@ class Detection(NamedTuple):
|
|
216 |
score: float
|
217 |
box: np.ndarray
|
218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
# Angle calculation function
|
220 |
def calculate_angle(a, b, c):
|
221 |
a = np.array(a)
|
@@ -229,10 +240,11 @@ def calculate_angle(a, b, c):
|
|
229 |
|
230 |
|
231 |
# Detection Queue
|
232 |
-
result_queue: queue.Queue[List[Detection]] = queue.Queue()
|
233 |
|
234 |
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
235 |
image = frame.to_ndarray(format="bgr24")
|
|
|
236 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
237 |
|
238 |
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
|
@@ -294,11 +306,14 @@ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
|
294 |
|
295 |
|
296 |
# WebRTC streamer configuration
|
|
|
297 |
webrtc_streamer(
|
298 |
key="squat-detection",
|
299 |
-
|
|
|
300 |
media_stream_constraints={"video": True, "audio": False},
|
301 |
-
|
|
|
302 |
)
|
303 |
|
304 |
|
|
|
190 |
#)
|
191 |
|
192 |
import logging
|
193 |
+
import queue
|
194 |
+
from pathlib import Path
|
195 |
+
from typing import List, NamedTuple
|
196 |
+
|
197 |
+
import av
|
198 |
import cv2
|
199 |
import numpy as np
|
|
|
200 |
import streamlit as st
|
201 |
+
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
202 |
+
|
203 |
+
from sample_utils.download import download_file
|
204 |
+
from sample_utils.turn import get_ice_servers
|
205 |
|
206 |
# Logging setup
|
207 |
logger = logging.getLogger(__name__)
|
|
|
220 |
score: float
|
221 |
box: np.ndarray
|
222 |
|
223 |
+
@st.cache_resource # type: ignore
|
224 |
+
def generate_label_colors():
|
225 |
+
return np.random.uniform(0, 255, size=(len(CLASSES), 3))
|
226 |
+
|
227 |
+
|
228 |
+
COLORS = generate_label_colors()
|
229 |
+
|
230 |
# Angle calculation function
|
231 |
def calculate_angle(a, b, c):
|
232 |
a = np.array(a)
|
|
|
240 |
|
241 |
|
242 |
# Detection Queue
|
243 |
+
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
244 |
|
245 |
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
246 |
image = frame.to_ndarray(format="bgr24")
|
247 |
+
h, w = image.shape[:2]
|
248 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
249 |
|
250 |
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
|
|
|
306 |
|
307 |
|
308 |
# WebRTC streamer configuration
|
309 |
+
|
310 |
webrtc_streamer(
|
311 |
key="squat-detection",
|
312 |
+
mode=WebRtcMode.SENDRECV,
|
313 |
+
rtc_configuration={"iceServers": get_ice_servers(), "iceTransportPolicy": "relay"},
|
314 |
media_stream_constraints={"video": True, "audio": False},
|
315 |
+
video_frame_callback=video_frame_callback,
|
316 |
+
async_processing=True,
|
317 |
)
|
318 |
|
319 |
|