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


If you would like to support this project, consider supporting my Patreon page.

Alternatively:



21 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?

    • 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!

  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’

  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

    • 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

      • 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)

  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.

    • 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.

  5. Hey, great video I loved it so much that I actually tried to replicate it for an assignment due very soon, however, I am receiving an error and was wondering If you could help me out, please? After
    git clone –single-branch https://github.com/ageitgey/face_recognition.git
    When I type in
    cd ./face_recognition/examples && python3 facerec_on_raspberry_pi.py
    I receive this cd ./face_recognition/examples && python3 facerec_on_raspberry_pi.py
    mmal: mmal_vc_component_create: failed to create component ‘vc.ril.camera’ (1:ENOMEM)
    mmal: mmal_component_create_core: could not create component ‘vc.ril.camera’ (1)
    Traceback (most recent call last):
    File “/usr/lib/python3/dist-packages/picamera/camera.py”, line 456, in _init_camera
    self._camera = mo.MMALCamera()
    File “/usr/lib/python3/dist-packages/picamera/mmalobj.py”, line 2279, in __init__
    super(MMALCamera, self).__init__()
    File “/usr/lib/python3/dist-packages/picamera/mmalobj.py”, line 633, in __init__
    prefix=”Failed to create MMAL component %s” % self.component_type)
    File “/usr/lib/python3/dist-packages/picamera/exc.py”, line 184, in mmal_check
    raise PiCameraMMALError(status, prefix)
    picamera.exc.PiCameraMMALError: Failed to create MMAL component b’vc.ril.camera’: Out of memory

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File “facerec_on_raspberry_pi.py”, line 16, in
    camera = picamera.PiCamera()
    File “/usr/lib/python3/dist-packages/picamera/camera.py”, line 431, in __init__
    self._init_camera(camera_num, stereo_mode, stereo_decimate)
    File “/usr/lib/python3/dist-packages/picamera/camera.py”, line 460, in _init_camera
    “Camera is not enabled. Try running ‘sudo raspi-config’ ”
    picamera.exc.PiCameraError: Camera is not enabled. Try running ‘sudo raspi-config’ and ensure that the camera has been enabled

    • Hi TechJunie101,

      Apologies for the late reply, and hope you’re able to sort this out before your due date.
      Your camera seems not to be enabled, run sudo raspi-config and enable the camera port in the interface section. If that’s not it, your camera connection may be loose.

    • Hi. there isn’t any specific script. You can copy and paste the commands (one by one) to install the dependencies.Cheers.

  6. sudo apt-get install python3-picamera
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package python3-picamera is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘python3-picamera’ has no installation candidate

    • Hi,

      I would recommend updating your packages: sudo apt-get update
      If not, it may be worth etching the pre-made demo on the site

  7. I have no question yet, I’m barely going through the installation of the libraries and dependencies, but I’m just blown away at the few vids I discovered from you. (The smart CCTV and the DIY Arduino arm). I was looking for almost exactly that. You are awesome and the simplicity in the instructions is super welcoming to a person like me. Thanks, you’ll most definitely see a question from me here or there. peace.

    • Thanks Marco, Much appreciated!
      Sure, you know where to find me. More bigger projects on the way!

      Eben.

Comments are closed.