Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Install dlib and face_recognition on a Raspberry Pi

Instructions tested with a Raspberry Pi 2 with an 8GB memory card. Probably also works fine on a Raspberry Pi 3.

Steps

Download the latest Raspbian Jessie Light image. Earlier versions of Raspbian won't work.

Write it to a memory card using Etcher, put the memory card in the RPi and boot it up.

Log in. Default username / password is pi / raspberry.

Set up Wifi (if you are using Wifi) according to the Raspberry Pi instructions.

Run sudo raspi-config and configure the basics:

  • Set up your keyboard layout (It defaults to a British keyboard layout)
  • Change default user password
  • Enable the Raspberry Pi camera (if you have one attached)
  • Configure gpu memory split under 'Advanced'. Set it up '16'.
  • Save changes and reboot.

Install required libraries with these commands:

sudo apt-get update
sudo apt-get upgrade
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
sudo pip3 install --upgrade picamera[array]

Temporarily enable a larger swap file size (so the dlib compile won't fail due to limited memory):

sudo nano /etc/dphys-swapfile

< change CONF_SWAPSIZE=100 to CONF_SWAPSIZE=1024 and save / exit nano >

sudo /etc/init.d/dphys-swapfile restart

Download and install dlib v19.6:

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

Revert the swap file size change now that dlib is installed:

sudo nano /etc/dphys-swapfile

< change CONF_SWAPSIZE=1024 to CONF_SWAPSIZE=100 and save / exit nano >

sudo /etc/init.d/dphys-swapfile restart

Download the face recognition code examples:

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

Totally Optional: If you want a desktop GUI, install PIXEL:

sudo apt-get install --no-install-recommends xserver-xorg xinit raspberrypi-ui-mods
@mrtass
Copy link

mrtass commented Sep 17, 2019

Working Flawlessly on PI2 Model B,

real-time performance is decent using the FACEREC_FROM_WEBCAM_FASTER.py example
of course i tweaked the script and used encoding from a file using this example
webcam resolution is 640x480
processing every 5 frames for better performance.

Few Notes which might help others:
Make Sure you have Python 3.6 or later with pip3
instead of : git clone -b 'v19.6' --single-branch https://github.com/davisking/dlib.git dlib/
use : git clone https://github.com/davisking/dlib.git dlib/ thanks to @superleggera24 (no need to specify branch, i compiled the latest version and it worked fine)

i'm using the mini version of Raspian
installed the default desktop env for raspian using sudo apt-get install --no-install-recommends xserver-xorg xinit raspberrypi-ui-mods
Compiled & Installed Opencv Link here
some other dependencies were missing caused OpenCV compilation to fail. (all can be easily googled)

DLIB compiled in around 10 hours OPENCV took around 4 hours

@scottlaw
Copy link

scottlaw commented Dec 9, 2019

Same error here as well:

sudo pip3 install face_recognition
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting face_recognition
  Downloading https://files.pythonhosted.org/packages/3f/ed/ad9a28042f373d4633fc8b49109b623597d6f193d3bbbef7780a5ee8eef2/face_recognition-1.2.3-py2.py3-none-any.whl
Collecting face-recognition-models>=0.3.0 (from face_recognition)
  Downloading https://www.piwheels.org/simple/face-recognition-models/face_recognition_models-0.3.0-py2.py3-none-any.whl (100.6MB)
     |████████▉                       | 27.8MB 6.3MB/s eta 0:00:12
Requirement already satisfied: Click>=6.0 in /usr/lib/python3/dist-packages (from face_recognition) (6.6)
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages (from face_recognition) (4.0.0)
Collecting dlib>=19.7 (from face_recognition)
  Downloading https://files.pythonhosted.org/packages/05/57/e8a8caa3c89a27f80bc78da39c423e2553f482a3705adc619176a3a24b36/dlib-19.17.0.tar.gz (3.4MB)
     |████████████████████████████████| 3.4MB 2.2MB/s
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from face_recognition) (1.12.1)
ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
    face-recognition-models>=0.3.0 from https://www.piwheels.org/simple/face-recognition-models/face_recognition_models-0.3.0-py2.py3-none-any.whl#sha256=8d6b0af2e37a17120c3f13107974bc252142a4ffcb4e58eabdfcf26608e52c24 (from face_recognition):
        Expected sha256 8d6b0af2e37a17120c3f13107974bc252142a4ffcb4e58eabdfcf26608e52c24
             Got        32e569dd8706c7b501e0c8a6120c922c26d80c9b9eb5df38c658d2470862f7be

I tried --no-cache-dir option but no luck

Try this:

sudo pip3 install --no-cache-dir --upgrade face_recognition

the result:

