【Face Recognition人脸识别】4. 多张图片人脸识别

【 1. 导入已知图片 】

使用load_image_file导入这些图片:

# 加载已知图片
known_image_cc = face_recognition.load_image_file("know/reba.jpg")
known_image_xy = face_recognition.load_image_file("know/jiangxin.jpg")
known_image_smy = face_recognition.load_image_file("know/xiayu.jpg")
known_image_zch= face_recognition.load_image_file("know/zhangyishan.jpg")

【 2. 编码已知图片 】

然后,使用face_encodings对图片进行编码,获取128维特征向量。
同时,之后我们需要遍历已经照片来识别,所以先将已知人脸存为数组。

  • 代码如下:
# 对图片进行编码,获取128维特征向量
rb_encoding = face_recognition.face_encodings(known_image_rb)[0]
jx_encoding = face_recognition.face_encodings(known_image_jx)[0]
xy_encoding = face_recognition.face_encodings(known_image_xy)[0]
zys_encoding = face_recognition.face_encodings(known_image_zys)[0]
# 存为数组以便之后识别
known_faces = [
    rb_encoding,
    jx_encoding,
    xy_encoding,
    zys_encoding
]

【 3. 导入未知图片 】

四张照片分布对应于不同的已知照片中的任务。

  • 代码如下:
# 加载待识别图片
unknown_image_1 = face_recognition.load_image_file("unknow/reba1.jpg")
unknown_image_2 = face_recognition.load_image_file("unknow/reba2.jpg")
unknown_image_3 = face_recognition.load_image_file("unknow/xy.jpg")
unknown_image_4 = face_recognition.load_image_file("unknow/zys.jpg")
unknown_faces = [
    unknown_image_1, 
    unknown_image_2, 
    unknown_image_3,
    unknown_image_4
]

【 4. 遍历识别 】

遍历未知图片,对每一种未知图片,获取其人脸位置和特征向量。将得到的位置图片特征向量与所有已知的特征向量进行比较,判断是否为同一个人。
需要注意的是这里我们设置 tolerance 为0.5,实际应用时,可以根据自己对准确度的要求,进行调整。

# 初始化一些变量
face_locations = []
face_encodings = []
face_names = []
frame_number = 0
for frame in unknown_faces:
    face_names = []
    # 获取人脸区域位置
    face_locations = face_recognition.face_locations(frame)
    # 对图片进行编码,获取128维特征向量
    face_encodings = face_recognition.face_encodings(frame, face_locations)
    for face_encoding in face_encodings:
        # 识别图片中人脸是否匹配已知图片
        match = face_recognition.compare_faces(
            known_faces, face_encoding, tolerance=0.5)

得到是否是同一个人的结果之后,我们可以对应其姓名,添加到face_names数组中。

  • 代码如下:
name = None
if match[0]:
     name = "Dilireba"
elif match[1]:
     name = "Jang Xin"
elif match[2]:
     name = "Xia Yu"
elif match[3]:
     name = 'Zhang Yishan'
else:
     name = 'Unknown'
face_names.append(name)

【 5. 绘制姓名和人脸 】

得到对应的人脸识别结果之后,我们将遍历每一张未知图片中的人脸,通过 OpenCV的rectangle绘制脸部区域框和putText对应的人名。

  • 代码如下:
# 结果打上标签
for (top, right, bottom, left), name in zip(face_locations, face_names):
    # 绘制脸部区域框
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
    # 在脸部区域下面绘制人名
    cv2.rectangle(frame, (left, bottom - 25),
                    (right, bottom), (0, 0, 255), cv2.FILLED)
    font = cv2.FONT_HERSHEY_DUPLEX
    cv2.putText(frame, name, (left + 6, bottom - 6),
                font, 0.5, (255, 255, 255), 1)

最后,再将绘制完成的代码展示或者保存。

【 6. 范例 】

  • 整合以上五个步骤,完整的代码示例如下:
# 加载模块
import face_recognition
import cv2

# 加载已知图片
known_image_rb = face_recognition.load_image_file(
    "know/reba.jpg")
known_image_jx = face_recognition.load_image_file(
    "know/jiangxin.jpg")
known_image_xy = face_recognition.load_image_file(
    "know/xiayu.jpg")
known_image_zys = face_recognition.load_image_file(
    "know/zhangyishan.jpg")

# 对图片进行编码,获取128维特征向量
rb_encoding = face_recognition.face_encodings(known_image_rb)[0]
jx_encoding = face_recognition.face_encodings(known_image_jx)[0]
xy_encoding = face_recognition.face_encodings(known_image_xy)[0]
zys_encoding = face_recognition.face_encodings(known_image_zys)[0]

# 把已识别图片的编码存为列表
known_faces = [
    rb_encoding,
    jx_encoding,
    xy_encoding,
    zys_encoding
]

# 加载待识别图片
unknown_image_1 = face_recognition.load_image_file(
    "unknow/reba1.jpg")
unknown_image_2 = face_recognition.load_image_file(
    "unknow/reba2.jpg")
unknown_image_3 = face_recognition.load_image_file(
    "unknow/xy.jpg")
unknown_image_4 = face_recognition.load_image_file(
    "unknow/zys.jpg")

# 把待识别图片存为列表
unknown_faces = [
    unknown_image_1,
    unknown_image_2,
    unknown_image_3,
    unknown_image_4
]

# 初始化一些变量
face_locations = []
face_encodings = []
face_names = []
frame_number = 0

# 将待识别图片列表遍历
for frame in unknown_faces:
    face_names = []
    # 获取待识别图片人脸区域位置
    face_locations = face_recognition.face_locations(frame)
    # 对待识别图片人脸区域位置进行编码,获取128维特征向量
    face_encodings = face_recognition.face_encodings(frame, face_locations)

    # 对待识别图片的编码列表遍历
    for face_encoding in face_encodings:
        # 识别图片中人脸是否匹配已知图片
        match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.5)
        name = None
        if match[0]:
            name = "Dilireba"
        elif match[1]:
            name = "Jang Xin"
        elif match[2]:
            name = "Xia Yu"
        elif match[3]:
            name = 'Zhang Yishan'
        else:
            name = 'Unknown'
        face_names.append(name)


    # 结果打上标签
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        if not name:
            continue
        # 绘制脸部区域框
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        # 在脸部区域下面绘制人名
        cv2.rectangle(frame, (left, bottom - 25),
                      (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6),
                    font, 0.5, (255, 255, 255), 1)
    # 显示图片
    image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    cv2.imshow("Lao Wang.jpg", image_rgb)
    cv2.waitKey(0)

已知图片:
【Face Recognition人脸识别】4. 多张图片人脸识别_第1张图片
未知图片:
【Face Recognition人脸识别】4. 多张图片人脸识别_第2张图片
运行结果:
【Face Recognition人脸识别】4. 多张图片人脸识别_第3张图片【Face Recognition人脸识别】4. 多张图片人脸识别_第4张图片【Face Recognition人脸识别】4. 多张图片人脸识别_第5张图片【Face Recognition人脸识别】4. 多张图片人脸识别_第6张图片

你可能感兴趣的:(人脸识别)