-
Notifications
You must be signed in to change notification settings - Fork 104
/
tracker.py
executable file
·713 lines (554 loc) · 26.2 KB
/
tracker.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import importlib
import os
import numpy as np
from collections import OrderedDict
from pytracking.evaluation.environment import env_settings
import time
import cv2 as cv
from pytracking.utils.visdom import Visdom
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from pytracking.utils.plotting import draw_figure, overlay_mask
from pytracking.utils.convert_vot_anno_to_rect import convert_vot_anno_to_rect
from ltr.data.bounding_box_utils import masks_to_bboxes
from pytracking.evaluation.multi_object_wrapper import MultiObjectWrapper
from pathlib import Path
import torch
import ltr.data.processing_utils as prutils
_tracker_disp_colors = {1: (0, 255, 0), 2: (0, 0, 255), 3: (255, 0, 0),
4: (255, 255, 255), 5: (0, 0, 0), 6: (0, 255, 128),
7: (123, 123, 123), 8: (255, 128, 0), 9: (128, 0, 255)}
def trackerlist(name: str, parameter_name: str, run_ids = None, display_name: str = None):
"""Generate list of trackers.
args:
name: Name of tracking method.
parameter_name: Name of parameter file.
run_ids: A single or list of run_ids.
display_name: Name to be displayed in the result plots.
"""
if run_ids is None or isinstance(run_ids, int):
run_ids = [run_ids]
return [Tracker(name, parameter_name, run_id, display_name) for run_id in run_ids]
class Tracker:
"""Wraps the tracker for evaluation and running purposes.
args:
name: Name of tracking method.
parameter_name: Name of parameter file.
run_id: The run id.
display_name: Name to be displayed in the result plots.
"""
def __init__(self, name: str, parameter_name: str, run_id: int = None, display_name: str = None):
assert run_id is None or isinstance(run_id, int)
self.name = name
self.parameter_name = parameter_name
self.run_id = run_id
self.display_name = display_name
env = env_settings()
if self.run_id is None:
self.results_dir = '{}/{}/{}'.format(env.results_path, self.name, self.parameter_name)
self.segmentation_dir = '{}/{}/{}'.format(env.segmentation_path, self.name, self.parameter_name)
else:
self.results_dir = '{}/{}/{}_{:03d}'.format(env.results_path, self.name, self.parameter_name, self.run_id)
self.segmentation_dir = '{}/{}/{}_{:03d}'.format(env.segmentation_path, self.name, self.parameter_name, self.run_id)
tracker_module_abspath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'tracker', self.name))
if os.path.isdir(tracker_module_abspath):
tracker_module = importlib.import_module('pytracking.tracker.{}'.format(self.name))
self.tracker_class = tracker_module.get_tracker_class()
else:
self.tracker_class = None
self.visdom = None
def _init_visdom(self, visdom_info, debug):
visdom_info = {} if visdom_info is None else visdom_info
self.pause_mode = False
self.step = False
if debug > 0 and visdom_info.get('use_visdom', True):
try:
self.visdom = Visdom(debug, {'handler': self._visdom_ui_handler, 'win_id': 'Tracking'},
visdom_info=visdom_info)
# Show help
help_text = 'You can pause/unpause the tracker by pressing ''space'' with the ''Tracking'' window ' \
'selected. During paused mode, you can track for one frame by pressing the right arrow key.' \
'To enable/disable plotting of a data block, tick/untick the corresponding entry in ' \
'block list.'
self.visdom.register(help_text, 'text', 1, 'Help')
except:
time.sleep(0.5)
print('!!! WARNING: Visdom could not start, so using matplotlib visualization instead !!!\n'
'!!! Start Visdom in a separate terminal window by typing \'visdom\' !!!')
def _visdom_ui_handler(self, data):
if data['event_type'] == 'KeyPress':
if data['key'] == ' ':
self.pause_mode = not self.pause_mode
elif data['key'] == 'ArrowRight' and self.pause_mode:
self.step = True
def create_tracker(self, params):
tracker = self.tracker_class(params)
tracker.visdom = self.visdom
return tracker
def run_sequence(self, seq, visualization=None, debug=None, visdom_info=None, multiobj_mode=None):
"""Run tracker on sequence.
args:
seq: Sequence to run the tracker on.
visualization: Set visualization flag (None means default value specified in the parameters).
debug: Set debug level (None means default value specified in the parameters).
visdom_info: Visdom info.
multiobj_mode: Which mode to use for multiple objects.
"""
params = self.get_parameters()
visualization_ = visualization
debug_ = debug
if debug is None:
debug_ = getattr(params, 'debug', 0)
if visualization is None:
if debug is None:
visualization_ = getattr(params, 'visualization', False)
else:
visualization_ = True if debug else False
params.visualization = visualization_
params.debug = debug_
self._init_visdom(visdom_info, debug_)
if visualization_ and self.visdom is None:
self.init_visualization()
# Get init information
init_info = seq.init_info()
is_single_object = not seq.multiobj_mode
if multiobj_mode is None:
multiobj_mode = getattr(params, 'multiobj_mode', getattr(self.tracker_class, 'multiobj_mode', 'default'))
if multiobj_mode == 'default' or is_single_object:
tracker = self.create_tracker(params)
elif multiobj_mode == 'parallel':
tracker = MultiObjectWrapper(self.tracker_class, params, self.visdom)
else:
raise ValueError('Unknown multi object mode {}'.format(multiobj_mode))
output = self._track_sequence(tracker, seq, init_info)
return output
def _track_sequence(self, tracker, seq, init_info):
# Define outputs
# Each field in output is a list containing tracker prediction for each frame.
# In case of single object tracking mode:
# target_bbox[i] is the predicted bounding box for frame i
# time[i] is the processing time for frame i
# segmentation[i] is the segmentation mask for frame i (numpy array)
# In case of multi object tracking mode:
# target_bbox[i] is an OrderedDict, where target_bbox[i][obj_id] is the predicted box for target obj_id in
# frame i
# time[i] is either the processing time for frame i, or an OrderedDict containing processing times for each
# object in frame i
# segmentation[i] is the multi-label segmentation mask for frame i (numpy array)
output = {'target_bbox': [],
'time': [],
'segmentation': []}
def _store_outputs(tracker_out: dict, defaults=None):
defaults = {} if defaults is None else defaults
for key in output.keys():
val = tracker_out.get(key, defaults.get(key, None))
if key in tracker_out or val is not None:
output[key].append(val)
# Initialize
image = self._read_image(seq.frames[0])
if tracker.params.visualization and self.visdom is None:
self.visualize(image, init_info.get('init_bbox'))
start_time = time.time()
out = tracker.initialize(image, init_info)
if out is None:
out = {}
prev_output = OrderedDict(out)
init_default = {'target_bbox': init_info.get('init_bbox'),
'time': time.time() - start_time,
'segmentation': init_info.get('init_mask')}
_store_outputs(out, init_default)
for frame_num, frame_path in enumerate(seq.frames[1:], start=1):
while True:
if not self.pause_mode:
break
elif self.step:
self.step = False
break
else:
time.sleep(0.1)
image = self._read_image(frame_path)
start_time = time.time()
info = seq.frame_info(frame_num)
info['previous_output'] = prev_output
out = tracker.track(image, info)
prev_output = OrderedDict(out)
_store_outputs(out, {'time': time.time() - start_time})
segmentation = out['segmentation'] if 'segmentation' in out else None
if self.visdom is not None:
tracker.visdom_draw_tracking(image, out['target_bbox'], segmentation)
elif tracker.params.visualization:
self.visualize(image, out['target_bbox'], segmentation)
for key in ['target_bbox', 'segmentation']:
if key in output and len(output[key]) <= 1:
output.pop(key)
return output
def run_video(self, videofilepath, optional_box=None, debug=None, visdom_info=None, save_results=False):
"""Run the tracker with the vieofile.
args:
debug: Debug level.
"""
params = self.get_parameters()
debug_ = debug
if debug is None:
debug_ = getattr(params, 'debug', 0)
params.debug = debug_
params.tracker_name = self.name
params.param_name = self.parameter_name
self._init_visdom(visdom_info, debug_)
multiobj_mode = getattr(params, 'multiobj_mode', getattr(self.tracker_class, 'multiobj_mode', 'default'))
if multiobj_mode == 'default':
tracker = self.create_tracker(params)
if hasattr(tracker, 'initialize_features'):
tracker.initialize_features()
elif multiobj_mode == 'parallel':
tracker = MultiObjectWrapper(self.tracker_class, params, self.visdom, fast_load=True)
else:
raise ValueError('Unknown multi object mode {}'.format(multiobj_mode))
assert os.path.isfile(videofilepath), "Invalid param {}".format(videofilepath)
", videofilepath must be a valid videofile"
output_boxes = []
cap = cv.VideoCapture(videofilepath)
display_name = 'Display: ' + tracker.params.tracker_name
cv.namedWindow(display_name, cv.WINDOW_NORMAL | cv.WINDOW_KEEPRATIO)
cv.resizeWindow(display_name, 960, 720)
success, frame = cap.read()
cv.imshow(display_name, frame)
def _build_init_info(box):
return {'init_bbox': OrderedDict({1: box}), 'init_object_ids': [1, ], 'object_ids': [1, ],
'sequence_object_ids': [1, ]}
if success is not True:
print("Read frame from {} failed.".format(videofilepath))
exit(-1)
if optional_box is not None:
assert isinstance(optional_box, (list, tuple))
assert len(optional_box) == 4, "valid box's foramt is [x,y,w,h]"
tracker.initialize(frame, _build_init_info(optional_box))
output_boxes.append(optional_box)
else:
while True:
# cv.waitKey()
frame_disp = frame.copy()
cv.putText(frame_disp, 'Select target ROI and press ENTER', (20, 30), cv.FONT_HERSHEY_COMPLEX_SMALL,
1.5, (0, 0, 0), 1)
x, y, w, h = cv.selectROI(display_name, frame_disp, fromCenter=False)
init_state = [x, y, w, h]
tracker.initialize(frame, _build_init_info(init_state))
output_boxes.append(init_state)
break
while True:
ret, frame = cap.read()
if frame is None:
break
frame_disp = frame.copy()
# Draw box
out = tracker.track(frame)
state = [int(s) for s in out['target_bbox'][1]]
output_boxes.append(state)
cv.rectangle(frame_disp, (state[0], state[1]), (state[2] + state[0], state[3] + state[1]),
(0, 255, 0), 5)
font_color = (0, 0, 0)
cv.putText(frame_disp, 'Tracking!', (20, 30), cv.FONT_HERSHEY_COMPLEX_SMALL, 1,
font_color, 1)
cv.putText(frame_disp, 'Press r to reset', (20, 55), cv.FONT_HERSHEY_COMPLEX_SMALL, 1,
font_color, 1)
cv.putText(frame_disp, 'Press q to quit', (20, 80), cv.FONT_HERSHEY_COMPLEX_SMALL, 1,
font_color, 1)
# Display the resulting frame
cv.imshow(display_name, frame_disp)
key = cv.waitKey(1)
if key == ord('q'):
break
elif key == ord('r'):
ret, frame = cap.read()
frame_disp = frame.copy()
cv.putText(frame_disp, 'Select target ROI and press ENTER', (20, 30), cv.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
(0, 0, 0), 1)
cv.imshow(display_name, frame_disp)
x, y, w, h = cv.selectROI(display_name, frame_disp, fromCenter=False)
init_state = [x, y, w, h]
tracker.initialize(frame, _build_init_info(init_state))
output_boxes.append(init_state)
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
if save_results:
if not os.path.exists(self.results_dir):
os.makedirs(self.results_dir)
video_name = Path(videofilepath).stem
base_results_path = os.path.join(self.results_dir, 'video_{}'.format(video_name))
tracked_bb = np.array(output_boxes).astype(int)
bbox_file = '{}.txt'.format(base_results_path)
np.savetxt(bbox_file, tracked_bb, delimiter='\t', fmt='%d')
def run_webcam(self, debug=None, visdom_info=None):
"""Run the tracker with the webcam.
args:
debug: Debug level.
"""
params = self.get_parameters()
debug_ = debug
if debug is None:
debug_ = getattr(params, 'debug', 0)
params.debug = debug_
params.tracker_name = self.name
params.param_name = self.parameter_name
self._init_visdom(visdom_info, debug_)
multiobj_mode = getattr(params, 'multiobj_mode', getattr(self.tracker_class, 'multiobj_mode', 'default'))
if multiobj_mode == 'default':
tracker = self.create_tracker(params)
elif multiobj_mode == 'parallel':
tracker = MultiObjectWrapper(self.tracker_class, params, self.visdom, fast_load=True)
else:
raise ValueError('Unknown multi object mode {}'.format(multiobj_mode))
class UIControl:
def __init__(self):
self.mode = 'init' # init, select, track
self.target_tl = (-1, -1)
self.target_br = (-1, -1)
self.new_init = False
def mouse_callback(self, event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN and self.mode == 'init':
self.target_tl = (x, y)
self.target_br = (x, y)
self.mode = 'select'
elif event == cv.EVENT_MOUSEMOVE and self.mode == 'select':
self.target_br = (x, y)
elif event == cv.EVENT_LBUTTONDOWN and self.mode == 'select':
self.target_br = (x, y)
self.mode = 'init'
self.new_init = True
def get_tl(self):
return self.target_tl if self.target_tl[0] < self.target_br[0] else self.target_br
def get_br(self):
return self.target_br if self.target_tl[0] < self.target_br[0] else self.target_tl
def get_bb(self):
tl = self.get_tl()
br = self.get_br()
bb = [min(tl[0], br[0]), min(tl[1], br[1]), abs(br[0] - tl[0]), abs(br[1] - tl[1])]
return bb
ui_control = UIControl()
cap = cv.VideoCapture(0)
display_name = 'Display: ' + self.name
cv.namedWindow(display_name, cv.WINDOW_NORMAL | cv.WINDOW_KEEPRATIO)
cv.resizeWindow(display_name, 960, 720)
cv.setMouseCallback(display_name, ui_control.mouse_callback)
next_object_id = 1
sequence_object_ids = []
prev_output = OrderedDict()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
frame_disp = frame.copy()
info = OrderedDict()
info['previous_output'] = prev_output
if ui_control.new_init:
ui_control.new_init = False
init_state = ui_control.get_bb()
info['init_object_ids'] = [next_object_id, ]
info['init_bbox'] = OrderedDict({next_object_id: init_state})
sequence_object_ids.append(next_object_id)
next_object_id += 1
# Draw box
if ui_control.mode == 'select':
cv.rectangle(frame_disp, ui_control.get_tl(), ui_control.get_br(), (255, 0, 0), 2)
if len(sequence_object_ids) > 0:
info['sequence_object_ids'] = sequence_object_ids
out = tracker.track(frame, info)
prev_output = OrderedDict(out)
if 'segmentation' in out:
frame_disp = overlay_mask(frame_disp, out['segmentation'])
if 'target_bbox' in out:
for obj_id, state in out['target_bbox'].items():
state = [int(s) for s in state]
cv.rectangle(frame_disp, (state[0], state[1]), (state[2] + state[0], state[3] + state[1]),
_tracker_disp_colors[obj_id], 5)
# Put text
font_color = (0, 0, 0)
cv.putText(frame_disp, 'Select target', (20, 30), cv.FONT_HERSHEY_COMPLEX_SMALL, 1, font_color, 1)
cv.putText(frame_disp, 'Press r to reset', (20, 55), cv.FONT_HERSHEY_COMPLEX_SMALL, 1,
font_color, 1)
cv.putText(frame_disp, 'Press q to quit', (20, 85), cv.FONT_HERSHEY_COMPLEX_SMALL, 1,
font_color, 1)
# Display the resulting frame
cv.imshow(display_name, frame_disp)
key = cv.waitKey(1)
if key == ord('q'):
break
elif key == ord('r'):
next_object_id = 1
sequence_object_ids = []
prev_output = OrderedDict()
info = OrderedDict()
info['object_ids'] = []
info['init_object_ids'] = []
info['init_bbox'] = OrderedDict()
tracker.initialize(frame, info)
ui_control.mode = 'init'
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
def run_vot2020(self, debug=None, visdom_info=None):
params = self.get_parameters()
params.tracker_name = self.name
params.param_name = self.parameter_name
params.run_id = self.run_id
debug_ = debug
if debug is None:
debug_ = getattr(params, 'debug', 0)
if debug is None:
visualization_ = getattr(params, 'visualization', False)
else:
visualization_ = True if debug else False
params.visualization = visualization_
params.debug = debug_
self._init_visdom(visdom_info, debug_)
tracker = self.create_tracker(params)
tracker.initialize_features()
output_segmentation = tracker.predicts_segmentation_mask()
import pytracking.evaluation.vot2020 as vot
def _convert_anno_to_list(vot_anno):
vot_anno = [vot_anno[0], vot_anno[1], vot_anno[2], vot_anno[3]]
return vot_anno
def _convert_image_path(image_path):
return image_path
"""Run tracker on VOT."""
if output_segmentation:
handle = vot.VOT("mask")
else:
handle = vot.VOT("rectangle")
vot_anno = handle.region()
image_path = handle.frame()
if not image_path:
return
image_path = _convert_image_path(image_path)
image = self._read_image(image_path)
if output_segmentation:
vot_anno_mask = vot.make_full_size(vot_anno, (image.shape[1], image.shape[0]))
bbox = masks_to_bboxes(torch.from_numpy(vot_anno_mask), fmt='t').squeeze().tolist()
else:
bbox = _convert_anno_to_list(vot_anno)
vot_anno_mask = None
out = tracker.initialize(image, {'init_mask': vot_anno_mask, 'init_bbox': bbox})
if out is None:
out = {}
prev_output = OrderedDict(out)
# Track
while True:
image_path = handle.frame()
if not image_path:
break
image_path = _convert_image_path(image_path)
image = self._read_image(image_path)
info = OrderedDict()
info['previous_output'] = prev_output
out = tracker.track(image, info)
prev_output = OrderedDict(out)
if output_segmentation:
pred = out['segmentation'].astype(np.uint8)
else:
state = out['target_bbox']
pred = vot.Rectangle(*state)
handle.report(pred, 1.0)
segmentation = out['segmentation'] if 'segmentation' in out else None
if self.visdom is not None:
tracker.visdom_draw_tracking(image, out['target_bbox'], segmentation)
elif tracker.params.visualization:
self.visualize(image, out['target_bbox'], segmentation)
def run_vot(self, debug=None, visdom_info=None):
params = self.get_parameters()
params.tracker_name = self.name
params.param_name = self.parameter_name
params.run_id = self.run_id
debug_ = debug
if debug is None:
debug_ = getattr(params, 'debug', 0)
if debug is None:
visualization_ = getattr(params, 'visualization', False)
else:
visualization_ = True if debug else False
params.visualization = visualization_
params.debug = debug_
self._init_visdom(visdom_info, debug_)
tracker = self.create_tracker(params)
tracker.initialize_features()
# import pytracking.evaluation.vot as vot
import pytracking.VOT.vot as vot
def _convert_anno_to_list(vot_anno):
vot_anno = [vot_anno[0][0][0], vot_anno[0][0][1], vot_anno[0][1][0], vot_anno[0][1][1],
vot_anno[0][2][0], vot_anno[0][2][1], vot_anno[0][3][0], vot_anno[0][3][1]]
return vot_anno
def _convert_image_path(image_path):
image_path_new = image_path[20:- 2]
return "".join(image_path_new)
"""Run tracker on VOT."""
handle = vot.VOT("polygon")
vot_anno_polygon = handle.region()
vot_anno_polygon = _convert_anno_to_list(vot_anno_polygon)
init_state = convert_vot_anno_to_rect(vot_anno_polygon, tracker.params.vot_anno_conversion_type)
image_path = handle.frame()
if not image_path:
return
image_path = _convert_image_path(image_path)
image = self._read_image(image_path)
tracker.initialize(image, {'init_bbox': init_state})
# Track
while True:
image_path = handle.frame()
if not image_path:
break
image_path = _convert_image_path(image_path)
image = self._read_image(image_path)
out = tracker.track(image)
state = out['target_bbox']
handle.report(vot.Rectangle(state[0], state[1], state[2], state[3]))
segmentation = out['segmentation'] if 'segmentation' in out else None
if self.visdom is not None:
tracker.visdom_draw_tracking(image, out['target_bbox'], segmentation)
elif tracker.params.visualization:
self.visualize(image, out['target_bbox'], segmentation)
def get_parameters(self):
"""Get parameters."""
param_module = importlib.import_module('pytracking.parameter.{}.{}'.format(self.name, self.parameter_name))
params = param_module.parameters()
return params
def init_visualization(self):
self.pause_mode = False
self.fig, self.ax = plt.subplots(1)
self.fig.canvas.mpl_connect('key_press_event', self.press)
plt.tight_layout()
def visualize(self, image, state, segmentation=None):
self.ax.cla()
self.ax.imshow(image)
if segmentation is not None:
self.ax.imshow(segmentation, alpha=0.5)
if isinstance(state, (OrderedDict, dict)):
boxes = [v for k, v in state.items()]
else:
boxes = (state,)
for i, box in enumerate(boxes, start=1):
col = _tracker_disp_colors[i]
col = [float(c) / 255.0 for c in col]
rect = patches.Rectangle((box[0], box[1]), box[2], box[3], linewidth=1, edgecolor=col, facecolor='none')
self.ax.add_patch(rect)
if getattr(self, 'gt_state', None) is not None:
gt_state = self.gt_state
rect = patches.Rectangle((gt_state[0], gt_state[1]), gt_state[2], gt_state[3], linewidth=1, edgecolor='g', facecolor='none')
self.ax.add_patch(rect)
self.ax.set_axis_off()
self.ax.axis('equal')
draw_figure(self.fig)
if self.pause_mode:
keypress = False
while not keypress:
keypress = plt.waitforbuttonpress()
def reset_tracker(self):
pass
def press(self, event):
if event.key == 'p':
self.pause_mode = not self.pause_mode
print("Switching pause mode!")
elif event.key == 'r':
self.reset_tracker()
print("Resetting target pos to gt!")
def _read_image(self, image_file: str):
im = cv.imread(image_file)
return cv.cvtColor(im, cv.COLOR_BGR2RGB)