Installing Face Recognition Library on Raspberry Pi 4

Ever wanted to use face recognition on your Raspberry Pi 4?

Well, look no further. We’re talking about using your Raspberry Pi to identify users’ profiles and many more. In our example, we have the face recognition library controlling our home appliances. By the end of this article, you would have installed Dlib and face_recognition libraries onto your Pi.

Want to skip all the steps? Download the face recognition Raspberry Pi image.

Note: Although this module may work on previous pi models, the Raspberry Pi 4 is recommended

Applications of Face Recognition

Smart Mirror Touchscreen - Face ID
Face Recognition Module

Smart Mirror Touchscreen - Face ID turning on IoT Home Appliances
Controlling Smart Plug Home Appliances

Follow the below terminal command to install the library dependencies for face recognition on Raspberry Pi.
Note: We recommend the Raspberry Pi 4 for the face recognition library. Older Pi Models may not be as responsive

1) Copy and Paste the following (Install one at a time):  

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

2) Install the following:

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

3) Increase the SWAP FILE:

sudo nano /etc/dphys-swapfile

Change CONF_SWAPSIZE=100 to CONF_SWAPSIZE=1024 and save / exit nano

If the below step produces an error, restart you Pi. (Same Equivalent)

sudo /etc/init.d/dphys-swapfile 

4) Git clone and Install dlib library

mkdir -p dlib
git clone -b 'v19.6' --single-branch https://github.com/davisking/dlib.git dlib/
cd ./dlib
sudo apt-get install cmake
mkdir build; cd build; cmake .. ; cmake --build 

Install dlib via pip3

pip3 install dlib

5) Decrease the SWAP Size:

sudo nano /etc/dphys-swapfile

Change CONF_SWAPSIZE=1024 to CONF_SWAPSIZE=100 and save / exit nano

If the step fails follow the same as Step 3

6) Install supporting dlib libraries:

pip3 install numpy
pip3 install scikit-image
sudo apt-get install python3-scipy
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install python3-pyqt5
sudo apt install libqt4-test
pip3 install opencv-python==3.4.6.27
pip3 install face_recognition 

7) Clone the Face recognition library

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

11 thoughts on “Installing Face Recognition Library on Raspberry Pi 4”

  1. This all makes sense until I go to load up my own photos and look into my own camera. The MMM-Face-Recognition-SMAI keeps saying I am Tony Stark. 🙁 I put pictures of myself, and other people, into “/home/pi/MagicMirror/modules/MMM-Face-Recognition-SMAI/public”, each labeled “uniquename-id.jpg”. I know things are working somewhat correctly, since it is seeing the faces when I run ‘face_detection’ command. What am I missing?

    Reply
    • Hi Lindsay,
      Sounds great. The Smart Mirror module is currently developed to work for one profile.
      However, I’ve developed the feature of adding multiple profiles in a separate project.

      https://github.com/EbenKouao/SmartCCTV-Camera in camera.py file.

      This way you can store all images in the profile folder and for it to be recognized. If you’re familiar with Python, you can work on building a solution to this. If you do, please contribute this feature to Git!

      Reply
  2. Hi I really need your help to complete my assignment.
    Do you know the reason of this error please .
    Thank you so much.

    Traceback (most recent call last):
    File “/home/pi/SmartCCTV-Camera/main.py”, line 3, in
    from camera import VideoCamera
    File “/home/pi/SmartCCTV-Camera/camera.py”, line 7, in
    import face_recognition
    ModuleNotFoundError: No module named ‘face_recognition’

    Reply
  3. hey i try to install with pip3 installl face_recognition but i got this

    The scripts face_detection and face_recognition are installed in ‘/home/pi/.local/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.

    after that i tried to use your smart cctv i got modulenotfind again

    Reply
    • also i have several similiar error when installing the libs like lsm2bin , triffle,..image io installed in /home/pi/.local/bin is not in path

      Reply
      • Hi Agung,

        This is something new.
        I would suggest:
        sudo apt-get update
        sudo apt-get upgrade

        Ensure that pip is installed. This would install the face_detection module.

        If not, I would also try reinstalling the raspbian OS to start from scratch. You can find a raspbian demo with the packages already installed on the site (Smart Mirror AI – same library you can work on top of that)

        Reply
  4. Please can you guide me about the further settings related to camera. I have noir V2.. The file is running properly but it is continuously showing unknown person. The camera gets stuck when a face is detected and it is so so slow that it might not being able to process the pictures and couldn’t match from profiles. I have raspberry pi 4 (4 gb). Please tell me what further settings i can do to make it smooth and so it starts processing the pictures when shown in front of camera. Please as fast as you can 🙂
    Best Regards.

    Reply
    • Hi Ahmer,

      Good that’s it working. It should be relatively fast. (Detection of face < 2 seconds) I would possibly recommend enabling CLI only and access via SSH to speed up the face detection. Hope this helps. Eben.

      Reply

Leave a comment