|
| 1 | +# --------------------------------------------------------- |
| 2 | +# IOU Tracker |
| 3 | +# Copyright (c) 2017 TU Berlin, Communication Systems Group |
| 4 | +# Licensed under The MIT License [see LICENSE for details] |
| 5 | +# Written by Erik Bochinski |
| 6 | +# --------------------------------------------------------- |
| 7 | + |
| 8 | +from time import time |
| 9 | + |
| 10 | +from util import load_mot, save_to_csv, iou |
| 11 | + |
| 12 | + |
| 13 | +def track_iou(detections, sigma_l, sigma_h, sigma_iou, t_min): |
| 14 | + """ |
| 15 | + Simple IOU based tracker. |
| 16 | + See "High-Speed Tracking-by-Detection Without Using Image Information by E. Bochinski, V. Eiselein, T. Sikora" for |
| 17 | + more information. |
| 18 | +
|
| 19 | + Args: |
| 20 | + detections (list): list of detections per frame, usually generated by util.load_mot |
| 21 | + sigma_l (float): low detection threshold. |
| 22 | + sigma_h (float): high detection threshold. |
| 23 | + sigma_iou (float): IOU threshold. |
| 24 | + t_min (float): minimum track length in frames. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + list: list of tracks. |
| 28 | + """ |
| 29 | + |
| 30 | + tracks_active = [] |
| 31 | + tracks_finished = [] |
| 32 | + |
| 33 | + for frame_num, detections_frame in enumerate(detections, start=1): |
| 34 | + # apply low threshold to detections |
| 35 | + dets = [det for det in detections_frame if det['score'] >= sigma_l] |
| 36 | + |
| 37 | + updated_tracks = [] |
| 38 | + for track in tracks_active: |
| 39 | + # matches = [det for det in dets if track.get_iou(det['bbox']) >= min_iou] |
| 40 | + # high_matches = [match for match in matches if match['score'] >= thresh_high] |
| 41 | + high_matches = [det for det in dets if iou(track['bboxes'][-1], det['bbox']) >= sigma_iou] |
| 42 | + |
| 43 | + if len(high_matches) > 0: |
| 44 | + # extract best matching detection |
| 45 | + best_match = max(high_matches, key=lambda x: iou(track['bboxes'][-1], x['bbox'])) |
| 46 | + track['bboxes'].append(best_match['bbox']) |
| 47 | + track['max_score'] = max(track['max_score'], best_match['score']) |
| 48 | + |
| 49 | + updated_tracks.append(track) |
| 50 | + |
| 51 | + # remove from detections |
| 52 | + del dets[dets.index(best_match)] |
| 53 | + |
| 54 | + else: |
| 55 | + # finish track when there is no suitable match |
| 56 | + if track['max_score'] >= sigma_h and len(track['bboxes']) >= t_min: |
| 57 | + tracks_finished.append(track) |
| 58 | + |
| 59 | + # create new tracks |
| 60 | + new_tracks = [{'bboxes': [det['bbox']], 'max_score': det['score'], 'start_frame': frame_num} for det in dets] |
| 61 | + tracks_active = updated_tracks + new_tracks |
| 62 | + |
| 63 | + # finish all remaining active tracks |
| 64 | + tracks_finished += [track for track in tracks_active |
| 65 | + if track['max_score'] >= sigma_h and len(track['bboxes']) >= t_min] |
| 66 | + |
| 67 | + return tracks_finished |
| 68 | + |
| 69 | + |
| 70 | +def track_iou_matlab_wrapper(detections, sigma_l, sigma_h, sigma_iou, t_min): |
| 71 | + """ |
| 72 | + Matlab wrapper of the iou tracker for the detrac evaluation toolkit. |
| 73 | +
|
| 74 | + Args: |
| 75 | + detections (numpy.array): numpy array of detections, usually supplied by run_tracker.m |
| 76 | + sigma_l (float): low detection threshold. |
| 77 | + sigma_h (float): high detection threshold. |
| 78 | + sigma_iou (float): IOU threshold. |
| 79 | + t_min (float): minimum track length in frames. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + float: speed in frames per second. |
| 83 | + list: list of tracks. |
| 84 | + """ |
| 85 | + |
| 86 | + detections = detections.reshape((7, -1)).transpose() |
| 87 | + dets = load_mot(detections) |
| 88 | + start = time() |
| 89 | + tracks = track_iou(dets, sigma_l, sigma_h, sigma_iou, t_min) |
| 90 | + end = time() |
| 91 | + |
| 92 | + id_ = 1 |
| 93 | + out = [] |
| 94 | + for track in tracks: |
| 95 | + for i, bbox in enumerate(track['bboxes']): |
| 96 | + out += [float(bbox[0]), float(bbox[1]), float(bbox[2] - bbox[0]), float(bbox[3] - bbox[1]), |
| 97 | + float(track['start_frame'] + i), float(id_)] |
| 98 | + id_ += 1 |
| 99 | + |
| 100 | + num_frames = len(dets) |
| 101 | + speed = num_frames / (end - start) |
| 102 | + |
| 103 | + return speed, out |
0 commit comments