Face Recognition Using TensorFlow Pre-Trained Model & OpenCV

Swastik Somani
3 min readJan 16, 2019

--

Hi, I’m Swastik Somani, a machine learning enthusiast. Today I will share you how to create a face recognition model using TensorFlow pre-trained model and OpenCv used to detect the face.

Hope you will like my content!!!!

This blog divided into four parts-

  1. Introduction of Face recognition.
  2. Detect the Face using OpenCV.
  3. Create the Face Recognition Model.
  4. Convert the TensorFlow Model(.pb) into TensorFlow Lite(.tflite).

Introduction of Face Recognition

Face Recognition system is used to identify the face of the person from image or video using the face features of the person. We create the face recognition model using the deep learning algorithm. It uses Convolution Neural Network to detect the face of the person.

How CNN Works??

Convolution neural network inspired by the biological process in the connectively pattern of neurons. CNN Face Classification takes input as a image, then it will process it and classify into the different categories which is stored in the our dataset. The hidden layer of CNN consist Convolutional layer, Activation Function(ReLu, Sigmoid & any other), pooling layers, fully connected layers and normalization layers.

Lets do coding!!!

Detect the Face using OpenCV

Install the OpenCV using the cmd

pip install opencv-python

Or

pip3 install opencv-python

Import the libraries -

First, we need to collect the images from the directory.

Now, we are using the “haarcascade_frontalface_default.xml” file you can easy download this xml file from the google.

Now detect the face from the images

Run the python code -

python face.py
Before
After

Yeahhh we detect the face from the images!!!

Create the Face Recognition Model

We create the our face recognition model by using the mobilenet pre-trained model.

You can download the retrain.py by using this link-

After downloading file, use this cmd to retrained the model

python retrain.py \
--bottleneck_dir=tf_files/bottlenecks \
--how_many_training_steps=500 \
--model_dir=tf_files/models/ \
--summaries_dir=tf_files/training_summaries/mobilenet_0.50_224 \
--output_graph=tf_files/retrained_graph.pb \
--output_labels=tf_files/retrained_labels.txt \
--architecture=mobilenet_0.50_224 \
--image_dir=tf_files/people

After running this cmd, it gives tensorflow model after re-training of the model. Now you have your own face recognition model 😮 😃.

To run the TensorBoard, run this commond

tensorboard --logdir tf_files/training_summaries &

TensorBoard Accuracy graph for the trained model

The orange line shows the accuracy of the model on the training data. While the blue line shows the accuracy on the test set.

Convert the TensorFlow Model(.pb) into TensorFlow Lite(.tflite).

You can also run one simple cmd to create tensorflow lite file.

tflite_convert \
--graph_def_file=tf_files/retrained_graph.pb \
--output_file=tf_files/optimized_graph.lite \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TFLITE \
--input_shape=1,224,224,3 \
--input_array=input \
--output_array=final_result \
--inference_type=FLOAT \
--input_data_type=FLOAT

After running this cmd, it convert tensorflow file(.pb) into tensorflow lite file(.tflite). You can use this tflite file into your android and ios phone.

--

--