forked from adipandas/multi-object-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_tracker.py
More file actions
171 lines (140 loc) · 6.5 KB
/
sort_tracker.py
File metadata and controls
171 lines (140 loc) · 6.5 KB
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
import numpy as np
from scipy.optimize import linear_sum_assignment
from motrackers.utils.misc import iou_xywh as iou
from motrackers.track import KFTrackSORT, KFTrack4DSORT
from motrackers.centroid_kf_tracker import CentroidKF_Tracker
def assign_tracks2detection_iou(bbox_tracks, bbox_detections, iou_threshold=0.3):
"""
Assigns detected bounding boxes to tracked bounding boxes using IoU as a distance metric.
Parameters
----------
bbox_tracks : numpy.ndarray
bbox_detections : numpy.ndarray
iou_threshold : float
Returns
-------
tuple :
Tuple containing the following elements
- matches: (numpy.ndarray) Array of shape `(n, 2)` where `n` is number of pairs formed after
matching tracks to detections. This is an array of tuples with each element as matched pair
of indices`(track_index, detection_index)`.
- unmatched_detections : (numpy.ndarray) Array of shape `(m,)` where `m` is number of unmatched detections.
- unmatched_tracks : (numpy.ndarray) Array of shape `(k,)` where `k` is the number of unmatched tracks.
"""
if (bbox_tracks.size == 0) or (bbox_detections.size == 0):
return np.empty((0, 2), dtype=int), np.arange(len(bbox_detections), dtype=int), np.empty((0,), dtype=int)
if len(bbox_tracks.shape) == 1:
bbox_tracks = bbox_tracks[None, :]
if len(bbox_detections.shape) == 1:
bbox_detections = bbox_detections[None, :]
iou_matrix = np.zeros((bbox_tracks.shape[0], bbox_detections.shape[0]), dtype=np.float32)
for t in range(bbox_tracks.shape[0]):
for d in range(bbox_detections.shape[0]):
iou_matrix[t, d] = iou(bbox_tracks[t, :], bbox_detections[d, :])
assigned_tracks, assigned_detections = linear_sum_assignment(-iou_matrix)
unmatched_detections, unmatched_tracks = [], []
for d in range(bbox_detections.shape[0]):
if d not in assigned_detections:
unmatched_detections.append(d)
for t in range(bbox_tracks.shape[0]):
if t not in assigned_tracks:
unmatched_tracks.append(t)
# filter out matched with low IOU
matches = []
for t, d in zip(assigned_tracks, assigned_detections):
if iou_matrix[t, d] < iou_threshold:
unmatched_detections.append(d)
unmatched_tracks.append(t)
else:
matches.append((t, d))
if len(matches):
matches = np.array(matches)
else:
matches = np.empty((0, 2), dtype=int)
return matches, np.array(unmatched_detections), np.array(unmatched_tracks)
class SORT(CentroidKF_Tracker):
"""
SORT - Multi object tracker.
Parameters
----------
max_lost : int
Max. number of times a object is lost while tracking.
tracker_output_format : str
Output format of the tracker.
iou_threshold : float
Intersection over union minimum value.
process_noise_scale : float or numpy.ndarray
Process noise covariance matrix of shape (3, 3) or covariance magnitude as scalar value.
measurement_noise_scale : float or numpy.ndarray
Measurement noise covariance matrix of shape (1,) or covariance magnitude as scalar value.
time_step : int or float
Time step for Kalman Filter.
"""
def __init__(
self, max_lost=0,
tracker_output_format='mot_challenge',
iou_threshold=0.3,
process_noise_scale=1.0,
measurement_noise_scale=1.0,
time_step=1
):
self.iou_threshold = iou_threshold
super().__init__(
max_lost=max_lost, tracker_output_format=tracker_output_format,
process_noise_scale=process_noise_scale,
measurement_noise_scale=measurement_noise_scale, time_step=time_step
)
def _add_track(self, frame_id, bbox, detection_confidence, class_id, **kwargs):
# self.tracks[self.next_track_id] = KFTrackSORT(
# self.next_track_id, frame_id, bbox, detection_confidence, class_id=class_id,
# data_output_format=self.tracker_output_format, process_noise_scale=self.process_noise_scale,
# measurement_noise_scale=self.measurement_noise_scale, **kwargs
# )
self.tracks[self.next_track_id] = KFTrack4DSORT(
self.next_track_id, frame_id, bbox, detection_confidence, class_id=class_id,
data_output_format=self.tracker_output_format, process_noise_scale=self.process_noise_scale,
measurement_noise_scale=self.measurement_noise_scale, kf_time_step=1, **kwargs)
self.next_track_id += 1
def update(self, bboxes, detection_scores, class_ids):
self.frame_count += 1
bbox_detections = np.array(bboxes, dtype='int')
# track_ids_all = list(self.tracks.keys())
# bbox_tracks = []
# track_ids = []
# for track_id in track_ids_all:
# bb = self.tracks[track_id].predict()
# if np.any(np.isnan(bb)):
# self._remove_track(track_id)
# else:
# track_ids.append(track_id)
# bbox_tracks.append(bb)
track_ids = list(self.tracks.keys())
bbox_tracks = []
for track_id in track_ids:
bb = self.tracks[track_id].predict()
bbox_tracks.append(bb)
bbox_tracks = np.array(bbox_tracks)
matches, unmatched_detections, unmatched_tracks = assign_tracks2detection_iou(
bbox_tracks, bbox_detections, iou_threshold=0.3)
for i in range(matches.shape[0]):
t, d = matches[i, :]
track_id = track_ids[t]
bbox = bboxes[d, :]
cid = class_ids[d]
confidence = detection_scores[d]
self._update_track(track_id, self.frame_count, bbox, confidence, cid, lost=0)
for d in unmatched_detections:
bbox = bboxes[d, :]
cid = class_ids[d]
confidence = detection_scores[d]
self._add_track(self.frame_count, bbox, confidence, cid)
for t in unmatched_tracks:
track_id = track_ids[t]
bbox = bbox_tracks[t, :]
confidence = self.tracks[track_id].detection_confidence
cid = self.tracks[track_id].class_id
self._update_track(track_id, self.frame_count, bbox, detection_confidence=confidence, class_id=cid, lost=1)
if self.tracks[track_id].lost > self.max_lost:
self._remove_track(track_id)
outputs = self._get_tracks(self.tracks)
return outputs