A comprehensive desktop application built in Python that uses the OpenCV library and the LBPH (Local Binary Patterns Histograms ) algorithm to create a real-time face recognition system. The application allows users to register new faces, train the model on all registered individuals, and then recognize them through a live webcam feed.
- Real-Time Recognition: Identifies faces directly from the live camera stream.
- New User Registration: An easy-to-use interface for collecting and labeling new face data.
- Comprehensive Model Training: Trains the model on all registered users to ensure high accuracy.
- Clean Code Architecture: Built with Object-Oriented Programming (OOP) principles for modularity and easy maintenance.
- Automatic Saving: Automatically saves the updated model (
model.yml) and labels file (labels.json). - Simple User Interface: Fully controlled via keyboard shortcuts.
- Python 3.x
- OpenCV (
opencv-python): For image/video processing and face detection. - NumPy: For numerical operations on arrays.
- JSON: For storing and managing user labels.
Follow these steps to get the application running on your local machine.
- Ensure you have Python 3.8 or newer installed on your system.
- Ensure you have Git installed on your system.
Open your terminal and clone this repository to your local machine:
git clone https://github.com/MekdadGhazal/face-recognition-opencv.git
cd face-recognition-opencvIt is a best practice to use a virtual environment to isolate project dependencies.
# Create a virtual environment
python -m venv venv
# Activate the environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
# Install the required packages
pip install -r requirements.txt(Note: You will need to create a requirements.txt file containing opencv-python and numpy )
After activating the environment and installing the dependencies, run the main script:
python code.pyor
python list.pyWhen you run the application, the webcam feed window will appear. You can control the application using the following keys:
-
Y- Register a New Face:- Press
Y. - A prompt will appear in the terminal asking you to enter a name.
- Type the name and press
Enter. - The application will automatically start capturing 50 images of your face. Try to move your head slightly to capture different angles.
- Once finished, the application will automatically retrain the model on all registered faces.
- Press
-
N- Toggle Recognition Mode:- Press
Nto activate recognition mode. The application will start drawing boxes around faces and identifying them. - Press
Nagain to deactivate recognition mode and return to the normal feed.
- Press
-
S- Take a Screenshot:- Press
Sto save the current frame as an image in thedata/rawdirectory.
- Press
-
X- Exit the Application:- Press
Xto safely close the program.
- Press
face-recognition-opencv/
│
├── .gitignore # Specifies intentionally untracked files to ignore
├── data/ # Directory for storing data
│ ├── faces/ # Stores collected face images (one folder per user)
│ └── raw/ # Stores manual screenshots
│
├── labels.json # Stores user names and their corresponding numeric labels
├── model.yml # The trained recognizer model, created automatically
├── code.py # The main application script using oop
├── list.py # The application script using functions
└── README.md # This file
Contributions are always welcome! If you have a suggestion or a fix, feel free to fork the repository and create a pull request.
- Fork the Project.
- Create your Feature Branch (
git checkout -b feature/AmazingFeature). - Commit your Changes (
git commit -m 'Add some AmazingFeature'). - Push to the Branch (
git push origin feature/AmazingFeature). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for more details.
