In this article, we will look at what face recognition is and how it differs from face detection.

We first briefly understand the principle of face recognition, and then jump to the coding part!! At the end of this article, you can also develop a face recognition program to recognize faces in images!!

Overview of Face Detection

What if machines could automatically detect objects in images without human intervention?

Everyone’s face may be slightly different, but after all it’s safe to say that all faces have certain features. Various face detection algorithms exist, but the Viola-Jones algorithm is the oldest one still in use today.

Face detection is often the first step towards many face-related applications, such as face recognition or face verification. But face detection has very useful applications. One of the most successful applications for face detection may be “taking pictures.” Example: When you click on a friend’s photo, the camera with the built-in face-detection algorithm detects where the face is and adjusts the focus accordingly.

Face Recognition Overview

Generally speaking, face recognition is a method of personal identity through face recognition or verification. Various algorithms can be used for face recognition, but their accuracy can vary. In this article I will discuss how to use deep learning for face recognition. Now let’s see how deep learning can be used to recognize faces.

Here, we use face embedding, where each face is transformed into a vector. The technique for turning faces into vectors is called deep metric learning. Let me break this process down into three simple steps for better and easier understanding:

1. Face detection:

The first task we perform is to detect faces in an image (photo) or video stream. Now we know the exact coordinates/position of the face, so we extract the face for further processing.

2. Feature extraction:

Now you see we’ve cropped the face from the image, so we’ve extracted certain features from it. Here, we’ll see how to use face embedding to extract these features of a face.

As we know, the neural network takes an image of a face as input and outputs a vector that represents the face’s most important features! In machine learning, this vector is just a so-called embedding, so we call it vector face embedding.

Now how is this going to help identify different people’s faces? When we train the neural network, the network learns to output similar vectors for similar-looking faces. Let’s consider an example, if I have multiple images of faces at different time intervals, obviously some features might change but not much. So in this problem, the vectors associated with faces are similar, or we could say they’re very close in vector space.

Now that we know how the network works, let’s see how we can use it on our own data. Here, we pass all the images in the data to the pre-trained network to get the appropriate embeddings and save them in a file for the next step.

3. Face comparison:

We saved face embeds for every face in the data in the file, and the next step is to identify new images that are not in our data. So the first step is to compute the face embed of the image using the same network we used before, and then compare this embed with the rest of the embed we have. If the generated embed is closer to or similar to any other embed, we can recognize the face.

Know what OpenCV is

Computer vision is one of the most interesting and challenging tasks in the field of artificial intelligence. Computer vision acts as a bridge between computer software and visualization. Computer vision allows computer software to understand and visualize its surroundings.

Let’s look at an example: determining fruit by shape, color, and size. This task is easy for the human brain, but in the computer vision pipeline, first we need to collect data, then we perform data processing operations, and then we train and teach models to understand how to distinguish the size, shape, and color of fruit based fruits.

Today, a variety of software packages are available to perform machine learning, deep learning, and computer vision problems. Computer vision is by far the best module for solving such complex problems. OpenCV is an open source library. It is supported by different programming languages such as R, Python, and so on. It probably runs on most platforms, such as Windows, Linux, and macOS.

OpenCV benefits:

1. Open CV is a free and Open source library.

2. Open CV is fast because it is written in C/C++ language compared to other languages

3. The less RAM the system has, the better OpenCV works.

4. Supports most operating systems, such as Windows, Linux, and macOS.

perform

In this section, we will implement face recognition using OpenCV and Python.

First, let’s look at which libraries we need and how to install them:

1.OpenCV 

2.dlib 

3. Face recognition

OpenCV is a video and image processing library for image and video analysis, such as face detection, license plate reading, photo editing, advanced robot vision, and more.

The Dlib library contains our implementation of “deep metric learning” for building face embeds for the actual recognition process.

The face_recognition library is very easy to use and we will use it in our code.

First, remember to install the Dlib library before installing face_recognition. The output of any face recognition application using OpenCV will look like this:

To install OpenCV, dlib, and face recognition, type the following code snippet at the command prompt.

pip install opencv-pythonconda install -c conda-forge dlibpip install face_recognition
Copy the code

Now let’s do the code!

Extracting features from human faces

