python - small_frame = cv2.resize(frame, (128,128)) cv2.error: OpenCV(3.4.5) error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
问题描述
我正在使用 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() in function '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()
解决方案
尝试将帧处理置于此条件下
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'])
...
推荐阅读
- php - 正则表达式在本地和服务器上给出不同的结果
- java - 如何将嵌套 JSONObject/JSONArray 转换为 flatJSON 并生成多条记录
- python - Python烧瓶 - 在嵌套循环中使用变量值
- r - 如何将 sf 对象绑定在一起
- c# - 如何合并日期 + 时间并传递给 CRM 日期时间字段
- python - Androguard: 'NameError: name 'AnalyzeAPK' is not defined' 错误
- c# - 如何将多个相似的方法组合成一个返回三个结果的方法?
- node.js - res.redirect 永远不会在 NodeJS 中使用 express 和 passport
- mysql-workbench - 在 unicenta 中连接多个用户 mysql 数据库
- git - 如何将 Git Bash 中的文件 cp 到新目录而无需 mv?