File size: 1,074 Bytes
791ec16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import streamlit as st
import cv2
import numpy as np
from ultralytics import YOLO
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
import av

st.set_page_config(page_title="Face Detection", layout="wide")

st.title("🎥 Live Face Detection (YOLOv8 + WebRTC)")

@st.cache_resource
def load_model():
    return YOLO("best.pt")

model = load_model()

conf_threshold = st.slider(
    "Confidence Threshold",
    min_value=0.1,
    max_value=1.0,
    value=0.7,
    step=0.05
)

class FaceProcessor(VideoProcessorBase):
    def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
        img = frame.to_ndarray(format="bgr24")

        results = model.predict(
            source=img,
            conf=conf_threshold,
            imgsz=640,
            verbose=False
        )

        annotated = results[0].plot()
        return av.VideoFrame.from_ndarray(annotated, format="bgr24")

webrtc_streamer(
    key="face-detection",
    video_processor_factory=FaceProcessor,
    media_stream_constraints={"video": True, "audio": False},
    async_processing=True,
)