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

FIX: Added explicit secondary STUN server to resolve NoneType transport error in aioice.

Browse files
Files changed (1) hide show
  1. app.py +68 -36
app.py CHANGED
@@ -1,60 +1,83 @@
1
  import streamlit as st
2
- import cv2 # Essential for processing video frames
3
  import numpy as np
4
- from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, WebRtcMode
 
 
 
 
5
 
6
- # --- [PLACEHOLDER IMPORTS FOR YOUR PROJECT STRUCTURE] ---
7
- # NOTE: Replace these with your actual imports if using src/ files
8
  # from src.detect import detect_faces
 
9
  # from src.recognize import recognize_face
10
- # from src.log import LogManager
11
 
12
- # --- CONFIGURATION (Move to a separate config file if complex) ---
13
- # Assuming you have a list of known users for recognition
14
- KNOWN_USERS = ["Adharsh", "Jane Doe", "Guest"]
15
- FRAME_SKIP = 5 # Process every 5th frame for performance
16
 
17
- # --- VIDEO PROCESSING CLASS ---
 
 
 
 
18
 
 
19
  # VideoTransformerBase handles receiving frames and sending them back
20
  class FaceRecognitionTransformer(VideoTransformerBase):
21
  """
22
  A class that processes video frames in real-time for face recognition.
23
  """
24
- def __init__(self, recognition_threshold=0.6):
25
- # Initialize any models or trackers here
26
- # Example: self.model = load_recognition_model()
27
- self.recognition_threshold = recognition_threshold
28
- # Placeholder for face detection/recognition models
29
- self.detector = None
30
- self.recognizer = None
31
-
 
 
 
32
  def transform(self, frame: np.ndarray) -> np.ndarray:
33
- # Convert the frame from BGR (OpenCV default) to RGB
 
 
 
 
 
 
 
34
  img = frame.copy()
35
 
36
- # 1. Detect Faces
37
- # This function should return bounding boxes and confidence scores
38
- # Example: faces = detect_faces(img)
39
- faces = [] # Placeholder for detected faces (x, y, w, h)
 
 
 
 
40
 
41
  for (x, y, w, h) in faces:
42
- # 2. Recognize Face
43
- # recognized_name = recognize_face(img, x, y, w, h, self.recognizer)
44
  recognized_name = "Unknown" # Placeholder result
45
-
46
- # 3. Log/Display Result
47
- if recognized_name != "Unknown":
 
 
48
  color = (0, 255, 0) # Green for known user
49
- # LogManager.log_access(recognized_name)
50
  else:
51
  color = (0, 0, 255) # Red for unknown user
 
52
 
53
  # Draw bounding box
54
  cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
55
 
56
  # Draw label
57
- cv2.putText(img, recognized_name, (x, y - 10),
 
58
  cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
59
 
60
  return img
@@ -62,23 +85,32 @@ class FaceRecognitionTransformer(VideoTransformerBase):
62
  # --- STREAMLIT UI ---
63
 
64
  def main():
 
 
65
  st.title("Smart Office Face Recognition System 📸")
66
  st.sidebar.title("Configuration")
67
 
68
- # Sidebar controls (optional)
69
- recognition_threshold = st.sidebar.slider(
70
  "Recognition Threshold", min_value=0.0, max_value=1.0, value=0.6, step=0.05
71
  )
72
 
73
  # Start the WebRTC Streamer
74
- # NOTE: Set WebRtcMode.SENDONLY if you only need the camera feed
75
  webrtc_streamer(
76
  key="face-recognition-stream",
 
77
  mode=WebRtcMode.SENDRECV,
 
 
78
  rtc_configuration={
79
- "iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
 
 
 
 
 
80
  },
81
- video_transformer_factory=lambda: FaceRecognitionTransformer(recognition_threshold),
82
  async_transform=True
83
  )
84
 
@@ -86,7 +118,7 @@ def main():
86
  st.subheader("Access Log (Placeholder)")
87
  # Placeholder for displaying logs
88
  # if st.button("Refresh Log"):
89
- # st.dataframe(LogManager.get_logs())
90
 
91
  # --- EXECUTION ---
92
  if __name__ == "__main__":
 
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
9
 
10
+ # --- PLACEHOLDER IMPORTS (UNCOMMENT/ADJUST AS NEEDED) ---
 
11
  # from src.detect import detect_faces
12
+ # from src.embed import get_embeddings
13
  # from src.recognize import recognize_face
14
+ # from src.utils import LogManager
15
 
 
 
 
 
16
 
17
+ # --- CONFIGURATION ---
18
+ # NOTE: Adjust these values based on your model/system performance
19
+ RECOGNITION_THRESHOLD = 0.6
20
+ 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.
28
  """
29
+ def __init__(self):
30
+ # Initialize any models/trackers here to load them once
31
+ # Example: self.detector = deepface.DeepFace.build_model("mtcnn")
32
+ # Example: self.recognizer = load_your_recognizer_model()
33
+ self.frame_count = 0
34
+ self.detection_model = None # Placeholder
35
+ self.recognition_model = None # Placeholder
36
+
37
+ # Log manager placeholder
38
+ # self.log_manager = LogManager()
39
+
40
  def transform(self, frame: np.ndarray) -> np.ndarray:
41
+ # Increment frame count
42
+ self.frame_count += 1
43
+
44
+ # Skip frames to reduce CPU load
45
+ if self.frame_count % FRAME_SKIP != 0:
46
+ return frame
47
+
48
+ # Convert frame from BGR (OpenCV default) to RGB
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:
61
+ # 2. Recognize Face (Placeholder Logic)
62
+ # Example: recognized_name, score = recognize_face(img, x, y, w, h, self.recognition_model)
63
  recognized_name = "Unknown" # Placeholder result
64
+ score = 0.0
65
+
66
+ # --- Decision and Visualization ---
67
+
68
+ if recognized_name != "Unknown" and score >= RECOGNITION_THRESHOLD:
69
  color = (0, 255, 0) # Green for known user
70
+ # self.log_manager.log_access(recognized_name)
71
  else:
72
  color = (0, 0, 255) # Red for unknown user
73
+ recognized_name = "Unknown"
74
 
75
  # Draw bounding box
76
  cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
77
 
78
  # Draw label
79
+ label = f"{recognized_name}: {score:.2f}"
80
+ cv2.putText(img, label, (x, y - 10),
81
  cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
82
 
83
  return img
 
85
  # --- STREAMLIT UI ---
86
 
87
  def main():
88
+ global RECOGNITION_THRESHOLD
89
+
90
  st.title("Smart Office Face Recognition System 📸")
91
  st.sidebar.title("Configuration")
92
 
93
+ # Sidebar control for threshold
94
+ RECOGNITION_THRESHOLD = st.sidebar.slider(
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
 
 
118
  st.subheader("Access Log (Placeholder)")
119
  # Placeholder for displaying logs
120
  # if st.button("Refresh Log"):
121
+ # st.dataframe(LogManager().get_logs())
122
 
123
  # --- EXECUTION ---
124
  if __name__ == "__main__":