2017-03-31 16 views
3

私は〜8000フレームのビデオ(.mp4)を持っています。私はビデオで各フレームをつかむ必要がある時間と、取得するフレームの数を教えてくれているCSVを持っています。 number_of_frames in video = 8000 timesは配列のようです[0.004, 0.005, ... 732s] 与えられたデータの前回は732sです。したがって、これらの特定の時刻にビデオからイメージフレームを抽出できます。次に、それらのイメージパスを.csvファイルに書き込みます。私は複数のアプローチを試みたopencvとpythonまたはmoviepyを使って画像を抽出します

: 最初のアプローチ(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

このコードは、異なる時間にフレームを抽出するために、OK作品:

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変数の内容は何ですか?

関連する問題