2016-09-21 68 views
1

我想每隔10秒从视频文件中捕捉一帧,所以如果有人能帮助我,我会非常感激。我的Python代码就是这样:每10秒钟从视频文件中捕捉一帧

import cv2 


print(cv2.__version__) 
vidcap = cv2.VideoCapture('Standoff.avi') 
vidcap.set(cv2.CAP_PROP_POS_MSEC,96000) 
success,image = vidcap.read() 
count = 0 
success = True 
while success: 
    success,image = vidcap.read() 
    print 'Read a new frame: ', success 
    cv2.imwrite("frame%d.jpg" % count, image)  # save frame as JPEG file 
    cv2.waitKey(200) 
    count += 1 
+0

你能解释一下你与你当前的代码有问题? – Mick

回答

0

如果你可以从文件中的视频的帧率以下应工作(您可能需要检查语法,因为我没有测试过):

import numpy as np 
import cv2 

cap = cv2.VideoCapture('Standoff.avi') 
framerate = cap.get(cv2.cv.CV_CAP_PROP_FPS) 
framecount = 0 

while(True): 
    # Capture frame-by-frame 
    success, image = cap.read() 
    frame count += 1 

    # Check if this is the frame closest to 10 seconds 
    if framecount = (framerate * 10) 
     framecont = 0 
     cv2.imshow('image',image) 

    # Check end of video 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
      break 

# When everything done, release the capture 
cap.release() 
cv2.destroyAllWindows() 
+0

非常感谢,它给了我一个解决方案的想法。 –

1

我在这里经过10转换frames.you捕捉帧可以使用定时功能,同样捕捉帧if条件语句

import cv2 

vidcap = cv2.VideoCapture('testing.mp4'); 
success,image = vidcap.read() 
count = 0 
success = True 

while success: 
    success,image = vidcap.read() 
    print('read a new frame:',success) 
    if count%10 == 0 : 
     cv2.imwrite('frame%d.jpg'%count,image) 
     print('success') 
    count+=1