Skip to content

A complete face recognition security system using LBPH, MediaPipe, Flask, and Docker. Includes dataset capture, model training, threshold testing, real-time prediction, and a browser-based authentication UI.

Notifications You must be signed in to change notification settings

radheybansal06/Face-Security-System

Repository files navigation

πŸ” Face Security System

A complete end-to-end face recognition security system using LBPH (Local Binary Patterns Histograms), MediaPipe, Flask, and Docker. This system includes dataset capture, model training, threshold optimization, real-time prediction, and a browser-based authentication UI.

Python OpenCV Flask Docker License

πŸ“‹ Table of Contents

✨ Features

  • πŸŽ₯ Automated Dataset Capture - Capture facial images with MediaPipe face detection
  • πŸ”„ Data Augmentation - Automatic image transformations (brightness, rotation, flip) for robust training
  • 🧠 LBPH Model Training - Train face recognition model with 80/20 train-test split
  • πŸ“Š Threshold Optimization - Automated testing to find optimal confidence threshold
  • 🎯 Real-time Recognition - Local webcam-based face recognition
  • 🌐 Web Interface - Browser-based authentication UI with Flask backend
  • 🐳 Docker Support - Fully containerized deployment
  • βœ… Access Control - Distinguishes between authorized users and unknown intruders

🎬 Demo

The system provides two modes of operation:

  1. Local Recognition - Real-time webcam recognition with OpenCV GUI
  2. Web Application - Browser-based interface accessible at http://localhost:8080

πŸ› οΈ Tech Stack

Component Technology
Programming Language Python 3.10+
Face Detection MediaPipe
Face Recognition OpenCV (LBPH)
Web Framework Flask
Image Processing OpenCV, Pillow, NumPy
Containerization Docker

πŸ“ Project Structure

Face-Security-System/
β”œβ”€β”€ app.py                      # Flask backend server
β”œβ”€β”€ index.html                  # Web UI for recognition
β”œβ”€β”€ dataset_capuring.py         # Capture face images from webcam
β”œβ”€β”€ train_model.py              # Train LBPH recognition model
β”œβ”€β”€ test_model_accuracy.py      # Find optimal confidence threshold
β”œβ”€β”€ recognize_face.py           # Local real-time face recognition
β”œβ”€β”€ Dockerfile                  # Docker container configuration
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ trainer.yml                 # Trained model (generated after training)
β”œβ”€β”€ guidance.txt                # Detailed usage guide
└── dataset_enhanced/           # Dataset directory (created automatically)
    β”œβ”€β”€ train/                  # Training images (80%)
    └── test/                   # Testing images (20%)

πŸ“¦ Installation

Prerequisites

  • Python 3.10 or higher
  • Webcam
  • Docker (optional, for containerized deployment)

Local Installation

  1. Clone the repository

    git clone https://github.com/radheybansal06/Face-Security-System.git
    cd Face-Security-System
  2. Install dependencies

    pip install -r requirements.txt

    Note: Ensure opencv-contrib-python is installed for LBPH support.

πŸš€ Usage

1. Capture Dataset

Capture facial images for training the model:

python dataset_capuring.py

Instructions:

  • Position your face in front of the webcam
  • Move your head and change expressions for variation
  • The script captures images automatically (1 image every 10 frames)
  • Press q to quit early (target: 1500 images)

Output:

  • Training images (80%): dataset_enhanced/train/YourName/
  • Testing images (20%): dataset_enhanced/test/YourName/

2. Train Model

Train the LBPH face recognition model:

python train_model.py

Output:

  • Trained model file: trainer.yml

3. Test Accuracy & Find Optimal Threshold

Determine the best confidence threshold for your model:

python test_model_accuracy.py

Sample Output:

--- Model Accuracy vs. Confidence Threshold ---
Threshold | Accuracy
--------------------
   60     |  45.67%
   65     |  67.89%
   70     |  82.34%
   75     |  91.23%
   80     |  95.67%
   85     |  98.45%  ← Best
   90     |  97.12%
--------------------
πŸ† Best Performance: 98.45% at Threshold: 85

Action Required: Update the CONFIDENCE_THRESHOLD in both app.py and recognize_face.py:

CONFIDENCE_THRESHOLD = 85  # Replace with your optimal value

4. Real-time Recognition (Local)

Test face recognition locally with your webcam:

python recognize_face.py

Features:

  • βœ… Authorized users: Green bounding box + name
  • ❌ Unknown intruders: Red bounding box + "Unknown Intruder"
  • Press q to quit

5. Deploy with Docker

Build Docker Image

docker build -t face-recog-app .

Run Container

docker run -d -p 8080:8080 --name recognition-web-app face-recog-app

Access Web Application

Open your browser and navigate to:

http://localhost:8080

Web UI Features:

  • Start webcam
  • Capture and recognize face
  • Display authorization status

Docker Management Commands

Stop container:

docker stop recognition-web-app

Remove container:

docker rm recognition-web-app

Clean up Docker resources:

docker system prune -a

πŸ” How It Works

1. Face Detection

  • Uses MediaPipe for robust face detection
  • Extracts face regions with margin for better accuracy

2. Data Augmentation

  • Horizontal flip (50% chance)
  • Random brightness adjustment (70%-130%)
  • Slight rotation (Β±5 degrees)
  • Ensures model robustness to lighting and pose variations

3. LBPH Recognition

  • Local Binary Patterns Histograms algorithm
  • Face images resized to 128Γ—128 grayscale
  • Model trained on augmented dataset
  • Lower confidence score = better match

4. Threshold-based Authentication

  • Confidence score must be below threshold for authorization
  • Optimal threshold determined through automated testing
  • Balances security (false positives) vs usability (false negatives)

βš™οΈ Configuration

Key configuration variables can be modified in the respective files:

File Variable Description Default
dataset_capuring.py person_name Name of the person "Radhey"
dataset_capuring.py TOTAL_IMAGES Number of images to capture 1500
dataset_capuring.py TARGET_SIZE Image resize dimensions (128, 128)
train_model.py MODEL_FILE Trained model filename 'trainer.yml'
app.py CONFIDENCE_THRESHOLD Recognition threshold 85
app.py NAMES ID-to-name mapping {1: "Radhey", 0: "Unknown"}

πŸ› Troubleshooting

Model Not Loaded

Problem: trainer.yml not found

Solution:

python train_model.py

Access Denied for Known User

Problem: Legitimate user not recognized

Solutions:

  1. Capture more diverse images (different angles, lighting, expressions)
  2. Re-train the model
  3. Adjust CONFIDENCE_THRESHOLD to a higher value

Webcam Not Detected

Problem: Camera access denied or already in use

Solutions:

  1. Allow camera permissions in your browser (for web app)
  2. Close other applications using the webcam (Zoom, Teams, etc.)
  3. Check camera device index in code (default is 0)

Docker Build Fails

Problem: Missing dependencies or incompatible versions

Solution:

# Rebuild with no cache
docker build --no-cache -t face-recog-app .

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Radhey Bansal

Icey067

πŸ™ Acknowledgments

  • OpenCV - Computer vision library
  • MediaPipe - Face detection framework
  • Flask - Web framework
  • LBPH algorithm for face recognition

⭐ If you found this project helpful, please give it a star! ⭐

About

A complete face recognition security system using LBPH, MediaPipe, Flask, and Docker. Includes dataset capture, model training, threshold testing, real-time prediction, and a browser-based authentication UI.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •