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 edited

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

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 edited

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

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 edited

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 edited

Would dlib benefit from VFPV3 optimization in addition to NEON?

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