forked from open-mmlab/mmaction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_rawframes.py
159 lines (137 loc) · 5.67 KB
/
build_rawframes.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import argparse
import sys
import os
import os.path as osp
import glob
from pipes import quote
from multiprocessing import Pool, current_process
import mmcv
def dump_frames(vid_item):
full_path, vid_path, vid_id = vid_item
vid_name = vid_path.split('.')[0]
out_full_path = osp.join(args.out_dir, vid_name)
try:
os.mkdir(out_full_path)
except OSError:
pass
vr = mmcv.VideoReader(full_path)
for i in range(len(vr)):
if vr[i] is not None:
mmcv.imwrite(
vr[i], '{}/img_{:05d}.jpg'.format(out_full_path, i + 1))
else:
print('[Warning] length inconsistent!'
'Early stop with {} out of {} frames'.format(i + 1, len(vr)))
break
print('{} done with {} frames'.format(vid_name, len(vr)))
sys.stdout.flush()
return True
def run_optical_flow(vid_item, dev_id=0):
full_path, vid_path, vid_id = vid_item
vid_name = vid_path.split('.')[0]
out_full_path = osp.join(args.out_dir, vid_name)
try:
os.mkdir(out_full_path)
except OSError:
pass
current = current_process()
dev_id = (int(current._identity[0]) - 1) % args.num_gpu
image_path = '{}/img'.format(out_full_path)
flow_x_path = '{}/flow_x'.format(out_full_path)
flow_y_path = '{}/flow_y'.format(out_full_path)
cmd = osp.join(args.df_path, 'build/extract_gpu') + \
' -f={} -x={} -y={} -i={} -b=20 -t=1 -d={} -s=1 -o={} -w={} -h={}' \
.format(
quote(full_path),
quote(flow_x_path), quote(flow_y_path), quote(image_path),
dev_id, args.out_format, args.new_width, args.new_height)
os.system(cmd)
print('{} {} done'.format(vid_id, vid_name))
sys.stdout.flush()
return True
def run_warp_optical_flow(vid_item, dev_id=0):
full_path, vid_path, vid_id = vid_item
vid_name = vid_path.split('.')[0]
out_full_path = osp.join(args.out_dir, vid_name)
try:
os.mkdir(out_full_path)
except OSError:
pass
current = current_process()
dev_id = (int(current._identity[0]) - 1) % args.num_gpu
flow_x_path = '{}/flow_x'.format(out_full_path)
flow_y_path = '{}/flow_y'.format(out_full_path)
cmd = osp.join(args.df_path + 'build/extract_warp_gpu') + \
' -f={} -x={} -y={} -b=20 -t=1 -d={} -s=1 -o={}'.format(
quote(full_path), quote(flow_x_path), quote(flow_y_path),
dev_id, args.out_format)
os.system(cmd)
print('warp on {} {} done'.format(vid_id, vid_name))
sys.stdout.flush()
return True
def parse_args():
parser = argparse.ArgumentParser(description='extract optical flows')
parser.add_argument('src_dir', type=str)
parser.add_argument('out_dir', type=str)
parser.add_argument('--level', type=int,
choices=[1, 2],
default=2)
parser.add_argument('--num_worker', type=int, default=8)
parser.add_argument('--flow_type', type=str,
default=None, choices=[None, 'tvl1', 'warp_tvl1'])
parser.add_argument('--df_path', type=str,
default='../../mmaction/third_party/dense_flow')
parser.add_argument("--out_format", type=str, default='dir',
choices=['dir', 'zip'], help='output format')
parser.add_argument("--ext", type=str, default='avi',
choices=['avi', 'mp4'], help='video file extensions')
parser.add_argument("--new_width", type=int, default=0,
help='resize image width')
parser.add_argument("--new_height", type=int,
default=0, help='resize image height')
parser.add_argument("--num_gpu", type=int, default=8, help='number of GPU')
parser.add_argument("--resume", action='store_true', default=False,
help='resume optical flow extraction '
'instead of overwriting')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
if not osp.isdir(args.out_dir):
print('Creating folder: {}'.format(args.out_dir))
os.makedirs(args.out_dir)
if args.level == 2:
classes = os.listdir(args.src_dir)
for classname in classes:
new_dir = osp.join(args.out_dir, classname)
if not osp.isdir(new_dir):
print('Creating folder: {}'.format(new_dir))
os.makedirs(new_dir)
print('Reading videos from folder: ', args.src_dir)
print('Extension of videos: ', args.ext)
if args.level == 2:
fullpath_list = glob.glob(args.src_dir + '/*/*.' + args.ext)
done_fullpath_list = glob.glob(args.out_dir + '/*/*')
elif args.level == 1:
fullpath_list = glob.glob(args.src_dir + '/*.' + args.ext)
done_fullpath_list = glob.glob(args.out_dir + '/*')
print('Total number of videos found: ', len(fullpath_list))
if args.resume:
fullpath_list = set(fullpath_list).difference(set(done_fullpath_list))
fullpath_list = list(fullpath_list)
print('Resuming. number of videos to be done: ', len(fullpath_list))
if args.level == 2:
vid_list = list(map(lambda p: osp.join(
'/'.join(p.split('/')[-2:])), fullpath_list))
elif args.level == 1:
vid_list = list(map(lambda p: p.split('/')[-1], fullpath_list))
pool = Pool(args.num_worker)
if args.flow_type == 'tvl1':
pool.map(run_optical_flow, zip(
fullpath_list, vid_list, range(len(vid_list))))
elif args.flow_type == 'warp_tvl1':
pool.map(run_warp_optical_flow, zip(
fullpath_list, vid_list, range(len(vid_list))))
else:
pool.map(dump_frames, zip(
fullpath_list, vid_list, range(len(vid_list))))