Face Recognition - face_recognition (Python)

GitHub Address: https://github.com/ageitgey/face_recognition

Documents Address: https://face-recognition.readthedocs.io/en/latest/index.html

program

import face_recognition
from os import listdir

known_image_path = "picture/known"
unknown_image_path = "picture/unknown/test1.jpg"

# 保存已知的人脸名字
known_face_names = []
known_images = []
known_encodings = []

fileNameList = listdir(known_image_path)
for fileName in fileNameList:
    filePath = known_image_path + "/" + fileName
    index = fileName.rfind('.')
    known_face_names.append(fileName[:index])

    # 将文件加载到numpy数组中
    image = face_recognition.load_image_file(filePath)
    known_images.append(image)

    # 保存面部编码
    encoding = face_recognition.face_encodings(image)[0]
    known_encodings.append(encoding)

# 姓名和编码保存到字典
c_d = dict(zip(known_face_names, known_encodings))

# 待识别的人脸图片
unknown_image = face_recognition.load_image_file(unknown_image_path)
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

for key, value in c_d.items():
    result = face_recognition.compare_faces([value], unknown_encoding)
    # print(result)
    if result[0] == True:
        print("The persion is {}".format(key))

 

Published 47 original articles · won praise 121 · views 680 000 +

Guess you like

Origin blog.csdn.net/guoyunfei123/article/details/99627749