face_recognition
是一个轻量化的人脸识别包,能识别出人脸的眉毛、鼻子、嘴巴、眼睛等特征点,进行计算两张脸的距离,比较两张脸是否为同一个人等功能。
一、两张图像进行比较
取两张照片进行图像比对,确认是否同一个人,在进行比较时,发现两个不同的人比对结果为同一个人,原因是没有设定较为严格的阈值,需在
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)
在程序中添加 tolerance
,值越小,精度越高,官方说法是tolerance=0.6
是最优的,但是我用来识别时发现出现识别错误,需设定为更小的值,tolerance<0.5
时较优。
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding,tolerance=0.1)
完整代码
import face_recognition
picture_of_me = face_recognition.load_image_file(r"C:\Users\lenovo\Desktop\1.jpg")
my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
# my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!
unknown_picture = face_recognition.load_image_file(r"C:\Users\lenovo\Desktop\2.jpg")
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
# Now we can see the two face encodings are of the same person with `compare_faces`!
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding,tolerance=0.1)
if results[0] == True:
print("It's a picture of me!")
else:
print("It's not a picture of me!")
二、进行多人脸比对识别
假定我们已经采集到一批已知姓名的照片,照片名字以姓名进行命名,现需要识别出一张待识别的人是否在我们采集的样本库中。
如果待识别的照片在样本库中,把他的名字打印出来,否则打印出“陌生人”字样。
import os
import face_recognition
path=r"C:\Users\lenovo\Desktop\样本库照片"
piture=os.listdir(path)
known_faces=[]
for i in piture:
pp=os.path.join(path,i)
name=face_recognition.load_image_file(pp)
face_feature=face_recognition.face_encodings(name)[0]
known_faces.append(face_feature)
unknown_image = face_recognition.load_image_file(r"C:\Users\lenovo\Desktop\待识别.jpg")
unknown_imag_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces(known_faces,unknown_imag_encoding,tolerance=0.4)
while True in results:
for i in range(len(results)):
if results[i]==True:
print("姓名:",piture[i].split('.')[0])
break
else:
print("陌生人")
三、多人脸特征识别
我们有一张照片是这样的
我们需要识别出每张脸的特征并在该图上进行标注
原始教程中,是把一张脸上的特征画出来,第二张脸再画在另一张图上,经过我的修改,实现把所有脸都画在同一张图上。
附上代码
from PIL import Image, ImageDraw
import face_recognition
image = face_recognition.load_image_file(r"C:\Users\lenovo\Desktop\测试\00.jpg")
#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
facial_features =['chin','left_eyebrow','right_eyebrow','nose_bridge','nose_tip','left_eye', 'right_eye','top_lip','bottom_lip']
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for face_landmarks in face_landmarks_list:
#打印此图像中每个面部特征的位置
# for facial_feature in facial_features:
# print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
#让我们在图像中描绘出每个人脸特征!
for facial_feature in facial_features:
d.line(face_landmarks[facial_feature], width=1)
pil_image.show()
如果我们有每张脸的单独的名字,同样可以实现识别出每张脸的姓名,该部分功能与第二节是同样的程序,略。
四、 额外的拓展
由于我们在设定 tolerance 时,根据识别的results 进行调节,没有科学性。应该是取多个人的多张照片进行比对,例如取10个人的50张照片进行比对,使用 face_recognition.face_distance
函数。
1、同一个人的照片两两比对,得出49*49个距离数据,10个人就有10个矩阵,分别计算出同一组数据内的平均值、最大值、最小值。
2、依次取两个人的多张照片进行两两比对,得出一组距离数组,再计算平均值、最大值、最小值。
3、找出同第1、2点数据组间的最佳分隔线,从而得出最优的 tolerance
。
-
~ ~
完结!
face_recognition.face_distance
还有其他功能,在此就不在细讲。