ADharsh22 commited on
Commit
cb56c4a
·
1 Parent(s): a69de24

FIX: Wrapped webrtc_streamer in st.session_state to prevent race condition causing AttributeError on thread shutdown.

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import streamlit as st
2
  import cv2 # OpenCV for image processing
3
  import numpy as np
4
- import time
5
  from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, WebRtcMode, VideoProcessorBase
 
6
  # NOTE: Make sure these core libraries are in your requirements.txt
7
  # import deepface
8
  # import sklearn # if needed for recognition/clustering
@@ -21,7 +21,6 @@ FRAME_SKIP = 3 # Process every 3rd frame for performance
21
 
22
 
23
  # --- VIDEO PROCESSING CLASS ---
24
- # VideoTransformerBase handles receiving frames and sending them back
25
  class FaceRecognitionTransformer(VideoTransformerBase):
26
  """
27
  A class that processes video frames in real-time for face recognition.
@@ -49,12 +48,10 @@ class FaceRecognitionTransformer(VideoTransformerBase):
49
  img = frame.copy()
50
 
51
  # 1. Detect Faces (Placeholder Logic)
52
- # Replace with your actual detection function call
53
- # Example: faces = detect_faces(img, self.detection_model)
54
-
55
  # Placeholder: Assume one face in the middle for demonstration
56
  # In a real app, you'd get (x, y, w, h) for all faces
57
  h, w, _ = img.shape
 
58
  faces = [(w//4, h//4, w//2, h//2)]
59
 
60
  for (x, y, w, h) in faces:
@@ -95,24 +92,28 @@ def main():
95
  "Recognition Threshold", min_value=0.0, max_value=1.0, value=0.6, step=0.05
96
  )
97
 
98
- # Start the WebRTC Streamer
99
- webrtc_streamer(
100
- key="face-recognition-stream",
101
- # Use SENDRECV mode for two-way communication (video in, video out)
102
- mode=WebRtcMode.SENDRECV,
103
-
104
- # --- CRITICAL FIX: Enhanced STUN/TURN configuration to fix aioice errors ---
105
- rtc_configuration={
106
- "iceServers": [
107
- # Google's public STUN server (standard)
108
- {"urls": ["stun:stun.l.google.com:19302"]},
109
- # Mozilla's public STUN server (as backup)
110
- {"urls": ["stun:stun.services.mozilla.com"]}
111
- ]
112
- },
113
- video_transformer_factory=FaceRecognitionTransformer,
114
- async_transform=True
115
- )
 
 
 
 
116
 
117
  st.markdown("---")
118
  st.subheader("Access Log (Placeholder)")
 
1
  import streamlit as st
2
  import cv2 # OpenCV for image processing
3
  import numpy as np
 
4
  from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, WebRtcMode, VideoProcessorBase
5
+
6
  # NOTE: Make sure these core libraries are in your requirements.txt
7
  # import deepface
8
  # import sklearn # if needed for recognition/clustering
 
21
 
22
 
23
  # --- VIDEO PROCESSING CLASS ---
 
24
  class FaceRecognitionTransformer(VideoTransformerBase):
25
  """
26
  A class that processes video frames in real-time for face recognition.
 
48
  img = frame.copy()
49
 
50
  # 1. Detect Faces (Placeholder Logic)
 
 
 
51
  # Placeholder: Assume one face in the middle for demonstration
52
  # In a real app, you'd get (x, y, w, h) for all faces
53
  h, w, _ = img.shape
54
+ # Example: faces = detect_faces(img, self.detection_model)
55
  faces = [(w//4, h//4, w//2, h//2)]
56
 
57
  for (x, y, w, h) in faces:
 
92
  "Recognition Threshold", min_value=0.0, max_value=1.0, value=0.6, step=0.05
93
  )
94
 
95
+ # Use a stable key for the streamer
96
+ STREAMER_KEY = "face-recognition-stream-stable"
97
+
98
+ # --- CRITICAL FIX: Session State Wrapper ---
99
+ # Only initialize the streamer if it's not already in the session state.
100
+ # This prevents the thread initialization crash on re-runs.
101
+ if STREAMER_KEY not in st.session_state:
102
+ st.session_state[STREAMER_KEY] = webrtc_streamer(
103
+ key=STREAMER_KEY, # Use the stable key here
104
+ # Use SENDRECV mode for two-way communication (video in, video out)
105
+ mode=WebRtcMode.SENDRECV,
106
+
107
+ # --- CRITICAL FIX: Enhanced STUN/TURN configuration to fix aioice errors ---
108
+ rtc_configuration={
109
+ "iceServers": [
110
+ {"urls": ["stun:stun.l.google.com:19302"]},
111
+ {"urls": ["stun:stun.services.mozilla.com"]}
112
+ ]
113
+ },
114
+ video_transformer_factory=FaceRecognitionTransformer,
115
+ async_transform=True
116
+ )
117
 
118
  st.markdown("---")
119
  st.subheader("Access Log (Placeholder)")