Community

Blog
×
Community Blog Implementing Facial Recognition Applications with Raspberry Pi and Docker

Implementing Facial Recognition Applications with Raspberry Pi and Docker

This article illustrates how to simplify software development for a facial recognition application in Raspberry Pi using Docker.

Facial recognition technology is already supporting a lot of real-world applications. Today, we will learn how to use Docker containers to quickly create a facial recognition application on Raspberry Pi.

I have used the ageitgey/face_recognition open-source framework, with facial recognition functions supported by dlib (Deep Metric Learning). According to the Labeled Faces in the Wild benchmark test, dlib has an accuracy of 99.38%. Face_recognition application development is quite straightforward. You can create a facial recognition application with Raspberry Pi support using just a few Python commands.

You can find a guide for Raspberry Pi 2+ platform face_recognition installation here: gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65

rasp

Raspberry Pi is a development console beloved by software development community for its sophisticated software ecosystem and a wide range of I/O interfaces. However, it is difficult to study application development in depth on Raspberry Pi; we have listed below some of these complexities:

  1. To study application development, you must download and compile many packages using Raspberry Pi's weak compiler apps. This is time intensive, and you will require a lot of patience to accomplish it successfully.
  2. There are many open-source in-depth learning frameworks with different libraries and dependencies that may conflict with each other. For instance, some require Python 2.7, while others depend on Python 3.x. Although we can use virtualenv to isolate the Python environment, this will not solve some system-level dependency conflicts. In the long and slow build process, dependencies can lead to compiling failures and a great deal of frustration.
  3. To deploy the same application on another console, you will have to repeat the entire process.

In the following article, I will use Docker container to create and package an application image. This allows us to build and run the application in a single process while taking full advantage of the layering capabilities provided by Dockerfile for convenient adjustment of the dependency packages. This way, the development and deployment processes will be extremely efficient.

Deploying a Facial Recognition Application on Raspberry Pi


Thanks to Raspberry Pi and Docker, it is effortless to install and deploy a facial recognition development environment.

First, you need to install the latest version of Raspbian on Raspberry Pi 3.

Run the following command to install the latest Docker Engine community version:

# Install Docker
curl -sSL https://get.docker.com | sh

# Add pi to Docker group
sudo usermod pi -aG docker

# config cgroup for Docker
echo Adding " cgroup_enable=cpuset cgroup_enable=memory" to /boot/cmdline.txt
sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt
# if you encounter problems, try changing cgroup_memory=1 to cgroup_enable=memory.
orig="$(head -n1 /boot/cmdline.txt) cgroup_enable=cpuset cgroup_memory=1"
echo $orig | sudo tee /boot/cmdline.txt

sudo reboot

Install Raspberry Camera. I have used the Camera Module2, with the blue line indicating the Ethernet interface direction. Use the raspi-config command to start the camera module.

Develop and run the face_recognition application in a container. We can use the following command to start the container. This includes the complete face_recognition development environment and sample application. I will discuss the specific image details below.

docker run -it \
    --name face_recognition \
    --device /dev/vchiq \
      registry.cn-hangzhou.aliyuncs.com/denverdino/face_recognition \
      bash

Here, the key is to mount the camera device /dev/vchiq in the container, so we can use the application in the container to take photos and videos. You can use the docker cp command to copy files (such as photos) in the container, or use nano or another command in the container to edit the code.

Analyze the Facial Recognition Application


Based on examples/facerec_on_raspberry_pi.py, I have modified the facial recognition app as follows to serve as a reference:
# This is a demo of running face recognition on a Raspberry Pi.
# This program will print out the names of anyone it recognizes to the console.

# To run this, you need a Raspberry Pi 2 (or greater) with face_recognition and
# the picamera[array] module installed.
# You can follow these installation instructions to get your RPi set up:
# https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65

import face_recognition
import picamera
import numpy as np

known_face_encodings = []
names = []

def load_face_encoding(name, file_name):
    image = face_recognition.load_image_file(file_name)
    face_encoding = face_recognition.face_encodings(image)[0]
    known_face_encodings.append(face_encoding)
    names.append(name)

