Skip to content

Instantly share code, notes, and snippets.

@ageitgey
Last active October 16, 2024 03:49
Show Gist options
  • Save ageitgey/1ac8dbe8572f3f533df6269dab35df65 to your computer and use it in GitHub Desktop.
Save ageitgey/1ac8dbe8572f3f533df6269dab35df65 to your computer and use it in GitHub Desktop.

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
@HuitMahoon
Copy link

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

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 via email

@josiahcrossman
Copy link

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?

@cmnwankw-cmu-F24
Copy link

@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

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

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

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

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 via email

@mfwz247
Copy link

mfwz247 commented Apr 1, 2023

If you already built dlib 19.6 and found out latest face_recognition requires 19.7 or above just do
pip install face_recognition==1.0.0
It requires dlib>=19.5

@hosseindamavandi
Copy link

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!

I've encountered with same problem.
I'm using Raspberry Pi 3 B with python 3.9

@ovidiumoise
Copy link

Hello, is it still relevant in 2024?
I have Raspberry Pi 4 Model B 4G or Raspberry Pi 5 8GB with latest OS (Bookworm).
I tried at least 3 or 4 other alternatives and I'm sick of issues and errors. Did it anyone made it lately?
I will try today on Raspberry Pi4 Model B 4G.

@mardelmariam
Copy link

mardelmariam commented Jun 20, 2024

The library (face_recognition) continues working well for me on a Raspberry Pi 5 with the Raspbian Bookworm OS. However, all steps change due to various changes in the very OS. As I compiled OpenCV first, I changed and prepared many system packages, and prevented me further headaches. After that, I was able to install dlib and face recognition using pip, both at the most recent versions.

@OmarAKhan1
Copy link

The library (face_recognition) continues working well for me on a Raspberry Pi 5 with the Raspbian Bookworm OS. However, all steps change due to various changes in the very OS. As I compiled OpenCV first, I changed and prepared many system packages, and prevented me further headaches. After that, I was able to install dlib and face recognition using pip, both at the most recent versions.

Hi am currently working on a Raspberry Pi 4 with Raspbian Bookworm OS. I am struggling with finding the correct steps to download dlib and face recognition (I successfully downloaded OpenCV). Can you please give me a quick rundown on how you downloaded dlib and face recognition because whenever I try installing them I keep get an error about an "externally managed environment." Any help or guidance would be greatly appreciated! Thank you!!

@Bonzadog
Copy link

Bonzadog commented Jun 22, 2024 via email

@Bonzadog
Copy link

Bonzadog commented Jun 29, 2024 via email

@camoverride
Copy link

Anyone have any luck installing face_recognition on a Raspberry Pi 5 (running Bookworm)? All the instructions on the internet are out of date...

@mardelmariam
Copy link

For anyone who is struggling with the installation of the required libraries: you will need to compile OpenCV, so the OS will run C/C++ files instead of running OpenCV with the Python files. A guide for this step is here. If some errors appear, Google is your friend. After installing OpenCV, you may install dlib, and that should work.

@mardelmariam
Copy link

mardelmariam commented Sep 12, 2024 via email

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