如何修复人脸识别操作数错误?

How can I fix Face Recognition operands error?

进口face_recognition 从 PIL 导入图像,ImageDraw

burak_image=face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/friends2/burak1.jpg") burak_face_encoding = face_recognition.face_encodings(burak_image)

cansu_image=face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/friends2/cansu1.jpg") cansu_face_encoding = face_recognition.face_encodings(cansu_image)

elif_image=face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/friends2/elif2.jpg") elif_face_encoding = face_recognition.face_encodings(elif_image)

mert_image=face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/friends2/mert2.jpg") mert_face_encoding = face_recognition.face_encodings(mert_image)

merve_image=face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/friends2/merve1.jpg") merve_face_encoding = face_recognition.face_encodings(merve_image)

known_face_encodings = [ burak_face_encoding, cansu_face_encoding, elif_face_encoding, mert_face_encoding, merve_face_encoding

] known_face_names = [ “布拉克”, "灿速", "ELIF" , “默特”, “MERVE”

]

图片=face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/friends2/friends2.jpg")

face_locations = face_recognition.face_locations(图片) face_encodings = face_recognition.face_encodings(图片,face_locations)

pil_image = Image.fromarray(图片) 平局 = ImageDraw.Draw(pil_image)

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): 名称 =“未知”

for i,f in enumerate(known_face_encodings):
    matches = face_recognition.compare_faces(f, face_encoding)
    if True in matches:
        name = known_face_names[i]
        break

if True in matches:
    first_match_index = matches.index(True)
    name = known_face_names[first_match_index]


draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))


text_width, text_height = draw.textsize(name)
draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(48, 63, 159), outline=(48, 63, 159))
draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 0))

删除绘制

pil_image.show() pil_image pil_image.save("image_with_boxes.jpg")

我的问题是它只显示一个人的名字在已识别的 faces.It 只显示第一个 known_face_names("BURAK") 在所有已识别的面孔上只显示一个人的名字;已识别的同名人员的标签。

据我了解,您的错误来自这里:

matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

消息 ValueError: operands could not be broadcast together with shapes (3,) (128,) 表示 face_encodings 具有维度 3(对于 dlib 人脸编码来说不正常,但对于list) 并且 face_to_compare 的维度为 128(这是正常的 dlib 人脸编码的维度)。

我无法从您上面的代码中看出问题所在。因此,请尝试 print(known_face_encodings) 检查列表是否确实包含该编码。

根据 docsface_recognition.compare_faces 应该可以将数组作为第一个参数 - 但据我所知,它应该是 [=14= 返回的数组].要使用多个图像,循环遍历数组:

# note - not indexing the array with [0]
annem_face_encoding = face_recognition.face_encodings(annem_image)
babam_face_encoding = face_recognition.face_encodings(babam_image)
elif_face_encoding = face_recognition.face_encodings(elif_image)

....

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
    name = "UNKNOWN"
    # Use a loop to compare to each known face
    for i,f in enumerate(known_face_encodings):
        matches = face_recognition.compare_faces(f, face_encoding)
        if True in matches:
            name = known_face_names[i]
            break

     # draw rectangle stuff.....

另请注意,

末尾多了一个逗号
elif_face_encoding = face_recognition.face_encodings(elif_image) [0],

完整代码:

import face_recognition
from PIL import Image, ImageDraw

annem_image = face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/images/annem1.jpg")
annem_face_encoding = face_recognition.face_encodings(annem_image)

babam_image = face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/images/babam1.jpg")
babam_face_encoding = face_recognition.face_encodings(babam_image)

elif_image = face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/images/elif1.jpg")
elif_face_encoding = face_recognition.face_encodings(elif_image)

known_face_encodings = [
    annem_face_encoding,
    babam_face_encoding,
    elif_face_encoding
]
known_face_names = [
    "ANNEM",
    "BABAM",
    "ELIF"
]

image = face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/images/family.jpg")
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)

pil_image = Image.fromarray(image)
draw = ImageDraw.Draw(pil_image)

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
    name = "UNKNOWN"
    for i,f in enumerate(known_face_encodings):
        matches = face_recognition.compare_faces(f, face_encoding)
        if True in matches:
            name = known_face_names[i]
            break

    draw.rectangle(((left, top), (right, bottom)), outline=(48, 63, 159))

    text_width, text_height = draw.textsize(name)
    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(48, 63, 159), outline=(48, 63, 159))
    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 0))


del draw

pil_image.show()
pil_image