-
Notifications
You must be signed in to change notification settings - Fork 27
/
util.py
134 lines (110 loc) · 4.63 KB
/
util.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import argparse
import os
from functools import wraps
from easydict import EasyDict
def retry(tries=5):
def deco_retry(f):
@wraps(f)
def f_retry(*args, **kwargs):
mtries = tries
while mtries > 1:
try:
return f(*args, **kwargs)
except:
mtries -= 1
return f(*args, **kwargs)
return f_retry # true decorator
return deco_retry
class RawTextArgumentDefaultsHelpFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawTextHelpFormatter):
# RawTextHelpFormatter implements _split_lines
# ArgumentDefaultsHelpFormatter implements _get_help_string
# so we can guess that they will work together just fine.
pass
def parse_args():
parser = argparse.ArgumentParser(formatter_class=RawTextArgumentDefaultsHelpFormatter)
# Names and folders
parser.add_argument("annotation_file", type=str, help="The annotation file, in json format")
parser.add_argument("--db_name", type=str, help="The database to store extracted frames")
parser.add_argument("--db_type", type=str, choices=["LMDB", "HDF5", "FILE", "PKL"], default="HDF5",
help="Type of the database")
parser.add_argument("--tmp_dir", type=str, default="/tmp", help="Temporary folder")
# Clips
parser.add_argument("--clips", type=int, default=1, help="Num of clips per video")
parser.add_argument("--duration", type=float, default=-1, help="Length of each clip")
# Resize mode
parser.add_argument("--resize_mode", type=int, default=0, choices=[0, 1, 2],
help="Resize mode\n"
" 0: Do not resize\n"
" 1: 800x600: Resize to WxH\n"
" 2: L600 or S600: keep the aspect ration and scale the longer/shorter side to s"
)
parser.add_argument("--resize", type=str, help="Parameter of resize mode")
# Frame sampling options
parser.add_argument("--fps", type=float, default=-1, help="Sample the video at X fps")
parser.add_argument("--sample_mode", type=int, default=0, choices=[0, 1, 2, 3, 4],
help="Frame sampling options\n"
" 0: Keep all frames\n"
" 1: Uniformly sample n frames\n"
" 2: Randomly sample n continuous frames\n"
" 3: Randomly sample n frames\n"
" 4: Sample 1 frame every n frames"
)
parser.add_argument("--sample", type=int, help="How many frames")
# performance
parser.add_argument("--threads", type=int, default=0, help="Number of threads")
parser.add_argument("--keep", action="store_true", help="Do not delete temporary files at last")
args = parser.parse_args()
args = EasyDict(args.__dict__)
args = modify_args(args)
return args
def modify_args(args):
# check the options
if not args.db_name:
if args.annotation_file.lower().endswith(".json"):
args.db_name = args.annotation_file[:-5]
else:
args.db_name = args.annotation_file
if args.db_name.lower().endswith(".hdf5"):
args.db_type = 'HDF5'
elif args.db_name.lower().endswith(".lmdb"):
args.db_type = 'LMDB'
else:
if args.db_type == 'HDF5':
args.db_name += ".hdf5"
elif args.db_type == 'LMDB':
args.db_name += ".lmdb"
# Range check
args.clips = max(args.clips, 1)
args.duration = max(args.duration, 0)
# Parse the resize mode
args.vf_setting = []
if args.resize_mode == 0:
pass
elif args.resize_mode == 1:
W, H, *_ = args.resize.split("x")
W, H = int(W), int(H)
assert W > 0 and H > 0
args.vf_setting.extend([
"-vf", "scale={}:{}".format(W, H)
])
elif args.resize_mode == 2:
side = args.resize[0].lower()
assert side in ['l', 's'], "The (L)onger side, or the (S)horter side?"
scale = int(args.resize[1:])
assert scale > 0
args.vf_setting.extend([
"-vf",
"scale='iw*1.0/{0}(iw,ih)*{1}':'ih*1.0/{0}(iw,ih)*{1}'".format("max" if side == 'l' else 'min', scale)
])
else:
raise Exception('Unspecified frame scale option')
# Parse the fps setting
if args.fps > 0:
args.vf_setting.extend([
"-r", "{}".format(args.fps)
])
if args.threads:
if args.threads < 0:
args.threads = max(os.cpu_count() / 2, 1)
return args