Overview
TheMediaPipeDetector class uses Google’s MediaPipe face detection model with the short-range configuration. It provides fast, accurate face detection and returns bounding box coordinates for the highest-confidence face.
Initialization
Attributes
MediaPipe face detection module.
MediaPipe face detection instance configured with:
model_selection=0(short-range model, optimized for faces within 2 meters)min_detection_confidence=0.5(50% confidence threshold)
Methods
detect()
Detects a face in the given frame.Input image in BGR format (OpenCV default). Will be automatically converted to RGB internally.
Bounding box as
(x, y, width, height) in pixels, or None if no face detected.Returns only the first (highest confidence) detection.x: X-coordinate of top-left cornery: Y-coordinate of top-left cornerwidth: Width of bounding boxheight: Height of bounding box
close()
Cleanup method (no actual cleanup needed for MediaPipe).Usage Example
Basic Detection
Video Processing
With FaceDetector Manager
Recommended usage through the unified manager:Performance Characteristics
Speed
- Average FPS: ~25 FPS (from experiments)
- Detection Time: ~40ms per frame on typical hardware
- Real-time capable: Yes, suitable for live video
Accuracy
- Model: Short-range model optimized for faces within 2 meters
- Confidence Threshold: 0.5 (50%)
- Detection Quality: High accuracy for frontal faces
- Robustness: Good performance in various lighting conditions
Use Cases
Best suited for:- Real-time video processing
- Webcam applications
- Mobile/embedded devices
- Balanced speed and accuracy requirements
- Close-range face detection (within 2 meters)
Implementation Details
Color Space Conversion
MediaPipe requires RGB input, so BGR frames from OpenCV are automatically converted:Coordinate Conversion
MediaPipe returns normalized coordinates (0-1 range) which are converted to pixel coordinates:Single Face Detection
Only the first detection is returned (highest confidence):Configuration
The detector uses fixed configuration:- Model Selection: 0 (short-range model for faces < 2m)
- Min Detection Confidence: 0.5 (50%)
__init__ method:
Comparison with Other Detectors
| Feature | MediaPipe | Haar | MTCNN | YOLO |
|---|---|---|---|---|
| Speed | Fast (~25 FPS) | Fastest (~30+ FPS) | Slow (~10 FPS) | Moderate (~15 FPS) |
| Accuracy | Very Good | Good | Excellent | Excellent |
| Resource Usage | Low | Very Low | High | Moderate |
| Best For | Real-time balanced | Low-power devices | Maximum accuracy | High accuracy |
From
experiments/simple_run_ROI.py: MediaPipe achieves consistent ~25 FPS performance in benchmarks.