Skip to content

Instantly share code, notes, and snippets.

@ageitgey
Last active January 25, 2024 08:37
Star You must be signed in to star a gist
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
@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.

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