Skip to content

Uni-Creator/face_recognition

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎭 Face Recognition System

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.


📋 Table of Contents


Overview

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

Project Structure

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/
            └── ...

Models

🧠 CNN Model (model.py)

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 (default 300×300)

📊 LBPH Recognizer (LBPH_model.py)

Uses OpenCV's built-in LBPHFaceRecognizer on grayscale, 200×200 face crops detected with a Haar Cascade.


Installation

Prerequisites

  • Python 3.8+
  • Webcam

Install Dependencies

pip install opencv-python opencv-contrib-python torch torchvision pillow matplotlib seaborn scikit-learn numpy

GPU Support: Install the CUDA-compatible version of PyTorch from pytorch.org for faster CNN training/inference.


Usage

CNN Pipeline (main.py / trainer.py / dataCollection.py)

Step 1 — Collect Training Data

python dataCollection.py
  • Enter the person's name when prompted.
  • A webcam feed opens with live face detection (Haar Cascade).
  • Press s to save a cropped face image.
  • Press q or 0 to quit. Up to 2000 images per person.
  • Images are saved to data/train/<name>/.

Step 2 — Train the CNN

python trainer.py
  • Loads all images from data/train/, splits into train/unused (configurable via TRAIN_SPLIT).
  • Trains for NUM_EPOCHS epochs using Adam optimizer + CrossEntropyLoss.
  • Saves model weights to face_recognition_cnn.pth.
  • Displays a confusion matrix heatmap after training.

Step 3 — Run Real-Time Recognition

python main.py
  • Loads the saved .pth model.
  • Opens webcam; detects faces via Haar Cascade.
  • Classifies each face with the CNN — labels it with a name if confidence > 90%, otherwise "Unknown".
  • Press q to quit.

LBPH Pipeline (LBPH_model.py)

A self-contained script with an interactive menu:

python LBPH_model.py
1: 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 c to capture, ESC to stop.
  • Recognition: Labels a face as "Unknown" if LBPH confidence > 60 (higher = less certain in LBPH).

Configuration

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

How It Works

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.

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages