网络摄像头正在打开,但无法阅读

时间:2019-06-03 08:57:53

标签: python opencv

cv2.VideoCapture()运行正常,但是cap.read()无法正常工作。程序中没有错误。

代码运行良好,网络摄像头打开了几秒钟,但我无法查看实时草图。

要了解该问题,我写了print("hello")语句并发现 ret,frame=cap.read()造成了问题。

在刷新窗口之前,代码运行正常。

import cv2

cap=cv2.VideoCapture(0)

while True:
    print ("hello")
    ret,frame=cap.read()
    print ("hello")
    cv2.imshow("Our Live Sketch",frame)
    if cv2.waitKey(1)==13:
        break;

cap.release()
cv2.destroyAllWindows()

此代码将如何像以前一样再次工作?

1 个答案:

答案 0 :(得分:0)

OpenCV可以使用许多后端进行视频捕获-videoio。 为了从网络摄像机捕获,Windows可以使用vfw(旧),DirectShow(旧)和MSMF(现代)后端。 默认情况下,在最新的Windows 10中使用MSMF。但是某些网络摄像机只有DirectShow驱动程序。它需要手动设置:

cap=cv2.VideoCapture(0 + cv2.CAP_DSHOW)
相关问题