Face recognition is different from face detection. In face detection, we had only detected the location of human faces, and we recognized the identity of faces in the face recognition task. In this article, we are going to build a face recognition system using python with the help of face recognition library. We will also touch on how to import face recognition in Python?
There are many algorithms available in the market for face recognition. This broad computer vision challenge is detecting faces from videos and pictures. Many applications can be built on top of recognition systems. Many big companies are adopting recognition systems for their security and authentication purposes.
This article was published as a part of the Data Science Blogathon.
Face recognition systems are widely used in the modern era, and many new innovative systems are built on top of recognition systems.
There are a few used cases :
Several methods and algorithms implement facial recognition systems depending on the performance and accuracy.
Traditional face recognition algorithms don’t meet modern-day’s facial recognition standards. They were designed to recognize faces using old conventional algorithms.
OpenCV provides some traditional facial Recognition Algorithms.
These methods differ in the way they extract image information and match input and output images.
LBPH algorithm is a simple yet very efficient method still in use but it’s slow compared to modern days algorithms.
There are various deep learning-based facial recognition algorithms available.
DeepID
series of systems,Generally, face recognizers that are based on landmarks take face images and try to find essential feature points such as eyebrows, corners of the mouth, eyes, nose, lips, etc. There are more than 60 points.
Source: Medium.com
Source: wp.com
This article focuses on implementing face recognition using the library face_recognition, built on deep learning techniques and promises accuracy greater than 96% using a single training image.
Implementing a face recognition system using python. Implementing a Deep learning-based face recognition system using the face_recognition library.
In order to install the face recognition library, we need to first install the dlib
.
dlib
: It is a modern C++ toolkit that contains ML-related algorithms and tools.
# installing dlib
pip install dlib
face recognition
The actual face recognition library can be installed after dlib
.
# installing face recognition
pip install face recognition
Opencv
for some image pre-processing
# installing opencv
pip install opencv
Note: Sometimes installing dlib throws error in that case install install the C++ development toolkit using vs code community .
Importing Libraries
import cv2
import numpy as np
import face_recognition
We are done with installing and importing the libraries. It’s time to load some sample images to the face_recognition
library. Here is the link for the same.
The library face_recognition
supports only the BGR format of images. While printing the output image we should convert it into RGB using OpenCV.
Face_recognition
Loads images only in BGR format.
import cv2
import numpy as np
import face_recognition
img_bgr = face_recognition.load_image_file('student_images/modi.jpg')
img_rgb = cv2.cvtColor(img_bgr,cv2.COLOR_BGR2RGB)
cv2.imshow('bgr', img_bgr)
cv2.imshow('rgb', img_rgb)
cv2.waitKey
Output → BGR vs RGB
The library face_recognition
can quickly locate faces on its own, we don’t need to use haar_cascade
and other techniques.
img_modi=face_recognition.load_image_file('student_images/modi.jpg')
img_modi_rgb = cv2.cvtColor(img_modi,cv2.COLOR_BGR2RGB)
#--------- Detecting Face -------
face = face_recognition.face_locations(img_modi_rgb)[0]
copy = img_modi_rgb.copy()
# ------ Drawing bounding boxes around Faces------------------------
cv2.rectangle(copy, (face[3], face[0]),(face[1], face[2]), (255,0,255), 2)
cv2.imshow('copy', copy)
cv2.imshow('MODI',img_modi_rgb)
cv2.waitKey(0)
Output
The library face_recognition
is based on deep learning, it supports single-shot learning which means it needs a single picture to train itself to detect a person.
img_modi = face_recognition.load_image_file('student_images/modi.jpg')
img_modi = cv2.cvtColor(img_modi,cv2.COLOR_BGR2RGB)
#------to find the face location
face = face_recognition.face_locations(img_modi)[0]
#--Converting image into encodings
train_encode = face_recognition.face_encodings(img_modi)[0]
#----- lets test an image
test = face_recognition.load_image_file('student_images/modi2.jpg')
test = cv2.cvtColor(test, cv2.COLOR_BGR2RGB)
test_encode = face_recognition.face_encodings(test)[0]
print(face_recognition.compare_faces([train_encode],test_encode))
cv2.rectangle(img_modi, (face[3], face[0]),(face[1], face[2]), (255,0,255), 1)
cv2.imshow('img_modi', img_modi)
cv2.waitKey(0)
The above code took two pictures of the prime minister, and it returnedTrue
because both photos were of the same person.
face_recognition.face_encodings(imgelon)[0]
→Returns encoding of passed Image.face_recognition.compare_faces([train_encode],test_encode)
→ Takes a list of trained encodings and a test encoding of the unknown Image. It returns True
if both test encoding has a match in train encoding; otherwise, it returns. False
.Facing challenges while working on recognition systems in common, all you need to learn is how to get out of them. Here are some common challenges:
This article discussed how to implement a face recognition system using python with a single-shot image training technique. You can further use GUI like python Tkinter to design a GUI-based attendance system. We saw various challenges that affect a recognition system and how to solve them. In the next article, we will create a face recognition attendance system using the same concepts which we have discussed today.
A. Python is used in face recognition by utilizing libraries like OpenCV and Dlib to detect and recognize facial features, process images, and implement machine learning algorithms for identification.
A. OpenCV is one of Python’s best face recognition libraries due to its extensive functionality, real-time performance, and compatibility with various platforms and devices.
A. OpenCV is used in face recognition for its robust face detection capabilities, efficient image processing functions, and easy integration with Python, making it a popular choice for facial analysis tasks.
A. To detect face masks using Python, you can employ deep learning frameworks like TensorFlow or Keras to build a mask detection model, then use OpenCV to process video streams or images and identify faces with or without masks.
Face recognition uses algorithms like Eigenfaces, Fisherfaces, and Deep Neural Networks (DNNs). DNNs, including Convolutional Neural Networks (CNNs), are great at capturing detailed facial features.
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.