我有一个大约 8000 帧的视频 (.mp4)。我有一个 csv,它告诉我需要在视频中抓取每一帧的时间,以及要抓取的帧数。
number_of_frames in video = 8000
times 是一个数组,如[0.004, 0.005, ... 732s]
给定数据中的最后一次是 732s。因此FPS = 8000 / 732 = ~10
我希望能够在这些特定时间从视频中提取图像帧。然后将这些图像路径写入 .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],
})
然而,这种方法也不起作用,由于某种原因,我正在捕捉视频中的最后一帧数百次。