人臉識別技術已經被普遍應用在衆多場景中。今天咱們將利用Docker容器在樹莓派上快速打造一我的臉識別應用。python
樹莓派是Geek們最愛的開發板,其緣由就在於成熟的軟件生態和豐富的I/O接口,然而在樹莓派上搞深度學習應用開發並不是易事docker
下面咱們將利用Docker來構建打包應用鏡像,這樣能夠一次構建處處運行,也能夠充分利用Dockerfile自帶的分層能力,能夠方便地調整依賴包,這樣在開發部署過程當中格外高效。編程
得益於樹莓派和Docker安裝部署人臉識別開發環境很是簡單bash
# 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複製代碼
raspi-config
命令來開啓 camera 模塊docker run -it \
--name face_recognition \
--device /dev/vchiq \
registry.cn-hangzhou.aliyuncs.com/denverdino/face_recognition \
bash複製代碼
其中關鍵之處就在於將攝像頭設備/dev/vchiq
掛載到容器內部,這樣就可讓容器中的應用來拍攝照片和視頻。app
你們能夠利用 docker cp
命令,向容器中拷貝文件,好比照片,或者在容器中利用 nano 等命令來編輯代碼.框架
基於 examples/facerec_on_raspberry_pi.py
我修改了一個面部識別應用供參考,其實現以下:curl
# 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))複製代碼
首先,代碼中經過以下方法,加載指定人名的頭像照片,您能夠把本身、好基友的照片加入人臉庫。ide
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鏡像中,能夠添加修改Dockerfile,我就很少說了。
最後來曬一下個人樹莓派3配置,除了Camera以外還加裝了一個液晶顯示屏,經過GPIO驅動,能夠方便地經過編程來顯示CPU/Memory/溫度等各類信息。