This Python script captures video from a webcam and performs real-time face detection using OpenCV’s built-in Haar cascades.
Key features include:
- Detection of multiple faces per frame.
- Smoothing of face positions using Exponential Moving Average (EMA) to reduce jitter.
- Dynamic scaling of label text based on the detected face size.
- Drawing bounding boxes around detected faces.
- Display of FPS (Frames Per Second) to monitor processing speed.
All detection and display parameters are fully configurable via constants at the top of the script.
This includes:
- Camera source index (
CAMERA_SOURCE) - Haar cascade parameters (
FD_SCALE_FACTOR,FD_MIN_NEIGHBORS) - Face box appearance (
FACEBOX_COLOR,FACEBOX_THICKNESS) - Label text settings (
TEXT_FONT,TEXT_DEFAULT_SIZE,TEXT_COEFF,TEXT_MARGIN) - Smoothing parameters (
SMOOTH_ALPHA,SMOOTH_THRESHOLD) - Matching distance for tracking faces (
MATCH_MAX_DISTANCE) - FPS display settings (
FPS_COLOR,FPS_FONT_SCALE,FPS_POSITION,FPS_FONT)
This makes it easy to tweak the behavior and appearance of the face detection system without modifying the core logic.
- Make sure you have Python 3.10 or higher installed.
- Clone or download this repository.
- Install required dependencies using the
requirements.txtfile:pip install -r requirements.txt
-
Connect a webcam or ensure your camera source is available.
-
Run the script:
python main.py
-
A window will open showing the live video feed. Detected faces will have rectangles and labels.
-
Press
qto exit the video window.