pi@raspberrypi:~/dlib $ sudo pip3 install --no-cache-dir --upgrade face_recognition
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting face_recognition
  Downloading https://files.pythonhosted.org/packages/3f/ed/ad9a28042f373d4633fc8b49109b623597d6f193d3bbbef7780a5ee8eef2/face_recognition-1.2.3-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: dlib>=19.7 in /usr/local/lib/python3.7/dist-packages/dlib-19.18.0-py3.7-linux-armv7l.egg (from face_recognition) (19.18.0)
Requirement already satisfied, skipping upgrade: numpy in /usr/lib/python3/dist-packages (from face_recognition) (1.16.2)
Collecting Pillow (from face_recognition)
  Downloading https://files.pythonhosted.org/packages/5b/bb/cdc8086db1f15d0664dd22a62c69613cdc00f1dd430b5b19df1bea83f2a3/Pillow-6.2.1.tar.gz (37.7MB)
    100% |████████████████████████████████| 37.7MB 4.3MB/s
Collecting face-recognition-models>=0.3.0 (from face_recognition)
  Downloading https://www.piwheels.org/simple/face-recognition-models/face_recognition_models-0.3.0-py2.py3-none-any.whl (100.6MB)
    100% |████████████████████████████████| 100.6MB 5.9MB/s
Collecting Click>=6.0 (from face_recognition)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
    100% |████████████████████████████████| 81kB 2.8MB/s
Installing collected packages: Pillow, face-recognition-models, Click, face-recognition
  Running setup.py install for Pillow ... done
Successfully installed Click-7.0 Pillow-6.2.1 face-recognition-1.2.3 face-recognition-models-0.3.0

@mgallu
Copy link

mgallu commented Apr 30, 2020

One lib is obsolete. libatlas-dev is replaced by libatlas-base-dev

@HuitMahoon
Copy link

HuitMahoon commented May 16, 2020

has anyone got this to work on windows ?

you can follow the instructions here, and it works for me:
ageitgey/face_recognition#175 (comment)

@interterror
Copy link

interterror commented Aug 14, 2020

This shit doesn´t work. First, boost is missing. Then I download boost, and it doesn´t compile. Fuck linux, fuck this.

@mrtass
Copy link

mrtass commented Aug 14, 2020

@josiahcrossman
Copy link

josiahcrossman commented Aug 21, 2020

when I do:
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

i get this response, can anyone help me?
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package libatlas-dev 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
However the following packages replace it:
libatlas-base-dev

@DeSparta
Copy link

DeSparta commented Aug 21, 2020

Hi I have ran into a problem when trying to install dlib 19.6 when I try and execute the line below:
sudo python3 setup.py install --compiler-flags "-mfpu=neon"

I am getting the error message:

Traceback (most recent call last): File "setup.py", line 657, in <module> 'Topic :: Software Development', File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 145, in setup return distutils.core.setup(**attrs) File "/usr/lib/python3.7/distutils/core.py", line 148, in setup dist.run_commands() File "/usr/lib/python3.7/distutils/dist.py", line 966, in run_commands self.run_command(cmd) File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/usr/lib/python3/dist-packages/setuptools/command/install.py", line 67, in run self.do_egg_install() File "/usr/lib/python3/dist-packages/setuptools/command/install.py", line 109, in do_egg_install self.run_command('bdist_egg') File "/usr/lib/python3.7/distutils/cmd.py", line 313, in run_command self.distribution.run_command(command) File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command cmd_obj.run() File "setup.py", line 603, in run self.run_command("build") File "/usr/lib/python3.7/distutils/cmd.py", line 313, in run_command self.distribution.run_command(command) File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command cmd_obj.run() File "setup.py", line 410, in run self.build_dlib() File "setup.py", line 482, in build_dlib pip_exit = pip.main(['install', '-q', 'distro']) AttributeError: module 'pip' has no attribute 'main'

I have looked online and that refers to it being a pip version issue. I have updated my pip to the latest version but it's still not happy with something.

I also looked in the file and in the classifiers array I have noticed the programming language only goes up to Python 3.4, considering that I'm on Python 3.7 now should I add to that or does that not really matter?

@Minard-NG
Copy link

Minard-NG commented Sep 9, 2020

@melvudin
Thank you! I‘ve solved this problem successfully!

Hello @cmsfw-github . Having the same issue here on my raspbery pi. How did you resolve it?

@PhamMinhHieu1801
Copy link

PhamMinhHieu1801 commented Dec 7, 2020

How to run file facerec_on_raspberry_pi.py by using usb camera
Error when run by usb camera

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.

@PhamMinhHieu1801
Copy link

PhamMinhHieu1801 commented Dec 7, 2020

How to use usb camera with this code?

@Bonzadog
Copy link

Bonzadog commented Dec 7, 2020

