Fix the object detection demo to use Open Relay TURN server
Browse files- pages/1_object_detection.py +2 -1
- sample_utils/turn.py +26 -0
pages/1_object_detection.py
CHANGED
@@ -15,6 +15,7 @@ import streamlit as st
|
|
15 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
16 |
|
17 |
from sample_utils.download import download_file
|
|
|
18 |
|
19 |
HERE = Path(__file__).parent
|
20 |
ROOT = HERE.parent
|
@@ -137,7 +138,7 @@ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
|
137 |
webrtc_ctx = webrtc_streamer(
|
138 |
key="object-detection",
|
139 |
mode=WebRtcMode.SENDRECV,
|
140 |
-
rtc_configuration={"iceServers":
|
141 |
video_frame_callback=video_frame_callback,
|
142 |
media_stream_constraints={"video": True, "audio": False},
|
143 |
async_processing=True,
|
|
|
15 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
16 |
|
17 |
from sample_utils.download import download_file
|
18 |
+
from sample_utils.turn import get_ice_servers
|
19 |
|
20 |
HERE = Path(__file__).parent
|
21 |
ROOT = HERE.parent
|
|
|
138 |
webrtc_ctx = webrtc_streamer(
|
139 |
key="object-detection",
|
140 |
mode=WebRtcMode.SENDRECV,
|
141 |
+
rtc_configuration={"iceServers": get_ice_servers()},
|
142 |
video_frame_callback=video_frame_callback,
|
143 |
media_stream_constraints={"video": True, "audio": False},
|
144 |
async_processing=True,
|
sample_utils/turn.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
|
4 |
+
import requests
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
logger = logging.getLogger(__name__)
|
8 |
+
|
9 |
+
|
10 |
+
OPEN_RELAY_API_HOST = os.environ.get("OPEN_RELAY_API_HOST")
|
11 |
+
OPEN_RELAY_API_KEY = os.environ.get("OPEN_RELAY_API_KEY")
|
12 |
+
|
13 |
+
|
14 |
+
@st.cache_data
|
15 |
+
def get_ice_servers():
|
16 |
+
if not OPEN_RELAY_API_HOST or not OPEN_RELAY_API_KEY:
|
17 |
+
logger.warning(
|
18 |
+
"Open Relay API host or key is not set. Fallback to a free STUN server from Google." # noqa: E501
|
19 |
+
)
|
20 |
+
return [{"urls": ["stun:stun.l.google.com:19302"]}]
|
21 |
+
|
22 |
+
# Get response from Open Relay API
|
23 |
+
response = requests.get(
|
24 |
+
f"https://{OPEN_RELAY_API_HOST}/api/v1/turn/credentials?apiKey={OPEN_RELAY_API_KEY}" # noqa: E501
|
25 |
+
)
|
26 |
+
return response.json()
|