Create a fast real-time face recognition app with Python and OpenCV.
pip install -r requirements.txt
Required packages:
- opencv-python
- opencv-contrib-python
- pillow
- pyyaml
All settings are stored in src/config.yaml
:
- Camera settings (resolution, device index)
- Face detection parameters
- Training parameters
- File paths
- Confidence threshold (how confident the model has to be to recognize a face)
You can modify these settings without changing the code.
The system works in three steps:
Run face_taker.py
to capture training images:
python src/face_taker.py
- Enter your name when prompted
- 🚨 The script captures 120 images of your face. Make sure to have a good lighting and move your head around to capture different angles.
- Keep your face centered in the frame
- Images are saved in the
images
folder - Your name and ID are stored in
names.json
- Press 'ESC' to exit early
Format of names.json
:
{
"1": "Joe",
"2": "Jane"
}
Run face_train.py
to create the recognition model:
python src/face_train.py
- Processes all images in the
images
folder - Creates a trained model file
trainer.yml
- Shows number of faces trained
Note: Training images are saved as: Users-{id}-{number}.jpg
Run face_recognizer.py
to start real-time recognition:
python src/face_recognizer.py
- Your webcam will open and start recording
- Recognizes faces in real-time
- Shows name and confidence level
- Press 'ESC' to exit
├── src/
│ ├── config.yaml # Configuration settings
│ ├── config.py # Configuration loader
│ ├── face_taker.py # Capture training images
│ ├── face_train.py # Train the model
│ └── face_recognizer.py # Real-time recognition
├── images/ # Training images
├── names.json # Name-ID mappings
└── trainer.yml # Trained model