导师给的一个工作,利用人脸识别来做一个智能的考勤系统。只会Python,所以用Face_recognition来完成这个工作。话不多说,直接进入正题。

1.要顺利调用Face_recognition这个库,首先得安装好两个依赖库dlib和openCV

安装方法网上总结的特别多,这里就不一一赘述了(主要是不适合我,可能windows环境下变数太多),只给出我自己成功安装的方法:


直接去网上下载dlib(必须19.7.0以上否则无法支持face_recognition的安装)和openCV的whl文件(需要下载对应python版本的whl文件),放在Scripts文件夹下面。




再打开cmd窗口,进入Scripts文件夹下进行安装,如图




稍等一会,即可成功安装以上两个依赖库,安装好后,face_recognition的安装就很简单了,直接pip install
face_recognition即可。

2.图库

导师实验室大概有十几个人,所以测试图库的图片数选择为20,如下:




3.代码

基本要求有:能实时识别人脸,能输出识别成功人的名字和识别时间
# -*- coding: utf-8 -*- import face_recognition import cv2 import datetime
import glob2 as gb video_capture = cv2.VideoCapture(0)
img_path=gb.glob(r'F:\liuzhenya\photo\\*.jpg') known_face_names=[]
known_face_encodings=[] for i in img_path:
picture_name=i.replace('F:\liuzhenya\photo\\*.jpg','')
picture_newname=picture_name.replace('.jpg','') someone_img =
face_recognition.load_image_file(i) someone_face_encoding =
face_recognition.face_encodings(someone_img)[0]
known_face_names.append(picture_newname)
known_face_encodings.append(someone_face_encoding) someone_img=[]
someone_face_encoding=[] face_locations = [] face_encodings = [] face_names =
[] process_this_frame = True while True: ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame=small_frame[:,:,::-1] if process_this_frame: face_locations =
face_recognition.face_locations(rgb_small_frame) face_encodings =
face_recognition.face_encodings(rgb_small_frame, face_locations) face_names =
[] for i in face_encodings: match =
face_recognition.compare_faces(known_face_encodings,i,tolerance=0.39) if True
in match: match_index=match.index(True) name = "match" #To print name and time
cute_clock = datetime.datetime.now() print
(known_face_names[match_index]+':'+str(cute_clock)) else: name = "unknown"
face_names.append(name) process_this_frame = not process_this_frame for (top,
right, bottom, left), name in zip(face_locations, face_names): top *= 4 right
*= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom),
(0, 0, 255), 2) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0,
0, 255), 2) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left+6,
bottom-6), font, 1.0, (255, 255, 255), 1) cv2.imshow('Video', frame) if
cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release()
cv2.destroyAllWindows()
1>用glob库来获取文件夹下所有图片路径,便于提取人脸特征矩阵和输出姓名

2>用datatime来输出当前时间

3>人脸识别的核心代码没什么好讲的,有疑惑参考face_recognition官方文档即可,作者Adam Geitgey
<mailto:ageitgey@gmail.com>给了很多例子


4>阈值的选取,face_recognition.compare_faces()函数的默认阈值为0.6,阈值太低容易造成无法成功识别人脸,太高容易造成人脸识别混淆,(我没修改阈值时,图库容量=1or2能准确识别人脸,图库容量=3就会出现个别错误识别,图库容量>3时基本无法正确识别人脸),图库容量为20时,我选择的阈值是0.39。


4.结果




5.最后

1>之后会更一下这个工作的最终结果

2>阈值的讨论会继续深入

3>我的小可爱比最后一张图漂亮(没错,就是这样!)




友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信