@PhamMinhHieu1801 Perhpas the rapi mpeg is running, this give a strange error

Please try sudo killall raspimjpeg

@PhamMinhHieu1801
Copy link

PhamMinhHieu1801 commented Dec 8, 2020

@Bonzadog I try this but Terminal say
raspimjpeg: no process found

@shivansh7299
Copy link

shivansh7299 commented Dec 10, 2020

Traceback (most recent call last):
File "facerec_on_raspberry_pi.py", line 32, in
camera.capture(output, format="rgb")
File "/usr/lib/python3/dist-packages/picamera/camera.py", line 1423, in capture
'Timed out waiting for capture to end')
picamera.exc.PiCameraRuntimeError: Timed out waiting for capture to end

please help!!!!

@robotzero1
Copy link

robotzero1 commented Mar 1, 2021

I've just updated the tutorial using this on my blog. Seems to work fine with Pi OS LITE on a Pi Zero. dlib can be installed now without the compilation so the whole thing is much easier. Instructions here: https://robotzero.one/face-recognition-party-greeter-raspberry-pi/

@magentapenguin
Copy link

magentapenguin commented May 25, 2022

I'm having a problem where I installed Boost (The way the error says to install it), but it says that it could not find it when I run sudo python3 setup.py install --compiler-flags "-mfpu=neon".

I'm using a Raspberry Pi 4 B with python 3.9

Here is the full output:
running install running bdist_egg running build Detected Python architecture: 32bit Detected platform: linux Removing build directory /home/pi/dlib/./tools/python/build Configuring cmake ... /usr/lib/python3.9/subprocess.py:941: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used self.stdout = io.open(c2pread, 'rb', bufsize) CMake Warning (dev) in CMakeLists.txt: No project() command is present. The top-level CMakeLists.txt file must contain a literal, direct call to the project() command. Add a line of code such as project(ProjectName) near the top of the file, but after cmake_minimum_required(). CMake is pretending there is a "project(Project)" command on the first line. This warning is for project developers. Use -Wno-dev to suppress it. -- The C compiler identification is GNU 10.2.1 -- The CXX compiler identification is GNU 10.2.1 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:61 (FIND_PACKAGE) CMakeLists.txt:9 (include) CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:2018 (message): No header defined for python-py34; skipping header check (note: header-only libraries have no designated component) Call Stack (most recent call first): /home/pi/dlib/dlib/cmake_utils/add_python_module:61 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python-py34) (found suitable version "1.74.0", minimum required is "1.41.0") CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:63 (FIND_PACKAGE) CMakeLists.txt:9 (include) CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:2018 (message): No header defined for python-py35; skipping header check (note: header-only libraries have no designated component) Call Stack (most recent call first): /home/pi/dlib/dlib/cmake_utils/add_python_module:63 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python-py35) (found suitable version "1.74.0", minimum required is "1.41.0") CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:66 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python3) (found suitable version "1.74.0", minimum required is "1.41.0") CMake Warning at /usr/share/cmake-3.18/Modules/FindBoost.cmake:1187 (message): New Boost version may have incorrect or missing dependencies and imported targets Call Stack (most recent call first): /usr/share/cmake-3.18/Modules/FindBoost.cmake:1311 (_Boost_COMPONENT_DEPENDENCIES) /usr/share/cmake-3.18/Modules/FindBoost.cmake:1919 (_Boost_MISSING_DEPENDENCIES) /home/pi/dlib/dlib/cmake_utils/add_python_module:69 (FIND_PACKAGE) CMakeLists.txt:9 (include) -- Could NOT find Boost (missing: python) (found suitable version "1.74.0", minimum required is "1.41.0") -- Found PythonLibs: /usr/lib/arm-linux-gnueabihf/libpython3.9.so (found suitable version "3.9.2", minimum required is "3.4") -- ***************************************************************************************************** -- To compile Boost.Python yourself download boost from boost.org and then go into the boost root folder -- and run these commands: -- ./bootstrap.sh --with-libraries=python -- ./b2 -- sudo ./b2 install -- ***************************************************************************************************** CMake Error at /home/pi/dlib/dlib/cmake_utils/add_python_module:116 (message): Boost python library not found. Call Stack (most recent call first): CMakeLists.txt:9 (include) -- Configuring incomplete, errors occurred! See also "/home/pi/dlib/tools/python/build/CMakeFiles/CMakeOutput.log". error: cmake configuration failed!

@jmcausing
Copy link

jmcausing commented Sep 21, 2022

Can anyone update the latest instructions like the one mentioned here? https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65?permalink_comment_id=3030497#gistcomment-3030497

This instructions doesn't mention downloading and compiling opencv. @ageitgey

@mrtass
Copy link

mrtass commented Sep 21, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment