Skip to content

nguyenmaiductrong/insightface-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฏ InsightFace Demo - Face Recognition System

Python 3.11+ License: MIT FAISS InsightFace

A production-ready face recognition system with real-time processing, similarity search, and modern GUI interface.

๐Ÿš€ Quick Start โ€ข ๐Ÿ“– Documentation โ€ข ๐Ÿ”ง API Reference โ€ข ๐Ÿ’ก Examples


๐ŸŽจ Overview

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.

โœจ Features

๐Ÿ” Core Functionality

  • ๐Ÿ†š 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

โšก Performance & Scalability

  • ๐Ÿš€ 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

๐Ÿ–ฅ๏ธ User Interface

  • ๐ŸŽจ 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

๐Ÿ—๏ธ Architecture

๐Ÿ“ Project Structure

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

๐Ÿ”ง Core Components

๐Ÿง  FaceEngine (core/engine.py)

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

๐ŸŽจ User Interface (ui/)

Modern PySide6-based desktop application with three main modules:

  • verify_tab.py: Two-image face verification with confidence scores
  • library_tab.py: Search similar faces in large image collections
  • realtime_tab.py: Live webcam face recognition and identification
  • main_window.py: Main application container with tabbed interface

๐Ÿ› ๏ธ Processing Scripts (scripts/)

Batch processing tools for database preparation:

  • build_facebank_faiss.py: Creates searchable index from identity database
  • build_library_faiss.py: Builds vector index for general image library

๐Ÿ”ง Utility Modules (utils/)

Reusable helper functions:

  • face_utils.py: Face detection, embedding extraction, and normalization
  • image_utils.py: Image I/O, preprocessing, and format conversions

๐Ÿš€ Quick Start

๐Ÿ“‹ Prerequisites

  • 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

๐Ÿ› ๏ธ Installation Guide

Step 1: Clone Repository and Setup Virtual Environment

# 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

Step 2: Install Dependencies

# Upgrade pip to latest version
pip install --upgrade pip

# Install all required packages from requirements.txt
pip install -r requirements.txt

Step 3: Prepare Data Structure

Create 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.jpg

Data 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
    โ””โ”€โ”€ ...

Step 4: Build Library FAISS Index

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

Step 5: Build Facebank FAISS Index

Create 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.json

Step 6: Launch Application

Start the GUI application:

# Launch the main application
python -m src.faceid

# Alternative: Direct execution
python src/faceid/__main__.py

The 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

๐Ÿ“Š Performance Tips

  • 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

๐Ÿ“– Usage

๐Ÿ” Face Verification

  1. Open the "Face Verification" tab
  2. Click "Choose First Image" and select your first image
  3. Click "Choose Second Image" and select your second image
  4. Click "Compare" to get similarity score and match result

๐ŸŽฏ Library Search

  1. Open the "Library Search" tab
  2. Browse and select your library directory (default: data/library)
  3. Click "Refresh" to load image thumbnails
  4. Click "Choose Query Image" and select the image you want to search for
  5. Click "Search" to find similar faces in your library
  6. Results will show only images above the similarity threshold

๐Ÿ“น Real-time Recognition

  1. Open the "Real-time Recognition" tab
  2. Click "Start Camera" to begin webcam capture
  3. The system will detect and identify faces in real-time
  4. Known identities will be labeled with their names
  5. Click "Stop Camera" to end the session

โš™๏ธ Threshold Calibration

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 outputs

What 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 visualization
  • outputs/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:

  1. Update src/faceid/configs/default.yaml:
    threshold: 0.2946  # Use the recommended value
  2. Restart the application to use the new threshold

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages