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

2024-05-13 07:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图打开一个视频使用opencv3.1和python3.6使用工业相机“catchBest”。它的驱动程序已安装,设备列在设备管理器中。但它不是用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的索引,但不起作用。你知道吗


Tags: 代码importtrue管理器read视频video驱动程序
1条回答
网友
1楼 · 发布于 2024-05-13 07:30:02

这应该起作用:

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()

相关问题 更多 >