-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendervidoverlay.py
331 lines (268 loc) · 10.2 KB
/
rendervidoverlay.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import argparse
import cv2
import imageio
import multiprocessing as mp
import numpy as np
import os
import h5py
import socialutil
NOSE_INDEX = 0
LEFT_EAR_INDEX = 1
RIGHT_EAR_INDEX = 2
BASE_NECK_INDEX = 3
LEFT_FRONT_PAW_INDEX = 4
RIGHT_FRONT_PAW_INDEX = 5
CENTER_SPINE_INDEX = 6
LEFT_REAR_PAW_INDEX = 7
RIGHT_REAR_PAW_INDEX = 8
BASE_TAIL_INDEX = 9
MID_TAIL_INDEX = 10
TIP_TAIL_INDEX = 11
CONNECTED_SEGMENTS = [
[LEFT_FRONT_PAW_INDEX, CENTER_SPINE_INDEX, RIGHT_FRONT_PAW_INDEX],
[LEFT_REAR_PAW_INDEX, BASE_TAIL_INDEX, RIGHT_REAR_PAW_INDEX],
[
NOSE_INDEX, BASE_NECK_INDEX, CENTER_SPINE_INDEX,
BASE_TAIL_INDEX, MID_TAIL_INDEX, TIP_TAIL_INDEX,
],
]
# from: http://colorbrewer2.org/?type=qualitative&scheme=Set3&n=8
# COLOR_PALETTE = [
# (141,211,199),
# (255,255,179),
# (190,186,218),
# (251,128,114),
# (128,177,211),
# (253,180,98),
# (179,222,105),
# (252,205,229),
# ]
COLOR_PALETTE = [
(166,206,227),
(31,120,180),
(178,223,138),
(51,160,44),
(251,154,153),
(227,26,28),
(253,191,111),
(255,127,0),
(202,178,214),
(106,61,154),
(255,255,153)]
def render_pose_overlay(image, frame_points, exclude_points, color=(255 ,255, 255)):
# we need to fragment lines if exclude_points breaks up
# (or removes completely) line segments
def gen_line_fragments():
curr_fragment = []
for curr_pt_indexes in CONNECTED_SEGMENTS:
for curr_pt_index in curr_pt_indexes:
if curr_pt_index in exclude_points:
if len(curr_fragment) >= 2:
yield curr_fragment
curr_fragment = []
else:
curr_fragment.append(curr_pt_index)
if len(curr_fragment) >= 2:
yield curr_fragment
curr_fragment = []
line_pt_indexes = list(gen_line_fragments())
for curr_line_indexes in line_pt_indexes:
line_pts = np.array(
[(pt_x, pt_y) for pt_y, pt_x in frame_points[curr_line_indexes]],
np.int32)
cv2.polylines(image, [line_pts], False, (0, 0, 0), 2, cv2.LINE_AA)
for point_index in range(12):
if point_index in exclude_points:
continue
point_y, point_x = frame_points[point_index, :]
cv2.circle(image, (point_x, point_y), 3, (0, 0, 0), -1, cv2.LINE_AA)
for curr_line_indexes in line_pt_indexes:
line_pts = np.array(
[(pt_x, pt_y) for pt_y, pt_x in frame_points[curr_line_indexes]],
np.int32)
cv2.polylines(image, [line_pts], False, color, 1, cv2.LINE_AA)
for point_index in range(12):
if point_index in exclude_points:
continue
point_y, point_x = frame_points[point_index, :]
cv2.circle(image, (point_x, point_y), 2, color, -1, cv2.LINE_AA)
def render_pose_v3_overlay(
image,
frame_points,
frame_confidence,
frame_track_ids,
exclude_points):
instance_count = frame_points.shape[0]
id_color_dict = dict()
avail_color_idxs = set(range(len(COLOR_PALETTE)))
sorted_ids = sorted(frame_track_ids)
if len(frame_track_ids) <= len(COLOR_PALETTE):
for curr_id in sorted_ids:
curr_color_idx = curr_id % len(COLOR_PALETTE)
offset = 0
while curr_color_idx not in avail_color_idxs:
offset += 1
curr_color_idx = (curr_id + offset) % len(COLOR_PALETTE)
id_color_dict[curr_id] = COLOR_PALETTE[curr_color_idx]
avail_color_idxs.remove(curr_color_idx)
else:
id_color_dict = {i: (255, 255, 255) for i in sorted_ids}
for instance_index in range(instance_count):
# for this instance we add zero confidence points to the
# set of excluded point indexes
inst_confidence = frame_confidence[instance_index, :]
zero_conf_indexes = set((inst_confidence == 0).nonzero()[0])
inst_exclude_points = exclude_points | zero_conf_indexes
render_pose_overlay(
image,
frame_points[instance_index, ...],
inst_exclude_points,
id_color_dict[frame_track_ids[instance_index]])
def process_video(in_video_path, pose_h5_path, out_video_path, exclude_points):
if not os.path.isfile(in_video_path):
print('ERROR: missing file: ' + in_video_path, flush=True)
return
if not os.path.isfile(pose_h5_path):
print('ERROR: missing file: ' + pose_h5_path, flush=True)
return
with imageio.get_reader(in_video_path) as video_reader, \
h5py.File(pose_h5_path, 'r') as pose_h5, \
imageio.get_writer(out_video_path, fps=30) as video_writer:
vid_grp = next(iter(pose_h5.values()))
major_version = 2
if 'version' in vid_grp.attrs:
major_version = vid_grp.attrs['version'][0]
if major_version == 2:
all_points = vid_grp['points'][:]
for frame_index, image in enumerate(video_reader):
render_pose_overlay(
image,
all_points[frame_index, ...],
exclude_points)
video_writer.append_data(image)
elif major_version == 3:
all_points = vid_grp['points'][:]
all_confidence = vid_grp['confidence'][:]
all_instance_count = vid_grp['instance_count'][:]
all_track_id = vid_grp['instance_track_id'][:]
track_dict = socialutil.build_tracks(
all_points, all_confidence, all_instance_count, all_track_id)
for track in track_dict.values():
socialutil.interpolate_missing_points(track, 30, 60)
socialutil.update_track_tensors(
track_dict, all_points, all_confidence, all_instance_count, all_track_id)
for frame_index, image in enumerate(video_reader):
frame_instance_count = all_instance_count[frame_index]
if frame_instance_count > 0:
render_pose_v3_overlay(
image,
all_points[frame_index, :frame_instance_count, ...],
all_confidence[frame_index, :frame_instance_count, ...],
all_track_id[frame_index, :frame_instance_count],
exclude_points)
video_writer.append_data(image)
else:
print('ERROR: unknown version for file format:', vid_grp.attrs['version'])
print('finished generating video:', out_video_path, flush=True)
def process_video_relpath(video_relpath, pose_suffix, in_dir, exclude_points):
pose_suffex_noext, _ = os.path.splitext(pose_suffix)
if len(pose_suffex_noext) == 0:
print('ERROR: bad pose suffix: ' + pose_suffix, flush=True)
return
relpath_noext, _ = os.path.splitext(video_relpath)
in_video_path = os.path.join(in_dir, video_relpath)
pose_h5_path = os.path.join(in_dir, relpath_noext + pose_suffix)
out_video_path = os.path.join(in_dir, relpath_noext + pose_suffex_noext + '.avi')
process_video(in_video_path, pose_h5_path, out_video_path, exclude_points)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--exclude-forepaws',
action='store_true',
dest='exclude_forepaws',
default=False,
help='should we exclude the forepaws',
)
parser.add_argument(
'--exclude-ears',
action='store_true',
dest='exclude_ears',
default=False,
help='should we exclude the ears',
)
subparsers = parser.add_subparsers()
dir_parser = subparsers.add_parser(
'dir',
help='dir subcommand help (for processing a directory of videos)')
dir_parser.set_defaults(subcommand='dir')
dir_parser.add_argument(
'--in-dir',
help='input directory of videos to process',
required=True,
)
dir_parser.add_argument(
'--pose-suffix',
help='the suffix used for pose estimation files (appended to'
' video file after removing extension)',
nargs='+',
required=True,
)
dir_parser.add_argument(
'--num-procs',
help='the number of processes to use',
default=2,
type=int,
)
vid_parser = subparsers.add_parser(
'vid',
help='vid subcommand help (for processing a single video)')
vid_parser.set_defaults(subcommand='vid')
vid_parser.add_argument(
'--in-vid',
help='input video to process',
required=True,
)
vid_parser.add_argument(
'--in-pose',
help='input HDF5 pose file',
required=True,
)
vid_parser.add_argument(
'--out-vid',
help='output pose overlay video to generate',
required=True,
)
args = parser.parse_args()
exclude_points = set()
if args.exclude_forepaws:
exclude_points.add(LEFT_FRONT_PAW_INDEX)
exclude_points.add(RIGHT_FRONT_PAW_INDEX)
if args.exclude_ears:
exclude_points.add(LEFT_EAR_INDEX)
exclude_points.add(RIGHT_EAR_INDEX)
if 'subcommand' in args:
if args.subcommand == 'dir':
files_to_process = []
for dirname, _, filelist in os.walk(args.in_dir):
for fname in filelist:
if fname.lower().endswith('.avi'):
fpath = os.path.join(dirname, fname)
rel_fpath = os.path.relpath(fpath, args.in_dir)
files_to_process.append(rel_fpath)
with mp.Pool(args.num_procs) as p:
for rel_fpath in files_to_process:
for pose_suffix in args.pose_suffix:
p.apply_async(
process_video_relpath,
(rel_fpath, pose_suffix, args.in_dir, exclude_points),
dict(),
lambda x: None,
lambda x: print(x))
p.close()
p.join()
elif args.subcommand == 'vid':
process_video(args.in_vid, args.in_pose, args.out_vid, exclude_points)
else:
print('ERROR: dir or vid subcommand must be specified')
if __name__ == '__main__':
main()