Overview
TheFaceDetector class provides a unified interface to multiple face detection models with built-in ROI stabilization to reduce jitter and false positives. It supports Haar Cascade, MTCNN, YOLO, and MediaPipe detectors.
Initialization
Type of detector to use. Available options:
"haar"- Haar Cascade classifier (fastest)"mtcnn"- Multi-task Cascaded CNN (most accurate)"yolo"- YOLO-based detector"mediapipe"- MediaPipe Face Detection (balanced)
Additional parameters passed to the specific detector. See individual detector documentation for supported parameters.
Attributes
Dictionary mapping model type strings to detector classes:
Current detector instance (one of the detector types from MODELS).
History of recent ROI detections for stabilization (maxlen=5).
Current stabilized ROI coordinates as
(x, y, w, h) or None.Methods
detect_face()
Detect face in frame with stabilization and bounds checking.Input image frame in BGR format (OpenCV default).
Stabilized ROI coordinates as
(x, y, w, h) in pixels, or None if no face detected.x: X-coordinate of top-left cornery: Y-coordinate of top-left cornerw: Width of the ROIh: Height of the ROI
stabilize_roi()
Apply temporal smoothing to ROI using weighted averaging.New ROI detection as
(x, y, w, h) or None.Stabilized ROI coordinates or previous stable ROI if no new detection provided.Uses weighted average of recent ROIs (weights defined in
config.ROI_WEIGHTS).
Requires minimum 3 detections in history for stabilization to take effect.is_significant_change()
Check if ROI change exceeds threshold to avoid unnecessary updates.Previous ROI as
(x, y, w, h).Current ROI as
(x, y, w, h).True if change in any dimension exceeds
ROI_CHANGE_THRESHOLD pixels (default: 20px), False otherwise.close()
Release resources used by the detector.Usage Examples
Basic Usage with MediaPipe
Using YOLO Detector
Using Haar Cascade with Custom Parameters
Using MTCNN with Custom Confidence
Complete Pipeline Example
Fromexperiments/simple_run_ROI.py:
Stabilization Details
TheFaceDetector includes sophisticated ROI stabilization:
- Temporal Smoothing: Uses weighted average of last 5 detections
- Weights Configuration: More recent detections have higher weight (from
config.ROI_WEIGHTS) - Minimum History: Requires at least 3 detections before applying stabilization
- Change Threshold: Only updates when change exceeds 20 pixels (configurable)
- Bounds Checking: Ensures ROI stays within frame boundaries
Performance Characteristics
| Detector | Speed | Accuracy | Use Case |
|---|---|---|---|
| haar | Fastest (~30+ FPS) | Good | Real-time on low-power devices |
| mediapipe | Fast (~25 FPS) | Very Good | Balanced performance |
| yolo | Moderate (~15 FPS) | Excellent | High accuracy needed |
| mtcnn | Slow (~10 FPS) | Excellent | Maximum accuracy |
From
experiments/advance_run_complete.py: The detector tracks performance metrics including detection time, FPS, ROI stability (jitter), and consecutive failure counts.