树莓派人脸识别-face-recognition的安装与应用

该库可以通过python或者命令行即可实现人脸识别的功能。使用dlib深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild)上的准确率为99.38%。

安装过程

先在终端下安装一大堆需要的库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo apt-get update sudo apt-get install 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 \
python3-numpy \
python3-pip \
zip

如果使用树莓派的摄像头(CSI接口),执行下面的命令:

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

下载安装dlib:

1
2
3
4
mkdir -p dlib 
git clone -b 'v19.6' --single-branch https://github.com/davisking/dlib.git dlib/
cd ./dlib
sudo python3 setup.py install --compiler-flags "-mfpu=neon"

安装face_recognition:

1
sudo pip3 install face_recognition

下载示例代码并尝试运行(可选):

1
2
3
git clone --single-branch https://github.com/ageitgey/face_recognition.git
cd ./face_recognition/examples
python3 facerec_on_raspberry_pi.py

:过程缺少库时,使用

1
apt-cache search 库名

来搜索到那个库的安装包,然后用

1
sudo apt-get install 包名

来安装。

例如:缺少了libatlas.so.3,那我们就用apt-cache search libatlas来搜索,发现它的包名叫libatlas3-base,所以我们用sudo apt-get install libatlas3-base来安装。
后面测试摄像头的时候也会遇到这样的问题,解决办法是一样的。

待把CSI接口树莓派摄像头装上后,在raspi-config中启用摄像头,然后重启。
(详见博客内关于CSI摄像头的另一篇文章)

运行一下实时人脸识别的代码(可选):

1
python3 facerec_from_webcam_faster.py

过程可能报错,import cv2的时候缺少库,然后根据提示用之前安装方法安装就好了。装完一个库再运行的时候,发现又提示缺少别的库,然后再安装缺少的库。反复个多次,把缺少的库都装好即可。

再次运行的时候,会报别的错误,出错的代码是

1
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

这是因为video_capture.read()没有读到图片。
树莓派中的camera module是放在/boot/目录中以固件形式加载的,不是一个标准的V4L2的摄像头驱动,所以加载起来之后会找不到/dev/video0的设备节点。
解决方法:转载https://blog.csdn.net/deiki/article/details/71123947

之后可以使用下面的命令来加载驱动模块:

1
sudo modprobe bcm2835-v4l2

如果想开机自动加载,我们可以修改/etc/modules文件,添加一行:

1
bcm2835-v4l2

如下图所示:

安装过程基本完毕。

个人提供的样例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env 
#coding=utf-8
#从目录中读取一堆文件

import face_recognition
import cv2
import os
import RPi.GPIO
import time

RPi.GPIO.setmode(RPi.GPIO.BCM)

RPi.GPIO.setwarnings(False)

buzzer=4
RPi.GPIO.setup(buzzer,RPi.GPIO.OUT)

RPi.GPIO.output(buzzer,True)

strangerAppear=False
strangerNum=0


demo_filelist=[]
demo_face_encodings=[]
demo_face_names=[]
for f in os.listdir('/home/pi/demo/face_recognition/jpg//'):
demo_filelist.append(f)
demo_face_names.append(f[:-4])

for filename in demo_filelist:
print(u'正在加载....'+filename)
demo_image = face_recognition.load_image_file('/home/pi/demo/face_recognition/jpg//'+filename)
face_encoding = face_recognition.face_encodings(demo_image)[0]
demo_face_encodings.append(face_encoding)

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = 1

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

while True:
# Grab a single frame of video
ret, frame = video_capture.read()

# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.3, fy=0.3)

# Only process every other frame of video to save time
if process_this_frame==1:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
# print(u"我检测到了{}张脸。".format(len(face_locations)))
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces(demo_face_encodings, face_encoding,tolerance=0.6)#
print(match)
name = "Unknown"
i=-1
for m in match:
i+=1
if m:
name= demo_face_names[i]

if(name=="Unknown"):
RPi.GPIO.output(buzzer,False)
frame_1=cv2.flip(frame,1)
path="/home/pi/demo/face_recognition/"+str(strangerNum)+".png"
cv2.imwrite(path,frame_1)
strangerNum=strangerNum+1
strangerAppear=True
face_names.append(name)

# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Draw a box around the face
#cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face
# cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 5)
font = cv2.FONT_HERSHEY_DUPLEX#FONT_HERSHEY_DUPLEX
# print(name)
# boxsize, _ = cv2.getTextSize(fs.string, fs.face, fs.fsize, fs.thick)
# locx = int((right+left)/2-25 - 14*len(name)/2)
cv2.putText(frame, name, (20,20), font, 1.0, (0, 0, 255), 1)

process_this_frame += 1# not process_this_frame
if process_this_frame>1:
process_this_frame=1
# Display the resulting image

############
cv2.imshow('Video', frame)
cv2.moveWindow('Video',600,10)

# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
RPi.GPIO.output(buzzer,True)
strangerAppear=False
break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()