# Source: https://github.com/bochinski/iou-tracker from .utils import iou def track_iou(detections, sigma_l, sigma_h, sigma_iou, t_min): """ Simple IOU based tracker. See "High-Speed Tracking-by-Detection Without Using Image Information by E. Bochinski, V. Eiselein, T. Sikora" for more information. Args: detections (list): list of detections per frame, usually generated by util.load_mot sigma_l (float): low detection threshold. sigma_h (float): high detection threshold. sigma_iou (float): IOU threshold. t_min (float): minimum track length in frames. Returns: list: list of tracks. """ tracks_active = [] tracks_finished = [] for frame_num, detections_frame in enumerate(detections, start=1): # apply low threshold to detections dets = [det for det in detections_frame if det['score'] >= sigma_l] updated_tracks = [] for track in tracks_active: if len(dets) > 0: # get det with highest iou best_match = max(dets, key=lambda x: iou(track['bboxes'][-1], x['bbox'])) if iou(track['bboxes'][-1], best_match['bbox']) >= sigma_iou: track['bboxes'].append(best_match['bbox']) track['max_score'] = max(track['max_score'], best_match['score']) updated_tracks.append(track) # remove from best matching detection from detections del dets[dets.index(best_match)] # if track was not updated if len(updated_tracks) == 0 or track is not updated_tracks[-1]: # finish track when the conditions are met if track['max_score'] >= sigma_h and len(track['bboxes']) >= t_min: tracks_finished.append(track) # create new tracks new_tracks = [{'bboxes': [det['bbox']], 'max_score': det['score'], 'start_frame': frame_num} for det in dets] tracks_active = updated_tracks + new_tracks # finish all remaining active tracks tracks_finished += [track for track in tracks_active if track['max_score'] >= sigma_h and len(track['bboxes']) >= t_min] return tracks_finished