Create a gist now

Instantly share code, notes, and snippets.

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 "-mpfu=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

tilda commented Apr 22, 2017

Additional note:

If you are installing PIXEL, you should probably up your memory split to 64. 16 will not give much responsiveness.

Hi,
Although I go through all your Rpi manual installation, at the end, when i try python3 facerec_on_raspberry_pi.py, I've got:
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 "lib/face_recognition/examples/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.

QuentinCG commented May 10, 2017

@samiturki The error is quite clear, no?

PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.

https://www.raspberrypi.org/learning/getting-started-with-picamera/worksheet/

QuentinCG commented May 18, 2017

This is very very slow on my RPi3:

  • 20-30sec to initialize the facerec_on_raspberry_pi.py example <-- This is fine for me
  • 5-10sec to check each image <-- This is problematic

Is there a way to check images faster? (it is not real time at all... 1-2sec would be ok but 5-10sec is enormous)

jyurek commented May 19, 2017

I'd love to know if there's a good way to speed this up, too.

@jyurek try http://vmlaker.github.io/mpipe/
Utilizing all 4 cores of the Pi via process pipelines
Plus smaller resolution of 1080p/3

recently installed dlib with face recognition following this guide on my raspberry pi3 and when i try and run example programmes i am getting the message:
'File "face-recognition.py", line 50, in <module
import dlib
ImportError: dynamic module does not define init function (initdlib)'
any idea what is causing this? Thanks

masoudr commented Jun 26, 2017

Thanks, @vchatterji but can you explain a little more about how to use along with face_recognition examples?

tedm commented Jul 17, 2017

how would one add biden.jpg (which is already there) as the 'match[1]' in the example?

Good evening I have a problem when I install dlib v19.4: it always stop at 93% is that an error or Do i really need a really long patience thanks for the reply

cimzar commented Jul 26, 2017

is there any way to send an output signal to one of the gpio pins on raspberrypi3 when there is a match in a picture?

hcwoo2 commented Jul 26, 2017

@roei03jen Did you make sure to complete this step (make sure that you saved):

sudo nano /etc/dphys-swapfile

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

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

fumufu commented Aug 1, 2017

It's working fine on a RPi 3. I tested it successfully following the instructions above.
But it's quite slow to output the result on a RPi3, compared with the one in a VM on an Intel i5 PC.
So I am not sure if I can use the picamera to trace the face real-timely.

CosminNechifor commented Aug 16, 2017

When I try to install face_recognition using:

sudo pip3 install face_recognition

I get this:

Exception:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/response.py", line 186, in read
data = self._fp.read(amt)
File "/usr/lib/python3.4/http/client.py", line 500, in read
return super(HTTPResponse, self).read(amt)
File "/usr/lib/python3.4/http/client.py", line 539, in readinto
n = self.fp.readinto(b)
File "/usr/lib/python3.4/socket.py", line 371, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.4/ssl.py", line 745, in recv_into
return self.read(nbytes, buffer)
File "/usr/lib/python3.4/ssl.py", line 617, in read
v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 290, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/lib/python3/dist-packages/pip/req.py", line 1198, in prepare_files
do_download,
File "/usr/lib/python3/dist-packages/pip/req.py", line 1376, in unpack_url
self.session,
File "/usr/lib/python3/dist-packages/pip/download.py", line 572, in unpack_http_url
download_hash = _download_url(resp, link, temp_location)
File "/usr/lib/python3/dist-packages/pip/download.py", line 433, in _download_url
for chunk in resp_read(4096):
File "/usr/lib/python3/dist-packages/pip/download.py", line 421, in resp_read
chunk_size, decode_content=False):
File "/usr/lib/python3/dist-packages/urllib3/response.py", line 256, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/usr/lib/python3/dist-packages/urllib3/response.py", line 201, in read
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out.

Storing debug log for failure in /home/pi/.pip/pip.log

Can someone give me a hint?

UPDATE: it worked after I installed once again dlib

When I try running sudo python3 setup.py install --compiler-flags "-mpfu=neon", I get this error: error: option --compiler-flags not recognized, I also find no mention of this option in the setup.py file?

has anyone got this to work on windows ?

When I try running sudo python3 setup.py install --compiler-flags "-mpfu=neon", I get this error: error: option --compiler-flags not recognized, I also find no mention of this option in the setup.py file?

CosminNechifor commented Aug 26, 2017

Try installing dlib using:

sudo pip3 install dlib

It took me ~20 min, because I have a bad internet connection. And the face_recognition lib works fine.

Can anyone share some benchmarks like what FPS are they able to achieve with this? And on what resolution?

skerit commented Sep 7, 2017

The "mpfu" error is because it's a typo. It should be -mfpu=neon. F before p.

It'll compile without any problems then, but I haven't really noticed any performance boost.

i try with mfpu=neon and it works
error: unrecognized command line option ‘-mpfu=neon’;
did you mean
==> ‘-mfpu=neon’?

ss6054 commented Oct 14, 2017

Can it run at Raspberry Pi model B+?

noblekoshy commented Oct 19, 2017

Would dlib benefit from VFPV3 optimization in addition to NEON?

I'm having a problem installing the gui on raspberry pi pls help asap

Wondering if it can work with an USB camera ?

