How to implement Face Recognition using VGG Face in Python 3.7 and Tensorflow 2.0

Atul Singh
Analytics Vidhya
Published in
3 min readDec 23, 2019

INTRODUCTION

A facial recognition system is a technology capable of identifying or verifying a person from a digital image or a video frame from a video source or Image source. There are multiple methods in which facial recognition systems work, but in general, they work by comparing selected facial features from given image with faces within a Dataset.

Face Recognition

You can download the whole source code from my Github using this link.

PREREQUISITES

This post assumes familiarity with basic concepts like Python, Opencv, Numpy, Tensorflow and Keras.

WHY WE NEED FACIAL RECOGNITION?

Since face is a unique way to identify people, facial recognition has gained attention and growing rapidly across the world for providing safe and reliable security.

From creating attendance system as basic need to use Face Recognition as security in Airports, criminal detection, face tracking, forensic, public places etc. Of course we can’t able to find criminal using his fingerprint just by using surveillance camera.

So lets start and see how can we build a model that can help us to recognize person using pre-trained VGG Face2 Recognition Model.

The authors provided full source code here as well as pre-trained models that can be downloaded with standard deep learning frameworks such as Caffe and PyTorch, although there are not examples for TensorFlow or Keras.

We could convert the provided models to TensorFlow or Keras format and develop a model definition in order to load and use these pre-trained models. Thankfully, this work has already been done and can be used directly by third-party projects and libraries. We will going to use keras-vggface and MTCNN to help us to create Keras Model of VGGFace2

These libraries can be install via pip; for example:

sudo pip install git+https://github.com/rcmalli/keras-vggface.gitsudo pip install mtcnn

CREATE KERAS MODEL

We are going to create a model.py file and add the below code into it.

model.py

Now we will going to create a “crop_face” function which gonna crop face and send it to model for next process. Lets assumes that the input photo contains one face our face cropping method will return the first face detected.

crop face

In “crop_face” function we will going to detect face using MTCNN and then going to crop face out using Numpy image slicing on line 6. Now let see how our model going to perform. So lets create “main.py” and copy below code into it.

main.py

Lets test the model and see if it can perform well.

result

Ok it is able to find “Mr Perfectionist” 😃.

Thanks for your patience to read it to the end. Please contact for any kind of help or suggestion.

--

--

Atul Singh
Analytics Vidhya

I’m a Data Scientist in a startup. I love to spent time for learning new things about data science. Machine Learning, Cyber security & Data visualization is my