Real-Time Face Detection with OpenCV
This project demonstrates real-time face detection using OpenCVβs Haar Cascade classifier. The program leverages a webcam feed to detect faces dynamically, drawing green rectangles around detected faces and labeling them with customizable text.
Features
π₯ Real-Time Face Detection
- Captures video feed from your webcam.
- Processes each frame to identify faces in real time.
ποΈ Visual Feedback
- Draws green rectangles around detected faces.
- Displays customizable labels above the rectangles.
π User-Friendly Exit Mechanism
- Exit the program by pressing the
ESC
key directly in the OpenCV display window.
βοΈ Customizable Parameters
- Modify text labels, detection sensitivity, and other parameters directly in the script.
How It Works
- Capture Frames: The program captures video frames from the webcam.
- Convert to Grayscale: Each frame is converted to grayscale to simplify and speed up face detection.
- Detect Faces: Uses the Haar Cascade classifier to detect faces in the grayscale image.
- Draw and Label: Draws green rectangles around detected faces and labels them with customizable text.
- Display: Shows the processed frames in real time in a dedicated OpenCV window.
- Exit: Allows the user to quit by pressing the
ESC
key in the OpenCV window.
Getting Started
π οΈ Prerequisites
To run this project, ensure you have the following:
π File Structure
face_detection.py
: The main Python script.
haarcascade_frontalface_default.xml
: The pre-trained Haar Cascade model for detecting faces. This file must be downloaded and placed in the same directory as the script.
Step-by-Step Instructions
- Clone this repository or download the project files.
- Install the required Python libraries using the command:
pip install opencv-python
- Download the Haar Cascade file:
- Run the program using:
python face_detection.py
- To exit, press the
ESC
key on the OpenCV display window.
Expected Output
When you run the program:
- A video feed from your webcam will appear.
- Detected faces will be surrounded by green rectangles labeled with "Face Detected" (or your customized text).
- The top-left corner will display "Press ESC to Exit" as an instruction.

Future Enhancements
By integrating YOLO, the program can detect not just faces but also other objects like cars, animals, or household items. This makes the project more robust and versatile, suitable for applications like surveillance, retail analytics, or smart devices.