ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.36k stars 13.48k forks source link

ValueError: operands could not be broadcast together with shapes #848

Open Mutleymuffin opened 5 years ago

Mutleymuffin commented 5 years ago

Description

I followed your guide on the wiki as to how to save faces to a file, but I have encountered an issue. Whenever I run using an image which contains an unknown face, it returns an error:

ValueError: operands could not be broadcast together with shapes (3,128) (10,128)

I have discovered that this only appears when the image which is being searched contains a face which is unknown to the stored faces in the file. Could you please provide me with a code solution to avoid this error appearing when there is an unknown face present.

Other people who have asked this question have either not received a useful answer or haven't posted how they've fixed it.

What I Did

CODE:

import face_recognition
import pickle
import numpy as np

with open('all_faces.dat', 'rb') as f:
    all_face_encodings = pickle.load(f)

face_names = list(all_face_encodings.keys())
face_encodings = np.array(list(all_face_encodings.values()))

unknown_image = face_recognition.load_image_file("FaceRecImages/GroupPhoto.jpg")
unknown_face = face_recognition.face_encodings(unknown_image)
result = face_recognition.compare_faces(face_encodings, unknown_face)

names_with_result = list(zip(face_names, result))
print(names_with_result)

ERROR:

Traceback (most recent call last):
  File "/Users/Sam/Documents/Programming/FaceRecPython/FaceRecPHOTOSFILE.py", line 16, in <module>
    result = face_recognition.compare_faces(face_encodings, unknown_face)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/face_recognition/api.py", line 222, in compare_faces
    return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/face_recognition/api.py", line 72, in face_distance
    return np.linalg.norm(face_encodings - face_to_compare, axis=1)
ValueError: operands could not be broadcast together with shapes (3,128) (10,128) 
ageitgey commented 5 years ago

Your photo seems to contain (at least) 10 people. When you call this function:

unknown_face = face_recognition.face_encodings(unknown_image)

That doesn't give you one unknown_face. That gives you an array of 10 unknown faces it found in the image.

so instead of doing this:

unknown_face = face_recognition.face_encodings(unknown_image)
result = face_recognition.compare_faces(face_encodings, unknown_face)

You need to do this instead to handle each detected face individually:

# unknown faces is an array with one entry for each detected face
unknown_faces = face_recognition.face_encodings(unknown_image)

for unknown_face in unknown_faces:
    result = face_recognition.compare_faces(face_encodings, unknown_face)
   # Do whatever you want with the result for each face