Raspberry Pi-based Real-time Face Recognition Using OpenCV & Machine Learning

A beginners Tutorial

Besma Guesmi
7 min readJul 12, 2020

Face recognition is a method of identifying or detecting the identity of a person using his face imprints. These days, the Face recognition system is very useful; it is one of the most used advanced applications in the 2020 technology.

It is widely used in public and private security systems for its efficient role in keeping an eye on the environment in terms of supervising, attendance control, or even security monitoring …

That brings us to the main subject of the tutorial, entitled Raspberry Pi-based Real-Time Face Recognition System.

As a start it is crucial to clarify the difference between Face Recognition and Face detection, face detection is a part of the face recognition application, In face detection, the system can identify that there is a person's face present in an image or video without identifying his identity, otherwise, In face Recognition the system detects the person’s face using face detection program then compares it to the images in the database and identify the person’s ID.

How does facial recognition work?

According to the research, each human face has around 80 nodal points; the system can measure some of these points including the distance between the eyes, the width of the nose, the shape of the cheekbones, and the length of the jawline.

Figure 1: facial nodals
Facial nodals

Such software goes through several stages, identifying a person:

Face Recognition Process

Capturing: Take a picture using a camera and mark the position of the head, size, and posture (find the face) to detect, and then it will be stored in the database.

· Extracting: A photo of the face is captured; the system analyses the face and turns it into a mathematical formula. The facial feature becomes numbers in a code called ‘faceprint’ and each person has their own face print.

· Comparing: The picture is searched and matched to compare the template to another template on the database.

· Matching: The last step is to compare the face code with a database of another face print which has a photo with identification that can be compared and identify the person.

In the video below, can you explain more about the process of Face Recognition:

https://www.pandasecurity.com/mediacenter/src/uploads/2019/10/how-does-facial-recognition-work

What do we need to build our Recognition System?

Never mind, we will learn systematically how to build our own “Face Recognition” system using Webcam or “PiCam” that will be into 4 steps:

Step 1: Prepare the Components Required

- Raspberry Pi (I recommend you to use Raspberry Pi 4 (Model B), as it is the latest version of Raspberry Pi; it has a quad-core with 4GB of RAM)

- Power (Charger 3A for RPI4 and 2A for RPI3)

- Webcam or PiCam

NB. I recommend using RaspberryPi 4 (Model B), as it is the last version of RaspberryPi; it has quad-core with 4/8 GB of RAM.

Raspberry Pi 4

Step 2: Installing OpenCV 4 Package

To work on the Face Recognition program, we need to install the “OpenCV” library on RPI;

OpenCV is an Open Computer vision and machine learning library, used in real-time computer vision applications, and it also supports Deep Learning Frameworks.

NB. I recommend you to install OpenCV 4.0 version, it is stable and supports deep learning functions, including: Convolution, RNN, ReLU, Sigmoid, Scale, LRN and LSTM…

Following the commands below and installing your OpenCV library, follow carefully the commands one by one.

Open CV Install

Now, OpenCV is installed, we need to install the Required Packages;

Step 3: Installing the Required Packages:

Use Camera in RPI :

sudo apt-get install fswebcam

Install dlib (Dlib is a toolkit for real-world Machine Learning and data analysis applications)

pip install dlib

Install pillow (is an imaging library used to open, manipulate, and save images in different formats)

pip install pillow

Install face_recognition Library (using to train and recognize faces)

pip install face_recognition –no –cache-dir

When the entire package is installed successfully, we will move to prepare pour project main, which is the next step of our project.

Step 4: Prepare your Project Environment

Our project involves two parts; the first part is a Trainer program, which analyzes a set of photos of a particular person and creates a dataset, and the second part is the Recognizer program, which detects a face recognizes the face, and identifies the person.

Project Environnement

We start by creating a folder named “Face image” which contains the database (the pictures of the persons to want to identify), in the “Face image” folder we must create a folder with your name and take more than 5 photos to work well, Then we created our Trainer program which will analyse a set of photo that is saved in “Face image” folder and create a dataset (yml file), the second program is the “Recognizer program” which detects the face and compared it with the XML file to identify the person, this file called “haarcascade_frontalface_default.xml” used to detect the frontal face.

A computer program splits the images into the positive image (face image) and the negative image (non-face image) to create your own classifier; it is trained on hundreds of thousands of faces (face and non-face) images to train the program to be capable to classify a new image correctly.

Face Classification

The Face Trainer Program was used to analyse the images in the “Face Image” directory and search for similar nodal imprints. Once the face is detected, it gets cropped, converted into greyscale and NumPy array, and saved as a yml file called “Face Trainer” using the face_recognition library.

I shall explain important commands in the code, and you will find all the code with explanatory comments on my GitHub.

Referred to my GitHub space where I put the code :

To build our Face recognition system, we passed through 3 necessary steps:

1st step: Data Gathering (Prepare our Training Data): Read and analyse the images of each person along with their labels, detect faces from each image, train, and save it as a yml file (database).

2nd step: Train Face Recognition: Now, we use the database, which we prepared in step 1 to recognize the new picture, this is done by the specific OpenCV function “LBPH recognizer”.

3rd step: Prediction & Recognizer: This is the final phase of our project, we will compare our fresh face with the face captured and trained before and see if the system predicts it correctly.

Face Recognition phases

As we mentioned from the start, the face recognition system is growing as a major research field because of its importance and how it can help us in the fields of Authentication, Security (for factories, houses, banks, supermarkets…), Law enforcement (Criminal identification), Entertainment (Social Media: Snapchat…), Payment (online shopping…), Healthcare (identify illnesses by analyzing the patient’s features).

On quest arises: What is the future of Face Recognition?

In 2020, even retailers using Facial recognition based on Artificial Intelligence (AI) and Deep Learning (DL) technology, improved the service of the industry and got better security.

E.G., USA has enhanced its airport security with a facial recognition system for identification and registration the visitors.

China has built a vast network of cameras across the country that enable facial recognition technology; it has over 200 million surveillance cameras used at the end of 2018, and 626 million are expected by 2020 to enhance its security, the ambition of China in AI and Face Recognition system are high, it aims to become a world leader in AI by 2030.

Artificial Intelligence is indeed a fast-growing technology in the world, so I think the growth of Face recognition systems will be linear with AI technology and it will create massive revenue in the coming years.

The video below can explain more about how Artificial Neural Network algorithms are helping Face Recognition algorithms to be more accurate.

Technology has become very fast, and we cannot predict what is the next tech skills, but we believe that Technology still growing towards the change of the world.

So, more advanced face recognition algorithms are implemented using a combination of Artificial Intelligence and Face Recognition Technology.

I will write other articles for those more advanced methods as well.

I hope this project can help you to build your Face Recognition System!

GitHub: https://github.com/besmaGuesmi?tab=repositories

LinkedIn: https://www.linkedin.com/in/besma-guesmi-a94201161/

Twitter: https://twitter.com/BesmaGuesmii

E-mail: bessmagsm@gmail.com

Keep the enthusiasm! The next tutorial will be ready soon,

Thank you!

--

--

Besma Guesmi

Author @ Analytics Vidhya | Computer Vision Specialist | ML Trainer | Microelectronics & Embedded Systems Engineer