A lightweight, software-only face recognition system using OpenCV's Local Binary Patterns Histograms (LBPH). This project allows you to capture face data, train a local model, and run real-time recognition via a desktop webcam.
Note: This repository was adapted from an Arduino-integrated project. All hardware dependencies have been removed for a pure software implementation.
Face recognition
├── app.py # Real-time recognition (uses trainer.yml & labels.json)
├── capture_faces.py # Captures labeled face images to dataset/
├── train_model.py # Trains LBPH model; generates trainer.yml & labels.json
├── camera_test.py # Diagnostic tool for webcam access
├── dataset/ # Created by capture_faces.py (contains person subfolders)
├── trainer.yml # Trained model file
└── labels.json # ID-to-Name mapping (created during training)
Python: 3.8+ (3.10+ recommended)
Environment: Virtual environment recommended
- Setup Environment
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate- Install Dependencies
pip install --upgrade pip
pip install opencv-python opencv-contrib-python numpyNote: opencv-contrib-python is strictly required for the LBPH modules.
Follow these three steps in order to get the system running:
Run the capture script to build your dataset.
python capture_faces.py- Input: Enter the name of the person when prompted.
- Samples: Default is 50. For better accuracy, aim for 200–500 images.
- Controls: Press
Enterto finish early orqto cancel.
Process the images in the dataset/ folder into a machine-readable model.
python train_model.pyThis assigns numeric IDs to folders and generates trainer.yml and labels.json.
Start the live desktop demo.
python app.py- The script uses your webcam to identify faces in real-time.
- Controls: Press
qto exit the window.
- Sample Size: Capture 500+ images per person.
- Variety: Capture faces under different lighting, different angles, and with/without glasses.
- Thresholding: Adjust the
CONFIDENCE_THRESHOLDinapp.py. A lower number makes the system "stricter" about who it recognizes.
| Issue | Solution |
|---|---|
trainer.yml not found |
Run train_model.py first. Ensure dataset/ is not empty. |
| Camera not opening | Change the index in cv2.VideoCapture(0) to 1 or 2. |
AttributeError: 'cv2' has no 'face' |
Install opencv-contrib-python. |
| Low Accuracy | Increase dataset size, improve lighting, and ensure faces are cropped/resized properly. |
- Local Storage: All face data and models are stored locally on your machine.
- Consent: Do not capture or store face data of individuals without their explicit consent.
- Security: If using in a production environment, ensure
trainer.ymlandlabels.jsonare stored securely.
This project is licensed under the MIT License.
- facial recognition of existing datasets:
- setting up a new facial recognition :