【问题标题】:Cannot open a video using opencv 3.1 and python 3.6无法使用 opencv 3.1 和 python 3.6 打开视频
【发布时间】:2018-03-25 06:55:09
【问题描述】:

我正在尝试使用工业相机 "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 的索引,但它不起作用。

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    这应该可行:

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

    【讨论】:

    • 问题不在于索引号。问题是opencv没有检测到摄像头。
    猜你喜欢
    • 2015-12-29
    • 2015-09-11
    • 2017-10-19
    • 1970-01-01
    • 2021-03-22
    • 2012-05-08
    • 2015-09-04
    • 2017-05-03
    • 2014-06-29
    相关资源
    最近更新 更多