Face Recognition Raspberry Pi Zero Party Greeter

party-blowers.jpg

Building a face recognition system to recognize the faces of my friends and welcome them to a party.

There’s a few ways of building a system like this. Possibly the simplest approach is using this library – https://github.com/ageitgey/face_recognition. I’m using a Raspberry Pi Zero W with the Pi Zero Camera for the face recognition and a cheap Bluetooth module and speaker to announce friends with a greeting when a face is recognized. If the face is known to the system than the persons name is included in the greeting.

The instructions below are a modified version of the instructions here: https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65 with corrections from the comments and other tips I’ve read elsewhere. This set up is optimized for the Pi Zero W and its BCM2835 processor. The more powerful Raspberries should be set up in an alternative way.

The instructions are much easier to work with if you connect to the Pi Zero remotely over SSH. Read the tutorial Headless Install of Raspbian Stretch Lite for help. There are a lot of commands and it’s much easier to paste them from your browser rather than type them directly.

Raspbian Installation

Download the Lite version here: https://www.raspberrypi.org/downloads/raspbian/ and unzip it.
Burn the img file to a microSD card using Etcher – https://etcher.io/
Set up SSH access as above or access the Pi directly with a keyboard and HDMI monitor

Run:

sudo raspi-config

and configure the basics:

  • Enable the Raspberry Pi camera (under Interfacing Options)
  • Set gpu memory split to 16 (under Advanced)

Reboot and then run the following commands to update the Pi:

sudo apt-get update
sudo apt-get upgrade

Install the necessary software. I’m not sure everything below is needed but install it all anyway!

sudo apt-get install build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-dev \
    libatlas-base-dev \
    liblapack-dev \
    libatlas3-base \
    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

This might make the image processing faster:

sudo apt-get install libopenblas-dev

Camera support:

sudo apt-get install python3-picamera
sudo pip3 install --upgrade picamera[array]

 

dlib Installation

The face recognition uses the dlib library – https://github.com/davisking/dlib . The Debian version of dlib is very out of date so you need to compile this from source to get a new version.

First you need to change the swapfile setting so dlib will compile:

sudo nano /etc/dphys-swapfile

In the text editor that opens, comment the line CONF_SWAPSIZE=100 and uncomment the line  CONF_SWAPFACTOR=2 so it looks like this:

Edit Swapfile

Press ctrl-x and then y to save and exit nano. Run the following command to reinitialize the swapfile:

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

Download dlib. First check the releases page to get the latest version – https://github.com/davisking/dlib/releases:

mkdir -p dlib
git clone -b 'v19.16' --single-branch https://github.com/davisking/dlib.git dlib/
cd ./dlib

Compile and install dlib:

sudo python3 setup.py install --compiler-flags "-mcpu=arm1176jzf-s -mfpu=vfp"

Depending on the microSD card, compiling takes from about 5 hours to over 12 hours on the Pi Zero. If you are connecting over SSH and lose the connection you can run pip3 freeze to check dlib was successfully installed:

pip3 freeze command

Python Library Installation

Install the python face recognition library (https://github.com/ageitgey/face_recognition)

sudo pip3 install face_recognition

Revert the swap file settings now that dlib is installed:

sudo nano /etc/dphys-swapfile

Edit the file so it looks like this:

Revert Swapfile

Reinitialize the swapfile again:

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

Download the face recognition code examples:

git clone --single-branch https://github.com/ageitgey/face_recognition.git

Run a quick test:

cd ./face_recognition/examples
python3 facerec_on_raspberry_pi.py

The test script above (you can see it here)  loads an jpg of Barack Obama and encodes it.

obama_image = face_recognition.load_image_file("obama_small.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

The script then runs a loop that takes a frame from the camera stream and looks for faces in that frame. If it finds a face it then checks the encoded version of the face against the encoded face of Obama and prints a message. If you hold up an image of Obama on your phone to the camera you should see the I see someone named Barack Obama message!

To add more than one person to the system, put one image per person in a folder named ‘friends’ and upload this to the same directory as the test script (./face_recognition/examples). Each file should use the persons name as the filename. Then make the following changes to the test script:



#new variables
known_person=[]
known_image=[]
known_face_encoding=[]

#Loops to add images in friends folder
for file in os.listdir("friends"):
    try:
        #Extracting person name from the image filename eg: david.jpg
        known_person.append(file.replace(".jpg", ""))
        file=os.path.join("friends/", file)
        known_image = face_recognition.load_image_file(file)
        known_face_encoding.append(face_recognition.face_encodings(known_image)[0])
    except Exception as e:
        pass

#change recognition loop to
while True:
    print("Capturing image.")
    # Grab a single frame of video from the RPi camera as a numpy array
    camera.capture(output, format="rgb")

    # Find all the faces and face encodings in the current frame of video
    face_locations = face_recognition.face_locations(output)
    print("Found {} faces in image.".format(len(face_locations)))
    face_encodings = face_recognition.face_encodings(output, face_locations)

    # Loop over each face found in the frame to see if it's someone we know.
    for face_encoding in face_encodings:

        match = face_recognition.compare_faces(known_face_encoding, face_encoding)
        matches=np.where(match)[0] #Checking which image is matched
        if len(matches)>0:
          name = str(known_person[matches[0]])
        else:
          name = ""

        print("I see someone named {}!".format(name))

Or download the entire script from here: facerec_on_raspberry_pi_group.zip and upload it to the same directory.

If you run this new script and point the camera at the the the same people you just uploaded to the friends folder the Pi should recognize them and print a message. If there are lots of people it takes a while before the system is ready.

To make the system a little more fun you can add Bluetooth audio output to your headless Pi so it announces the entrance of your friends to the party, For a full explanation of connecting a Bluetooth speaker and voice synthesis see this post – https://robotzero.one/pi-zero-w-bluetooth-voice/

You can then update the script by adding:

os.system("espeak  'Hello {}' -ven+f5 -k5 -s150 --stdout | aplay -D bluealsa:HCI=hci0,DEV=7B:08:72:E8:DD:13,PROFILE=a2dp ".format(name))

above the line:

print("I see someone named {}!".format(name))

and the Pi Zero will say the name of anybody it recognizes.

 

Live Test

My friends aren’t too keen about appearing on YouTube so I bought a Spanish celebrity magazine and selected some pages to use in a live test of  this system. I found other photos of the same people from the internet to train the face recognition.

Here’s the guest list for my party:

  • Pilar
  • Concha
  • Paula
  • Kendall
  • Catherine
  • Soraya
  • Jesus
  • Antonio
  • Letizia
  • Melanie
  • Julia

All the images above are random images taken from the internet. I uploaded these to the friends folder and ran the facerec_on_raspberry_pi_group.py script. In most of the tests, only one of the people wasn’t recognized.

More Information

This project on YouTube – https://www.youtube.com/watch?v=2qDCxVRbge0
Pi compiler options: https://www.raspberrypi.org/forums/viewtopic.php?t=11629

Pre-built Pi Zero W Img Download

If you just want to quickly test whether this face recognition system will work on your Pi Zero W and don’t want to spend ages compiling etc I’ve created an image of Raspbian with everything installed so that you can flash it to a blank microSD card with Etcher and be running in 10 minutes. Download the img file from here: face-recognition-zero-w-img.zip

Leave a Reply

Your email address will not be published. Required fields are marked *

scroll to top