Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,44 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import av
|
| 3 |
-
import asyncio
|
| 4 |
-
import numpy as np
|
| 5 |
-
import streamlit as st
|
| 6 |
-
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
| 7 |
-
|
| 8 |
-
# Fix asyncio event loop issue on Windows
|
| 9 |
-
if hasattr(asyncio, 'WindowsSelectorEventLoopPolicy'):
|
| 10 |
-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 11 |
-
|
| 12 |
-
# Load Haarcascade model
|
| 13 |
-
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 14 |
-
|
| 15 |
-
# Face detection function for WebRTC
|
| 16 |
-
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import av
|
| 3 |
+
import asyncio
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
| 7 |
+
|
| 8 |
+
# Fix asyncio event loop issue on Windows (this is useful for local testing on Windows)
|
| 9 |
+
if hasattr(asyncio, 'WindowsSelectorEventLoopPolicy'):
|
| 10 |
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 11 |
+
|
| 12 |
+
# Load Haarcascade model
|
| 13 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 14 |
+
|
| 15 |
+
# Face detection function for WebRTC
|
| 16 |
+
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
| 17 |
+
# Convert the frame to a numpy array (OpenCV format)
|
| 18 |
+
img = frame.to_ndarray(format="bgr24")
|
| 19 |
+
|
| 20 |
+
# Convert image to grayscale
|
| 21 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 22 |
+
|
| 23 |
+
# Detect faces in the grayscale image
|
| 24 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 25 |
+
|
| 26 |
+
# Draw rectangles around detected faces
|
| 27 |
+
for (x, y, w, h) in faces:
|
| 28 |
+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 29 |
+
|
| 30 |
+
# Return the processed frame
|
| 31 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
| 32 |
+
|
| 33 |
+
# Streamlit UI
|
| 34 |
+
st.title("Real-Time Face Detection")
|
| 35 |
+
st.write("Using OpenCV and Haar Cascade Model")
|
| 36 |
+
|
| 37 |
+
# WebRTC streamer (webrtc_streamer already handles async tasks internally)
|
| 38 |
+
webrtc_streamer(
|
| 39 |
+
key="face-detection",
|
| 40 |
+
mode=WebRtcMode.SENDRECV,
|
| 41 |
+
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
|
| 42 |
+
video_frame_callback=video_frame_callback,
|
| 43 |
+
async_processing=True # Keep this enabled for async processing
|
| 44 |
)
|