-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_video.py
38 lines (34 loc) · 1001 Bytes
/
parse_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import argparse
import cv2
import os
import glob
from pathlib import Path
# example usage: python parse_video.py --folder RoboTurk_videos/bins-Bread/
def vid_to_frames(fname):
fname_base = Path(fname).stem
try:
os.mkdir(fname_base)
except FileExistsError as error:
pass
vidcap = cv2.VideoCapture(fname)
success,image = vidcap.read()
count = 0
while success:
frame_fname = fname_base + '/frame_{:04d}.jpg'.format(count)
cv2.imwrite(frame_fname, image)
success,image = vidcap.read()
count += 1
def convert_vid_folder(folder):
os.chdir(folder)
files = glob.glob('*.mp4')
count = 0
for file in files:
vid_to_frames(file)
if (count % 10 == 0):
print('{}/{}'.format(count, len(files)))
count += 1
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--folder', type=str)
args = parser.parse_args()
convert_vid_folder(args.folder)