Skip to main content

Face recognition using TensorFlow

Project description

Facial Recognition and Alignment

What's this?

This repository contains a refactored implementation of David Sandberg's FaceNet and InsightFace for facial recognition. It also contains an implementation of MTCNN and Faceboxes for face cropping and alignment. What is in the refactor:

  • Made algorithms easily and efficiently usable with convenience classes.
  • Added much more efficient methods of batch processing face recognition and alignment
  • Added true face alignment (with affine transformation) to align face to bottom-center of image: code
  • Added proportional margin to alignment as per this issue
  • Ability to easily switch between insightface and facenet at inference time

More information on customizing and implementing new face detection algorithms can be found here.


To use in other projects, this implementation can be pip installed as follows:

pip install facenet_sandberg

To use locally:

  1. Clone repo
  2. cd to base directory with
  3. run:
pip install -e .

^(installs package in development mode)

Important Requirements

  1. Python 3.5
  2. Tensorflow==1.7
  3. Tensorlayer==1.7 The rest is specified in requirements.txt


Links to pretrained models:


Links to download training datasets (!big files!):

Image directory structure

This repo assumes images are in LFW format:


If your dataset is not like this you can use to put your images into the right format like so (from facenet_sandberg/facenet_sandberg):

python --image_directory PATH_TO_YOUR_BASE_IMAGE_DIRECTORY


Alignment is done with a combination of Faceboxes and MTCNN. While Faceboxes is more accurate and works with more images than MTCNN, it does not return facial landmarks. Whichever algorithm returns more results is used.

Use the script to align an entire image directory:

                        --output_dir PATH_TO_OUTPUT_ALIGNED_IMAGES \
                        --facenet_model_checkpoint PATH_TO_PRETRAINED_FACENET_MODEL \
                        --image_height DESIRED_IMAGE_HEIGHT \
                        --image_width DESIRED_IMAGE_WIDTH \
                        --margin DESIRED_PROPORTIONAL_MARGIN \
                        --scale_factor DESIRED_SCALE_FACTOR \
                        --steps_threshold DESIRED_STEPS \
                        --detect_multiple_faces \
                        --use_faceboxes \
                        --use_affine \
                        --num_processes NUM_PROCESSES_TO_USE
  • Default values for most arguments are provided here

Generate Pairs.txt

A pairs.txt file is used in training and testing. It follows this format. In order to generate your own pairs.txt run:

                        --pairs_file_name OUTPUT_NAME_OF_PAIRS_FILE \
                        --num_folds NUMBER_OF_FOLDS_FOR_CROSS_VALIDATION \
                        --num_matches_mismatches NUMBER_OF_MATCHES_AND_MISMATCHES


MIT License from original repo

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

facenet_sandberg-0.0.8.tar.gz (42.8 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page