|
1 | 1 | import numpy as np
|
2 | 2 | import supervision as sv
|
| 3 | +from scipy.optimize import linear_sum_assignment |
3 | 4 |
|
4 | 5 | from trackers.core.base import BaseTracker
|
5 | 6 | from trackers.core.sort.kalman_box_tracker import SORTKalmanBoxTracker
|
@@ -76,25 +77,17 @@ def _get_associated_indices(
|
76 | 77 | unmatched_trackers = set(range(len(self.trackers)))
|
77 | 78 | unmatched_detections = set(range(len(detection_boxes)))
|
78 | 79 |
|
79 |
| - if iou_matrix.size > 0: |
80 |
| - row_indices, col_indices = np.where(iou_matrix > self.minimum_iou_threshold) |
81 |
| - # Sort in descending order of IOU. Higher = better match. |
82 |
| - sorted_pairs = sorted( |
83 |
| - zip(row_indices, col_indices), |
84 |
| - key=lambda x: iou_matrix[x[0], x[1]], |
85 |
| - reverse=True, |
86 |
| - ) |
87 |
| - # keep each unique row/col pair at most once |
88 |
| - used_rows = set() |
89 |
| - used_cols = set() |
90 |
| - for row, col in sorted_pairs: |
91 |
| - if (row not in used_rows) and (col not in used_cols): |
92 |
| - used_rows.add(row) |
93 |
| - used_cols.add(col) |
| 80 | + if len(self.trackers) > 0 and len(detection_boxes) > 0: |
| 81 | + # Find optimal assignment using scipy.optimize.linear_sum_assignment. |
| 82 | + # Note that it uses a a modified Jonker-Volgenant algorithm with no |
| 83 | + # initialization instead of the Hungarian algorithm as mentioned in the |
| 84 | + # SORT paper. |
| 85 | + row_indices, col_indices = linear_sum_assignment(iou_matrix, maximize=True) |
| 86 | + for row, col in zip(row_indices, col_indices): |
| 87 | + if iou_matrix[row, col] >= self.minimum_iou_threshold: |
94 | 88 | matched_indices.append((row, col))
|
95 |
| - |
96 |
| - unmatched_trackers = unmatched_trackers - used_rows |
97 |
| - unmatched_detections = unmatched_detections - used_cols |
| 89 | + unmatched_trackers.remove(row) |
| 90 | + unmatched_detections.remove(col) |
98 | 91 |
|
99 | 92 | return matched_indices, unmatched_trackers, unmatched_detections
|
100 | 93 |
|
|
0 commit comments