我正在使用python 3.6,但出现此错误
回溯(最近通话最近): 文件“ C:\ Users \ mchaf \ Music \ face \ facerec_from_webcam_faster.py”,第49行,在 small_frame = cv2.resize(frame,(128,128)) cv2.error:OpenCV(3.4.5)C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ resize.cpp:3784:error:(-215:Assertion failed)!ssize.empty()在函数中'cv :: resize'
我该怎么做才能解决此问题? 这是我的行代码
from distutils.core import setup
import face_recognition
from cv2 import *
import subprocess
import time
video_capture = cv2.VideoCapture(0)
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
known_face_encodings = [
obama_face_encoding,
biden_face_encoding
]
known_face_names = [
"Barack Obama",
"Joe Biden"
]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (128,128))
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_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:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
time.sleep(5)
imshow("Operator",frame)
video_capture.release()
cv2.destroyAllWindows()
subprocess.call([r'C:\Users\mchaf\Desktop\run.bat'])
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
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)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
答案 0 :(得分:0)
尝试将帧处理置于这种状态
if ret:
cv2.resize...
并使用
删除行Video_capture.release()
cv2.destroyAllWindows()
看起来您的框架是空的,这就是为什么您无法调整大小 原因是您从网络摄像头释放了捕获(断开连接)。释放后,您想读取新框架,但它为空
您的代码显然是从不同的地方复制的。即使您更正了这些,您的代码也无法正常工作。
例如:
...
name = "Unknown"
time.sleep(5) # Why sleep?
imshow("Operator",frame) # OpenCV cv2 class is missing
video_capture.release() # Why you release the capture? - main error
cv2.destroyAllWindows() # Why you destroy window if you will want to see result in the next loop
subprocess.call([r'C:\Users\mchaf\Desktop\run.bat'])
...