osmadja commented Nov 9, 2017

Yes, it is working with usb Camera on the PI 3. I currently use a logitech c920 and it is working. But even reducing image resolution to 320x240, the performance is bad : the face detection is 3s / frame and 1.2 to compute encodings. So it is more than 4s per frame. Anyone managed better performance ? using the PI 3 gpu ?

@osmadja. 👍

cd ./face_recognition/examples

python3 facerec_from_webcam_faster.py

I was running this code with a USB webcam and got an error.

So I want to do a real-time face recognition (As shown in this link -> https://cloud.githubusercontent.com/assets/896692/24430398/36f0e3f0-13cb-11e7-8258-4d0c9ce1e419.gif ) with a USB webcam (Logitech QuickCam Pro 9000) that supports 1600x1200 resolution

Are you using it for real-time the same way like I do ?

Hi! When I try to run this command: sudo python3 setup.py install --compiler-flags '-mpfu=neon' on raspberrypi 3b,I get errors as follows:

Checking .pth file support in /usr/local/lib/python3.4/dist-packages/
/usr/bin/python3 -E -c pass
TEST PASSED: /usr/local/lib/python3.4/dist-packages/ appears to support .pth files
running bdist_egg
running build
Detected Python architecture: 32bit
Detected platform: linux
Removing build directory /usr/local/dlib/./tools/python/build
Configuring cmake ...
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is unknown
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- broken
CMake Error at /usr/share/cmake-3.0/Modules/CMakeTestCXXCompiler.cmake:54 (message):
The C++ compiler "/usr/bin/c++" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: /usr/local/dlib/tools/python/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTryCompileExec3550236372/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec3550236372.dir/build.make
CMakeFiles/cmTryCompileExec3550236372.dir/build
make[1]: Entering directory
'/usr/local/dlib/tools/python/build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report
/usr/local/dlib/tools/python/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building CXX object
CMakeFiles/cmTryCompileExec3550236372.dir/testCXXCompiler.cxx.o
/usr/bin/c++ -mpfu=neon -o
CMakeFiles/cmTryCompileExec3550236372.dir/testCXXCompiler.cxx.o -c
/usr/local/dlib/tools/python/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
c++: error: unrecognized command line option ‘-mpfu=neon’
CMakeFiles/cmTryCompileExec3550236372.dir/build.make:57: recipe for target
'CMakeFiles/cmTryCompileExec3550236372.dir/testCXXCompiler.cxx.o' failed
make[1]: ***
[CMakeFiles/cmTryCompileExec3550236372.dir/testCXXCompiler.cxx.o] Error 1
make[1]: Leaving directory
'/usr/local/dlib/tools/python/build/CMakeFiles/CMakeTmp'
Makefile:118: recipe for target 'cmTryCompileExec3550236372/fast' failed
make: *** [cmTryCompileExec3550236372/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
-- Configuring incomplete, errors occurred!
See also "/usr/local/dlib/tools/python/build/CMakeFiles/CMakeOutput.log".
See also "/usr/local/dlib/tools/python/build/CMakeFiles/CMakeError.log".
error: cmake configuration failed!

What should I do to correct these errors? Thank you for your reply!****

RuntimeError: Error deserializing object of type short

how can i fix this error, can you help me

When I run ¨sudo pip3 install face-recognition¨, I recieve this rather long error:

Exception:
Traceback (most recent call last):
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 417, in wrap_socket
cnx.do_handshake()
File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1426, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1149, in _raise_ssl_error
raise WantReadError()
OpenSSL.SSL.WantReadError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 350, in _make_request
self._validate_conn(conn)
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 837, in validate_conn
conn.connect()
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connection.py", line 323, in connect
ssl_context=context)
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/ssl
.py", line 324, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 421, in wrap_socket
raise timeout('select timed out')
socket.timeout: select timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 594, in urlopen
chunked=chunked)
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 353, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 313, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='www.piwheels.hostedpi.com', port=443): Read timed out. (read timeout=15)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 353, in run
wb.build(autobuilding=True)
File "/usr/lib/python3/dist-packages/pip/wheel.py", line 749, in build
self.requirement_set.prepare_files(self.finder)
File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 554, in _prepare_file
require_hashes
File "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 278, in populate_link
self.link = finder.find_requirement(self, upgrade)
File "/usr/lib/python3/dist-packages/pip/index.py", line 465, in find_requirement
all_candidates = self.find_all_candidates(req.name)
File "/usr/lib/python3/dist-packages/pip/index.py", line 423, in find_all_candidates
for page in self._get_pages(url_locations, project_name):
File "/usr/lib/python3/dist-packages/pip/index.py", line 568, in _get_pages
page = self._get_page(location)
File "/usr/lib/python3/dist-packages/pip/index.py", line 683, in _get_page
return HTMLPage.get_page(link, session=self.session)
File "/usr/lib/python3/dist-packages/pip/index.py", line 792, in get_page
"Cache-Control": "max-age=600",
File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 501, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python3/dist-packages/pip/download.py", line 386, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 423, in send
timeout=timeout
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 643, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

Does anyone know how to fix this?
Thanks!

melvudin commented Dec 9, 2017

@cmsfw-github
Use -mfpu instead of -mpfu

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

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