北京志远天辰科技有限公司-旗下
首页 » 新闻快递 » 人脸比对实现判断并查找面部特征

人脸比对实现判断并查找面部特征

作者:钉钉硬件分类: 新闻快递 时间:2022-2-14 16:13浏览:589次
简介: compare_faces( ) compare_faces( known_face_encodings, face_encoding_to_check, tolerance=0.6 ) 比较脸部编码列表和候选编码,看看它们是否匹配。 参数: known_face_encodings:已知的人脸编码列表 face_encoding_to_check:待进行比照的单张人脸编码数据 tolerance:两张脸之间有多少距离才算匹配。该值越小比照越严厉,0.6是典型的最佳值。 返回值: 一个 True或许False值的列表,该表指示了known_face_encodings列表的每个成员的匹

人脸比对

上一期中咱们已经知道了face_encodings编码,而且经过其实现了128维的向量化。本章咱们就经过face_recognition中的compare_face()函数运用face_encodings编码进行比对。

compare_faces( )

compare_faces( known_face_encodings, face_encoding_to_check, tolerance=0.6 ) 比较脸部编码列表和候选编码,看看它们是否匹配。 参数: known_face_encodings:已知的人脸编码列表 face_encoding_to_check:待进行比照的单张人脸编码数据 tolerance:两张脸之间有多少距离才算匹配。该值越小比照越严厉,0.6是典型的最佳值。 返回值: 一个 True或许False值的列表,该表指示了known_face_encodings列表的每个成员的匹配成果。

具体代码如下

import face_recognition

know_image = face_recognition.load_image_file("6.png")
unknow_image = face_recognition.load_image_file("5.png") # 由于我这图画只要一个人脸,取图画中的第一个编码,取索引0。 kface_encodings = face_recognition.face_encodings(know_image)[0]
unface_encodings = face_recognition.face_encodings(unknow_image)[0] #  比照判断 result = face_recognition.compare_faces([kface_encodings], unface_encodings) if result[0] == True:
    print("他们是同一个人!") else:
    print("他们不是同一个人!")

图片就由咱们自行替换测验,我这里运用了帅气彭于晏为咱们演示。
彭于晏
tu1

查找面部特征

当咱们需要一个图画中人脸的面部特征时,咱们需要调用face_landmarks函数,从该图片中,得到一个由脸部特征关键点方位组成的字典记载列表: (脸部特征包括:鼻梁nose_bridge、鼻尖nose_tip、 下巴chin、左眼left_eye、右眼right_eye、左眉 left_eyebrow、右眉right_eyebrow、上唇top_lip、下唇bottom_lip)

人脸特征提取函数——face_landmarks

face_landmarks( face_image , face_locations=None, model="large" ) 给定一个图画,提取图画中每个人脸的脸部特征方位 参数: face_image :输入的人脸图片 face_locations=None : 可选参数,默许值为None,代表默许解码图片中的每一个人脸。 若输入face_locations()[i]可指定人脸进行解码 model="large" :输出的特征模型,默许为“large”,可选“small”。 当选择为"small"时,只提取左眼left_eye、右眼right_eye、鼻尖nose_tip这三种脸部特征。

具体运用代码如下

import face_recognition from PIL import Image, ImageDraw def demolandmarks(path): # 将文件加载到numpy数组中 image = face_recognition.load_image_file(path) # 查找图画中所有人脸中的所有面部特征 face_landmarks_list = face_recognition.face_landmarks(image)
    faceNum = len(face_landmarks_list)
    print(f"我发现了{faceNum}张脸在这张图片中!") # 创立一个 PIL imagedraw 目标,以便咱们可以在图片上绘图 Drawpic = Image.fromarray(image)
    Draw = ImageDraw.Draw(Drawpic) # 经过阅览face_landmarks源码咱们可以知道相貌特征有如下为了便利理解我将其列出(上面也已经进行了说明): facial_features = [ 'chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge', 'nose_tip', 'left_eye', 'right_eye', 'top_lip', 'bottom_lip'] for face_landmarks in face_landmarks_list: # 输出这张图片中每个面部特征的方位 for facial_feature in facial_features:
            print(f"The {facial_feature} in this face has the following points: {face_landmarks[facial_feature]}")
            Draw.line(face_landmarks[facial_feature], width=1, fill=(0, 255, 0)) # 用一条线将面部特征进行描写 Drawpic.show()
demolandmarks("2.png")

图片咱们可以自行替换

成果展现

tu1
tu2
tu3

总结

人脸辨认第三期,经过运用compare_faces比照face_encodings编码来进行人脸比对,使用face_landmarks找出人脸特征然后运用pillow库的Image.Draw来描出轮廓。