Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,13 @@ import cv2
|
|
2 |
import streamlit as st
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
# Load the pre-trained Haar Cascade face detector
|
7 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
8 |
|
|
|
9 |
def detect_faces(frame):
|
10 |
-
"""
|
11 |
-
Detect faces in the frame.
|
12 |
-
Returns the frame with bounding boxes drawn around detected faces.
|
13 |
-
"""
|
14 |
# Convert the frame to grayscale (Haar Cascade works on grayscale images)
|
15 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
16 |
|
@@ -26,23 +24,34 @@ def detect_faces(frame):
|
|
26 |
# Streamlit UI for the app
|
27 |
st.title("Real-Time Face Detection")
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
# Convert the camera image into a numpy array
|
35 |
-
img = Image.open(camera)
|
36 |
-
img_array = np.array(img)
|
37 |
-
|
38 |
-
# Convert the image to a format OpenCV can process (BGR)
|
39 |
-
img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
|
48 |
-
|
|
|
2 |
import streamlit as st
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
import time
|
6 |
|
7 |
# Load the pre-trained Haar Cascade face detector
|
8 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
9 |
|
10 |
+
# Define a function to detect faces in a frame
|
11 |
def detect_faces(frame):
|
|
|
|
|
|
|
|
|
12 |
# Convert the frame to grayscale (Haar Cascade works on grayscale images)
|
13 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
14 |
|
|
|
24 |
# Streamlit UI for the app
|
25 |
st.title("Real-Time Face Detection")
|
26 |
|
27 |
+
# Create a container for the webcam video feed
|
28 |
+
video_placeholder = st.empty()
|
29 |
|
30 |
+
# Start the webcam feed
|
31 |
+
cap = cv2.VideoCapture(0) # 0 is the default webcam device
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
if not cap.isOpened():
|
34 |
+
st.error("Error: Could not access the webcam.")
|
35 |
+
else:
|
36 |
+
# Start capturing video frames
|
37 |
+
while True:
|
38 |
+
ret, frame = cap.read()
|
39 |
+
|
40 |
+
if not ret:
|
41 |
+
st.error("Failed to grab frame.")
|
42 |
+
break
|
43 |
+
|
44 |
+
# Detect faces in the current frame
|
45 |
+
result_frame = detect_faces(frame)
|
46 |
+
|
47 |
+
# Convert BGR (OpenCV format) to RGB (for Streamlit display)
|
48 |
+
result_frame_rgb = cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB)
|
49 |
+
|
50 |
+
# Display the frame in Streamlit (dynamically updating the image)
|
51 |
+
video_placeholder.image(result_frame_rgb, channels="RGB", use_column_width=True)
|
52 |
|
53 |
+
# Add a small delay to control the frame rate (optional)
|
54 |
+
time.sleep(0.03) # This gives roughly 30 FPS
|
55 |
|
56 |
+
# Release the webcam when the stream ends
|
57 |
+
cap.release()
|