【问题标题】:Opening video with openCV +python使用 openCV +python 打开视频
【发布时间】:2012-05-08 09:36:48
【问题描述】:

我使用 python 2.7 和 openCV 2.3.1 (win 7)。 我正在尝试打开视频文件:

stream = cv.VideoCapture("test1.avi")
if stream.isOpened() == False:
print "Cannot open input video!"
exit()

但我有警告:

warning: Error opening file (../../modules/highgui/src/cap_ffmpeg_impl_v2.hpp:394)

如果使用摄像机 (stream = cv.VideoCapture(0)),此代码有效。 关于我做错了什么的任何想法? 非常感谢大家!

【问题讨论】:

    标签: python opencv video-capture


    【解决方案1】:

    尝试改用cv.CaptureFromFile()。

    如果必须,请复制此代码:Watch Video in Python with OpenCV。

    【讨论】:

    • 还有其他更简单的方法吗?
    • 这是更简单的方法。
    【解决方案2】:

    您可以使用 OpenCV (cv2) 的新接口,它是从 c++ 绑定的面向对象的接口。 我发现它更容易阅读。

    注意:如果你用这个打开图片,fps 没有任何意义,所以图片保持静止。

    import cv2
    import sys
    
    try:
        vidFile = cv2.VideoCapture(sys.argv[1])
    except:
        print "problem opening input stream"
        sys.exit(1)
    if not vidFile.isOpened():
        print "capture stream not open"
        sys.exit(1)
    
    nFrames = int(vidFile.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) # one good way of namespacing legacy openCV: cv2.cv.*
    print "frame number: %s" %nFrames
    fps = vidFile.get(cv2.cv.CV_CAP_PROP_FPS)
    print "FPS value: %s" %fps
    
    ret, frame = vidFile.read() # read first frame, and the return code of the function.
    while ret:  # note that we don't have to use frame number here, we could read from a live written file.
        print "yes"
        cv2.imshow("frameWindow", frame)
        cv2.waitKey(int(1/fps*1000)) # time to wait between frames, in mSec
        ret, frame = vidFile.read() # read next frame, get next return code
    

    【讨论】:

      【解决方案3】:

      从this answer 开始,尝试将所有.dll 文件从您的OpenCV 安装复制到C:\Python27。

      【讨论】:

        猜你喜欢
        • 2014-10-02
        • 2015-12-29
        • 2015-09-11
        • 1970-01-01
        • 2017-10-19
        • 2012-11-27
        • 1970-01-01
        • 2021-03-22
        • 2016-07-10
        相关资源
        最近更新 更多