首页 > 解决方案 > OpenCV 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'imshow'

问题描述

我是 OpenCV 的新手,正在尝试在我的计算机上制作实时面部跟踪器。我的视频输入似乎有问题(它没有得到任何东西吗?)。我在 Mac 笔记本电脑上,正在尝试使用我的内置网络摄像头。

错误:

cv2.error: OpenCV(4.3.0) /Users/travis/build/skvark/opencv- 
python/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 
&& size.height>0 in function 'imshow'

代码:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while True:
    #capture frame by frame
    ret, img = cap.read()
    cv2.imshow('test', img)


cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()
cap.release()

我试图将 cv2.VideoCapture(0) 更改为 cv2.VideoCapture(1),但这导致了这个错误:

You might be loading two sets of Qt binaries into the same process. Check that all plugins 
are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that 
only one set of binaries are being loaded.
QObject::moveToThread: Current thread (0x7ff06a82c5d0) is not the object's thread 
(0x7ff06a876c30).
Cannot move to target thread (0x7ff06a82c5d0)

标签: pythonpython-3.xnumpyopencv

解决方案


import cv2
import numpy as np

cap = cv2.VideoCapture(0)

# Check if camera opened successfully
if (cap.isOpened()== False): 
  print("Error opening video stream or file")

while(cap.isOpened()):
  ret, frame = cap.read()
  if ret == True:

    cv2.imshow('Frame',frame)

    if cv2.waitKey(25) & 0xFF == ord('q'):
      break

  else: 
    break

cap.release()

cv2.destroyAllWindows()

推荐阅读