Search code examples
pythonface-recognition

How can I fix Face Recognition operands error?


import face_recognition from PIL import Image, 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 = [ "BURAK", "CANSU", "ELIF" , "MERT" , "MERVE"

]

image = face_recognition.load_image_file("C:/Users/EMRE/Desktop/codfacerecog-master/codfacerecog-master/recognize/friends2/friends2.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

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))

del draw

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

MY PROBLEM IS that It shows only one person's name on identified faces.It only shows first known_face_names("BURAK")shows only one person's name on all identified faces; tags of identified people with the same name.


Solution

  • According to the docs, face_recognition.compare_faces should be able to take an array as the first parameter - but as far as I can tell, it should be the array returned by face_recognition.face_encodings. To use multiple images, loop through the array:

    # 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.....
    

    Also note, you have an extra comma at the end of

    elif_face_encoding = face_recognition.face_encodings(elif_image) [0],
    

    Full code:

    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