-
Notifications
You must be signed in to change notification settings - Fork 0
/
SFSORT.py
287 lines (231 loc) · 12.1 KB
/
SFSORT.py
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
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
# ******************************************************************** #
# ****************** Sharif University of Technology ***************** #
# *************** Department of Electrical Engineering *************** #
# ************************ Deep Learning Lab ************************* #
# ************************ SFSORT Version 4.0 ************************ #
# ************ Authors: Mehrdad Morsali - Zeinab Sharifi ************* #
# *********** mehrdadmorsali@gmail.com - zsh.5ooo@gmail.com ********** #
# ******************************************************************** #
# ******************************************************************** #
# ********************** Packages and Libraries ********************** #
# ******************************************************************** #
import numpy as np
use_lap=True
try:
import lap
except ImportError:
from scipy.optimize import linear_sum_assignment
use_lap=False
# ******************************************************************** #
# ***************************** Classes ****************************** #
# ******************************************************************** #
class DotAccess(dict):
"""Provides dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
class TrackState:
"""Enumeration of possible states of a track"""
Active = 0
Lost_Central = 1
Lost_Marginal = 2
class Track:
"""Handles basic track attributes and operations"""
def __init__(self, bbox, frame_id, track_id):
"""Track initialization"""
self.track_id = track_id
self.bbox = bbox
self.state = TrackState.Active
self.last_frame = frame_id
def update(self, box, frame_id):
"""Updates a matched track"""
self.bbox = box
self.state = TrackState.Active
self.last_frame = frame_id
class SFSORT:
"""Multi-Object Tracking System"""
def __init__(self, args):
"""Initialize a tracker with given arguments"""
args = DotAccess(args)
# Register tracking arguments
self.low_th = args.low_th
self.match_th_second = args.match_th_second
self.high_th = args.high_th
self.match_th_first = args.match_th_first
self.new_track_th = args.new_track_th
if args.dynamic_tuning:
self.cth = args.cth if args.cth else 0.7
self.hthm = args.high_th_m if args.high_th_m else 0
self.nthm = args.new_track_th_m if args.new_track_th_m else 0
self.mthm = args.match_th_first_m if args.match_th_first_m else 0
self.marginal_timeout = args.marginal_timeout
self.central_timeout = args.central_timeout
self.l_margin = args.horizontal_margin
self.t_margin = args.vertical_margin
self.r_margin = args.frame_width - args.horizontal_margin
self.b_margin = args.frame_height - args.vertical_margin
# Initialize the tracker
self.frame_no = 0
self.id_counter = 0
self.active_tracks = []
self.lost_tracks = []
def update(self, boxes, scores):
"""Updates tracker with new detections"""
# Adjust dynamic arguments
count = len(scores[scores>self.cth])
if count < 1:
count = 1
lnc = np.log10(count)
hth = self.high_th - (self.hthm * lnc)
nth = self.new_track_th + (self.nthm * lnc)
mth = self.match_th_first - (self.mthm * lnc)
# Increase frame number
self.frame_no += 1
# Variable: Active tracks in the next frame
next_active_tracks = []
# Remove long-time lost tracks
for track in self.lost_tracks:
if track.state == TrackState.Lost_Central:
if self.frame_no - track.last_frame > self.central_timeout:
self.lost_tracks.remove(track)
del track
else:
if self.frame_no - track.last_frame > self.marginal_timeout:
self.lost_tracks.remove(track)
del track
# Gather out all previous tracks
track_pool = self.active_tracks + self.lost_tracks
# Try to associate tracks with high score detections
unmatched_tracks = np.array([])
high_score = scores > hth
if high_score.any():
definite_boxes = boxes[high_score]
definite_scores = scores[high_score]
if track_pool:
cost = self.calculate_cost(track_pool, definite_boxes)
matches, unmatched_tracks, unmatched_detections = self.linear_assignment(cost, mth)
# Update/Activate matched tracks
for track_idx, detection_idx in matches:
box = definite_boxes[detection_idx]
track = track_pool[track_idx]
track.update(box, self.frame_no)
next_active_tracks.append(track)
# Remove re-identified tracks from lost list
if track in self.lost_tracks:
self.lost_tracks.remove(track)
# Identify eligible unmatched detections as new tracks
for detection_idx in unmatched_detections:
if definite_scores[detection_idx] > nth:
box = definite_boxes[detection_idx]
track = Track(box, self.frame_no, self.id_counter)
next_active_tracks.append(track)
self.id_counter += 1
else:
# Associate tracks of the first frame after object-free/null frames
for detection_idx, score in enumerate(definite_scores):
if score > nth:
box = definite_boxes[detection_idx]
track = Track(box, self.frame_no, self.id_counter)
next_active_tracks.append(track)
self.id_counter += 1
# Add unmatched tracks to the lost list
unmatched_track_pool = []
for track_address in unmatched_tracks:
unmatched_track_pool.append(track_pool[track_address])
next_lost_tracks = unmatched_track_pool.copy()
# Try to associate remained tracks with intermediate score detections
intermediate_score = np.logical_and((self.low_th < scores), (scores < hth))
if intermediate_score.any():
if len(unmatched_tracks):
possible_boxes = boxes[intermediate_score]
cost = self.calculate_cost(unmatched_track_pool, possible_boxes, iou_only=True)
matches, unmatched_tracks, unmatched_detections = self.linear_assignment(cost, self.match_th_second)
# Update/Activate matched tracks
for track_idx, detection_idx in matches:
box = possible_boxes[detection_idx]
track = unmatched_track_pool[track_idx]
track.update(box, self.frame_no)
next_active_tracks.append(track)
# Remove re-identified tracks from lost list
if track in self.lost_tracks:
self.lost_tracks.remove(track)
next_lost_tracks.remove(track)
# All tracks are lost if there are no detections!
if not (high_score.any() or intermediate_score.any()):
next_lost_tracks = track_pool.copy()
# Update the list of lost tracks
for track in next_lost_tracks:
if track not in self.lost_tracks:
self.lost_tracks.append(track)
u = track.bbox[0] + (track.bbox[2] - track.bbox[0]/2)
v = track.bbox[1] + (track.bbox[3] - track.bbox[1]/2)
if (self.l_margin < u < self.r_margin) and (self.t_margin < v < self.b_margin):
track.state = TrackState.Lost_Central
else:
track.state = TrackState.Lost_Marginal
# Update the list of active tracks
self.active_tracks = next_active_tracks.copy()
return np.asarray([[x.bbox, x.track_id] for x in next_active_tracks], dtype=object)
@staticmethod
def calculate_cost(tracks, boxes, iou_only=False):
"""Calculates the association cost based on IoU and box similarity"""
eps = 1e-7
active_boxes = [track.bbox for track in tracks]
# Get the coordinates of bounding boxes
b1_x1, b1_y1, b1_x2, b1_y2 = np.array(active_boxes).T
b2_x1, b2_y1, b2_x2, b2_y2 = np.array(boxes).T
h_intersection = (np.minimum(b1_x2[:, None], b2_x2) - np.maximum(b1_x1[:, None], b2_x1)).clip(0)
w_intersection = (np.minimum(b1_y2[:, None], b2_y2) - np.maximum(b1_y1[:, None], b2_y1)).clip(0)
# Calculate the intersection area
intersection = h_intersection * w_intersection
# Calculate the union area
box1_height = b1_x2 - b1_x1
box2_height = b2_x2 - b2_x1
box1_width = b1_y2 - b1_y1
box2_width = b2_y2 - b2_y1
box1_area = box1_height * box1_width
box2_area = box2_height * box2_width
union = (box2_area + box1_area[:, None] - intersection + eps)
# Calculate the IoU
iou = intersection / union
if iou_only:
return 1.0 - iou
# Calculate the DIoU
centerx1 = (b1_x1 + b1_x2) / 2.0
centery1 = (b1_y1 + b1_y2) / 2.0
centerx2 = (b2_x1 + b2_x2) / 2.0
centery2 = (b2_y1 + b2_y2) / 2.0
inner_diag = np.abs(centerx1[:, None] - centerx2) + np.abs(centery1[:, None] - centery2)
xxc1 = np.minimum(b1_x1[:, None], b2_x1)
yyc1 = np.minimum(b1_y1[:, None], b2_y1)
xxc2 = np.maximum(b1_x2[:, None], b2_x2)
yyc2 = np.maximum(b1_y2[:, None], b2_y2)
outer_diag = np.abs(xxc2 - xxc1) + np.abs(yyc2 - yyc1)
diou = iou - (inner_diag / outer_diag)
# Calculate the BBSI
delta_w = np.abs(box2_width - box1_width[:, None])
sw = w_intersection / np.abs(w_intersection + delta_w + eps)
delta_h = np.abs(box2_height - box1_height[:, None])
sh = h_intersection / np.abs(h_intersection + delta_h + eps)
bbsi = diou + sh + sw
# Normalize the BBSI
cost = (bbsi)/3.0
return 1.0 - cost
@staticmethod
def linear_assignment(cost_matrix, thresh):
"""Linear assignment"""
if cost_matrix.size == 0:
return np.empty((0, 2), dtype=int), tuple(range(cost_matrix.shape[0])), tuple(range(cost_matrix.shape[1]))
if use_lap:
_, x, y = lap.lapjv(cost_matrix, extend_cost=True, cost_limit=thresh)
matches = [[ix, mx] for ix, mx in enumerate(x) if mx >= 0]
unmatched_a = np.where(x < 0)[0]
unmatched_b = np.where(y < 0)[0]
else:
row_ind, col_ind = linear_sum_assignment(cost_matrix)
matches = np.array([[row, col] for row, col in zip(row_ind, col_ind) if cost_matrix[row, col] <= thresh])
matched_rows = set(row_ind)
matched_cols = set(col_ind)
unmatched_a = np.array([i for i in range(cost_matrix.shape[0]) if i not in matched_rows])
unmatched_b = np.array([j for j in range(cost_matrix.shape[1]) if j not in matched_cols])
return matches, unmatched_a, unmatched_b