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.
- Features
- Demo
- Tech Stack
- Project Structure
- Installation
- Usage
- How It Works
- Configuration
- Troubleshooting
- Contributing
- License
- π₯ 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
The system provides two modes of operation:
- Local Recognition - Real-time webcam recognition with OpenCV GUI
- Web Application - Browser-based interface accessible at
http://localhost:8080
| Component | Technology |
|---|---|
| Programming Language | Python 3.10+ |
| Face Detection | MediaPipe |
| Face Recognition | OpenCV (LBPH) |
| Web Framework | Flask |
| Image Processing | OpenCV, Pillow, NumPy |
| Containerization | Docker |
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%)
- Python 3.10 or higher
- Webcam
- Docker (optional, for containerized deployment)
-
Clone the repository
git clone https://github.com/radheybansal06/Face-Security-System.git cd Face-Security-System -
Install dependencies
pip install -r requirements.txt
Note: Ensure
opencv-contrib-pythonis installed for LBPH support.
Capture facial images for training the model:
python dataset_capuring.pyInstructions:
- 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
qto quit early (target: 1500 images)
Output:
- Training images (80%):
dataset_enhanced/train/YourName/ - Testing images (20%):
dataset_enhanced/test/YourName/
Train the LBPH face recognition model:
python train_model.pyOutput:
- Trained model file:
trainer.yml
Determine the best confidence threshold for your model:
python test_model_accuracy.pySample 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 valueTest face recognition locally with your webcam:
python recognize_face.pyFeatures:
- β Authorized users: Green bounding box + name
- β Unknown intruders: Red bounding box + "Unknown Intruder"
- Press
qto quit
docker build -t face-recog-app .docker run -d -p 8080:8080 --name recognition-web-app face-recog-appOpen your browser and navigate to:
http://localhost:8080
Web UI Features:
- Start webcam
- Capture and recognize face
- Display authorization status
Stop container:
docker stop recognition-web-appRemove container:
docker rm recognition-web-appClean up Docker resources:
docker system prune -a- Uses MediaPipe for robust face detection
- Extracts face regions with margin for better accuracy
- Horizontal flip (50% chance)
- Random brightness adjustment (70%-130%)
- Slight rotation (Β±5 degrees)
- Ensures model robustness to lighting and pose variations
- Local Binary Patterns Histograms algorithm
- Face images resized to 128Γ128 grayscale
- Model trained on augmented dataset
- Lower confidence score = better match
- Confidence score must be below threshold for authorization
- Optimal threshold determined through automated testing
- Balances security (false positives) vs usability (false negatives)
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"} |
Problem: trainer.yml not found
Solution:
python train_model.pyProblem: Legitimate user not recognized
Solutions:
- Capture more diverse images (different angles, lighting, expressions)
- Re-train the model
- Adjust
CONFIDENCE_THRESHOLDto a higher value
Problem: Camera access denied or already in use
Solutions:
- Allow camera permissions in your browser (for web app)
- Close other applications using the webcam (Zoom, Teams, etc.)
- Check camera device index in code (default is
0)
Problem: Missing dependencies or incompatible versions
Solution:
# Rebuild with no cache
docker build --no-cache -t face-recog-app .Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Radhey Bansal
- GitHub: @radheybansal06
Icey067
- GitHub: @Icey067
- 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! β