# Techiebouncer

Get Informed out of Data

# Face Recognition using Raspberry pi 4

Today, Face Unlock and Face detection becomes an feature on almost all platforms, from mobile phones to normal camera's.

But, Technically the working of it lies under the Machine Learning concept. In this article, we are going to get details on both Machine Learning and Facial Recognition on Raspberry Pi.

Here, the idea is to blend the Machine Learning and Raspberry pi cam to recognise face which a computer do it in math way.

Every Machine Learning algorithm takes a dataset as input and learns from its data. The algorithm goes through the data and identifies patterns in the data. For instance, suppose we wish to identify whose face is present in a given image, there are multiple things we can look at as a pattern:

•     Height/width of the face.
•     Height and width may not be reliable since the image could be rescaled to a smaller face.          However, even after rescaling, what remains unchanged are the ratios – the ratio of height of      the face to the width of the face won’t change.
•     Color of the face.
•     Width of other parts of the face like lips, nose, etc.

Similar faces have similar dimensions. The challenging part is to convert a particular face into numbers – Machine Learning algorithms only understand numbers. This numerical representation of a “face” (or an element in the training set) is termed as a feature vector. A feature vector comprises of various numbers in a specific order.

we can map a “face” into a feature vector which can comprise various features like:

Height of face (cm)
Width of face (cm)
Average color of face (R, G, B)
Width of lips (cm)
Height of nose (cm)

when we have 2 faces (images) that represent the same person, the feature vectors derived will be quite similar. Put it the other way, the “distance” between the 2 feature vectors will be quite small.

Machine Learning can help us here with 2 things:

1.     Deriving the feature vector: it is difficult to manually list down all of the features because there are just so many. A Machine Learning algorithm can intelligently label out many of such features. For instance, a complex features could be: ratio of height of nose and width of forehead. Now it will be quite difficult for a human to list down all such “second order” features.
2.     Matching algorithms: Once the feature vectors have been obtained, a Machine Learning algorithm needs to match a new image with the set of feature vectors present in the corpus.

Now, for Attaining the Goal of Facial Recognition we need several support on software side, hence let's get into installation on Raspberry Pi.

Install required libraries with these commands:

sudo apt-get update
sudo apt-get install build-essential \
cmake \
gfortran \
git \
wget \
curl \
graphicsmagick \
libgraphicsmagick1-dev \
libatlas-dev \
libavcodec-dev \
libavformat-dev \
libboost-all-dev \
libgtk2.0-dev \
libjpeg-dev \
liblapack-dev \
libswscale-dev \
pkg-config \
python3-dev \
python3-numpy \
python3-pip \
zip
sudo apt-get clean

Install the picamera python library with array support (if you are using a camera):

sudo apt-get install python3-picamera

mkdir -p dlib
git clone -b 'v19.6' --single-branch https://github.com/davisking/dlib.git dlib/
cd ./dlib
sudo python3 setup.py install --compiler-flags "-mfpu=neon"

Install face_recognition:

sudo pip3 install face_recognition

git clone --single-branch https://github.com/ageitgey/face_recognition.git
cd ./face_recognition/examples
python3 facerec_on_raspberry_pi.py

Speeding up Face Recognition

Face recognition can be done in parallel if you have a computer with
multiple CPU cores. For example if your system has 4 CPU cores, you can
process about 4 times as many images in the same amount of time by using
all your CPU cores in parallel.

If you are using Python 3.4 or newer, pass in a --cpus <number_of_cpu_cores_to_use> parameter:

face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/

You can also pass in --cpus -1 to use all CPU cores in your system.