April 20, 2024

Impact & Learning

Learn to impact

Face recognition in Python

Processing data from images is a task that requires a high level of machine learning, especially for the faces. Currently there are several libraries for face recognition in Python, where the learning process is done by the machine, through the reading of many images with some face, and their analysis in each one, to build an information network that allows us to identify faces.

In this publication we show users some great tools through the Face Recognition library, to facilitate the implementation of facial recognition in your development, easily implementable in different programming languages, to facilitate the task according to your skill and ease of use. Moreover, this library is open source, with MIT licensing.

Introduction to Face Recognition

Face Recognition is a library that allows facial recognition in Python. It is easy to use and uses C++ dlib library for face recognition. The algorithm makes an in-depth learning with 99.38% accurate according to their site. The library can be cloned directly from Github or implemented via Git in your project.

git clone https://github.com/ageitgey/face_recognition.git

Examples of facial recognition with Face Recognition

Recognize faces in images

To find all faces in an image, it is enough to use the following three lines of code included the face_recognition library:

import face_recognition
image = face_recognition.load_image_file("my_image.jpg")
findFace = face_recognition.face_locations(image)

To show the result, we have the following image whose file name is my_image.jpg

If we run the previous code, the existing faces in the image are extracted:

Add contour to a face

It is possible to add an outline around the face, eyes, nose, mouth and chin with Face recognition in Python library. The code is:

import face_recognition
image = face_recognition.load_image_file("my_image.jpg")
marked_face = face_recognition.face_landmarks(image)

Identify faces in an image

To identify a person’s face in an image, a machine learning (or training) process is required, reading several images and storing their biometric content and then comparing it with various samples. Through comparisons, the machine defines the person shown in the image. The code used for facial identification or recognition in an image is as follows:

import face_recognition
knownImage = face_recognition.load_image_file("kennedy.jpg")
UnknownImage = face_recognition.load_image_file("otherImage.jpg")

codedKennedy = face_recognition.face_encodings(knownImage)[0]
encodedUnknown = face_recognition.face_encodings(UnknownImage)[0]

result = face_recognition.compare_faces([encodedKennedy], encodedUnknown)

Where if we use the following image kennedy.jpg, the image will recognize us as John F. Kennedy.

Source: John F. Kennedy

Other examples of Face Recognition

The complete project, as well as other projects derived from the use of this library, can also be found on Github. There are also other tools for facial recognition in other programming languages, for example using C++ or with the integration of the OpenCV library (integration in multiple languages, such as Javascript and Ruby), etc.

Also there are better technologies to process great amount of data, such the use of GPU cores with AMD, this is faster if we have many images to process.