A dual-model face recognition system built with Python, supporting both a custom CNN (PyTorch) and a classical LBPH (OpenCV) approach for real-time face identification via webcam.
This project provides two independent face recognition pipelines:
| Feature | CNN Model | LBPH Model |
|---|---|---|
| Backend | PyTorch | OpenCV |
| Speed | GPU-accelerated (if available) | CPU-only |
| Accuracy | Higher (deep learning) | Moderate (classical ML) |
| Training time | Longer | Very fast |
| Confidence threshold | 90% softmax probability | ≤ 60 LBPH distance |
face_recognition/
├── config.py # Centralized configuration (paths, hyperparameters)
├── model.py # CNN model architecture definition
├── dataCollection.py # Webcam-based face image collection (for CNN)
├── trainer.py # CNN model training + confusion matrix evaluation
├── main.py # Real-time CNN face recognition (webcam)
├── LBPH_model.py # All-in-one LBPH pipeline (collect → train → recognize)
├── face_recognition_cnn.pth # Saved CNN model weights (generated after training)
├── trainer.yml # Saved LBPH recognizer (generated after training)
└── data/
└── train/
├── Person_A/ # One folder per person
│ ├── 1.jpg
│ └── ...
└── Person_B/
└── ...
A custom 3-layer Convolutional Neural Network:
- Conv layers: 3×3 kernels, ReLU activation, channels:
3 → 32 → 64 → 128 - Pooling: MaxPool2d (×2) after conv2 and conv3, halving spatial dims twice
- FC layers:
Flattened → 128 → num_classes(raw logits for CrossEntropyLoss) - Input size: Configurable via
IMAGE_SIZE(default300×300)
Uses OpenCV's built-in LBPHFaceRecognizer on grayscale, 200×200 face crops detected with a Haar Cascade.
- Python 3.8+
- Webcam
pip install opencv-python opencv-contrib-python torch torchvision pillow matplotlib seaborn scikit-learn numpyGPU Support: Install the CUDA-compatible version of PyTorch from pytorch.org for faster CNN training/inference.
python dataCollection.py- Enter the person's name when prompted.
- A webcam feed opens with live face detection (Haar Cascade).
- Press
sto save a cropped face image. - Press
qor0to quit. Up to 2000 images per person. - Images are saved to
data/train/<name>/.
python trainer.py- Loads all images from
data/train/, splits into train/unused (configurable viaTRAIN_SPLIT). - Trains for
NUM_EPOCHSepochs using Adam optimizer + CrossEntropyLoss. - Saves model weights to
face_recognition_cnn.pth. - Displays a confusion matrix heatmap after training.
python main.py- Loads the saved
.pthmodel. - Opens webcam; detects faces via Haar Cascade.
- Classifies each face with the CNN — labels it with a name if confidence > 90%, otherwise "Unknown".
- Press
qto quit.
A self-contained script with an interactive menu:
python LBPH_model.py1: Collect Dataset → Capture up to 500 grayscale face images per person
2: Train Recognizer → Train LBPH and save to trainer.yml
3: Live Recognition → Recognize faces in real-time from webcam
4: Exit
- Collection: Press
cto capture,ESCto stop. - Recognition: Labels a face as "Unknown" if LBPH confidence > 60 (higher = less certain in LBPH).
All key parameters live in config.py:
| Variable | Default | Description |
|---|---|---|
DATA_PATH |
data/train |
Root dataset directory |
MODEL_PATH |
face_recognition_cnn.pth |
CNN model save path |
TRAINER_YML_PATH |
trainer.yml |
LBPH model save path |
HAARCASCADE_PATH |
haarcascade_frontalface_default.xml |
Haar Cascade file |
IMAGE_SIZE |
(300, 300) |
Input size for CNN |
BATCH_SIZE |
32 |
Training batch size |
LEARNING_RATE |
0.001 |
Adam optimizer LR |
NUM_EPOCHS |
2 |
Training epochs |
TRAIN_SPLIT |
0.5 |
Fraction of data used for training |
DEVICE |
auto (CUDA / CPU) | PyTorch compute device |
DETECTION_CONFIDENCE |
0.7 |
Face detection threshold |
OFFSET |
20 |
Pixel padding around detected faces |
Webcam Frame
│
▼
Haar Cascade ← Detects face bounding boxes
│
▼
Face Crop + Resize ← Normalized to fixed resolution
│
├─── CNN Path ──► PyTorch Model ──► Softmax ──► Name (if conf > 90%)
│
└─ LBPH Path ──► LBPH Predict ──► Name (if dist ≤ 60)
Both pipelines use OpenCV's Haar Cascade (haarcascade_frontalface_default.xml) bundled with OpenCV for face detection, then hand off the cropped region to their respective recognizer.