2017-03-31 55 views
3

我有一個有〜8000幀的視頻(.mp4)。我有一個csv,告訴我需要抓取視頻中每個幀的時間以及要抓取的幀數。 number_of_frames in video = 8000 times是一個數組像[0.004, 0.005, ... 732s] 在給出的數據中的最後一次是在732s。因此FPS = 8000/732 = ~10使用opencv和python或moviepy提取圖像

我希望能夠在這些特定時間從視頻中提取圖像幀。然後將這些圖像路徑寫入.csv文件。

我試過多種方法: 第一種方法(OpenCV的):

with open('./data/driving.csv', 'w') as csvfile: 
fieldnames = ['image_path', 'time', 'speed'] 
writer = csv.DictWriter(csvfile, fieldnames = fieldnames) 
writer.writeheader() 
vidcap = cv2.VideoCapture('./data/drive.mp4') 
for idx, item in enumerate(ground_truth): 
    # set video capture to specific time frame 
    # multiply time by 1000 to convert to milliseconds 
    vidcap.set(cv2.CAP_PROP_POS_MSEC, item[0] * 1000) 
    # read in the image 
    success, image = vidcap.read() 
    if success: 
     image_path = os.path.join('./data/IMG/', str(item[0]) + 
    '.jpg') 
     # save image to IMG folder 
     cv2.imwrite(image_path, image) 
     # write row to driving.csv 
     writer.writerow({'image_path': image_path, 
       'time':item[0], 
       'speed':item[1], 
       }) 

這種做法卻並沒有給我想要的幀的總數。它只給了我對應於FPS = 25視頻的幀數。我相信我的FPS = 8000/732s = 10.928s。

我然後使用moviepy捕捉到類似的風格每個圖像的嘗試:

from moviepy.editor import VideoFileClip 
clip1 = VideoFileClip('./data/drive.mp4') 
with open('./data/driving.csv', 'w') as csvfile: 
    fieldnames = ['image_path', 'time', 'speed'] 
    writer = csv.DictWriter(csvfile, fieldnames = fieldnames) 
    writer.writeheader() 

    # Path to raw image folder 
    abs_path_to_IMG = os.path.join('./data/IMG/') 
    for idx, item in enumerate(ground_truth): 
     image_path = os.path.join('./data/IMG/', str(item[0]) + '.jpg') 
     clip1.save_frame(image_path, t = item[0]) 
     # write row to driving.csv 
     writer.writerow({'image_path': image_path, 
      'time':item[0], 
      'speed':item[1], 
      }) 

但是這種方法也不能工作,由於某種原因,我捕捉視頻數百倍的最後一幀。

回答

2

此代碼確定在不同的時間提取幀:

import os 
from moviepy.editor import * 

def extract_frames(movie, times, imgdir): 
    clip = VideoFileClip(movie) 
    for t in times: 
     imgpath = os.path.join(imgdir, '{}.png'.format(t)) 
     clip.save_frame(imgpath, t) 

movie = 'movie.mp4' 
imgdir = 'frames' 
times = 0.1, 0.63, 0.947, 1.2, 3.8, 6.7 

extract_frames(movie, times, imgdir) 

什麼是你ground_truth變量的內容?

相關問題