为啥 Numpy 会抛出这个错误 ValueError: operands could not be broadcast together with shapes (3,0) (128,)
Posted
技术标签:
【中文标题】为啥 Numpy 会抛出这个错误 ValueError: operands could not be broadcast together with shapes (3,0) (128,)【英文标题】:Why does Numpy throw this error ValueError: operands could not be broadcast together with shapes (3,0) (128,)为什么 Numpy 会抛出这个错误 ValueError: operands could not be broadcast together with shapes (3,0) (128,) 【发布时间】:2021-06-29 12:28:13 【问题描述】:python 文件正常读取视频文件并在桌面上播放。它引发了一个我无法解决的错误,因此非常感谢任何帮助和指针。视频是 mp4 格式,编解码器也可能会导致问题,因为我看到有些人转换为 avi,或者这与播放正常有关。
最后一个错误调用来自第 57 行:
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)```
Any help, much appreciated!!
import face_recognition
import cv2
import numpy as np
import os
import glob
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture("/home/dave/Desktop/smots_1.mp4")
#make array of sample pictures with encodings
known_face_encodings = []
known_face_names = []
dirname = os.path.dirname(__file__)
path = os.path.join(dirname, 'known_people/')
#make an array of all the saved jpg files' paths
list_of_files = [f for f in glob.glob(path+'*.jpg')]
#find number of known faces
number_files = len(list_of_files)
names = list_of_files.copy()
for i in range(number_files):
globals()['image_'.format(i)] = face_recognition.load_image_file(list_of_files[i])
globals()['image_encoding_'.format(i)] = face_recognition.face_encodings(globals()
['image_'.format(i)])#[0]
known_face_encodings.append(globals()['image_encoding_'.format(i)])
# Create array of known names
names[i] = names[i].replace("known_people/", "")
known_face_names.append(names[i])
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# Capture a single frame of video
ret, frame = video_capture.read()
# Resize video frame
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
# Process every 2nd frame
if process_this_frame:
# Find all the faces & face encodings in video Frame
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# Check face match in known faces
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
# Show Result
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label for face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the video
cv2.imshow('Video', frame)
# 'q' quits the video window
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release video handle
video_capture.release()
【问题讨论】:
【参考方案1】:感谢 Aakash 的回复,从那以后我就明白了。不再支持不规则的嵌套数组。 第 57 行缺少 np.array='object':
matches = face_recognition.compare_faces(known_face_encodings, face_encoding) ```
【讨论】:
以上是关于为啥 Numpy 会抛出这个错误 ValueError: operands could not be broadcast together with shapes (3,0) (128,)的主要内容,如果未能解决你的问题,请参考以下文章