我是如何用树莓派 + Docker 轻松实现人脸识别应用的?
Posted ITPUB技术小栈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我是如何用树莓派 + Docker 轻松实现人脸识别应用的?相关的知识,希望对你有一定的参考价值。
很多包需要下载编译,以孱弱的Raspberry Pi编译应用,需要极大的耐心。
然而开源的深度学习框架很多,不同类库的依赖不同,有些会互相冲突,比如有些需要Python 2.7,有些则依赖 3.x。虽然我们可以用virtualenv对Python环境进行隔离,但是对于一些系统级的依赖冲突就不好办了。在漫长构建中遇到依赖导致编译失败,让人非常有挫败感。
如果需要在另外一块板上部署相同应用,整个过程需要重新来过。
下面我们将利用Docker来构建打包应用镜像,这样可以一次构建到处运行,也可以充分利用Dockerfile自带的分层能力,可以方便地调整依赖包,这样在开发部署过程中格外高效。
# 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
docker run -it
--name face_recognition
--device /dev/vchiq
registry.cn-hangzhou.aliyuncs.com/denverdino/face_recognition
bash
# 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 this 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))
load_face_encoding("Yi Li", "yili.jpg")
face_locations = face_recognition.face_locations(output)
...
face_encodings = face_recognition.face_encodings(output, face_locations)
matches = face_recognition.face_distance(known_face_encodings, face_encoding)
# 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!
...
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
以上是关于我是如何用树莓派 + Docker 轻松实现人脸识别应用的?的主要内容,如果未能解决你的问题,请参考以下文章