DeepFace

repository·master·Indexed 12 days ago

https://github.com/serengil/deepface

A lightweight Python framework for face recognition and facial attribute analysis, including age, gender, emotion, and race. It wraps multiple state-of-the-art models such as VGG-Face, FaceNet, ArcFace, and GhostFaceNet, and supports various face detectors like retinaface, mtcnn, and yolov8. Key features include face verification, directory and database-backed search (supporting ANN), embedding extraction, real-time analysis via stream, and a REST API implementation.

Tokens
12.1K
Snippets
40
Records
54
Agent score
97%

What's inside DeepFace

  1. Overview of DeepFace capabilities

    master

    DeepFace is a hybrid face recognition framework that wraps several state-of-the-art models, including:

    • VGG-Face
    • FaceNet
    • OpenFace
    • DeepFace
    • DeepID
    • ArcFace
    • Dlib
    • SFace
    • GhostFaceNet
    • Buffalo_L

    It provides high-level functions for:

    1. Face Recognition: Verifying identities or finding faces in a database.
    2. Facial Attribute Analysis: Predicting age, gender, emotion, and race.

    The framework automates the standard face recognition pipeline (detect, align, normalize, represent, and verify) so you can perform complex tasks with single-line function calls.

  2. Understand DeepFace configuration impact on accuracy

    master

    DeepFace's accuracy is significantly influenced by the combination of four main configuration parameters. When choosing a configuration for your application, consider how these interact:

    • Facial Recognition Model: The core model used for feature extraction (e.g., Facenet512, VGG-Face, ArcFace, Dlib, SFace).
    • Face Detector Model: The model used to locate faces in an image (e.g., retinaface, mtcnn, yolov8, mediapipe, opencv).
    • Distance Metric: The mathematical method used to compare face embeddings (e.g., euclidean, euclidean_l2, cosine).
    • Alignment Mode: Whether or not to perform facial alignment (True/False).
  3. Understand deepface model and detector licenses

    master

    DeepFace wraps several external face recognition models, face detectors, and anti-spoofing tools. Because DeepFace inherits the licenses of these underlying models, you must check the specific license for each model you intend to use for production purposes.

    Face Recognition Models wrapped by DeepFace:

    • VGG-Face
    • Facenet (128d and 512d)
    • OpenFace
    • DeepFace
    • DeepID
    • ArcFace
    • Dlib
    • SFace
    • GhostFaceNet
    • Buffalo_L

    Face Detectors wrapped by DeepFace:

    • OpenCv
    • Ssd
    • Dlib
    • MtCnn
    • Fast MtCnn
    • RetinaFace
    • MediaPipe
    • YuNet
    • Yolo
    • CenterFace

    Other features:

    • Age, gender, and race/ethnicity models (trained on VGG-Face backbone).
    • Face anti-spoofing (optional).
  4. Install DeepFace via pip or source

    master

    You can install DeepFace using pip to automatically handle the library and its prerequisites. Alternatively, you can install from source to access new features not yet published on PyPI.

    Using pip:

    $ pip install deepface

    From source:

    $ git clone https://github.com/serengil/deepface.git
    $ cd deepface
    $ pip install -e .

    Once installed, import the main class:

    from deepface import DeepFace
  5. Reproduce DeepFace benchmark results

    master

    You can reproduce the benchmark experiments conducted on the LFW dataset by executing the following Jupyter notebooks in the repository:

    1. Perform-Experiments.ipynb: Used to run the actual experiments.
    2. Evaluate-Results.ipynb: Used to evaluate the outcomes of the experiments.
  6. Run DeepFace as a REST API

    master

    You can run DeepFace as a standalone service using Gunicorn. This allows external systems (mobile apps, web apps) to call the functionality via HTTP.

    Start the service:

    # Running the service directly
    cd scripts && ./service.sh
    
    # Running the service via docker
    cd scripts && ./dockerize.sh

    Example API calls using curl:

    • Represent: POST /represent
    • Verify: POST /verify
    • Analyze: POST /analyze
    • Register: POST /register
    • Search: POST /search

    Example curl command for verification:

    $ curl -X POST http://localhost:5005/verify \
       -d '{"img1":"img1.jpg", "img2":"img3.jpg"}'
  7. Convert distance to a probabilistic confidence score

    master

    DeepFace's default classification is binary (based on a threshold). To get a 'soft' measure of certainty (a confidence score), you can map distances to a probability using a logistic regression model.

    Conceptual Workflow:

    1. Generate Data: Collect pairs of images labeled as 'Same Person' or 'Different Person'.
    2. Extract Embeddings & Distances: Use DeepFace.represent() and find_distance() to get distances for these pairs.
    3. Determine Decisions: Use find_threshold() to label distances as 1 (Same) or 0 (Different).
    4. Train Logistic Regression: Fit a sklearn.linear_model.LogisticRegression model where the input $X$ is the distance and the target $y$ is the decision.
    5. Calculate Confidence: For a new distance, calculate the probability using the sigmoid function: $P = \frac{100}{1 + e^{-(w \cdot distance + b)}}$.
    6. Denormalize (Optional): Rescale the resulting confidence so that 'Same Person' results fall within a specific range (e.g., 51-100) and 'Different Person' results fall within another (e.g., 0-49).
  8. Calculate alignment and detection impact

    master

    The benchmark logic allows you to quantify how much performance changes based on two factors:

    1. Alignment Impact: Calculated by subtracting the performance of unaligned images from aligned images (df_aligned - df_unaligned).
    2. Detection Impact: Calculated by comparing the performance of specific detectors against the performance when detection is skipped (ref_df where index is skip).

    These metrics help identify which detectors or alignment steps are most critical for specific models and distance metrics.

  9. Analyze DeepFace benchmark results

    master

    This notebook provides methods to evaluate DeepFace performance across different models, detectors, distance metrics, and alignment settings. It uses pre-generated CSV files located in results/ and outputs/test/ to visualize accuracy, alignment impact, and detection impact.

    Key components for evaluation include:

    • Models: Facenet512, Facenet, VGG-Face, ArcFace, Dlib, GhostFaceNet, SFace, OpenFace, DeepFace, DeepID.
    • Detectors: retinaface, mtcnn, fastmtcnn, dlib, yolov8, yunet, centerface, mediapipe, ssd, opencv, skip.
    • Distance Metrics: euclidean, euclidean_l2, cosine.
    • Alignment: True or False.
  10. Run face verification experiments with DeepFace.verify()

    master

    To perform large-scale experiments (e.g., on the LFW dataset), you can iterate through different combinations of model_name, detector_backend, distance_metric, and align.

    When running batch experiments, it is recommended to set enforce_detection=False in DeepFace.verify() to prevent the execution from crashing if a face is not detected in one of the images. You can also use expand_percentage to adjust the detection area.

    from deepface import DeepFace
    
    result = DeepFace.verify(
        img1_path="path_to_image1.jpg",
        img2_path="path_to_image2.jpg",
        model_name="Facenet",
        detector_backend="retinaface",
        distance_metric="euclidean_l2",
        align=True,
        enforce_detection=False,
        expand_percentage=0
    )
    
    distance = result["distance"]