树莓派 + Docker - 轻松实现人脸识别应用

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 人脸识别技术已经被广泛应用在众多场景中。今天我们将利用Docker容器在树莓派上快速打造一个人脸识别应用。

15153134217709

人脸识别技术已经被广泛应用在众多场景中。今天我们将利用Docker容器在树莓派上快速打造一个人脸识别应用。

本文使用 https://github.com/ageitgey/face_recognition 开源框架,基于 dlib (Deep Metric Learning) 支持人脸识别功能。dlib 在Labeled Faces in the Wild 测试基准上的准确率达到 99.38%。face_recognition的应用开发极为简单,只用几行 Python 命令行就可以轻松实现人脸识别应用,而且也提供了树莓派的支持。

在Raspberry Pi 2+ 平台安装face_recognition的指南如下
https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65

树莓派是Geek们最爱的开发板,其原因就在于成熟的软件生态和丰富的I/O接口,然而在树莓派上搞深度学习应用开发并非易事

  1. 很多包需要下载编译,以孱弱的Raspberry Pi编译应用,需要极大的耐心。
  2. 然而开源的深度学习框架很多,不同类库的依赖不同,有些会互相冲突,比如有些需要Python 2.7,有些则依赖 3.x。虽然我们可以用virtualenv对Python环境进行隔离,但是对于一些系统级的依赖冲突就不好办了。在漫长构建中遇到依赖导致编译失败,让人非常有挫败感。
  3. 如果需要在另外一块板上部署相同应用,整个过程需要重新来过。

下面我们将利用Docker来构建打包应用镜像,这样可以一次构建到处运行,也可以充分利用Dockerfile自带的分层能力,可以方便地调整依赖包,这样在开发部署过程中格外高效。

树莓派上部署人脸识别应用

得益于树莓派和Docker安装部署人脸识别开发环境非常简单

  • 在 Raspberry PI 3 安装最新的 Raspbian
  • 执行如下命令安装最新的 Docker Engine 社区版
# 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
  • 安装 Raspberry Camera ,我用的是Camera Module2 注意蓝色胶带对着以太网接口方向。并通过 raspi-config 命令来开启 camera 模块
  • 在容器中开发、运行face_recognition应用,我们可以利用如下的命令来启动容器。其包含了face_recognition 的完整开发环境和示例应用。下文会介绍镜像的具体信息
docker run -it \
    --name face_recognition \
    --device /dev/vchiq \
      registry.cn-hangzhou.aliyuncs.com/denverdino/face_recognition \
      bash

其中关键之处就在于将摄像头设备/dev/vchiq挂载到容器内部,这样就可以让容器中的应用来拍摄照片和视频。

大家可以利用 docker cp 命令,向容器中拷贝文件,比如照片,或者在容器中利用 nano 等命令来编辑代码.

人脸识别应用解析

基于 examples/facerec_on_raspberry_pi.py 我修改了一个面部识别应用供参考,其实现如下:

# 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!
...

效果符合预期,但是受限于树莓派的处理能力,还远远达不到实时的效果,识别出人脸需要几秒的延迟。但是已经可以应用于一些简单的场景了,大家自己去开脑洞自己开发吧。

大家如果需要定制自己的人脸识别应用,可以从 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我们不但可以看见容器在企业用户的生产环境中被广泛应用,容器技术也将无处不在,给我们更多的惊喜。

目录
相关文章
|
2月前
|
存储 Java Docker
使用Docker部署Java应用的最佳实践
使用Docker部署Java应用的最佳实践
|
4天前
|
运维 开发者 Docker
Docker容器化技术在运维中的应用实践
【8月更文挑战第27天】本文旨在探讨Docker容器化技术如何在现代运维工作中发挥核心作用,通过深入浅出的方式介绍Docker的基本概念、优势以及实际应用场景。文章将结合具体案例,展示如何利用Docker简化部署流程、提高资源利用率和加强应用的可移植性。读者将获得对Docker容器技术在实际运维中应用的全面认识,并能够理解其在提升运维效率与质量方面的重要性。
|
11天前
|
Kubernetes Shell 测试技术
在Docker中,可以在一个容器中同时运行多个应用进程吗?
在Docker中,可以在一个容器中同时运行多个应用进程吗?
|
11天前
|
存储 Kubernetes 监控
在Docker中,很多应用容器都是默认后台运行的,怎么查看它们的输出和日志信息?
在Docker中,很多应用容器都是默认后台运行的,怎么查看它们的输出和日志信息?
|
13天前
|
前端开发 PHP 开发者
React Server Component 使用问题之怎么使用Docker运行PHP应用
React Server Component 使用问题之怎么使用Docker运行PHP应用
|
2月前
|
运维 Linux Docker
Docker Hub 镜像拉取超时:1Panel面板中应用安装失败的临时解决方案
`1Panel` 是一款Linux服务器管理面板,提供图形化界面便于运维。若在安装应用时遇到问题,可以尝试使用Docker加速地址 <https://docker.1panel.live/>,但请注意该地址可能不稳定且仅限境内使用。首届 `1Panel` 运维节将在2024年7月24日举行,更多信息可访问官方链接。
1412 1
Docker Hub 镜像拉取超时:1Panel面板中应用安装失败的临时解决方案
|
3天前
|
Docker Python 容器
5 分钟,教你用 Docker 部署一个 Python 应用!
5 分钟,教你用 Docker 部署一个 Python 应用!
|
3天前
|
Kubernetes Linux 开发者
【实战秘籍】从零开始:用.NET与Docker打造现代化容器化应用之旅
【8月更文挑战第28天】本文详细介绍如何使用 .NET 框架构建并部署 Docker 容器化应用程序,涵盖环境搭建、项目创建、Dockerfile 编写等关键步骤。首先安装必要软件,如 Visual Studio 2022 及 Docker Desktop。接着创建 .NET Core 控制台应用,并在项目根目录编写 Dockerfile 文件。使用 .NET 运行时基础镜像,复制二进制文件,指定入口点。运行命令构建镜像并测试容器。为实现通信,映射端口。最后,标签化镜像并推送到 Docker Hub,为生产环境部署做好准备。掌握这些步骤,即可轻松应对从小型项目到大规模应用的各种需求。
15 0
|
7天前
|
开发框架 监控 .NET
【Azure 应用程序见解】在Docker中运行的ASP.NET Core应用如何开启Application Insights的Profiler Trace呢?
【Azure 应用程序见解】在Docker中运行的ASP.NET Core应用如何开启Application Insights的Profiler Trace呢?
|
7天前
|
缓存 JavaScript 应用服务中间件
深入理解Docker中的UnionFS联合文件系统及其应用
【8月更文挑战第24天】本文深入探讨了联合文件系统(UnionFS)在Docker中的作用及其实现容器高效运行的机制。UnionFS通过叠加多个文件系统形成统一视图,确保各容器间的文件系统修改相互隔离。在Docker中,镜像由多层构成,通过只读底层与可写顶层的设计极大节省了磁盘空间。文章还分享了最佳实践,包括最小化镜像大小、利用缓存、避免频繁写操作以及使用多阶段构建技术,帮助开发者构建更轻量、高效的Docker容器。
17 0
下一篇
云函数