Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
|
| 6 |
+
import av
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="Face Detection", layout="wide")
|
| 9 |
+
|
| 10 |
+
st.title("🎥 Live Face Detection (YOLOv8 + WebRTC)")
|
| 11 |
+
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_model():
|
| 14 |
+
return YOLO("best.pt")
|
| 15 |
+
|
| 16 |
+
model = load_model()
|
| 17 |
+
|
| 18 |
+
conf_threshold = st.slider(
|
| 19 |
+
"Confidence Threshold",
|
| 20 |
+
min_value=0.1,
|
| 21 |
+
max_value=1.0,
|
| 22 |
+
value=0.7,
|
| 23 |
+
step=0.05
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
class FaceProcessor(VideoProcessorBase):
|
| 27 |
+
def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
|
| 28 |
+
img = frame.to_ndarray(format="bgr24")
|
| 29 |
+
|
| 30 |
+
results = model.predict(
|
| 31 |
+
source=img,
|
| 32 |
+
conf=conf_threshold,
|
| 33 |
+
imgsz=640,
|
| 34 |
+
verbose=False
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
annotated = results[0].plot()
|
| 38 |
+
return av.VideoFrame.from_ndarray(annotated, format="bgr24")
|
| 39 |
+
|
| 40 |
+
webrtc_streamer(
|
| 41 |
+
key="face-detection",
|
| 42 |
+
video_processor_factory=FaceProcessor,
|
| 43 |
+
media_stream_constraints={"video": True, "audio": False},
|
| 44 |
+
async_processing=True,
|
| 45 |
+
)
|