Face_Detection / face_detection.py
roseyshi's picture
Upload 16 files
6f2dcbc verified
Raw
History Blame Contribute Delete
892 Bytes
# backend/core/face_detection.py
import cv2
from ultralytics import YOLO
import numpy as np
class FaceDetector:
def __init__(self, model_path='yolov8n-face.pt'):
self.model = YOLO(model_path)
self.confidence_threshold = 0.5
def detect_faces(self, frame):
"""Detect faces in a frame using YOLO"""
results = self.model(frame)
faces = []
for result in results:
boxes = result.boxes
for box in boxes:
if box.conf > self.confidence_threshold:
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
face = frame[y1:y2, x1:x2]
faces.append({
'bbox': (x1, y1, x2, y2),
'face_img': face,
'confidence': box.conf
})
return faces