本文介绍了如何解决"cv2.错误:(-215)size.width> 0&& size.height>在功能"imshow"中为"0"; Python错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于通过网络摄像头捕获视频的代码:

This is the code I am using for capturing video through my webcam:

import cv2
cap = cv2.VideoCapture(0)

while True:
    ret, frame= cap.read()
    # frame= cv2.resize(frame, None,fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
    cv2.imshow('Input', frame)
    c = cv2.waitKey(1)
    if c == 27:
        break
cap.release()
cv2.destroyAllWindows()

错误是:

我该如何解决?每当我运行上面的代码时,ret标志就永远不会为True.我已经尝试过sudo pip3 install opencv-contrib-python,但仍然无济于事.

How do I solve it? whenever I am running the code above, the ret flag is never True. I have tried sudo pip3 install opencv-contrib-python but still it is not helping.

推荐答案

imshow失败.就您而言,这很可能是由于您忽略了cap.read()中的空白帧并调用了imshow的事实,即使cap.read返回了空白图像.

imshow fails when you pass an empty image, as pointed out by @101. In your case, this is most likely due to the fact that you ignore empty frames from cap.read() and call imshow even if cap.read returned an empty image.

要解决此问题,请用while cap.isOpened()替换while True循环,或者在读取帧之前检查cap.isOpened()是否成功.

To fix this, replace your while True loop with a while cap.isOpened() or check whether cap.isOpened() has been successful before reading frames.

这篇关于如何解决"cv2.错误:(-215)size.width> 0&& size.height>在功能"imshow"中为"0"; Python错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:31