Real-time pipeline for analyzing and recognizing faces using a Custom Convolutional Neural Network (CNN) in PyTorch — with automated aspect-ratio preserving data collection, Haar cascade detection, and an alternative LBPH fallback (VerbaSign).
| Custom CNN | LBPH (VerbaSign) | |
|---|---|---|
| Approach | Deep Learning | Classical ML |
| Input Size | 300x300 RGB | 200x200 Grayscale |
| Model Size | ~350 MB | ~17 MB |
| Robustness | High (Illumination, Pose) | Medium |
| Prediction | WebCam Inference | WebCam Inference |
- Confidence Threshold:
> 0.90 - Loss Function:
CrossEntropyLoss - Optimizer:
Adam (lr=0.001) - Transforms: Resize, ToTensor, Normalize
- Confidence Threshold:
< 70.0 - Algorithm: Local Binary Patterns Histograms
- Setup Time: Very Fast
- Observation (3-channel RGB): Real-time webcam feed cropped by Haar cascade to
300x300resolution, normalized, and transformed to PyTorch tensors. - Action/Prediction (N-dim): Custom CNN outputs raw logits corresponding to
num_classesrepresenting different recognized individuals. - Key preprocessing components: Automated aspect-ratio preserving crop—
collect_data.pyensures no distorted faces enter the training set by mathematically centering and padding the cropped bounding box with white pixels. - Termination/Fallback: If the probability for the predicted class does not exceed the
0.90confidence threshold, it classifies the face asUnknown.
pip install torch torchvision opencv-python Pillow matplotlib seaborn scikit-learn numpyFace_Recognition/
├── config.py # Configuration paths, parameters
├── collect_data.py # Face extraction using Haar Cascade
├── train_cnn.py # Custom CNN PyTorch Training Loop
├── inference_cnn.py # Real-time face recognition inference
├── cnn_model.py # CNN Architecture Definition
├── verify_data.py # Verification script for corrupted images
└── verbasign.py # Alternative LBPH approach
# 1. Collect Data (Automatically detects, crops, and centers faces)
python collect_data.py
# 2. Verify dataset integrity
python verify_data.py
# 3. Train the Custom CNN model
python train_cnn.py
# 4. Evaluate & Real-time Inference (CNN)
python inference_cnn.py
# 5. Alternative (LBPH Face Recognizer Collection, Training, & Inference)
python verbasign.pyNote: Typical training progression over 2 epochs with Adam optimizer.
| Epoch | Batch loss | Train/Val Split |
|---|---|---|
| 1 | ~1.5 | 50% |
| 2 | ~0.5 | 50% |
- Custom PyTorch CNN — flexible CNN architecture using robust tensor transformations (
transforms.Normalize) and pooling, achieving high-accuracy spatial feature extraction. - Higher confidence threshold (0.90) — ensures false positives are strictly classified as "Unknown" for better security.
- Automated aspect-ratio preserving crop —
collect_data.pymathematically centers and pads the cropped bounding box with white pixels, so no stretched or distorted faces negatively impact the dataset. - LBPH Fallback (VerbaSign) — provides an immediate, low-resource deployment alternative without requiring heavy tensor computations.
MIT — see PyTorch and OpenCV for dependencies.
