Skip to content

Droid-DevX/FaceRecognition

Repository files navigation

Face Recognition Custom CNN with PyTorch

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).

Pipeline

Results at a Glance

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

Custom CNN Model (Trained)

  • Confidence Threshold: > 0.90
  • Loss Function: CrossEntropyLoss
  • Optimizer: Adam (lr=0.001)
  • Transforms: Resize, ToTensor, Normalize

LBPH Model (Baseline)

  • Confidence Threshold: < 70.0
  • Algorithm: Local Binary Patterns Histograms
  • Setup Time: Very Fast

How It Works

  • Observation (3-channel RGB): Real-time webcam feed cropped by Haar cascade to 300x300 resolution, normalized, and transformed to PyTorch tensors.
  • Action/Prediction (N-dim): Custom CNN outputs raw logits corresponding to num_classes representing different recognized individuals.
  • Key preprocessing components: Automated aspect-ratio preserving crop—collect_data.py ensures 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.90 confidence threshold, it classifies the face as Unknown.

Setup

pip install torch torchvision opencv-python Pillow matplotlib seaborn scikit-learn numpy
Face_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

Usage

# 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.py

Training Progression

Note: Typical training progression over 2 epochs with Adam optimizer.

Epoch Batch loss Train/Val Split
1 ~1.5 50%
2 ~0.5 50%

Key Design Decisions

  • 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 cropcollect_data.py mathematically 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.

License

MIT — see PyTorch and OpenCV for dependencies.

About

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).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages