使用Facecognition与Opencv怎么实现一个人脸识别功能

  介绍

使用Facecognition与Opencv怎么实现一个人脸识别功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

Facecognition人脸识别原理大体可分为:

1,通过猪算子定位人的脸,也可以用cnn模型,但本文没试过,

2, Dlib有专门的函数和模型,实现人脸68个特征点的定位。通过图像的几何变换(仿射,旋转,缩放),使各个特征点对齐(将眼睛,嘴等部位移到相同位置);

3,训练一个神经网络,将输入的脸部图像生成为128维的预测值。训练的大致过程为:将同一人的两张不同照片和另一人的照片一起喂入神经网络,不断迭代训练,使同一人的两张照片编码后的预测值接近,不同人的照片预测值拉远;

4,将陌生人脸预测为128维的向量,与人脸库中的数据进行比对,找出阈值范围内欧氏距离最小的人的脸,完成识别。

1开发环境

PyCharm: PyCharm Community Edition 2020.3.2 x64

Python: Python 3.8.7 

Opencv: opencv-python 4.5.1.48

Facecognition: 1.3.0

下文:下文0.5.0

2环境搭建

本文不做PyCharm和Python安装,这个自己搞不定,就别玩了~

pip  install  opencv-python   pip  install 人脸识别   pip  install  face-recognition-models   pip  install 下文

3打造自己的人脸库

通过opencv, facecogniton定位人脸并保存人脸头像,生成人脸数据集,代码如下:

import  face_recognition   import  cv2   import 操作系统   ,   def  builddataset ():   时间=Video_face 才能;cv2.VideoCapture (0)   num才能=0   while 才能正确的:   ,,,国旗,frame =, Video_face.read ();   ,,,if 国旗:   ,,,,,cv2.imshow(& # 39;帧# 39;,,)   ,,,,,cv2.waitKey (2)   ,,,:   ,,,,,休息   ,,,face_locations =, face_recognition.face_locations(框架)   ,,,if  face_locations:   ,,,,,x_face =,框架(face_locations [0] [0] -50: face_locations[0][2] + 50岁,face_locations [0] [3] -50: face_locations [0] [1] + 50);   ,,,,,# x_face =, cv2.resize (x_face, dsize=(200,, 200));   ,,,,,bo_photo =, cv2.imwrite (“% s \ % d.jpg", %, (“traindataset/ylb",, num),, x_face);   ,,,,,印刷(“保存成功:% d", %, num)   ,,,,num=num + 1   ,,,:   ,,,,,印刷(“* * * *未检查到头像* * * *“)   ,   Video_face.release才能()   ,   if  __name__ ==, & # 39; __main__ # 39;:   builddataset才能();   通过才能

4模型训练与保存

通过数据集进行训练,得到人脸识别码,以numpy数据形式保存(人脸识别码)模型

, def  __init__(自我,,trainpath, labelname, modelpath,, predictpath):   ,,,self.trainpath =trainpath   ,,,self.labelname =labelname   ,,,self.modelpath =modelpath   ,,,self.predictpath =predictpath   ,   #,才能no 医生   def 才能;火车(trainpath,自我,还以为;modelpath):   ,,,encodings =, []   ,,,dirs =, os.listdir (trainpath)   ,,,for  k, dir 拷贝列举(dirs):   ,,,,,filelist =, os.listdir (trainpath + & # 39;/& # 39; + dir)   ,,,,,for 小姐:拷贝范围(0,,len(文件列表):   ,,,,,,,imgname =, trainpath  +, & # 39;/& # 39; + dir + & # 39;/% d.jpg& # 39;, %,(我)   ,,,,,,,picture_of_me =, face_recognition.load_image_file (imgname)   ,,,,,,,face_locations =, face_recognition.face_locations (picture_of_me)   ,,,,,,,if  face_locations:   ,,,,,,,,,印刷(face_locations)   ,,,,,,,,,my_face_encoding =, face_recognition.face_encodings (picture_of_me,,,,,,   ,,,,,,,,,,,,,,,,,,,face_locations) [0]   ,,,,,,,,,encodings.append (my_face_encoding)   ,,,if 编码:   ,,,,,numpy.save (modelpath,编码)   ,,,,,印刷(len(编码)   ,,,,,印刷(“model  train  is  sucess")   ,,,:   ,,,,,印刷(“model  train  is  failed")

5人脸识别及跟踪

通过opencv启动摄像头并获取视频,加载训练好模型完成识别及跟踪,为避免视频卡顿设置了隔帧处理。

,, def  predicvideo(自我,名字,模型):   ,,,Video_face =, cv2.VideoCapture (0)   ,,,num=0   ,,,奖励=[]   ,,,unknown_face_locations=[]   ,,,while 正确的:   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

使用Facecognition与Opencv怎么实现一个人脸识别功能