# Get a reference to the Raspberry Pi camera.
# If this fails, make sure you have a camera connected to the RPi and that you
# enabled your camera in raspi-config and rebooted first.
camera = picamera.PiCamera()
camera.resolution = (320, 240)
output = np.empty((240, 320, 3), dtype=np.uint8)

# Load a sample picture and learn how to recognize it.
print("Loading known face image(s)")
load_face_encoding("Yi Li", "yili.jpg")
load_face_encoding("Zhang Kai", "zhangkai.jpg")
load_face_encoding("Che Yang", "cheyang.jpg")

# Initialize some variables
face_locations = []
face_encodings = []

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:
        # See if the face is a match for the known face(s)
        matches = face_recognition.face_distance(known_face_encodings, face_encoding)
        name = "<Unknown Person>"
        
        min_distance = min(matches)
        if min_distance < 0.6:
            i = matches.argmin()
            name = names[i]
            
        
        print("I see someone named {}!".format(name))

First, in the code, use the following method to load a headshot for the specified name. You can add photos of yourself and friends to the face library.

load_face_encoding("Yi Li", "yili.jpg")

After that, use the camera to continuously take photos and detect facial information in the photos using the following method:

face_locations = face_recognition.face_locations(output)
...
face_encodings = face_recognition.face_encodings(output, face_locations)

Then, compare the facial information detected by the camera to the known facial information. If their similarity exceeds the set threshold, the application returns the name of best match. This completes the development of a simple facial recognition app. That was easy, right?

matches = face_recognition.face_distance(known_face_encodings, face_encoding)

The operation results are as follows:

# python3 facerec_on_raspberry_pi.py 
Loading known face image(s)
Found 0 faces in image.
Capturing image.
Found 0 faces in image.
Capturing image.
Found 1 faces in image.
I see someone named Yi Li!
...

These results are just what we expected. However, due to Raspberry Pi's limited processing capabilities, the response is very slow, far from real time. It takes several seconds to recognize a face. However, we can still use this app for simple scenarios. So feel free to develop an application that blows our minds.

If you need to customize your facial recognition application, you can find the Dockerfile on https://github.com/denverdino/face_recognition_pi and create a complete application that meets your needs.

FROM resin/raspberry-pi-python:3
COPY pip.conf /root/.pip/pip.conf
RUN apt-get -y update
RUN apt-get install -y --fix-missing \
    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 \
    zip \
    && apt-get clean && rm -rf /tmp/* /var/tmp/*
RUN python3 -m ensurepip --upgrade && pip3 install --upgrade picamera[array] dlib

# The rest of this file just runs an example script.

# If you wanted to use this Dockerfile to run your own app instead, maybe you would do this:
# COPY . /root/your_app_or_whatever
# RUN cd /root/your_app_or_whatever && \
#     pip3 install -r requirements.txt
# RUN whatever_command_you_run_to_start_your_app

RUN git clone --single-branch https://github.com/ageitgey/face_recognition.git
RUN cd /face_recognition && \
    pip3 install -r requirements.txt && \
    python3 setup.py install

CMD cd /face_recognition/examples && \
    python3 recognize_faces_in_pictures.py

If you want to, you can package your application in a Docker image and add and modify Dockerfiles, but I will not describe that here.

Finally, I posted my Raspberry 3 configuration. In addition to the camera, I added an LCD screen and used the GPIO driver to conveniently program the application to display CPU, Memory, temperature, and other information.
rasp2

Conclusion

Today, we learned how to run a facial recognition application on Raspberry Pi. You can find the code I used in the example on https://github.com/denverdino/face_recognition_pi.

Container technology is becoming crucial in the IoT, edge computing, and other scenarios. The use of this technology greatly simplifies lifecycle management for smart device applications.

In 2017, we have witnessed the rapid development of container technology, with a consensus forming in the ecosystem around Kubernetes, Containerd/OCI, and other container technology standards. This will lead to more innovation in application development. In 2018, we will not only see the wide application of containers in enterprise user production environments but also encounter container technology everywhere. I think we will be in for some great surprises.

Learn more about Alibaba Cloud products and solutions at www.alibabacloud.com.

0 0 0
Share on

Alibaba Clouder

553 posts | 51 followers

You may also like

Comments