人脸比对实现判断并查找面部特征 | 人脸识别系列 原创

Aasee
发布于 2022-2-8 20:41
浏览
3收藏

春节不停更,此文正在参加「星光计划-春节更帖活动」

人脸比对

上一期中我们已经知道了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("他们不是同一个人!")

图片就由大家自行更换尝试,我这里使用了帅气彭于晏为大家演示。
人脸比对实现判断并查找面部特征 | 人脸识别系列-鸿蒙开发者社区
人脸比对实现判断并查找面部特征 | 人脸识别系列-鸿蒙开发者社区

查找面部特征

当我们需要一个图像中人脸的面部特征时,我们需要调用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")

图片大家可以自行更换

结果展示

人脸比对实现判断并查找面部特征 | 人脸识别系列-鸿蒙开发者社区
人脸比对实现判断并查找面部特征 | 人脸识别系列-鸿蒙开发者社区
人脸比对实现判断并查找面部特征 | 人脸识别系列-鸿蒙开发者社区

总结

人脸识别第三期,通过使用compare_faces对比face_encodings编码来进行人脸比对,利用face_landmarks找出人脸特征然后使用pillow库的Image.Draw来描出轮廓。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
已于2022-2-8 20:52:10修改
5
收藏 3
回复
举报
回复
    相关推荐