Face Recognition Using TensorFlow Pre-Trained Model & OpenCV
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-
- Introduction of Face recognition.
- Detect the Face using OpenCV.
- Create the Face Recognition Model.
- 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-pythonOr
pip3 install opencv-pythonImport 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.pyYeahhh 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/peopleAfter 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
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=FLOATAfter 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.
