Spaces:
Build error
Build error
FINAL DEBUG PUSH: Testing WebRTC stability by isolating model loading with @st .cache_resource.
Browse files
app.py
CHANGED
|
@@ -1,15 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2 # OpenCV for image processing
|
| 3 |
import numpy as np
|
| 4 |
-
import av # REQUIRED
|
| 5 |
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, WebRtcMode
|
| 6 |
|
| 7 |
-
# --- PLACEHOLDER IMPORTS (
|
| 8 |
-
# NOTE:
|
| 9 |
-
#
|
| 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
|
|
@@ -19,10 +16,10 @@ FRAME_SKIP = 3
|
|
| 19 |
# --- CRITICAL FIX 1: CACHE THE HEAVY MODELS SEPARATELY ---
|
| 20 |
@st.cache_resource
|
| 21 |
def load_deepface_models():
|
| 22 |
-
"""Loads all heavy models
|
| 23 |
-
# NOTE:
|
| 24 |
-
#
|
| 25 |
-
return "
|
| 26 |
|
| 27 |
|
| 28 |
# --- CRITICAL FIX 2: CACHE THE FACTORY ---
|
|
@@ -33,24 +30,20 @@ def get_face_processor_factory():
|
|
| 33 |
class FaceRecognitionProcessor(VideoProcessorBase):
|
| 34 |
"""Processes video frames using the standard VideoProcessorBase."""
|
| 35 |
def __init__(self):
|
| 36 |
-
#
|
| 37 |
-
|
|
|
|
| 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)
|
| 42 |
img = frame.to_ndarray(format="bgr24")
|
| 43 |
|
| 44 |
-
|
| 45 |
-
if self.frame_count % FRAME_SKIP != 0:
|
| 46 |
-
return frame # Return the original frame if skipping
|
| 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)
|
| 54 |
|
| 55 |
# Convert back to av.VideoFrame before returning
|
| 56 |
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
|
@@ -88,7 +81,7 @@ def main():
|
|
| 88 |
)
|
| 89 |
|
| 90 |
st.markdown("---")
|
| 91 |
-
|
| 92 |
|
| 93 |
# --- EXECUTION ---
|
| 94 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2 # OpenCV for image processing
|
| 3 |
import numpy as np
|
| 4 |
+
import av # 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 |
+
# NOTE: The logic here is temporarily simplified to test WebRTC stability.
|
| 9 |
+
# The heavy imports (deepface, etc.) should be outside the cached function.
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# --- CONFIGURATION ---
|
| 12 |
RECOGNITION_THRESHOLD = 0.6
|
|
|
|
| 16 |
# --- CRITICAL FIX 1: CACHE THE HEAVY MODELS SEPARATELY ---
|
| 17 |
@st.cache_resource
|
| 18 |
def load_deepface_models():
|
| 19 |
+
"""Loads all heavy models only once, safely outside the thread."""
|
| 20 |
+
# NOTE: Keep your actual heavy model loading logic here (e.g., DeepFace.build_model)
|
| 21 |
+
# This function is retained to verify if the issue is in the loading itself.
|
| 22 |
+
return "LOADED_MODELS_PLACEHOLDER"
|
| 23 |
|
| 24 |
|
| 25 |
# --- CRITICAL FIX 2: CACHE THE FACTORY ---
|
|
|
|
| 30 |
class FaceRecognitionProcessor(VideoProcessorBase):
|
| 31 |
"""Processes video frames using the standard VideoProcessorBase."""
|
| 32 |
def __init__(self):
|
| 33 |
+
# We call the model loading function, but we won't use the result in recv
|
| 34 |
+
# This tests if the memory consumption is the issue.
|
| 35 |
+
self.models_placeholder = load_deepface_models()
|
| 36 |
self.frame_count = 0
|
| 37 |
|
| 38 |
def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
|
| 39 |
+
# --- SIMPLIFIED PROCESSING LOGIC (TEMPORARY DEBUGGING) ---
|
| 40 |
+
|
| 41 |
# Convert frame from av.VideoFrame to numpy array (BGR)
|
| 42 |
img = frame.to_ndarray(format="bgr24")
|
| 43 |
|
| 44 |
+
# Simple, lightweight OpenCV draw operation for verification
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
h, w, _ = img.shape
|
| 46 |
+
cv2.putText(img, "WebRTC OK", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Convert back to av.VideoFrame before returning
|
| 49 |
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
st.markdown("---")
|
| 84 |
+
st.subheader("Access Log (Placeholder)")
|
| 85 |
|
| 86 |
# --- EXECUTION ---
|
| 87 |
if __name__ == "__main__":
|