You need a dataset first, or even create your own dataset. Just be sure to arrange all the images in folders, each containing only one person’s image.

Now, save the data set in the same folder as the file you want to make. Here’s the code, I’ve added comments where necessary:

from imutils import paths #imutils includes opencv functions
import face_recognition
import pickle
import cv2
import os

#get paths of each file in folder named Images
#Images here that contains data(folders of various people)
imagePath = list(paths.list_images('Images'))
kEncodings = []
kNames = []

# loop over the image paths
for (i, ip) in enumerate(imagePath):
# extract the person name from the image path
name = ip.split(os.path.sep)[-2]
# load the input image and convert it from BGR
image = cv2.imread(ip)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

boxes = face_recognition.face_locations(rgb,model='hog')
# compute the facial embedding for the any face
encodings = face_recognition.face_encodings(rgb, boxes)
# loop over the encodings
for encoding in encodings:
kEncodings.append(encoding)
kNames.append(name)

#save emcodings along with their names in dictionary data
data = {"encodings": kEncodings, "names": kNames}
#use pickle to save data into a file for later use
f = open("face_enc", "wb")
f.write(pickle.dumps(data))#to open file in write mode
f.close()#to close file
Copy the code

Now see that we’ve stored the inserts in a file called “face_enc”, so we can use them to recognize faces in images (photos) or live video streams. In the next section we’ll see how to recognize faces from images.

How to recognize faces in images

The following script is used to detect and recognize faces in images. I have provided comments alongside the code where necessary to help beginners understand the code effectively.

import face_recognition import imutils #imutils includes opencv functions import pickle import time import cv2 import os  #to find path of xml file containing haarCascade file cfp = os.path.dirname(cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml" # load the harcaascade in the cascade classifier fc = cv2.CascadeClassifier(cfp) # load the known faces and embeddings saved in last file data = pickle.loads(open('face_enc',  "rb").read()) #Find path to the image you want to detect face and pass it here image = cv2.imread(Path-to-img) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #convert image to Greyscale for HaarCascade gray = cv2.cvtColor(image, DetectMultiScale (Gray, scaleFactor=1.1, minNeighbors=6, minSize=(60, 60), flags=cv2.CASCADE_SCALE_IMAGE) # the facial embeddings for face in input encodings = face_recognition.face_encodings(rgb) names = [] # loop over the facial embeddings incase # we have multiple embeddings for multiple fcaes for encoding in encodings: #Compare encodings with encodings in data["encodings"] #Matches contain array with boolean values True and False matches  = face_recognition.compare_faces(data["encodings"], encoding) #set name =unknown if no encoding matches name = "Unknown" # check to see if we have found a match if True in matches: #Find positions at which we get True and store them matchedIdxs = [i for (i, b) in enumerate(matches) if b] count = {} # loop over the matched indexes and maintain a count for # each recognized face face for i in matchedIdxs: #Check the names at respective indexes we stored in matchedIdxs name = data["names"][i] #increase count for the name we got count[name] = count.get(name, 0) + 1 #set name which has highest count name = max(count, key=count.get) # will update the list of names names.append(name) # do loop over the recognized faces for ((x, y, w, h), name) in zip(faces, names): # rescale the face coordinates # draw the predicted face name on the image cv2.rectangle(image, (x, y), (x + w, y + h), PutText (image, name, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) cv2.imshow("Frame", image) cv2.waitKey(0)Copy the code

The output

conclusion

Now we have completed the process of face recognition.

Develop reading

Face recognition technology is a kind of high precision, easy to use, high stability, difficult to counterfeit biometric technology, has extremely broad market application prospects. In the public security, national defense, customs, transportation, finance, social security, medical and other civil security control industries and departments there is a wide range of demand.

Our R&d staff in TSINGSEE Qingxi Video are also actively developing AI technologies such as face detection, face recognition, traffic statistics and helmet detection, and actively integrating them into existing video platforms. Typical examples are EasyCVR video fusion cloud service, which is capable of AI face recognition, license plate recognition, voice intercom, pso control, sound and light alarm, surveillance video analysis and data summary. It is widely used in intelligent access control of residential areas and buildings, perimeter suspicious wandering detection, scenic spot traffic statistics and other scenes.