【人脸识别系列】| 人脸比对判断并查找面部特征

本文详细介绍了如何使用Python的face_recognition库进行人脸比对,通过compare_faces函数对比face_encodings,并展示了如何提取人脸特征,包括face_landmarks的功能和应用实例。通过实际操作演示了人脸识别技术的基本流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

人脸比对

上一期中我们已经知道了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来描出轮廓。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aasee.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值