Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

python - sentdex's face_recognition library ValueError: operands could not be broadcast together with shapes

I am relatively new to programming so my debugging skills are way bellow average. My question is how do I fix this: ValueError: operands could not be broadcast together with shapes (128,) (0,) in line 38.

Line 38 is: results = face_recognition.compare_faces(face_encoding, known_faces, TOLERANCE)

This code is slightly modified from sentdex's "face_recognition" library tutorial.

import face_recognition
import os
import cv2
import numpy as np

KNOWN_FACES = "known_faces"
UNKNOWN_FACES = "unknown_faces"
TOLERANCE = 0.6
THICKNESS = 3
MODEL = "cnn"

print("loading known faces")

known_faces = []
known_names = []

for name in os.listdir(KNOWN_FACES):
    dir_path = os.path.join(KNOWN_FACES, name)
    # if it's a directory
    if os.path.isdir(dir_path):
       for filename in os.listdir(dir_path):
          # if the file is a valid file (a better way could be to check your specific extension, e.g., png)
          if not filename.startswith('.'): 
              filepath = os.path.join(dir_path, filename)
              image = face_recognition.load_image_file(filepath)

print("processing unknown_faces")
for filename in os.listdir(UNKNOWN_FACES):
    print(filename)

    image = face_recognition.load_image_file(f"{UNKNOWN_FACES}/{filename}")
    locations = face_recognition.face_locations(image, model=MODEL)
    encodings = face_recognition.face_encodings(image, locations)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    for face_encoding, face_locations in zip(encodings, locations):
        
        results = face_recognition.compare_faces(face_encoding, known_faces, TOLERANCE)
        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])
            bot_right = (face_location[1], face_location[2])

            color = [0, 255, 0]

            cv2.rectangle(image, top_left, bot_right, color, THICKNESS)

            top_left = (face_location[3], face_location[0])
            bot_right = (face_location[1], face_location[2] + 22)
            cv2.rectangle(image, top_left, bot_right, color, cv2.FILLED)
            cv2.putText(image, math, (face_location[3]+10, face_location[2])+15, cv2.FONT_HERSEY_SIMPLEX, 0.5, (200,200,200), THICKNESS)

    cv2.imshow(filename, image)
    cv2.waitKey(10000)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...