Face Recognition in Python
In this tutorial, we will understand what is face recognition and how it is achieved in python.
Face recognition is of great utility in real-world scenarios. It is an extended step of face detection. The former allows only the detection of the location of human faces in a human body, but the latter allows the identification of humans in any picture or even in a video.
Application
Face recognition is mainly used for security purposes. Let us see some of the applications.
- It is useful for ride-sharing companies to confirm the right person is picked by the right driver.
- It is needed in biometrics in a number of areas.
- It is of great use at the checkpoints of international borders to ensure the right person is crossing the boundary.
- It can automate many existing human-aided systems.
Earlier Methods:
Now, let us learn the involved steps in the detection of faces.
Following are the four steps that were performed one by one in a separate module in the earlier days
- Detection of Face
- Alignment of Face
- Extractions of features
- Recognition of face
Newer Methods:
These days, A single library is available which enables the user to perform these four steps in one step only.
Steps:
- Installing the required librarie
dilib
# installing the library named dilib
pip install dlib
face recognition
# installing the library named face recognition
pip install face recognition
Opencv
# installing the library named opencv
pip install opencv
- Importing the required libraries
After the installation of the libraries, these three libraries need to be imported.
import cv2
import numpy as np
import face_recognition
- Loading the image
Image loading comes after importing.
A library named face_recognition loads images in the form of BGR,
To be able to print the image, with the aid of OpenCV, one should convert the image into RGB (Red, Green and Blue).
imgelon_bgr = face_recognition.load_image_file('flower.jpg')
imgelon_rgb = cv2.cvtColor(imgelon_bgr,cv2.COLOR_BGR2RGB)
cv2.imshow('bgr', imgelon_bgr)
cv2.imshow('rgb', imgelon_rgb)
cv2.waitKey(0)
- Draw Bounding Boxes after finding the location of the face
One needs to draw a bounding box around the human face just to show whether the human face has been detected or not.
Training the Image for Face Recognition
This library is made in such a way that it discovers the face and works only on faces, Thus, the requirement of cropping the face out of pictures gets eliminated.
Training:
At this step, we alter the train image into nearly encodings and accumulate the encodings with the provided name of the individual for that image.
Testing:
For testing, an image is loaded and converted into encodings, and then encodings are matched with the stored encodings during training, this matching is grounded on finding extreme resemblance. When the encoding matching the test image is found, one gets the tag connected with train encodings.
If the individual in both images is the same, True is returned. Otherwise, False is returned.
Building a Face Recognition System
To build a face recognition system, one needs to import certain libraries.
Following are the libraries that need to be imported:
import cv2
import face_recognition
import os
import numpy as np
from datetime import datetime
import pickle
Challenges confronted by the Face Recognition Systems
Making a face recognition system is not as easy as it seems to be. There exist numerous challenges in building the model. Following are the challenges faced during the creation of the model:
- Illumination: Illumination causes change in the appearance of the face drastically; it is detected that even the slightest deviations in lighting conditions causes an important influence on its results.
- Pose: Facial Recognition systems are extremely delicate to the pose, which may result in damaged recognition or no recognition if the folder is only accomplished on the front face view.
- Facial Expressions: The same person can give different expressions and the model can get confused. However, Modern recognizers can easily overcome this issue.
- Low Resolution: In this, Training should be done on a picture of good quality or good resolution otherwise the model will fail to recognize the image and extract features from it.
- Aging: With the growing age, the features of the human face such as figure, shapes, and texture changes too.
Summary:
In this tutorial, we have understood face recognition in python, its applications, and constraints. We saw some steps to create a face recognition model.