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 | class ByteTrack(BaseTracker):
"""
BYTETracker: A tracking algorithm based on ByteTrack, which utilizes motion-based tracking.
Args:
min_conf (float, optional): Threshold for detection confidence. Detections below this threshold are discarded.
track_thresh (float, optional): Threshold for detection confidence. Detections above this threshold are considered for tracking in the first association round.
match_thresh (float, optional): Threshold for the matching step in data association. Controls the maximum distance allowed between tracklets and detections for a match.
track_buffer (int, optional): Number of frames to keep a track alive after it was last detected. A longer buffer allows for more robust tracking but may increase identity switches.
frame_rate (int, optional): Frame rate of the video being processed. Used to scale the track buffer size.
per_class (bool, optional): Whether to perform per-class tracking. If True, tracks are maintained separately for each object class.
"""
def __init__(
self,
min_conf: float = 0.1,
track_thresh: float = 0.45,
match_thresh: float = 0.8,
track_buffer: int = 25,
frame_rate: int = 30,
per_class: bool = False,
):
super().__init__(per_class=per_class)
self.active_tracks = [] # type: list[STrack]
self.lost_stracks = [] # type: list[STrack]
self.removed_stracks = [] # type: list[STrack]
self.frame_id = 0
self.track_buffer = track_buffer
self.per_class = per_class
self.min_conf = min_conf
self.track_thresh = track_thresh
self.match_thresh = match_thresh
self.det_thresh = track_thresh
self.buffer_size = int(frame_rate / 30.0 * track_buffer)
self.max_time_lost = self.buffer_size
self.kalman_filter = KalmanFilterXYAH()
@BaseTracker.setup_decorator
@BaseTracker.per_class_decorator
def update(
self, dets: np.ndarray, img: np.ndarray = None, embs: np.ndarray = None
) -> np.ndarray:
self.check_inputs(dets, img)
dets = np.hstack([dets, np.arange(len(dets)).reshape(-1, 1)])
self.frame_count += 1
activated_starcks = []
refind_stracks = []
lost_stracks = []
removed_stracks = []
confs = dets[:, 4]
remain_inds = confs > self.track_thresh
inds_low = confs > self.min_conf
inds_high = confs < self.track_thresh
inds_second = np.logical_and(inds_low, inds_high)
dets_second = dets[inds_second]
dets = dets[remain_inds]
if len(dets) > 0:
"""Detections"""
detections = [STrack(det, max_obs=self.max_obs) for det in dets]
else:
detections = []
""" Add newly detected tracklets to tracked_stracks"""
unconfirmed = []
tracked_stracks = [] # type: list[STrack]
for track in self.active_tracks:
if not track.is_activated:
unconfirmed.append(track)
else:
tracked_stracks.append(track)
""" Step 2: First association, with high conf detection boxes"""
strack_pool = joint_stracks(tracked_stracks, self.lost_stracks)
# Predict the current location with KF
STrack.multi_predict(strack_pool)
dists = iou_distance(strack_pool, detections)
# if not self.args.mot20:
dists = fuse_score(dists, detections)
matches, u_track, u_detection = linear_assignment(
dists, thresh=self.match_thresh
)
for itracked, idet in matches:
track = strack_pool[itracked]
det = detections[idet]
if track.state == TrackState.Tracked:
track.update(detections[idet], self.frame_count)
activated_starcks.append(track)
else:
track.re_activate(det, self.frame_count, new_id=False)
refind_stracks.append(track)
""" Step 3: Second association, with low conf detection boxes"""
# association the untrack to the low conf detections
if len(dets_second) > 0:
"""Detections"""
detections_second = [
STrack(det_second, max_obs=self.max_obs) for det_second in dets_second
]
else:
detections_second = []
r_tracked_stracks = [
strack_pool[i]
for i in u_track
if strack_pool[i].state == TrackState.Tracked
]
dists = iou_distance(r_tracked_stracks, detections_second)
matches, u_track, u_detection_second = linear_assignment(dists, thresh=0.5)
for itracked, idet in matches:
track = r_tracked_stracks[itracked]
det = detections_second[idet]
if track.state == TrackState.Tracked:
track.update(det, self.frame_count)
activated_starcks.append(track)
else:
track.re_activate(det, self.frame_count, new_id=False)
refind_stracks.append(track)
for it in u_track:
track = r_tracked_stracks[it]
if not track.state == TrackState.Lost:
track.mark_lost()
lost_stracks.append(track)
"""Deal with unconfirmed tracks, usually tracks with only one beginning frame"""
detections = [detections[i] for i in u_detection]
dists = iou_distance(unconfirmed, detections)
# if not self.args.mot20:
dists = fuse_score(dists, detections)
matches, u_unconfirmed, u_detection = linear_assignment(dists, thresh=0.7)
for itracked, idet in matches:
unconfirmed[itracked].update(detections[idet], self.frame_count)
activated_starcks.append(unconfirmed[itracked])
for it in u_unconfirmed:
track = unconfirmed[it]
track.mark_removed()
removed_stracks.append(track)
""" Step 4: Init new stracks"""
for inew in u_detection:
track = detections[inew]
if track.conf < self.det_thresh:
continue
track.activate(self.kalman_filter, self.frame_count)
activated_starcks.append(track)
""" Step 5: Update state"""
for track in self.lost_stracks:
if self.frame_count - track.end_frame > self.max_time_lost:
track.mark_removed()
removed_stracks.append(track)
self.active_tracks = [
t for t in self.active_tracks if t.state == TrackState.Tracked
]
self.active_tracks = joint_stracks(self.active_tracks, activated_starcks)
self.active_tracks = joint_stracks(self.active_tracks, refind_stracks)
self.lost_stracks = sub_stracks(self.lost_stracks, self.active_tracks)
self.lost_stracks.extend(lost_stracks)
self.lost_stracks = sub_stracks(self.lost_stracks, self.removed_stracks)
self.removed_stracks.extend(removed_stracks)
self.active_tracks, self.lost_stracks = remove_duplicate_stracks(
self.active_tracks, self.lost_stracks
)
# get confs of lost tracks
output_stracks = [track for track in self.active_tracks if track.is_activated]
outputs = []
for t in output_stracks:
output = []
output.extend(t.xyxy)
output.append(t.id)
output.append(t.conf)
output.append(t.cls)
output.append(t.det_ind)
outputs.append(output)
outputs = np.asarray(outputs)
return outputs
|