无法使用opencv 3.1和python 3.6打开视频

时间:2018-03-25 06:55:10

标签: python opencv

我正在尝试使用工业相机“catchBest”使用opencv 3.1和python 3.6打开视频。它的驱动程序已安装,设备列在设备管理器中。但它并没有使用opencv开放。

以下是代码:

import cv2
video = cv2.VideoCapture(0)

while True:
    cam = video.read()
    cv2.imshow("video", cam)
    cv2.waitKey(0)
video.release()    
cv2.destroyAllWindows()

我试过从0到9的索引,但它不起作用。

1 个答案:

答案 0 :(得分:0)

这应该有效:

import cv2
#you can run a loop from 0 to 100 to check which gives true, if you're unsure which number to use.
video = cv2.VideoCapture(0)
print(video.isOpened()) #this should return True if camera opens successfully.
while True:
    #first returned value of video.read() is boolean and second is frame so we have to use second.
    ret,cam = video.read() 
    cv2.imshow("video",cam)
   #you have to break from infinite loop to release the camera usage, here I'm using escape key to break you can choose any.
    if cv2.waitKey(1) & 0xFF == 27:
        break
video.release()
cv2.destroyAllWindows()