人脸识别

最近项目需要进行人人脸识别、人脸特征分析相关开发。

在Raspberry Pi中安装face_recognition库,github代码库https://github.com/ageitgey/face_recognition

Raspberry 操作系统信息:

硬件

需要准备Raspberry Pi主板和PiCamera摄像头。

image

安装dlib

face_recognition 依赖dlib库,先安装dlib库,参考 https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65

遇到的问题描述:

1.apt-get卡住0 [连接到archive.ubuntu.com],参考https://ubuntuqa.com/article/408.html 中的最佳解决方案 解决问题

2.sudo pip3 install face_recognition报错,错误如下

1
2
3
4
5
6
7
8
9
10
11
12
Exception:
Traceback (most recent call last):
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/response.py", line 298, in _error_catcher
.......
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main
.....
requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='www.piwheels.org', port=443): Read timed out.

尝试手动安装,从这里https://download.csdn.net/download/chentianlong150/10139353下载了个face_recognition_models,手动装了一下以后根本不是face_recognition项目也不知道是个啥东西于是卸载了

最后没办法了又反复运行了下面的命令(3次,中间也有失败)。但最后成功了。怀疑是墙的原因Orz

1
$ sudo pip3 install face_recognition

3.缺少libatlas.so.3库

错误信息:

1
2
3
4
5
6
7
8
Traceback (most recent call last):
File "facerec_on_raspberry_pi.py", line 9, in <module>
import face_recognition
File "/usr/local/lib/python3.5/dist-packages/face_recognition/__init__.py", line 7, in <module>
from .api import load_image_file, face_locations, batch_face_locations, face_landmarks, face_encodings, compare_faces, face_distance
File "/usr/local/lib/python3.5/dist-packages/face_recognition/api.py", line 4, in <module>
import dlib
ImportError: libatlas.so.3: cannot open shared object file: No such file or directory

提示没有libatlas.so.3 参考https://askubuntu.com/questions/719538/libatlas-package

1
$ sudo apt-get install -y libatlas-*

4.提示没有摄像头

1
2
3
4
Traceback (most recent call last):
File "facerec_on_raspberry_pi.py", line 10, in <module>
import picamera
ImportError: No module named 'picamera'

安装摄像头库

1
2
$ sudo apt-get install python3-picamera
$ sudo pip3 install --upgrade picamera[array]

最开始没有摄像头报错,上淘宝买个吧!

1
picamera.exc.PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.

picamera 使用

picamera API 地址:https://picamera.readthedocs.io/en/release-1.13/quickstart.html

遇到的问题描述:

摄像头没插紧报错:

1
2
3
4
5
6
pi@raspberrypi ~ $ raspistill -o image.jpg
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: Failed to create camera component
mmal: main: Failed to create camera component
mmal: Camera is not detected. Please check carefully the camera module is installed correctly

参考下面blog都没解决问题

1
2
3
4
5
https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=101291
http://raspberrypi.stackexchange.com/questions/10545/noir-camera-not-working
http://www.element14.com/community/thread/31835?start=0&tstart=0
http://www.raspberrypi.org/forums/viewtopic.php?f=43&t=79890
http://www.raspberrypi.org/forums/viewtopic.php?f=43&p=655380

最后发现是摄像头上面没装紧(好坑呵…),注意不是主板与picamera接口出没插紧,摄像头是可装可拆的,见下图。刚接触对摄像头构造了解。

参考文档汇总:

Python PIL库:https://blog.csdn.net/zhangziju/article/details/79123275

face_recognition Github库:https://github.com/ageitgey/face_recognition

face_recognition中文文档:https://www.jianshu.com/p/4dd131567015

face_recognition API中文:https://www.jianshu.com/p/a0e61f5fd570

Raspberry Pi安装dlib文档:https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65

macOS或Ubuntu上安装dlib文档:https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf

PiCarema 文档 :https://picamera.readthedocs.io/en/release-1.13/install.html