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
@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