2016-09-21 128 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