Real-Time Face Recognition on Raspberry Pi

Using face_recognition for real-time face recognition on Raspberry Pi

Real-Time Face Recognition on Raspberry Pi

Recently, I’ve been experimenting with face_recognition, a Python-based face recognition library. The author demonstrated real-time face recognition using a webcam on a desktop, as shown in the following example:

example

I wanted to replicate this effect on a Raspberry Pi. If embedded devices can achieve this, it would be amazing—just like something from a movie.

Installation Steps

You can follow the installation guide from the project page: Raspberry Pi 2+ installation instructions. I’m using a Raspberry Pi 2B with a quad-core 900MHz CPU and 1GB RAM (though face_recognition only uses one core). I’ve overclocked the CPU to 1050MHz. (For details about overclocking, check out my blog post: Performance and Power Consumption of Overclocked Raspberry Pi)

Below are the steps I followed. (I won’t cover how to install the system itself—there are plenty of tutorials online. Make sure to change your apt sources; I used USTC’s mirror as I’m in a university network.)

First, install a bunch of required libraries:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

If you’re using the Raspberry Pi camera module (CSI interface), run the following:

1
2
sudo apt-get install python3-picamera
sudo pip3 install --upgrade picamera[array]

Download and install dlib:

1
2
3
4
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:

1
sudo pip3 install face_recognition

Clone the example code and try running it:

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

At this point, I encountered an error about a missing library. Normally, we can search for the package name with apt-cache search <library_name> and then install it using sudo apt-get install <package_name>.

The error mentioned a missing libatlas.so.3. After running apt-cache search libatlas, I found that the package name is libatlas3-base, so I installed it with:

1
sudo apt-get install libatlas3-base

You’ll encounter similar issues when testing the camera, and the solution is the same.

After installation, the code ran successfully.

Our goal is real-time face recognition on the Raspberry Pi, so I connected the camera module (CSI interface), enabled the camera in raspi-config, and rebooted. (If you’re unfamiliar, search for “how to use Raspberry Pi camera module”.)

Then, I ran the real-time face recognition code:

1
python3 facerec_from_webcam_faster.py

An error occurred during import cv2, complaining about missing libraries. Just follow the instructions and install the missing libraries. You may need to go through a few rounds of installing missing dependencies before everything works.

Next, the program threw another error at this line: small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) This happened because video_capture.read() failed to grab a frame. But the camera was connected, so why was it not reading properly? Was the camera broken? No.

This is because the Raspberry Pi camera module is loaded as firmware under /boot/, and not as a standard V4L2 camera driver. Therefore, /dev/video0 does not exist. Source: Using CSI Camera in OpenCV on Raspberry Pi

To resolve this, load the V4L2 driver module manually:

1
sudo modprobe bcm2835-v4l2

To load it automatically on boot, edit /etc/modules and add:

1
bcm2835-v4l2

As shown below:

Auto-load module on boot

Now run the code again:

1
python3 facerec_from_webcam_faster.py

This time it works! A video window appears showing the camera feed. Here’s what my test looked like:

Obama test

Putin test

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
This is Yumi’s blog where I share technology and life.
Built with Hugo
Theme Stack designed by Jimmy