Search code examples
pythonpython-3.xpython-moduleface-recognition

Unable to see True value after performing face_recognition.compare_faces


I am creating a python script and using the module face_recognition to perform comparing the unknown celebrity image to the folder, famos_figures which consists of three images, Barack Obama, John Cena and Bruce Willis to determine if there is any match? The unknown celebrity image is Barack Obama and it should match but the result did not show any TRUE value. I want to show if there is a match and show the result that the unknown image is the Barack Obama.

  1. I performed loading all the images from the famous_figures folder and the unknown_image using face_recognition.load_image_file function.
  2. Then I perform face_recognition.face_encodings for all images and compile all the famous images into a variable, known_faces
  3. Then I perform comparison of the known_face to the unknown_face using the function, face_recognition.compare_faces and load the output to result. I print the result but the result does not show any True value..

I have listed the images below for your review.

import face_recognition
#Load the jpg files into numpy arrays
obama_image = face_recognition.load_image_file("/home/timothy/Desktop/famous_figures/Barack Obama.jpg")
john_image = face_recognition.load_image_file("/home/timothy/Desktop/famous_figures/John_Cena.jpg")
bruce_image = face_recognition.load_image_file("/home/timothy/Desktop/famous_figures/Bruce_Willis.jpg")
unknown_image = face_recognition.load_image_file("/home/timothy/Desktop/famous_figures/unknown_celebrity.jpg")

try:
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    john_face_encoding = face_recognition.face_encodings(john_image)[0]
    bruce_face_encoding = face_recognition.face_encodings(bruce_image)[0]

    unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
except IndexError:
    print("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting...")
    quit()

known_faces = [
    obama_face_encoding,
    justin_face_encoding,
    ryan_face_encoding
]

# results is an array of True/False telling if the unknown face matched anyone in the 
known_faces array
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
print(results)

The results of my code show the output as shown below. There is no true value

(False, False)

I want the output to be

Yes there is a match. The image is Barack Obama.

Please kindly help.

Thank you.

Barack Obama

Bruce Willis

John Cena

unknown


Solution

  • Here you are creating face encodings as variables, named obama.., john... and bruce...:

    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    john_face_encoding = face_recognition.face_encodings(john_image)[0]
    bruce_face_encoding = face_recognition.face_encodings(bruce_image)[0]
    

    But the list of known face encodings you are creating from the DIFFERENT variables, such as justine.. and ryan...

    known_faces = [
        obama_face_encoding,
        justin_face_encoding,
        ryan_face_encoding]
    

    Normally your code should not work at all because missing variables justin_face_encoding and ryan_face_encoding

    Finally, to get the output such as

    "Here is Obama"

    you can use the following:

    list_of_known_celebs = ["Obama", "John", "Bruce"]
    for i, result in enumerate(results):
        if result:
            print(f"Here is {list_of_known_celebs[i]}")