A lightweight OpenCV project for real-time face detection in both static images and webcam video streams using a Haar Cascade classifier.
FaceDetectCV demonstrates a classic computer vision workflow:
- Load a pre-trained Haar Cascade model (
haarcascade_frontalface_default.xml) - Convert frames/images to grayscale
- Detect faces with
detectMultiScale(...) - Draw bounding boxes around detected faces
- Visualize results in an OpenCV window
This is a simple, beginner-friendly starter repository for developers who want to understand face detection basics before moving on to deep learning-based approaches.
- Face detection from a single image
- Face detection from live webcam feed
- Uses OpenCV's built-in Haar Cascade model
- Minimal dependencies and easy setup
.
├── detect_face_image.py # Detect faces in an image
├── detect_face_video.py # Detect faces in webcam stream
├── haarcascade_frontalface_default.xml # Pre-trained Haar Cascade model
├── requirements.txt # Python dependencies
├── fac_recog.jpg # Example image asset
├── pic2.jpeg # Example image asset
└── README.md
- Python 3.8+
- Webcam (for
detect_face_video.py) - OS with GUI support for OpenCV windows (
cv2.imshow)
Install dependencies:
pip install -r requirements.txtNote:
requirements.txtcurrently pins older OpenCV/Numpy versions. If installation fails on modern Python versions, consider upgrading those pins.
python detect_face_image.pyBy default, detect_face_image.py reads a hardcoded image file:
img = cv2.imread('maaya_img.jpg')Update this path to one of your own images (or an existing file in this repo) before running.
python detect_face_video.pyControls:
- Press
Escto close the video window and stop the program.
Both scripts follow the same detection pipeline:
- Initialize the Haar Cascade classifier
- Read image/frame input
- Convert input to grayscale
- Run
face_cascade.detectMultiScale(gray, 1.1, 4) - Draw rectangles around detections
- Display output
- Haar Cascades are fast but less robust than modern deep learning detectors
- Detection quality can drop in poor lighting, occlusion, or extreme face angles
- Current scripts are minimal and use hardcoded file/webcam inputs
- Add CLI arguments for input image/video path
- Save output image/video to disk
- Add confidence filtering and configurable parameters
- Add support for processing video files
- Upgrade dependencies for modern Python compatibility
- Add unit/integration tests