2012-04-27 159 views
2

我使用python 2.7和openCV 2.3.1(win 7)。 我試圖打開視頻文件:用openCV + python打開視頻

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

請嘗試使用cv.CaptureFromFile()代替。

如需保護,請複製此代碼:Watch Video in Python with OpenCV

+0

有沒有其他更簡單的方法來做到這一點? – 2014-03-12 09:18:12

+1

這是更簡單的方法。 – karlphillip 2014-03-12 12:57:58

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 
0

由於從this answer,嘗試所有從您的OpenCV安裝.dll文件複製到C:\Python27