forked from fabbrimatteo/JTA-Dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_imgs.py
54 lines (43 loc) · 1.84 KB
/
to_imgs.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
# ---------------------
import sys
import click
import imageio
from path import Path
imageio.plugins.ffmpeg.download()
H1 = 'directory where you want to save the extracted frames'
H2 = 'number from which to start counting the video frames; DEFAULT = 1'
H3 = 'the format to use to save the images/frames; DEFAULT = jpg'
# check python version
assert sys.version_info >= (3, 6), '[!] This script requires Python >= 3.6'
@click.command()
@click.option('--out_dir_path', type=click.Path(), prompt='Enter \'out_dir_path\'', help=H1)
@click.option('--first_frame', type=int, default=1, help=H2)
@click.option('--img_format', type=str, default='jpg', help=H3)
def main(out_dir_path, first_frame, img_format):
# type: (str, int, str) -> None
"""
Script that splits all the videos into frames and saves them
in a specified directory with the desired format
"""
out_dir_path = Path(out_dir_path)
if not out_dir_path.exists():
out_dir_path.makedirs()
for dir in Path('videos').dirs():
out_subdir_path = out_dir_path / dir.basename()
if not out_subdir_path.exists():
out_subdir_path.makedirs()
print(f'▸ extracting \'{dir.basename()}\' set')
for video in dir.files():
out_seq_path = out_subdir_path / video.basename().split('.')[0]
if not out_seq_path.exists():
out_seq_path.makedirs()
reader = imageio.get_reader(video)
print(f'▸ extracting frames of \'{Path(video).abspath()}\'')
for frame_number, image in enumerate(reader):
n = first_frame + frame_number
imageio.imwrite(out_seq_path / f'{n}.{img_format}', image)
print(f'\r▸ progress: {100 * (frame_number / 899):6.2f}%', end='')
print()
if __name__ == '__main__':
main()