A production-ready face recognition system with real-time processing, similarity search, and modern GUI interface.
๐ Quick Start โข ๐ Documentation โข ๐ง API Reference โข ๐ก Examples
InsightFace Demo is a comprehensive face recognition system built with state-of-the-art deep learning models. It combines the power of InsightFace for accurate face recognition, FAISS for lightning-fast vector similarity search, and PySide6 for a modern desktop GUI experience.
- ๐ Face Verification: Compare two faces with confidence scores and similarity metrics
- ๐ Face Search: Find similar faces in large image collections using vector similarity
- ๐น Real-time Recognition: Live face detection and recognition from webcam streams
- ๐ฅ Multi-face Detection: Process multiple faces in a single image simultaneously
- ๐ FAISS Integration: Ultra-fast approximate nearest neighbor search
- ๐ฎ GPU Acceleration: CUDA support for high-throughput processing
- ๐พ Memory Efficient: Optimized memory usage for large-scale deployments
- ๐ Batch Processing: Process multiple images efficiently
- ๐จ Modern GUI: Clean, intuitive desktop interface built with PySide6
- ๐ฑ Responsive Design: Adaptive layouts for different screen sizes
- ๐ Real-time Metrics: Live performance monitoring and statistics
insightface-demo/
โโโ ๐ src/faceid/ # Main application package
โ โโโ ๐ฎ __main__.py # Application entry point
โ โโโ โ๏ธ configs/ # Configuration files
โ โ โโโ default.yaml # Default settings (models, paths, thresholds)
โ โโโ ๐ง core/ # Core engine and business logic
โ โ โโโ __init__.py # Package initialization
โ โ โโโ engine.py # FaceEngine - main processing logic
โ โโโ ๐ ๏ธ scripts/ # Utility scripts and tools
โ โ โโโ build_facebank_faiss.py # Build identity database index
โ โ โโโ build_library_faiss.py # Build image library index
โ โโโ ๐ฅ๏ธ ui/ # User interface components
โ โ โโโ main_window.py # Main application window
โ โ โโโ verify_tab.py # Face verification interface
โ โ โโโ library_tab.py # Image library search interface
โ โ โโโ realtime_tab.py # Real-time webcam recognition
โ โโโ ๐ง utils/ # Utility functions and helpers
โ โโโ face_utils.py # Face detection and processing
โ โโโ image_utils.py # Image loading and manipulation
โโโ ๐ data/ # Data storage directory
โ โโโ facebank/ # Known identity database
โ โ โโโ person_1/ # Individual identity folders
โ โ โ โโโ 01.jpg # Reference images for each person
โ โ โ โโโ ...
โ โ โโโ person_n/
โ โโโ library/ # General image collection
โ โโโ image_001.jpg # Searchable image database
โ โโโ ...
โโโ ๐ outputs/ # Generated indices and metadata
โ โโโ facebank.index # FAISS index for identity database
โ โโโ labels.json # Identity labels mapping
โ โโโ library.index # FAISS index for image library
โ โโโ library_meta.json # Image metadata and paths
โ โโโ similarity_matrix_*.csv # Precomputed similarity matrices
โโโ requirements.txt # Python dependencies
โโโ README.md # Project documentation
The central processing unit that orchestrates all face recognition operations:
- Face Detection: Utilizes InsightFace models for accurate face detection
- Feature Extraction: Generates 512-dimensional face embeddings
- Similarity Computation: Calculates cosine similarity between face vectors
- FAISS Integration: Manages vector indices for fast similarity search
- Threshold Management: Configurable similarity thresholds for matching decisions
Modern PySide6-based desktop application with three main modules:
verify_tab.py: Two-image face verification with confidence scoreslibrary_tab.py: Search similar faces in large image collectionsrealtime_tab.py: Live webcam face recognition and identificationmain_window.py: Main application container with tabbed interface
Batch processing tools for database preparation:
build_facebank_faiss.py: Creates searchable index from identity databasebuild_library_faiss.py: Builds vector index for general image library
Reusable helper functions:
face_utils.py: Face detection, embedding extraction, and normalizationimage_utils.py: Image I/O, preprocessing, and format conversions
- Python 3.11+: Ensure you have Python 3.11 or higher installed
- Linux: Ubuntu 20.04+ / CentOS 8+ / Fedora 35+ recommended
- Git: For cloning the repository
- Webcam: Optional, for real-time face recognition features
# Clone the repository
git clone https://github.com/nguyenmaiductrong/insightface-demo.git
cd insightface-demo
# Create Python virtual environment
python3 -m venv .venv
# Activate virtual environment
source .venv/bin/activate# Upgrade pip to latest version
pip install --upgrade pip
# Install all required packages from requirements.txt
pip install -r requirements.txtCreate the proper data directory structure and add your images:
# Create required directories
mkdir -p data/facebank data/library outputs
# Example: Add known identities to facebank
# Each person should have their own folder with reference images
mkdir -p data/facebank/john_doe
mkdir -p data/facebank/jane_smith
# Copy reference images (5-10 images per person recommended)
# data/facebank/john_doe/01.jpg
# data/facebank/john_doe/02.jpg
# data/facebank/jane_smith/01.jpg
# data/facebank/jane_smith/02.jpg
# Add general images to library for similarity search
# Copy your image collection to data/library/
# data/library/event_photo_001.jpg
# data/library/event_photo_002.jpgData Structure Example:
data/
โโโ facebank/ # Known identities database
โ โโโ person_1/ # Individual person folder
โ โ โโโ 01.jpg # Reference image 1
โ โ โโโ 02.jpg # Reference image 2
โ โ โโโ ...
โ โโโ person_2/
โ โโโ 01.jpg
โ โโโ ...
โโโ library/ # General image collection
โโโ photo_001.jpg
โโโ photo_002.jpg
โโโ ...
Generate the searchable index for your image library:
# Build FAISS index for image library (similarity search)
python -m src.faceid.scripts.build_library_faiss
# Expected output:
# Applied providers: ['CPUExecutionProvider']...
# Building library embeddings: 100%|โโโโโโโโโโ| 150/150 [01:47<00:00, 1.40img/s]
# Built library index with 831 faces
# Index -> /path/to/outputs/library.index
# Meta -> /path/to/outputs/library_meta.jsonCreate the identity database index for known persons:
# Build FAISS index for facebank (identity recognition)
python -m src.faceid.scripts.build_facebank_faiss
# Expected output:
# Applied providers: ['CPUExecutionProvider']...
# Processing facebank: 100%|โโโโโโโโโโ| 4/4 [00:15<00:00, 3.85s/person]
# Built facebank with 4 identities, 127 total faces
# Index -> /path/to/outputs/facebank.index
# Labels -> /path/to/outputs/labels.jsonStart the GUI application:
# Launch the main application
python -m src.faceid
# Alternative: Direct execution
python src/faceid/__main__.pyThe application window will open with three tabs:
- Face Verification: Compare two face images
- Library Search: Find similar faces in your image collection
- Real-time Recognition: Live webcam face recognition
- Image Quality: Use high-resolution images (>300px face size) for better accuracy
- Lighting: Ensure good lighting conditions in reference images
- Multiple Angles: Include 5-10 reference images per person from different angles
- Batch Processing: For large datasets, consider processing in smaller batches
- Open the "Face Verification" tab
- Click "Choose First Image" and select your first image
- Click "Choose Second Image" and select your second image
- Click "Compare" to get similarity score and match result
- Open the "Library Search" tab
- Browse and select your library directory (default:
data/library) - Click "Refresh" to load image thumbnails
- Click "Choose Query Image" and select the image you want to search for
- Click "Search" to find similar faces in your library
- Results will show only images above the similarity threshold
- Open the "Real-time Recognition" tab
- Click "Start Camera" to begin webcam capture
- The system will detect and identify faces in real-time
- Known identities will be labeled with their names
- Click "Stop Camera" to end the session
Optimize the similarity threshold for your specific facebank to improve recognition accuracy:
# Run threshold calibration with default settings
python3 -m src.faceid.scripts.calibrate_threshold
# Or with custom paths
python3 -m src.faceid.scripts.calibrate_threshold \
--facebank-dir data/facebank \
--config src/faceid/configs/default.yaml \
--output-dir outputsWhat it does:
- Analyzes all face images in your facebank
- Computes similarity scores between same/different person pairs
- Finds optimal threshold using F1-Score maximization
- Generates visualization chart and recommendations
Output files:
outputs/threshold_analysis.png- F1-Score vs Threshold visualizationoutputs/optimal_threshold.txt- Recommended threshold configuration
Example output:
THRESHOLD OPTIMIZATION RESULTS
==================================================
Current threshold: 0.36
Optimal threshold: 0.2946
==================================================
PERFORMANCE METRICS:
Accuracy: 97.4%
Precision: 95.2%
Recall: 98.1%
F1-Score: 96.6%
==================================================
RECOMMENDATION: Lower threshold from 0.36 to 0.2946
BENEFIT: Better recall (catch more true matches)
How to apply the results:
- Update
src/faceid/configs/default.yaml:threshold: 0.2946 # Use the recommended value
- Restart the application to use the new threshold