question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValueError: operands could not be broadcast together with shapes (2,1,128) (413,369,3)

See original GitHub issue
  • face_recognition version:1.2.3
  • Python version: 3.5.4
  • Operating System: Windows 10 (x64)

Description

Hi everyone. I am building a facial recognition software that is supposed to identify faces with openCV, take the identified faces, save them as a .jpg file and use that said file for comparison, however there seemed to be some complication when I use the face_recognition.compare_faces function.

What I Did

` def getMatches(self): matches = [] encodings = [] name = “” faceNames = [ “Jass”,“Neo” ] faceToCheck = fr.load_image_file(“facelock.jpg”) faceEncoding = fr.face_encodings(faceToCheck)[0]

    face2 = fr.load_image_file("Neo.jpg") 
    encodings.append(fr.face_encodings(face2)[0])
    
    matches = fr.compare_faces(encodings, faceEncoding)
    name = "UNKNOWN"

    if True in matches:
        ind = matches.index(True)
        name = faceNames[ind]
        print("The name is:" +str(name))

` I am getting this: Traceback (most recent call last): File “C:\Users\Janda\Desktop\MUKA FINAL\MukaMain.py”, line 201, in getFace self.getMatches() File “C:\Users\Janda\Desktop\MUKA FINAL\MukaMain.py”, line 219, in getMatches matches = fr.compare_faces(encodings, faceEncoding) File “C:\Users\Janda\AppData\Local\Programs\Python\Python35-32\lib\site-packages\face_recognition-1.2.3-py3.5.egg\face_recognition\api.py”, line 222, in compare_faces return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance) File “C:\Users\Janda\AppData\Local\Programs\Python\Python35-32\lib\site-packages\face_recognition-1.2.3-py3.5.egg\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 (1,128) (103,103,3)

Here are the images: Facelock facelock Neo neo

Thank you for your help 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
Gangeya003commented, May 1, 2020

The issue is when we encode images with improper light on your face, the method face_encoding returns an empty list and when you compare this with that new image’s encoding this error occurs… this might even occur in the reverse case.

0reactions
root-Akshaycommented, Mar 23, 2020

@NeoDaenium @ageitgey I am getting this error , tried to resolve by looking some solution mentioned on web but could’nt make it work :

import face_recognition import os import cv2

KNOWN_FACES_DIR = “known_faces” UNKNOWN_FACES_DIR = “unknown_faces” TOLERANCE = 0.6 FRAME_THICKNESS = 3 FONT_THICKNESS = 2 MODEL = “cnn” #hog for cpu, cnn works slow on cpu

print(“Loading known faces”)

known_faces = [] known_names = []

for name in os.listdir(KNOWN_FACES_DIR): for filename in os.listdir(f"{KNOWN_FACES_DIR}/{name}“): image = face_recognition.load_image_file(f”{KNOWN_FACES_DIR}/{name}/{filename}") # encoding = face_recognition.face_encodings(image)[0] encoding = face_recognition.face_encodings(image) known_faces.append(encoding) # print(known_faces) known_names.append(name) print(known_names)

print("processing unknown faces ")

for filename in os.listdir(UNKNOWN_FACES_DIR): print(filename) image = face_recognition.load_image_file(f"{UNKNOWN_FACES_DIR}/{filename}") locaations = face_recognition.face_locations(image, model=MODEL) encodings = face_recognition.face_encodings(image, locaations) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

for face_encoding, face_location in zip(encodings, locaations):
    results = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE)
    print(results)
    match = None
    if True in results:
        match = known_names[results.index(True)]
        print(f"Match Found : {match}")

        top_left = (face_location[3], face_location[0])
        bottom_right = (face_location[1], face_location[2])

        color = [0, 255, 0]

        cv2.rectangle(image, top_left, bottom_right, color, FRAME_THICKNESS)

        top_left = (face_location[3], face_location[2])
        bottom_right = (face_location[1], face_location[2]+22)
        cv2.rectangle(image, top_left, bottom_right, color, cv2.FILLED)
        cv2.putText(image, match, (face_location[2]+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200,200,200), FONT_THICKNESS)


cv2.imshow(filename, image)
cv2.waitKey(0)
cv2.destroyWindow(filename)

The problem lies over here, encoding = face_recognition.face_encodings(image) known_faces.append(encoding)

Instead of appending encoded list into another list, directly pass the encoding to face_recognition.compare_faces()

known_encoding = face_recognition.face_encodings(image) results = face_recognition.compare_faces(known_encoding, face_encoding, TOLERANCE)

I get what you are trying to achieve with this code, for a rectangle border to be shown on the image:

cv2.rectangle(image, top_left, bottom_right, color, cv2.BORDER_WRAP)

Read more comments on GitHub >

github_iconTop Results From Across the Web

python numpy ValueError: operands could not be broadcast ...
When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two ...
Read more >
How to Fix: ValueError: operands could not be ... - Statology
This tutorial explains how to fix the following error in Python: operands could not be broadcast together with shapes.
Read more >
How to Fix: ValueError: Operands could not ... - GeeksforGeeks
It throws an error like operands could not be broadcast together with shapes. There are some scenarios when broadcasting can occur and when ......
Read more >
Operands could not be broadcast together with shapes
The operands could not be broadcast together with shapes error will comes when you multiply two arrays. Know how to solve this TypeError....
Read more >
operands could not be broadcast together with shapes while ...
The answer to this question would be : The objects created by the xs method of the Pandas DataFrame look like two-dimensional arrays...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found