#获取两个人的数据
face1=face_encodings[0]
face2=face_encodings[1]
result=face_recognition.compare_faces([face1],face2,tolerance=0.6)#人脸比较,,误差不超过0.6则可以,默认值也为0.6
print(result)
返回:
判断出为不是同一个人。
再稍微修改一下,让表达更清楚:
if result==[True]:
name='same'
print('两个人为同一个人')
else:
print('两者不是同一个人')
name='different'
返回:
第六步:框出人脸写上文字获取两个人脸位置坐标:
for i in range(len(face_encodings)):
face_encoding=face_encodings[(i-1)] #倒序获取
face_location = face_locations[(i - 1)]
print(face_location)#获取人脸位置
返回:
元祖四个数值分别表示框人脸矩形框的四个点坐标。
获取到坐标后开始画框框并写上文字:
top,right,bottom,left=face_location#确定出坐标
#画框框
cv2.rectangle(face_img,(left,top),(right,bottom),(255,0,0))#传参分别为:图片,坐标,RGB颜色,框粗细
#写字上去
cv2.putText(face_img,name,(left-10,top-10),cv2.FONT_HERSHEY_DUPLEX,0.8,(255,255,0),2)#传参数分别为:图片,文字,坐标,字体,字体大小,颜色,粗细
第七步:显示处理好的图像
face_img_rgb=cv2.cvtColor(face_img,cv2.COLOR_BGR2RGB)#确保颜色不要混乱
#展示图像
cv2.imshow('compare',face_img_rgb)
#设置等待关闭
cv2.waitKey(0)
效果: