File size: 4,656 Bytes
851e5ba | 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | """YuNet face detection (OpenCV ``FaceDetectorYN``) for the reframing pipeline.
The bundled ONNX model replaces MediaPipe's legacy ``mp.solutions`` detector,
which newer mediapipe wheels no longer ship (every render silently fell back to
the Haar cascade). Haar stays as the fallback when the model can't be loaded.
"""
from pathlib import Path
from types import SimpleNamespace
from typing import List, Optional, Tuple
import cv2
import numpy as np
from .common import logger
YUNET_MODEL_PATH = Path(__file__).parent / "models" / "face_detection_yunet_2023mar.onnx"
YUNET_SCORE_THRESHOLD = 0.6
YUNET_NMS_THRESHOLD = 0.3
YUNET_TOP_K = 50
# Frames wider than this are downscaled before detection: YuNet's cost grows
# with input size, and talking-head faces stay well above its ~10px minimum.
YUNET_MAX_INPUT_WIDTH = 960
FaceBox = Tuple[float, float, float, float, float] # (x, y, w, h, score) in px
class YuNetFaceDetector:
"""Thin stateful wrapper that resizes YuNet's input per frame size."""
def __init__(
self,
model_path: Path = YUNET_MODEL_PATH,
score_threshold: float = YUNET_SCORE_THRESHOLD,
nms_threshold: float = YUNET_NMS_THRESHOLD,
top_k: int = YUNET_TOP_K,
max_input_width: int = YUNET_MAX_INPUT_WIDTH,
):
if not Path(model_path).is_file():
raise FileNotFoundError(f"YuNet model not found: {model_path}")
self._detector = cv2.FaceDetectorYN.create(
str(model_path), "", (320, 320), score_threshold, nms_threshold, top_k
)
self._input_size: Optional[Tuple[int, int]] = None
self._max_input_width = max(64, int(max_input_width))
def detect(self, frame_bgr: np.ndarray) -> List[FaceBox]:
"""Return face boxes in the frame's own pixel coordinates."""
height, width = frame_bgr.shape[:2]
if width <= 0 or height <= 0:
return []
scale = 1.0
image = frame_bgr
if width > self._max_input_width:
scale = self._max_input_width / float(width)
image = cv2.resize(
frame_bgr,
(self._max_input_width, max(1, int(round(height * scale)))),
interpolation=cv2.INTER_AREA,
)
if image.ndim == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
size = (image.shape[1], image.shape[0])
if size != self._input_size:
self._detector.setInputSize(size)
self._input_size = size
_, faces = self._detector.detect(np.ascontiguousarray(image))
if faces is None:
return []
boxes: List[FaceBox] = []
for face in faces:
x, y, w, h = (float(v) / scale for v in face[:4])
score = float(face[-1])
# Clip to the frame: YuNet boxes can extend past the edges.
x0, y0 = max(0.0, x), max(0.0, y)
x1, y1 = min(float(width), x + w), min(float(height), y + h)
if x1 - x0 > 1 and y1 - y0 > 1:
boxes.append((x0, y0, x1 - x0, y1 - y0, score))
return boxes
def process(self, frame_rgb: np.ndarray) -> SimpleNamespace:
"""MediaPipe ``FaceDetection.process`` compatible result.
Call sites written against ``mp.solutions`` read
``results.detections[i].location_data.relative_bounding_box`` and
``.score``; this keeps them working with YuNet unchanged.
"""
height, width = frame_rgb.shape[:2]
frame_bgr = cv2.cvtColor(frame_rgb, cv2.COLOR_RGB2BGR)
detections = []
for x, y, w, h, score in self.detect(frame_bgr):
box = SimpleNamespace(
xmin=x / width, ymin=y / height, width=w / width, height=h / height
)
detections.append(
SimpleNamespace(
location_data=SimpleNamespace(relative_bounding_box=box),
score=[score],
)
)
return SimpleNamespace(detections=detections or None)
def close(self) -> None:
"""No resources to release; kept for MediaPipe-style call sites."""
def open_yunet_detector(
model_path: Path = YUNET_MODEL_PATH, **kwargs
) -> Optional[YuNetFaceDetector]:
"""Load YuNet, or return None (callers fall back to Haar)."""
try:
return YuNetFaceDetector(model_path, **kwargs)
except Exception as exc:
logger.warning("YuNet face detector unavailable (%s); using Haar", exc)
return None
def open_haar_cascade():
return cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
|