Face recognition for Automatic attendance management using Face Recognition

Midhun EM
3 min readFeb 27, 2019

--

In this Article, I am going to explain one of the easiest methods for face recognition using FaceNet. One of the applications of this is attendance management. This article will describe the idea behind the FaceNet paper (arXiv:1503.03832) and how to train a custom data set of images for face recognition using a pre-trained model. Find the python code in Github.

In this blog, I am going to explain the following topics:

  1. Introduction and idea behind FaceNet
  2. Data collection
  3. Data pre-process
  4. Training
  5. Test on different images

Introduction and idea behind FaceNet

FaceNet uses a deep neural network that encodes a face image into a vector of 128 numbers. By comparing two such vectors, you can then determine if the two pictures are of the same person[1][2].
Learning the networks is done by the Triplet loss function. Training will use triplets of images (A, P, N).
A is an “Anchor” image — a picture of a person.
P is a “Positive” image — a picture of the same person as the Anchor image.
N is a “Negative” image — a picture of a different person than the Anchor image.

Figure 1 : Model Structure

In FaceNet model, training loss is calculated as follows:

  1. Extract embedding from Anchor image(A), Positive image(P) and Negative image (N) using Deep Architecture.
  2. Calculate Euclidean distance between Anchor image(A) and Positive image(P), Anchor image(A) and Negative image(N)
  3. The distance d(A,P) should be as small as possible, while the d(A,N) should be as big as possible.
  4. Make sure that d(A,P) is smaller that d(N,P) by a margin alpha.
Image Source [1]

5. Thus, the loss that is being minimized is

Triplet loss[1]

For more details : arXiv:1503.03832

Data collection

Collect at least 10 images from each employee . Arrange the images in the below format.

Data tree structure

Data pre-process

In pre-processing, the face is extracted from each image of the data folders. These are then saved in Processed_data folder like the same tree structure shown above. Use code preprocess_image.py from Github.

Train a model for the data using a pretrained weight

The pre-trained model we used in this work is from facenet. We can see how to use the pre-trained weight in our application.

  1. For all the pre-processed images, extract 128 dimensional unit (embedding) using pre-trained model. For more details check Github.
  2. Save the embedding with respect to the employee names.

Now that the embedding is ready, let’s see how to use this embedding for image recognition.

Test on different images

When an employee faces the camera, the model will extract face using haarcascade_frontalface model. After that, extract the 128 embedding from extracted face using the pre-trained FaceNet model and then compare with the saved embedding (from training) using Euclidean distance. Then the system will return the label (name of person) with least Euclidean distance.

Sample output

Test image : Yann LeCun

Output image :

NOTE: To incorporate an unknown person, set min threshold for Euclidean distance. If min distance(least euclidean distance) > min threshold, set the person in the image as unknown. Here, the images used for training is of few legends in the field of Artificial Intelligence. All images are collected from google.
Code for attendance management using video feed is not updated in Github.

Actually published in : INAnalytics

References :

  1. https://arxiv.org/abs/1503.03832
  2. https://www.coursera.org/learn/convolutional-neural-networks
  3. https://github.com/davidsandberg/facenet

--

--