-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderbehavioroverlay.py
371 lines (314 loc) · 14.5 KB
/
renderbehavioroverlay.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import argparse
import cv2
import h5py
import imageio
import numpy as np
import os
from pathlib import Path
import urllib.parse as urlparse
import yaml
import gensocialstats
import rendervidoverlay
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,
],
]
OVERLAY_COLOR = (255, 102, 0)
# colors from color brewer: https://colorbrewer2.org/?type=qualitative&scheme=Paired&n=9
QUALITATIVE_COLORS = [
(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),
]
FRAME_BUFFER = 30 * 3
VIDEO_ANNOTATION_PADDING_PX = 112
TEXT_HEIGHT_PX = 22
FRAME_NUM_VERTICAL_OFFSET_PX = 90
# share_root='/run/user/1000/gvfs/smb-share:server=bht2stor.jax.org,share=vkumar'
# python -u ~/projects/social-interaction/renderbehavioroverlay.py \
# --batch-file ~/projects/rotta-data/rotta-labeler-comp_2020_12_18.txt \
# --video-root-dir "${share_root}" \
# --behavior-root-dirs rotta-labeler-comp_2020_12_18_arojit \
# rotta-labeler-comp_2020_12_18_yehya \
# --behavior Approach \
# --annotator-names AM YB \
# --out-dir behavior-out-vids-arojit-yehya \
# --allow-missing-video
# share_root='/run/user/1000/gvfs/smb-share:server=bht2stor.jax.org,share=vkumar'
# python -u ~/projects/social-interaction/renderbehavioroverlay.py \
# --batch-file ~/projects/rotta-data/rotta-labeler-comp_2020_12_18.txt \
# --video-root-dir "${share_root}" \
# --behavior-root-dirs rotta-labeler-comp_2020_12_18_arojit \
# rotta-labeler-comp_2020_12_18_yehya \
# --behavior Leave \
# --annotator-names AM YB \
# --out-dir behavior-out-vids-arojit-yehya \
# --allow-missing-video
# share_root='/run/user/1000/gvfs/smb-share:server=bht2stor.jax.org,share=vkumar'
# python -u ~/projects/social-interaction/renderbehavioroverlay.py \
# --batch-file ~/projects/rotta-data/rotta-labeler-comp_2020_12_18.txt \
# --video-root-dir "${share_root}" \
# --behavior-root-dirs rotta-labeler-comp_2020_12_18_arojit \
# rotta-labeler-comp_2020_12_18_yehya \
# --behavior Chase \
# --annotator-names AM YB \
# --out-dir behavior-out-vids-arojit-yehya \
# --allow-missing-video
# share_root='/run/user/1000/gvfs/smb-share:server=bht2stor.jax.org,share=vkumar'
# python -u ~/projects/social-interaction/renderbehavioroverlay.py \
# --batch-file ~/projects/rotta-data/rotta-labeler-comp_2021-01-27_amelie/rotta-labeler-comp_2020_12_18.txt \
# --video-root-dir "${share_root}" \
# --behavior-root-dirs rotta-labeler-comp_2021-01-27_amelie \
# rotta-labeler-comp_2021-01-27_arojit \
# rotta-labeler-comp_2021-01-27_yehya \
# --behavior Leave \
# --behavior-aliases Leave Leave_A_P Leave2 \
# --annotator-names AB AM YB \
# --out-dir rotta-labeler-comp_2021-01-27_vid-out3 \
# --allow-missing-video
#
# share_root='/run/user/1000/gvfs/smb-share:server=bht2stor.jax.org,share=vkumar'
# python -u ~/projects/social-interaction/renderbehavioroverlay.py \
# --batch-file ~/projects/rotta-data/rotta-labeler-comp_2021-01-27_amelie/rotta-labeler-comp_2020_12_18.txt \
# --video-root-dir "${share_root}" \
# --behavior-root-dirs rotta-labeler-comp_2021-01-27_arojit \
# rotta-labeler-comp_2021-01-27_yehya \
# --behavior Leave \
# --behavior-aliases Leave_A_P Leave2 \
# --annotator-names AM YB \
# --out-dir rotta-labeler-comp_2021-01-27_vid-out2 \
# --allow-missing-video
def main():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--batch-file',
help='the batch file',
)
group.add_argument(
'--video-file',
help='the single video to process',
)
parser.add_argument(
'--video-root-dir',
help='input root directory for videos',
default='.',
)
parser.add_argument(
'--behavior-root-dirs',
nargs='+',
help='input root directories for behavior data (HDF5 files)',
required=True,
)
parser.add_argument(
'--behavior',
help='behavior to process',
required=True,
)
parser.add_argument(
'--behavior-aliases',
nargs='+',
help='if the labelers used names that don\'t match the given'
' behavior you can use this option to list what users'
' specified. This option should either be missing or'
' it should have the same length as "--behavior-root-dirs"',
default=[],
)
parser.add_argument(
'--annotator-names',
nargs='+',
help='names to match up with each annotator',
required=True,
)
parser.add_argument(
'--out-dir',
help='output directory for behavior clips',
required=True,
)
parser.add_argument(
'--allow-missing-video',
help='allow missing videos with warning',
action='store_true',
)
args = parser.parse_args()
assert len(args.annotator_names) == len(args.behavior_root_dirs)
video_root_dir = Path(args.video_root_dir)
behavior_root_dirs = [Path(d) for d in args.behavior_root_dirs]
out_dir = Path(args.out_dir)
exclude_points = set()
exclude_points.add(rendervidoverlay.LEFT_FRONT_PAW_INDEX)
exclude_points.add(rendervidoverlay.RIGHT_FRONT_PAW_INDEX)
exclude_points.add(rendervidoverlay.LEFT_EAR_INDEX)
exclude_points.add(rendervidoverlay.RIGHT_EAR_INDEX)
def gen_net_ids():
if args.video_file is not None:
yield args.video_file
else:
with open(args.batch_file, 'r') as batch_file:
for net_id in batch_file:
yield net_id.strip()
for net_id in gen_net_ids():
net_id = net_id.strip()
print(f"Working on {net_id}")
#rotta-labeler-comp_2020_12_18_amelie/NV16-UCSD/2019-11-07/3901455_2019-11-08_11-00-00_behavior/v1/Approach/3901455_2019-11-08_11-00-00.h5
vid_path = video_root_dir / net_id
pose_path = vid_path.parent / (vid_path.stem + '_pose_est_v3.h5')
if not vid_path.exists():
print(f'Skipping because "{vid_path}" does not exist')
continue
pose_exists = pose_path.exists()
# if not pose_exists:
# print(f'Skipping because "{pose_path}" does not exist')
# continue
if pose_exists:
with h5py.File(pose_path, 'r') as pose_data:
all_points = pose_data['poseest/points'][:]
all_instance_count = pose_data['poseest/instance_count'][:]
all_instance_track_id = pose_data['poseest/instance_track_id'][:]
all_points_mask = pose_data['poseest/confidence'][:] > 0
def find_track_for_frame(frame_index, candidate_ids):
frame_instance_count = all_instance_count[frame_index]
for frame_track_id in all_instance_track_id[frame_index, :frame_instance_count]:
if int(frame_track_id) in candidate_ids:
# print('FOUND IT', frame_track_id)
return int(frame_track_id)
return None
collapsed_pred_class = []
all_pred_class = []
all_id_to_tracks = []
for i, behavior_root_dir in enumerate(behavior_root_dirs):
curr_behavior = args.behavior
if args.behavior_aliases:
curr_behavior = args.behavior_aliases[i]
h5_path = behavior_root_dir / net_id
h5_path = h5_path.parent / (h5_path.stem + '_behavior') / 'v1' / curr_behavior / (h5_path.stem + '.h5')
# print(f'"{net_id}": {vid_path.exists()}, {h5_path}, {h5_path.exists()}')
with h5py.File(h5_path, 'r') as h5_data:
pred_class = h5_data['predictions/predicted_class'][:] == 1
pred_any_true = pred_class.any(axis=0)
all_pred_class.append(pred_class)
collapsed_pred_class.append(pred_any_true)
id_to_track = h5_data['predictions/identity_to_track'][:]
id_count, frame_count = id_to_track.shape
id_to_tracks = []
for curr_id in range(id_count):
track_id_set = set()
for curr_track_id in id_to_track[curr_id, :]:
if curr_track_id >= 0:
track_id_set.add(int(curr_track_id))
id_to_tracks.append(track_id_set)
all_id_to_tracks.append(id_to_tracks)
collapsed_pred_class = np.stack(collapsed_pred_class)
annotator_count, frame_count = collapsed_pred_class.shape
out_video_path = out_dir / args.behavior / net_id
out_video_path.parent.mkdir(parents=True, exist_ok=True)
with imageio.get_reader(vid_path) as video_reader, \
imageio.get_writer(out_video_path, fps=30) as video_writer:
for frame_index, frame in enumerate(video_reader):
active_track_to_annotators = dict()
if frame_index % 10000 == 0:
print(f"Processed frame {frame_index + 1}")
win_start = max(0, frame_index - FRAME_BUFFER)
win_stop = min(frame_count, frame_index + FRAME_BUFFER + 1)
keep_frame = collapsed_pred_class[:, win_start:win_stop].sum() >= 1
if keep_frame:
frame_row_count, frame_col_count, frame_color_count = frame.shape
annotation_start_row = frame_row_count - VIDEO_ANNOTATION_PADDING_PX
cv2.putText(
frame,
'Frame #: {}'.format(frame_index + 1),
(5, annotation_start_row + FRAME_NUM_VERTICAL_OFFSET_PX),
cv2.FONT_HERSHEY_COMPLEX,
1.0,
OVERLAY_COLOR,
)
track_id_to_true_annos = dict()
for anno_index in range(annotator_count):
# print('anno_index:', anno_index)
behavior_active = collapsed_pred_class[anno_index, frame_index] == 1
anno_x = 5
anno_y = annotation_start_row + FRAME_NUM_VERTICAL_OFFSET_PX - ((anno_index + 1) * 30)
cv2.putText(
frame,
f"{args.behavior} - {args.annotator_names[anno_index]}",
(anno_x + TEXT_HEIGHT_PX * 2, anno_y),
cv2.FONT_HERSHEY_COMPLEX,
1.0,
OVERLAY_COLOR,
)
cv2.rectangle(
frame,
(anno_x, anno_y - TEXT_HEIGHT_PX),
(anno_x + TEXT_HEIGHT_PX, anno_y),
OVERLAY_COLOR,
cv2.FILLED if behavior_active else 1,
)
if pose_exists:
if behavior_active:
curr_anno_all_preds = all_pred_class[anno_index]
id_count, frame_count = curr_anno_all_preds.shape
curr_anno_id_to_tracks = all_id_to_tracks[anno_index]
for curr_id in range(id_count):
if curr_anno_all_preds[curr_id, frame_index]:
# the current annotator has called true for behavior
# for the current ID at the current frame. We still
# need to locate the corresponding track
active_track = find_track_for_frame(
frame_index,
all_id_to_tracks[anno_index][curr_id])
if active_track is not None:
if active_track in active_track_to_annotators:
active_track_to_annotators[active_track].append(anno_index)
else:
active_track_to_annotators[active_track] = [anno_index]
for track, anno_ids in active_track_to_annotators.items():
# get track location
frame_instance_count = all_instance_count[frame_index]
curr_frame_track_ids = [
int(tid)
for tid in all_instance_track_id[frame_index, :frame_instance_count]
]
frame_track_index = curr_frame_track_ids.index(track)
if all_points_mask[frame_index, frame_track_index, CENTER_SPINE_INDEX]:
center_spine_y, center_spine_x = all_points[frame_index, frame_track_index, CENTER_SPINE_INDEX, :]
cv2.circle(
frame,
(center_spine_x, center_spine_y),
5,
OVERLAY_COLOR,
cv2.FILLED,
)
cv2.putText(
frame,
', '.join(sorted([args.annotator_names[i] for i in anno_ids])),
(center_spine_x + 6, center_spine_y + 6),
cv2.FONT_HERSHEY_COMPLEX,
1.0,
OVERLAY_COLOR,
)
video_writer.append_data(frame)
if __name__ == '__main__':
main()