-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_association.py
80 lines (65 loc) · 2.75 KB
/
data_association.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
"""
As implemented in https://github.com/abewley/sort but with some modifications
For each detected item, it computes the intersection over union (IOU) w.r.t. each tracked object. (IOU matrix)
Then, it applies the Hungarian algorithm (via linear_assignment) to assign each det. item to the best possible
tracked item (i.e. to the one with max. IOU).
Note: a more recent approach uses a Deep Association Metric instead.
see https://github.com/nwojke/deep_sort
"""
import numpy as np
from numba import jit
#from sklearn.utils.linear_assignment_ import linear_assignment
from scipy.optimize import linear_sum_assignment as linear_assignment
@jit
def iou(bb_test, bb_gt):
"""
Computes IUO between two boundary boxes in the form [x1,y1,x2,y2]
"""
xx1 = np.maximum(bb_test[0], bb_gt[0])
yy1 = np.maximum(bb_test[1], bb_gt[1])
xx2 = np.minimum(bb_test[2], bb_gt[2])
yy2 = np.minimum(bb_test[3], bb_gt[3])
w = np.maximum(0., xx2 - xx1)
h = np.maximum(0., yy2 - yy1)
wh = w * h
o = wh / ((bb_test[2] - bb_test[0]) * (bb_test[3] - bb_test[1])
+ (bb_gt[2] - bb_gt[0]) * (bb_gt[3] - bb_gt[1]) - wh)
return(o)
def associate_detections_to_trackers(detections, trackers, iou_threshold = 0.3):
"""
Assigns detections to tracked object (both represented as bounding boxes)
Returns 3 lists of matches, unmatched_detections and unmatched_trackers
"""
if len(trackers) == 0:
return np.empty((0, 2),dtype=int), np.arange(len(detections)), np.empty((0, 5),dtype=int)
iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
for d, det in enumerate(detections):
for t, trk in enumerate(trackers):
iou_matrix[d,t] = iou(det, trk)
# Minimise the total assignment cost
matched_indices = linear_assignment(-iou_matrix)
matched_indices = np.asarray(matched_indices)
matched_indices = np.transpose(matched_indices)
# Find unmatched detections
unmatched_detections = []
for d, det in enumerate(detections):
if d not in matched_indices[:, 0]:
unmatched_detections.append(d)
# Find lost tracklets
unmatched_trackers = []
for t, trk in enumerate(trackers):
if t not in matched_indices[:, 1]:
unmatched_trackers.append(t)
# Filter out matched with low IOU
matches = []
for m in matched_indices:
if iou_matrix[m[0], m[1]] < iou_threshold:
unmatched_detections.append(m[0])
unmatched_trackers.append(m[1])
else:
matches.append(m.reshape(1, 2))
if len(matches) == 0:
matches = np.empty((0, 2), dtype=int)
else:
matches = np.concatenate(matches, axis=0)
return matches, np.array(unmatched_detections), np.array(unmatched_trackers)