小白练手的~
欢迎指出不足~
import cv2
import face_recognition
from PIL import Image
#1.读取
face_image=face_recognition.load_image_file(r'C:\Users\hzy\Desktop\1.jpg')
#128维人脸编码 相当于 五官数据
face_encodings=face_recognition.face_encodings(face_image)
face_locations=face_recognition.face_locations(face_image)
face1=face_encodings[0]
face2=face_encodings[1]
#3.比较 tolerance越低越严格
results=face_recognition.compare_faces([face1],face2,tolerance=0.5)#布尔类型的数据
#4.结果
print(results)
#结果为true or false
if results==[True]:
print(1)
name='Yes'
else:
print(0)
name='No'
#绘图
for i in range(len(face_encodings)):
face_encoding=face_encodings[(i-1)]
face_location=face_locations[(i-1)]
top,right,bottom,left=face_location
#画框
cv2.rectangle(face_image,(left,top),(right,bottom),(0,255,0),2)
#写字
cv2.putText(face_image,name,(left-10,top-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,0,0),2)
face_image_rgb=cv2.cvtColor(face_image,cv2.COLOR_BGR2RGB)
cv2.imshow('output',face_image_rgb)
ps:这个程序识别刘亦菲和黄圣依的某张照片tolerance=0.5时结果为True,0.4时结果为False了。。。
关于face_recognition的安装我参考了这两篇:
https://blog.csdn.net/scc_722/article/details/80613933
https://blog.csdn.net/weixin_41453476/article/details/105854784