I have a video (.mp4) that has ~8000 frames. I have a csv that tells me the time that I need to grab each frame at in the video, and the number of frames to grab.
number_of_frames in video = 8000
times is an array like [0.004, 0.005, ... 732s]
The last time in the data given is at 732s. Therefore the FPS = 8000 / 732 = ~10
I want to be able to extract the image frames from the video at those specific times. Then write those image paths to a .csv file.
I tried multiple approaches: First approach(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],
})
This approach however, did not give me the total number of frames desired. It just gave me the number of frames that would correspond to a video at FPS = 25. I believe my FPS = 8000 / 732s = 10.928s.
I then tried using moviepy to capture each image in a similar style:
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],
})
However this approach did not work either, for some reason I am capturing the last frame in the video hundreds of times.
This code works OK to extract frames at different times:
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)
What's the content of your ground_truth
variable?
Try this
from PIL import Image
from moviepy.editor import *
clip = VideoFileClip("video.mp4")
img = Image.fromarray(clip.get_frame(1))
img.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With