Spaces:
Sleeping
Sleeping
Add Twilio integration for TURN server configuration in WebRTC setup, update video attributes, and create .gitignore for environment files and cache.
Browse files- .gitignore +4 -0
- app.py +50 -6
- requirements.txt +1 -0
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.vscode
|
2 |
+
.streamlit
|
3 |
+
.env
|
4 |
+
__pycache__
|
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import cv2
|
2 |
import math
|
3 |
import numpy as np
|
@@ -6,8 +7,14 @@ import streamlit as st
|
|
6 |
import pandas as pd
|
7 |
import altair as alt
|
8 |
import time
|
9 |
-
from streamlit_webrtc import
|
|
|
|
|
|
|
|
|
|
|
10 |
from streamlit_autorefresh import st_autorefresh
|
|
|
11 |
|
12 |
from line_detector import (
|
13 |
LineDetector,
|
@@ -28,6 +35,34 @@ st.set_page_config(
|
|
28 |
initial_sidebar_state="expanded",
|
29 |
)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Apply custom CSS for a modern minimalist design
|
32 |
st.markdown(
|
33 |
"""
|
@@ -265,11 +300,6 @@ st.markdown(
|
|
265 |
unsafe_allow_html=True,
|
266 |
)
|
267 |
|
268 |
-
# WebRTC Configuration
|
269 |
-
RTC_CONFIGURATION = RTCConfiguration(
|
270 |
-
{"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
|
271 |
-
)
|
272 |
-
|
273 |
# App header with minimalist design
|
274 |
st.title("π Drone Line Follower")
|
275 |
st.markdown("Vision-based line tracking with real-time PID control")
|
@@ -711,9 +741,23 @@ with col1:
|
|
711 |
# Create the webrtc component
|
712 |
webrtc_ctx = webrtc_streamer(
|
713 |
key="line-detection",
|
|
|
|
|
714 |
video_processor_factory=VideoTransformer,
|
715 |
media_stream_constraints={"video": True, "audio": False},
|
716 |
async_processing=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
717 |
)
|
718 |
|
719 |
# Pass the settings to the video transformer
|
|
|
1 |
+
import os
|
2 |
import cv2
|
3 |
import math
|
4 |
import numpy as np
|
|
|
7 |
import pandas as pd
|
8 |
import altair as alt
|
9 |
import time
|
10 |
+
from streamlit_webrtc import (
|
11 |
+
webrtc_streamer,
|
12 |
+
VideoProcessorBase,
|
13 |
+
WebRtcMode,
|
14 |
+
VideoHTMLAttributes,
|
15 |
+
)
|
16 |
from streamlit_autorefresh import st_autorefresh
|
17 |
+
from twilio.rest import Client
|
18 |
|
19 |
from line_detector import (
|
20 |
LineDetector,
|
|
|
35 |
initial_sidebar_state="expanded",
|
36 |
)
|
37 |
|
38 |
+
|
39 |
+
def get_ice_servers():
|
40 |
+
"""
|
41 |
+
Get ICE servers configuration.
|
42 |
+
For Streamlit Cloud deployment, a TURN server is required in addition to STUN.
|
43 |
+
This function will try to use Twilio's TURN server service if credentials are available,
|
44 |
+
otherwise it falls back to a free STUN server from Google.
|
45 |
+
"""
|
46 |
+
try:
|
47 |
+
# Try to get Twilio credentials from environment variables
|
48 |
+
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
|
49 |
+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
|
50 |
+
|
51 |
+
if account_sid and auth_token:
|
52 |
+
client = Client(account_sid, auth_token)
|
53 |
+
token = client.tokens.create()
|
54 |
+
return token.ice_servers
|
55 |
+
else:
|
56 |
+
st.warning(
|
57 |
+
"Twilio credentials not found. Using free STUN server only, which may not work reliably." # Removed Streamlit Cloud mention for generality
|
58 |
+
)
|
59 |
+
except Exception as e:
|
60 |
+
st.error(f"Error setting up Twilio TURN servers: {e}")
|
61 |
+
|
62 |
+
# Fallback to Google's free STUN server
|
63 |
+
return [{"urls": ["stun:stun.l.google.com:19302"]}]
|
64 |
+
|
65 |
+
|
66 |
# Apply custom CSS for a modern minimalist design
|
67 |
st.markdown(
|
68 |
"""
|
|
|
300 |
unsafe_allow_html=True,
|
301 |
)
|
302 |
|
|
|
|
|
|
|
|
|
|
|
303 |
# App header with minimalist design
|
304 |
st.title("π Drone Line Follower")
|
305 |
st.markdown("Vision-based line tracking with real-time PID control")
|
|
|
741 |
# Create the webrtc component
|
742 |
webrtc_ctx = webrtc_streamer(
|
743 |
key="line-detection",
|
744 |
+
mode=WebRtcMode.SENDRECV,
|
745 |
+
rtc_configuration={"iceServers": get_ice_servers()},
|
746 |
video_processor_factory=VideoTransformer,
|
747 |
media_stream_constraints={"video": True, "audio": False},
|
748 |
async_processing=True,
|
749 |
+
video_html_attrs=VideoHTMLAttributes(
|
750 |
+
autoPlay=True,
|
751 |
+
controls=False,
|
752 |
+
style={
|
753 |
+
"width": f"1280px",
|
754 |
+
"height": f"720px",
|
755 |
+
"border-radius": "8px",
|
756 |
+
"margin": "0 auto",
|
757 |
+
"display": "block",
|
758 |
+
"border": "2px solid #AAAAAA", # Changed border to lighter grey
|
759 |
+
},
|
760 |
+
),
|
761 |
)
|
762 |
|
763 |
# Pass the settings to the video transformer
|
requirements.txt
CHANGED
@@ -5,3 +5,4 @@ opencv-python==4.9.0.80
|
|
5 |
numpy==1.24.4
|
6 |
altair==5.5.0
|
7 |
matplotlib==3.10.0
|
|
|
|
5 |
numpy==1.24.4
|
6 |
altair==5.5.0
|
7 |
matplotlib==3.10.0
|
8 |
+
twilio~=9.4.3
|