This project implements a real-time face detection system using Python, OpenCV, and Haar cascades. The system utilizes the webcam to capture video frames, detect faces, and highlight the detected faces with bounding boxes. The system is designed to run cross-platform on Windows, Linux, and macOS, making it easily accessible to a broad audience.
The face detection process uses OpenCV's pre-trained Haar Cascade model (haarcascade_frontalface_default.xml
). Additionally, the system includes features such as real-time FPS (frames per second) display, face count, and the ability to toggle between grayscale and color video feeds.
This project can be extended to other facial recognition tasks, like tracking multiple faces or integrating deep learning-based face detection methods like DNN (Deep Neural Networks) for better accuracy.
- Real-Time Face Detection: Detect faces in live video streams.
- Cross-Platform Compatibility: Works on Windows, Linux, and macOS.
- FPS Display: Displays the frames per second (FPS) of the video feed.
- Face Count: Shows the number of faces detected in the current frame.
- Toggle Grayscale: Option to toggle between grayscale and color video feeds.
- Efficient Video Capture: Uses webcam video capture for real-time face detection.
Face-Detection/
│
├── code.py # Main Python script for face detection
├── requirements.txt # Dependencies for the project
├── setup.sh # Setup script for Linux/macOS systems
├── setup.bat # Setup script for Windows systems
├── haarcascade_frontalface_default.xml # Pre-trained Haar Cascade model
└── README.md # Detailed documentation of the project
Before running the project, ensure you have the necessary libraries and dependencies installed. The following libraries are required:
- OpenCV: Open Source Computer Vision Library
- NumPy: A fundamental package for scientific computing
- time: Standard Python library for time-related functions
opencv-python==4.9.0
numpy==1.24.2
To set up the project on Windows, follow these steps:
-
Clone the Repository:
- Open Command Prompt or PowerShell and run:
git clone https://github.com/samnaveenkumaroff/Face-Detection-using-Python.git
- Open Command Prompt or PowerShell and run:
-
Run the
setup.bat
Script:- Navigate to the project directory and run the
setup.bat
script. This will:- Create a virtual environment
- Install the required dependencies
setup.bat
- Navigate to the project directory and run the
-
Activate the Virtual Environment:
- After running the script, activate the virtual environment:
.\venv\Scripts\activate
- After running the script, activate the virtual environment:
-
Run the Application:
- Start the Python script:
python code.py
- Start the Python script:
To set up the project on Linux or macOS, follow these steps:
-
Clone the Repository:
- Open the terminal and run:
git clone https://github.com/samnaveenkumaroff/Face-Detection-using-Python.git
- Open the terminal and run:
-
Run the
setup.sh
Script:- Navigate to the project directory and run the
setup.sh
script. This will:- Create a virtual environment
- Install the required dependencies
bash setup.sh
- Navigate to the project directory and run the
-
Activate the Virtual Environment:
- After running the script, activate the virtual environment:
source venv/bin/activate
- After running the script, activate the virtual environment:
-
Run the Application:
- Start the Python script:
python code.py
- Start the Python script:
-
Launching the Application:
- Once the environment is set up and dependencies are installed, simply run the
code.py
script to launch the face detection system.python code.py
- Once the environment is set up and dependencies are installed, simply run the
-
Using the Application:
- The application will open a webcam feed in a window showing real-time video.
- Face Detection: The system will automatically detect faces and draw green rectangles around the detected faces.
- FPS Display: Displays the frames per second (FPS) on the top-left of the video feed.
- Face Count: The number of detected faces will be displayed at the bottom-left of the video feed.
- Toggle Grayscale: Press the
G
key to toggle between the grayscale and color video feed. - Exit: Press the
Q
key to exit the program.
- Haar Cascade Classifier: The project uses a pre-trained Haar Cascade model (
haarcascade_frontalface_default.xml
) from OpenCV to perform face detection. - Video Capture: The webcam feed is captured using OpenCV’s
cv2.VideoCapture(0)
method. - Real-Time FPS Calculation: The frames per second (FPS) are calculated by measuring the time difference between two consecutive frames.
- Face Detection: Faces are detected using
cv2.CascadeClassifier.detectMultiScale()
function, which identifies objects (in this case, faces) in the image. - Grayscale Toggle: Grayscale images are used for face detection as they generally provide better results. Press the
G
key to toggle the video feed between grayscale and color.
-
Error: "Cannot access webcam":
- Ensure that the webcam is not being used by another application.
- Check if the webcam is properly connected and accessible.
-
Error: "Failed to load face cascade":
- Ensure that the Haar Cascade model file (
haarcascade_frontalface_default.xml
) is located in the correct directory.
- Ensure that the Haar Cascade model file (
-
Error: "ModuleNotFoundError":
- Ensure that you have activated the virtual environment and installed all required dependencies using:
pip install -r requirements.txt
- Ensure that you have activated the virtual environment and installed all required dependencies using:
Contributions are welcome! If you'd like to contribute to the project, feel free to fork this repository, create a new branch, and submit a pull request with your changes. Here are a few ways you can contribute:
- Bug Fixes: Report and fix bugs in the current implementation.
- Enhancements: Add new features like face tracking, recognition, or integration with deep learning models.
- Code Refactoring: Help improve code readability, performance, and maintainability.
Please ensure your changes are well-documented and tested.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- OpenCV for providing the face detection model and library.
- Python and NumPy for being essential dependencies in machine learning and computer vision.
- All contributors to this project.
Author: Sam Naveenkumar V
Email: [email protected]
GitHub: @samnaveenkumaroff