Overview
TheHaarCascadeDetector class uses OpenCV’s pre-trained Haar Cascade classifier for face detection. It’s the fastest detector available, making it ideal for real-time applications on low-power devices.
Initialization
Scale factor for multi-scale detection.Specifies how much the image size is reduced at each image scale.
- Valid range: 1.01 to 1.5
- Smaller values (closer to 1.0): More thorough search, slower, more detections
- Larger values (closer to 1.5): Faster search, fewer detections
- Recommended: 1.1 for balanced performance
Minimum neighbors for detection reliability.Specifies how many neighbors each candidate rectangle should have to retain it.
- Higher values: Fewer false positives, may miss some faces
- Lower values: More detections, more false positives
- Recommended: 4-6 for most applications
Minimum face size to detect as (width, height) in pixels.Faces smaller than this will be ignored.
- Smaller values: Detect smaller/distant faces, slower
- Larger values: Faster, only detect larger/closer faces
- Recommended: (30, 30) for general use, (50, 50) for close-range
Attributes
Loaded Haar cascade classifier using OpenCV’s pre-trained frontal face model:
haarcascade_frontalface_default.xmlScale factor between successive image scales (1.01-1.5).
Minimum neighbors parameter for detection filtering.
Minimum face size as (width, height) in pixels.
Methods
detect()
Detect a single face in the input frame.Input image in BGR format (OpenCV default).Will be automatically converted to grayscale internally.
Coordinates of the first detected face as
(x, y, w, h), or None if no faces detected.x: X-coordinate of top-left cornery: Y-coordinate of top-left cornerw: Width of bounding boxh: Height of bounding box
close()
Release detector resources.Usage Examples
Basic Detection
Real-Time Webcam Detection
Tuning for Different Scenarios
High Accuracy (Low False Positives)
High Speed (Maximum FPS)
Distant Face Detection
With FaceDetector Manager
Recommended usage through the unified manager:Performance Characteristics
Speed
- Average FPS: 30+ FPS (fastest detector)
- Detection Time: ~30ms per frame on typical hardware
- Real-time capable: Yes, excellent for live video
Accuracy
- Best for: Frontal faces with good lighting
- Limitations: Struggles with side profiles, poor lighting, occlusions
- False Positives: Can be higher than deep learning methods
- Tuning: Requires parameter adjustment for optimal results
Resource Usage
- CPU Usage: Very low
- Memory: Minimal (~1MB for cascade file)
- GPU: Not required
- Best For: Embedded systems, Raspberry Pi, mobile devices
Implementation Details
Grayscale Conversion
Haar Cascade requires grayscale input, so conversion happens automatically:Single Face Return
Only the first detected face is returned:Cascade File Location
Uses OpenCV’s built-in cascade file:Parameter Tuning Guide
scale_factor
| Value | Search Thoroughness | Speed | Use Case |
|---|---|---|---|
| 1.05 | Very thorough | Slow | Maximum detection rate |
| 1.1 | Balanced | Medium | General purpose (recommended) |
| 1.2 | Less thorough | Fast | Real-time priority |
| 1.3+ | Minimal | Very fast | Speed-critical applications |
min_neighbors
| Value | False Positives | Missed Faces | Use Case |
|---|---|---|---|
| 2-3 | Higher | Fewer | Maximize detections |
| 4-5 | Balanced | Balanced | General purpose (recommended) |
| 6-7 | Lower | More | High confidence needed |
| 8+ | Very low | Many | Critical applications |
min_size
| Value | Use Case |
|---|---|
| (20, 20) | Detect distant/small faces |
| (30, 30) | General purpose (default) |
| (50, 50) | Close-range only, faster |
| (80, 80) | Very close faces, maximum speed |
Common Issues and Solutions
Too Many False Positives
Missing Faces
Too Slow
Comparison with Other Detectors
| Feature | Haar | MediaPipe | YOLO | MTCNN |
|---|---|---|---|---|
| Speed | Fastest (30+ FPS) | Fast (25 FPS) | Moderate (15 FPS) | Slow (10 FPS) |
| Accuracy | Good | Very Good | Excellent | Excellent |
| Setup | Easiest | Easy | Moderate | Moderate |
| Dependencies | OpenCV only | MediaPipe + OpenCV | Ultralytics + OpenCV | mtcnn + OpenCV |
| Best For | Embedded/Real-time | Balanced apps | High accuracy | Maximum accuracy |
Haar Cascade only detects frontal faces well. For profile faces or challenging angles, consider MediaPipe or YOLO.