用openCV + python打开视频

时间:2012-04-27 16:02:23

标签: python opencv video-capture

我使用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)),则此代码有效。 关于我做错了什么的任何想法? 非常感谢大家!

3 个答案:

答案 0 :(得分:3)

请尝试使用cv.CaptureFromFile()

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

答案 1 :(得分:1)

你可以使用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

答案 2 :(得分:0)

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