Face Recognition in Python using OpenCV

Face Recognization In Python Using Open CV (1)

In this article, we’re going to discuss face recognization in Python using OpenCV. We will take a look into its collusion with technology and how it is making our life easy. After that, we will jump into Python and implement the codes.

What is Facial Recognization?

Facial recognition is the ability to identify individuals by their facial features. It is a way of relating to or attesting to an existing identity using their face. These systems can be used to identify people’s images, and videos, or to verify any face in real time.

Facial recognition is an order of biometric security. Other forms of biometric software include voice recognition and eye retina or iris recognition. The technology is substantially used for security enforcement, but there’s adding interest in other areas of use. Face recognition technology is used in a variety of applications, including security, facial recognition software, and marketing.

Understanding How Facial Recognization Works

Majorly, There are two methods which are widely used to do facial recognization.

Method 1: Convert the face into matrix values/pixels.

Imagine setting out facial login on your phone. If you’ve done this before, you know the phone tries to get your facial structure from every angle. As soon as the full structure of the face is saved, it is stored as a matrix in the backend.
The next time you unlock your phone, all that’s done in these matrices is just a search. It’s not AI (which most phone companies claim). Based on that it provides the output in the BInary form(True/ False)

Method 2: Use deep learning to do face recognization

Deep learning algorithms are Data Hungry and we need to train the models to detect the faces but they are not trained to detect the new owner, So they get to fail.

However, this technology can be used for facial recognition. The best example is Facebook auto-tagging. If you are a new user, the Facebook algorithm has a hard time identifying you in new uploads. But once Facebook collects enough data (or its algorithms are trained to identify your face), it will soon be able to recognize us, even in group photos.

What is OpenCV?

OpenCV (Open-Source Computer Vision Library) is a Python library, which as the name suggests is open-sourced and uses machine learning capabilities to solve computer vision problems. It was developed to provide a common tool for computer vision applications and machine learning insights for commercial product-based corporations.

Under the hood, using the Berkeley Source Distribution (BSD) license which imposes minimum restrictions on the use and modifications of a software codebase, makes OpenCV really convenient for such kinds of companies to use and change the code as per their requirement. All the OpenCV algorithms are implemented in C++ language but the algorithms can be used in languages like Python, Java, Matlab etc.

There are around 2500 optimized algorithms which are being used by the OpenCV library.

Here’s a quick tutorial on reading images with OpenCV which will give you a foundational understanding of this library.

The user base for this library is also huge and has kept growing at a staggering rate. The number of downloads for this library exceeds 18 million. These algorithms can be utilized for real-time vision applications, face recognition, identifying objects, image processing, extracting 3D Models of objects and the list goes on and on.

Large corporations like Google, Microsoft and Intel also rely on OpenCV in their production environment and make use of this software extensively utilizing its massive capabilities. Various government organizations also use OpenCV for their projects like surveillance, monitoring landscapes, navigating AI robots for their tasks, disaster management and even performing rescue operations.

In a nutshell, OpenCV, with its tremendous capabilities, continues to be a top software library working seamlessly with all its glory in the current technology scenario.

Implementation of Facial Recognization in Python using OpenCV

Here, we are going to implement face recognition using OpenCV in Python. At first, we will install the Libraries we need to implement facial recognization.

  • OpenCV: OpenCV (Open-Source Computer Vision Library) is a software library, which as the name suggests is open-sourced and uses machine learning capabilities to solve computer vision problems as previously explained.
  • dlib: The dlib library contains an implementation of “deep metric learning” used to create face embeddings used in the actual recognition process.
  • Face_recognition: The face_recognition library is very easy to use and we will be using it in our code. It Recognizes and manipulates faces.

1. Installing the Libraries

#Install the libraries 
pip install opencv-python
conda install -c conda-forge dlib
pip install face_recognition

2. First image face encoding

It is to note that, OpenCV uses BGR(Blue, Green, Red) image format. So, when it reads an image using cv2.imread() it always will interpret it in BGR format by default. We can also use cvtColor() the method to convert a BGR image to RGB and vice-versa.

Extract the image (Elon_musk.png in this case) and convert it to RGB colour format using normal OpenCV procedures. Then do the “face coding” using the features of the facial recognition library.

#defing the variable and reading the file
image_1 = cv2.imread("Elon_musk.png")
#it is interpreting the image in BGR format 
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_encoding = face_recognition.face_encodings(rgb_img)[0]

3. Second image face encoding

Follow the same steps for the second image, only changing the name of the variable and of course the path of the second image. In this case images/Elon_musk.png.

#accessing the image in the file from we have to match 
image_2 = cv2.imread("images/Elon_musk.png")
rgb_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img_encoding2 = face_recognition.face_encodings(rgb_img2)[0]

4. Check if img_encoding_1 and img_encoding_2 are the same people

For matching purposes, we will make two cases:

In the First case, we will put image_1 in the defined folder where we are looking for the image and in the second case we will not put image_1 in the folder. So, Based on this we will have True as an output in the first case and False as an output in the second case.

For Matching the images, We have to do a comparison between the images

First Case

final_result = face_recognition.compare_faces([img_encoding], img_encoding2)
print("final_result: ", final_result)

output

Image

I am attaching my folder screenshot for a better understanding.

Image 1
image of the folder(Images)

In the above case, you can see that there is the image of the Elon_musk in this folder which is why we got the output as True

Second Case

Image 2

I am attaching my folder screenshot for a better understanding.

Image 3
image of the folder(Images)

In the above case, you can’t see the image of the Elon_musk in this folder which is why we got the output as False.

Complete Code for Implementing Face Recognition Using Python OpenCV

#Install the libraries 
pip install opencv-python
conda install -c conda-forge dlib
pip install face_recognition


#defing the variable and reading the file
image_1 = cv2.imread("Elon_musk.png")
#it is interpreting the image in BGR format 
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_encoding = face_recognition.face_encodings(rgb_img)[0]


#accessing the image in the file from we have to match 
image_2 = cv2.imread("images/Elon_musk.png")
rgb_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img_encoding2 = face_recognition.face_encodings(rgb_img2)[0]


#For Matching the images for cases 
final_result = face_recognition.compare_faces([img_encoding], img_encoding2)
print("final_result: ", final_result)

Conclusion

In this article, we went through how we can do face recognization in python by using the OpenCV library and how this technique is used by different industries for a better experience.

Reference

https://pypi.org/project/opencv-python/