樹莓派 + Docker - 輕鬆實現人臉識別應用

人臉識別技術已經被普遍應用在衆多場景中。今天咱們將利用Docker容器在樹莓派上快速打造一我的臉識別應用。

15153134217709

人臉識別技術已經被普遍應用在衆多場景中。今天咱們將利用Docker容器在樹莓派上快速打造一我的臉識別應用。python

本文使用 https://github.com/ageitgey/face_recognition 開源框架,基於 dlib (Deep Metric Learning) 支持人臉識別功能。dlib 在Labeled Faces in the Wild 測試基準上的準確率達到 99.38%。face_recognition的應用開發極爲簡單,只用幾行 Python 命令行就能夠輕鬆實現人臉識別應用,並且也提供了樹莓派的支持。git

在Raspberry Pi 2+ 平臺安裝face_recognition的指南以下
https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
github

樹莓派是Geek們最愛的開發板,其緣由就在於成熟的軟件生態和豐富的I/O接口,然而在樹莓派上搞深度學習應用開發並不是易事docker

  1. 不少包須要下載編譯,以孱弱的Raspberry Pi編譯應用,須要極大的耐心。
  2. 然而開源的深度學習框架不少,不一樣類庫的依賴不一樣,有些會互相沖突,好比有些須要Python 2.7,有些則依賴 3.x。雖然咱們能夠用virtualenv對Python環境進行隔離,可是對於一些系統級的依賴衝突就很差辦了。在漫長構建中遇到依賴致使編譯失敗,讓人很是有挫敗感。
  3. 若是須要在另一塊板上部署相同應用,整個過程須要從新來過。

下面咱們將利用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複製代碼
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!
...複製代碼

效果符合預期,可是受限於樹莓派的處理能力,還遠遠達不到實時的效果,識別出人臉須要幾秒的延遲。可是已經能夠應用於一些簡單的場景了,你們本身去開腦洞本身開發吧。

你們若是須要定製本身的人臉識別應用,能夠從 https://github.com/denverdino/face_recognition_pi 得到相關的Dockerfile,來根據本身的須要構建一個完整的應用

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/溫度等各類信息。

IMG_4519

總結

容器技術已經愈來愈多運用於IoT、邊緣計算等場景,利用容器能夠極大地簡化智能設備的應用生命週期管理。今天咱們演示了一個運行在樹莓派上的人臉識別應用。本文實例代碼能夠從 https://github.com/denverdino/face_recognition_pi 獲取。

2017咱們見證了容器技術的快速發展,Kubernetes,Containerd/OCI等容器技術標準獲得了生態的共識,這也將催生更多的應用創新。2018咱們不但能夠看見容器在企業用戶的生產環境中被普遍應用,容器技術也將無處不在,給咱們更多的驚喜。

原文連接

相關文章
相關標籤/搜索