Facial Recognition using PyTorch and OpenCV
A facial recognition system is built to identify a person from an image or video. It is highly useful across many applications and industries. Today, this technology helps news organizations identify celebrities in their coverage of significant events, providing secondary authentication for mobile applications, automatically indexing image and video files for media and entertainment companies, all the way to allowing humanitarian groups to identify and rescue human trafficking victims.
In this blog, I have tried to build a Face Recognition System that matches a person’s image with the passport size photograph in the Dataset and outputs whether it’s match or no-match.
The System can be divided into parts: Face Detection and Face Classifier
Face Detection
First, the Dataset containing passport-size images and selfies is loaded. It is then divided into train data and validation.
Here the Pins Face Recognition by Burak on Kaggle can be used.
> pip install split-folders
This library helps in partitioning the dataset into train, test, and validation data.
import splitfolderssplitfolders.ratio('dataset', output="/data", seed=1337, ratio=(.8, 0.2))
This creates a data directory with train and valid sub-folders, having the dataset partitioned 80% and 20% into training and validation sets respectively.
Now, we will try to extract faces from the images. For this, I have used OpenCV’s pre-trained Haar Cascade classifiers for the face.
First, we need to load the haarcascade_frontalface_default XML classifier. Then load our input image (or video) in grayscale mode. If faces are found, it returns the positions of detected faces as Rect(x,y,w,h). Then, these locations, are used to create an ROI for the face.
This replaces all the images in the directory with the detected faces in the images. The data preparation part for the classifier is now done. We will now load this dataset.
This will import all the necessary libraries. Now we will load the dataset, in order to increase the size of the dataset, various data augmentation is applied.
Let us now visualize the dataset.
Let us now build the classifier model. Here, we will use InceptionResnetV1 pretrained on the VGGFace2 dataset as the base model.
Now we will train the model.
And, finally, we will evaluate the model and save it.
Now we will feed the input image to the saved model and check the match.
If you find this story useful, kindly follow me @Ritik Bompilwar, Thanks!