ADharsh22 commited on
Commit
62c200f
·
1 Parent(s): 09b3522

added app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -1,28 +1,41 @@
1
- import av
2
  import streamlit as st
3
  import cv2 # OpenCV for image processing
4
- import numpy as np # <--- REQUIRED: Must be in requirements.txt (pip install av)
 
5
  from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, WebRtcMode
6
 
7
- # --- PLACEHOLDER IMPORTS (Adjust as needed) ---
8
- # ...
 
 
 
 
9
 
10
  # --- CONFIGURATION ---
11
  RECOGNITION_THRESHOLD = 0.6
12
  FRAME_SKIP = 3
13
 
14
- # --- CACHE THE MODEL/FACTORY (CRITICAL FIX FOR THREADING) ---
15
- # This ensures the processor and its threads are initialized only ONCE.
 
 
 
 
 
 
 
 
 
16
  @st.cache_resource
17
  def get_face_processor_factory():
18
- """Returns the processor class, ensuring it is cached."""
19
 
20
  class FaceRecognitionProcessor(VideoProcessorBase):
21
  """Processes video frames using the standard VideoProcessorBase."""
22
  def __init__(self):
23
- # Initialize models once here (safely cached by @st.cache_resource)
 
24
  self.frame_count = 0
25
- # Example: self.detection_model = load_your_model()
26
 
27
  def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
28
  # Convert frame from av.VideoFrame to numpy array (BGR)
@@ -34,6 +47,7 @@ def get_face_processor_factory():
34
 
35
  # --- Your Face Detection and Recognition Logic Goes Here ---
36
  h, w, _ = img.shape
 
37
  # Placeholder drawing logic:
38
  x, y, w_box, h_box = w//4, h//4, w//2, h//2
39
  cv2.rectangle(img, (x, y), (x + w_box, y + h_box), (0, 0, 255), 2)
@@ -51,14 +65,16 @@ def main():
51
  st.title("Smart Office Face Recognition System 📸")
52
  st.sidebar.title("Configuration")
53
 
54
- # ... (Sidebar control for threshold) ...
 
 
55
 
56
  # --- FINAL WEBRTC STREAMER CALL ---
57
  webrtc_streamer(
58
  key="face-recognition-stream-final-cache",
59
  mode=WebRtcMode.SENDRECV,
60
 
61
- # --- Enhanced STUN/TURN configuration ---
62
  rtc_configuration={
63
  "iceServers": [
64
  {"urls": ["stun:stun.l.google.com:19302"]},
@@ -76,5 +92,4 @@ def main():
76
 
77
  # --- EXECUTION ---
78
  if __name__ == "__main__":
79
- # IMPORTANT: Ensure 'av' is in your requirements.txt
80
  main()
 
 
1
  import streamlit as st
2
  import cv2 # OpenCV for image processing
3
+ import numpy as np
4
+ import av # REQUIRED for VideoProcessorBase
5
  from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, WebRtcMode
6
 
7
+ # --- PLACEHOLDER IMPORTS (UNCOMMENT/ADJUST AS NEEDED) ---
8
+ # NOTE: Make sure these core libraries are in your requirements.txt
9
+ # import deepface
10
+ # from deepface import DeepFace # Example import if using deepface
11
+ # from src.detect import detect_faces
12
+ # from src.recognize import recognize_face
13
 
14
  # --- CONFIGURATION ---
15
  RECOGNITION_THRESHOLD = 0.6
16
  FRAME_SKIP = 3
17
 
18
+
19
+ # --- CRITICAL FIX 1: CACHE THE HEAVY MODELS SEPARATELY ---
20
+ @st.cache_resource
21
+ def load_deepface_models():
22
+ """Loads all heavy models (DeepFace, etc.) only once, safely outside the thread."""
23
+ # NOTE: Replace 'return "Loaded Models"' with your actual model loading logic
24
+ # Example: return DeepFace.build_model("VGG-Face")
25
+ return "Loaded Models"
26
+
27
+
28
+ # --- CRITICAL FIX 2: CACHE THE FACTORY ---
29
  @st.cache_resource
30
  def get_face_processor_factory():
31
+ """Returns the processor class, ensuring its initialization is thread-safe."""
32
 
33
  class FaceRecognitionProcessor(VideoProcessorBase):
34
  """Processes video frames using the standard VideoProcessorBase."""
35
  def __init__(self):
36
+ # Load models from the cached function, reducing memory strain on thread start
37
+ self.models = load_deepface_models()
38
  self.frame_count = 0
 
39
 
40
  def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
41
  # Convert frame from av.VideoFrame to numpy array (BGR)
 
47
 
48
  # --- Your Face Detection and Recognition Logic Goes Here ---
49
  h, w, _ = img.shape
50
+
51
  # Placeholder drawing logic:
52
  x, y, w_box, h_box = w//4, h//4, w//2, h//2
53
  cv2.rectangle(img, (x, y), (x + w_box, y + h_box), (0, 0, 255), 2)
 
65
  st.title("Smart Office Face Recognition System 📸")
66
  st.sidebar.title("Configuration")
67
 
68
+ RECOGNITION_THRESHOLD = st.sidebar.slider(
69
+ "Recognition Threshold", min_value=0.0, max_value=1.0, value=0.6, step=0.05
70
+ )
71
 
72
  # --- FINAL WEBRTC STREAMER CALL ---
73
  webrtc_streamer(
74
  key="face-recognition-stream-final-cache",
75
  mode=WebRtcMode.SENDRECV,
76
 
77
+ # --- Enhanced STUN/TURN configuration to stabilize cloud connection ---
78
  rtc_configuration={
79
  "iceServers": [
80
  {"urls": ["stun:stun.l.google.com:19302"]},
 
92
 
93
  # --- EXECUTION ---
94
  if __name__ == "__main__":
 
95
  main()