Spaces:
Sleeping
Sleeping
Testimony Adekoya
commited on
Commit
·
d472afb
1
Parent(s):
4974f3a
WIP: Fix on Cam part hopefully
Browse files- pages/1_Live_Detection.py +8 -18
pages/1_Live_Detection.py
CHANGED
|
@@ -30,12 +30,6 @@ def load_app_config():
|
|
| 30 |
|
| 31 |
config, secrets = load_app_config()
|
| 32 |
|
| 33 |
-
# --- Initialize Session State for Queues at the TOP of the script ---
|
| 34 |
-
if "status_queue" not in st.session_state:
|
| 35 |
-
st.session_state.status_queue = queue.Queue()
|
| 36 |
-
if "audio_queue" not in st.session_state:
|
| 37 |
-
st.session_state.audio_queue = queue.Queue()
|
| 38 |
-
|
| 39 |
# --- Client-Side Audio Playback Function ---
|
| 40 |
def autoplay_audio(audio_bytes: bytes):
|
| 41 |
"""Injects HTML to autoplay audio in the user's browser."""
|
|
@@ -49,12 +43,12 @@ def autoplay_audio(audio_bytes: bytes):
|
|
| 49 |
|
| 50 |
# --- WebRTC Video Processor ---
|
| 51 |
class VideoProcessor(VideoProcessorBase):
|
| 52 |
-
|
| 53 |
-
def __init__(self, status_queue: queue.Queue, audio_queue: queue.Queue):
|
| 54 |
-
self.status_queue = status_queue
|
| 55 |
-
self.audio_queue = audio_queue
|
| 56 |
self._detector = get_detector(config)
|
| 57 |
self._alerter = get_alerter(config, secrets["gemini_api_key"])
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
|
| 60 |
img = frame.to_ndarray(format="bgr24")
|
|
@@ -62,23 +56,20 @@ class VideoProcessor(VideoProcessorBase):
|
|
| 62 |
strategy = config.get('detection_strategy')
|
| 63 |
if strategy == 'hybrid':
|
| 64 |
processed_frame, alert_triggered, active_alerts = self._detector.process_frame(img)
|
| 65 |
-
|
| 66 |
-
self.status_queue.put(active_alerts if alert_triggered or 'Low Light' in active_alerts else {"status": "Awake"})
|
| 67 |
else:
|
| 68 |
processed_frame, indicators = self._detector.process_frame(img)
|
| 69 |
-
alert_triggered = any(
|
| 70 |
-
self.status_queue.put(indicators)
|
| 71 |
|
| 72 |
if alert_triggered:
|
| 73 |
audio_data = self._alerter.trigger_alert()
|
| 74 |
if audio_data:
|
| 75 |
-
# Use self.audio_queue.put()
|
| 76 |
self.audio_queue.put(audio_data)
|
| 77 |
else:
|
| 78 |
self._alerter.reset_alert()
|
| 79 |
|
| 80 |
return av.VideoFrame.from_ndarray(processed_frame, format="bgr24")
|
| 81 |
-
|
| 82 |
# --- Page UI ---
|
| 83 |
st.title("📹 Live Drowsiness Detection")
|
| 84 |
st.info("Press 'START' to activate your camera and begin monitoring.")
|
|
@@ -101,9 +92,8 @@ col1, col2 = st.columns([3, 1])
|
|
| 101 |
with col1:
|
| 102 |
webrtc_ctx = webrtc_streamer(
|
| 103 |
key="drowsiness-detection",
|
| 104 |
-
# The factory now correctly passes the queues from session_state
|
| 105 |
video_processor_factory=VideoProcessor,
|
| 106 |
-
rtc_configuration=RTC_CONFIGURATION,
|
| 107 |
media_stream_constraints={"video": True, "audio": False},
|
| 108 |
async_processing=True,
|
| 109 |
)
|
|
|
|
| 30 |
|
| 31 |
config, secrets = load_app_config()
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# --- Client-Side Audio Playback Function ---
|
| 34 |
def autoplay_audio(audio_bytes: bytes):
|
| 35 |
"""Injects HTML to autoplay audio in the user's browser."""
|
|
|
|
| 43 |
|
| 44 |
# --- WebRTC Video Processor ---
|
| 45 |
class VideoProcessor(VideoProcessorBase):
|
| 46 |
+
def __init__(self):
|
|
|
|
|
|
|
|
|
|
| 47 |
self._detector = get_detector(config)
|
| 48 |
self._alerter = get_alerter(config, secrets["gemini_api_key"])
|
| 49 |
+
# Thread-safe queues for communication
|
| 50 |
+
self.status_queue = queue.Queue()
|
| 51 |
+
self.audio_queue = queue.Queue()
|
| 52 |
|
| 53 |
def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
|
| 54 |
img = frame.to_ndarray(format="bgr24")
|
|
|
|
| 56 |
strategy = config.get('detection_strategy')
|
| 57 |
if strategy == 'hybrid':
|
| 58 |
processed_frame, alert_triggered, active_alerts = self._detector.process_frame(img)
|
| 59 |
+
self.status_queue.put(active_alerts if alert_triggered else {"status": "Awake"})
|
|
|
|
| 60 |
else:
|
| 61 |
processed_frame, indicators = self._detector.process_frame(img)
|
| 62 |
+
alert_triggered = any(indicators.values())
|
| 63 |
+
self.status_queue.put(indicators if alert_triggered else {"status": "Awake"})
|
| 64 |
|
| 65 |
if alert_triggered:
|
| 66 |
audio_data = self._alerter.trigger_alert()
|
| 67 |
if audio_data:
|
|
|
|
| 68 |
self.audio_queue.put(audio_data)
|
| 69 |
else:
|
| 70 |
self._alerter.reset_alert()
|
| 71 |
|
| 72 |
return av.VideoFrame.from_ndarray(processed_frame, format="bgr24")
|
|
|
|
| 73 |
# --- Page UI ---
|
| 74 |
st.title("📹 Live Drowsiness Detection")
|
| 75 |
st.info("Press 'START' to activate your camera and begin monitoring.")
|
|
|
|
| 92 |
with col1:
|
| 93 |
webrtc_ctx = webrtc_streamer(
|
| 94 |
key="drowsiness-detection",
|
|
|
|
| 95 |
video_processor_factory=VideoProcessor,
|
| 96 |
+
rtc_configuration=RTC_CONFIGURATION, # Use the new robust configuration
|
| 97 |
media_stream_constraints={"video": True, "audio": False},
|
| 98 |
async_processing=True,
|
| 99 |
)
|