python dlib人脸识别代码实例

yipeiwu_com5年前Python基础

本文实例为大家分享了python dlib人脸识别的具体代码,供大家参考,具体内容如下

import matplotlib.pyplot as plt
import dlib
import numpy as np
import glob
import re
 
#正脸检测器
detector=dlib.get_frontal_face_detector()
#脸部关键形态检测器
sp=dlib.shape_predictor(r"D:\LB\JAVASCRIPT\shape_predictor_68_face_landmarks.dat")
#人脸识别模型
facerec = dlib.face_recognition_model_v1(r"D:\LB\JAVASCRIPT\dlib_face_recognition_resnet_model_v1.dat")
 
#候选人脸部描述向量集
descriptors=[]
 
photo_locations=[]
 
for photo in glob.glob(r'D:\LB\JAVASCRIPT\faces\*.jpg'):
  photo_locations.append(photo)
  img=plt.imread(photo)
  img=np.array(img)
  
  #开始检测人脸
  dets=detector(img,1)
  
  for k,d in enumerate(dets):
    #检测每张照片中人脸的特征
    shape=sp(img,d)
    face_descriptor=facerec.compute_face_descriptor(img,shape)
    v=np.array(face_descriptor)
    descriptors.append(v)
		
#输入的待识别的人脸处理方法相同
img=plt.imread(r'D:\test_photo10.jpg')
img=np.array(img)
dets=detector(img,1)
#计算输入人脸和已有人脸之间的差异程度(比如用欧式距离来衡量)
differences=[]
for k,d in enumerate(dets):
  shape=sp(img,d)
  face_descriptor=facerec.compute_face_descriptor(img,shape)
  d_test=np.array(face_descriptor)
  
  #计算输入人脸和所有已有人脸描述向量的欧氏距离
  for i in descriptors:
    distance=np.linalg.norm(i-d_test)
    differences.append(distance)
 
#按欧式距离排序 欧式距离最小的就是匹配的人脸
candidate_count=len(photo_locations)
candidates_dict=dict(zip(photo_locations,differences))
candidates_dict_sorted=sorted(candidates_dict.items(),key=lambda x:x[1])
 
#matplotlib要正确显示中文需要设置
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
 
plt.rcParams['figure.figsize'] = (20.0, 70.0) 
 
ax=plt.subplot(candidate_count+1,4,1)
ax.set_title("输入的人脸")
ax.imshow(img)
 
for i,(photo,distance) in enumerate(candidates_dict_sorted):
  img=plt.imread(photo)
  
  face_name=""
  photo_name=re.search(r'([^\\]*)\.jpg$',photo)
  if photo_name:
    face_name=photo_name[1]
    
  ax=plt.subplot(candidate_count+1,4,i+2)
  ax.set_xticks([])
  ax.set_yticks([])
  ax.spines['top'].set_visible(False)
  ax.spines['right'].set_visible(False)
  ax.spines['bottom'].set_visible(False)
  ax.spines['left'].set_visible(False)
  
  if i==0:
    ax.set_title("最匹配的人脸\n\n"+face_name+"\n\n差异度:"+str(distance))
  else:
    ax.set_title(face_name+"\n\n差异度:"+str(distance))
  ax.imshow(img)
 
plt.show()

以上所述是小编给大家介绍的python dlib人脸识别详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

python操作ie登陆土豆网的方法

本文实例讲述了python操作ie登陆土豆网的方法。分享给大家供大家参考。具体如下: 这里利用ie操作登陆土豆网,很简单,仅做一下记录,以备后用。 # -*- coding: utf...

python实现基于两张图片生成圆角图标效果的方法

本文实例讲述了python实现基于两张图片生成圆角图标效果的方法。分享给大家供大家参考。具体分析如下: 使用pil的蒙版功能,将原图片和圆角图片进行叠加,并将圆角图片作为mask,生成新...

Python基础入门之seed()方法的使用

 seed() 设置生成随机数用的整数起始值。调用任何其他random模块函数之前调用这个函数。 语法 以下是seed()方法的语法: seed ( [x] ) 注意...

python数据归一化及三种方法详解

python数据归一化及三种方法详解

数据标准化(归一化)处理是数据挖掘的一项基础工作,不同评价指标往往具有不同的量纲和量纲单位,这样的情况会影响到数据分析的结果,为了消除指标之间的量纲影响,需要进行数据标准化处理,以解决数...

在Python中编写数据库模块的教程

在一个Web App中,所有数据,包括用户信息、发布的日志、评论等,都存储在数据库中。在awesome-python-app中,我们选择MySQL作为数据库。 Web App里面有很多地...