def test_iou(self): r1 = np.random.rand(1000, 4) * 100 r2 = np.random.rand(1000, 4) * 100 r1[:, 2:] += r1[:, :2] - 1 r2[:, 2:] += r2[:, :2] - 1 for bound in [None, (50, 100), (100, 200)]: o1 = ops.rect_iou(r1, r2, bound=bound) o2 = ops.poly_iou(r1, r2, bound=bound) self.assertTrue((o1 - o2).max() < 1e-12) p1 = self._to_corner(r1) p2 = self._to_corner(r2) o3 = ops.poly_iou(p1, p2, bound=bound) self.assertTrue((o1 - o3).max() < 1e-12)
def _calc_iou(self, bboxes, anno, bound, burnin=False): # skip initialization frames if burnin: bboxes = bboxes.copy() init_inds = [i for i, bbox in enumerate(bboxes) if bbox == [1.0]] for ind in init_inds: bboxes[ind:ind + self.burnin] = [[0]] * self.burnin # calculate polygon ious ious = np.array([ ops.poly_iou(np.array(a), b, bound) if len(a) > 1 else np.NaN for a, b in zip(bboxes, anno) ]) return ious
def run_supervised(self, tracker, visualize=False): ops.sys_print('Running supervised experiment...') # loop over the complete dataset for s, (img_files, target) in enumerate(self.dataset): seq_name = self.dataset.seq_names[s] ops.sys_print('--Sequence %d/%d: %s' % (s + 1, len(self.dataset), seq_name)) # rectangular bounding boxes anno = target['anno'] anno_rects = anno.copy() if anno_rects.shape[1] == 8: anno_rects = self.dataset._corner2rect(anno_rects) # run multiple repetitions for each sequence for r in range(self.repetitions): # check if the tracker is deterministic if r > 0 and tracker.is_deterministic: break elif r == 3 and self._check_deterministic( 'baseline', tracker.name, seq_name): ops.sys_print(' Detected a deterministic tracker, ' 'skipping remaining trials.') break ops.sys_print(' Repetition: %d' % (r + 1)) # skip if results exist record_file = os.path.join(self.result_dir, tracker.name, 'baseline', seq_name, '%s_%03d.txt' % (seq_name, r + 1)) if os.path.exists(record_file): ops.sys_print(' Found results, skipping %s' % seq_name) continue # state variables bboxes = [] times = [] failure = False next_start = -1 # tracking loop for f, img_file in enumerate(img_files): img = ops.read_image(img_file) if tracker.input_type == 'image': frame = img elif tracker.input_type == 'file': frame = img_file start_time = time.time() if f == 0: # initial frame tracker.init(frame, anno_rects[0]) bboxes.append([1]) elif failure: # during failure frames if f == next_start: if np.all(anno_rects[f] <= 0): next_start += 1 start_time = np.NaN bboxes.append([0]) else: failure = False tracker.init(frame, anno_rects[f]) bboxes.append([1]) else: start_time = np.NaN bboxes.append([0]) else: # during success frames bbox = tracker.update(frame) iou = ops.poly_iou(anno[f], bbox, bound=img.shape[1::-1]) if iou <= 0.0: # tracking failure failure = True next_start = f + self.skip_initialize bboxes.append([2]) else: # tracking succeed bboxes.append(bbox) # store elapsed time times.append(time.time() - start_time) # visualize if required if visualize: if len(bboxes[-1]) == 4: ops.show_image(img, bboxes[-1]) else: ops.show_image(img) # record results self._record(record_file, bboxes, times)
def run_realtime(self, tracker, visualize=False): ops.sys_print('Running real-time experiment...') # loop over the complete dataset for s, (img_files, target) in enumerate(self.dataset): seq_name = self.dataset.seq_names[s] ops.sys_print('--Sequence %d/%d: %s' % (s + 1, len(self.dataset), seq_name)) # skip if results exist record_file = os.path.join(self.result_dir, tracker.name, 'realtime', seq_name, '%s_001.txt' % seq_name) if os.path.exists(record_file): ops.sys_print(' Found results, skipping %s' % seq_name) continue # rectangular bounding boxes anno = target['anno'] anno_rects = anno.copy() if anno_rects.shape[1] == 8: anno_rects = self.dataset._corner2rect(anno_rects) # state variables bboxes = [] times = [] next_start = 0 failure = False failed_frame = -1 total_time = 0.0 grace = 3 - 1 offset = 0 # tracking loop for f, img_file in enumerate(img_files): img = ops.read_image(img_file) if tracker.input_type == 'image': frame = img elif tracker.input_type == 'file': frame = img_file start_time = time.time() if f == next_start: # during initial frames tracker.init(frame, anno_rects[f]) bboxes.append([1]) # reset state variables failure = False failed_frame = -1 total_time = 0.0 grace = 3 - 1 offset = f elif not failure: # during success frames # calculate current frame if grace > 0: total_time += 1000.0 / 25 grace -= 1 else: total_time += max(1000.0 / 25, last_time * 1000.0) current = offset + int( np.round(np.floor(total_time * 25) / 1000.0)) # delayed/tracked bounding box if f < current: bbox = bboxes[-1] elif f == current: bbox = tracker.update(frame) iou = ops.poly_iou(anno[f], bbox, bound=img.shape[1::-1]) if iou <= 0.0: # tracking failure failure = True failed_frame = f next_start = current + self.skip_initialize bboxes.append([2]) else: # tracking succeed bboxes.append(bbox) else: # during failure frames if f < current: # skipping frame due to slow speed bboxes.append([0]) start_time = np.NaN elif f == current: # current frame bbox = tracker.update(frame) iou = ops.poly_iou(anno[f], bbox, bound=img.shape[1::-1]) if iou <= 0.0: # tracking failure bboxes.append([2]) bboxes[failed_frame] = [0] times[failed_frame] = np.NaN else: # tracking succeed bboxes.append(bbox) elif f < next_start: # skipping frame due to failure bboxes.append([0]) start_time = np.NaN # store elapsed time last_time = time.time() - start_time times.append(last_time) # visualize if required if visualize: if len(bboxes[-1]) == 4: ops.show_image(img, bboxes[-1]) else: ops.show_image(img) # record results self._record(record_file, bboxes, times)