Skip to content

refactor(trackers): decouple DeepSORTKalmanBoxTracker from SORTKalmanBoxTracker #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions trackers/core/deepsort/kalman_box_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,45 @@ class DeepSORTKalmanBoxTracker(SORTKalmanBoxTracker):
tracked object (bounding box), with a Kalman filter to predict and update
its position. It also maintains a feature vector for the object, which is
used to identify the object across frames.

Attributes:
tracker_id (int): Unique identifier for the tracker.
number_of_successful_updates (int): Number of times the object has been
updated successfully.
time_since_update (int): Number of frames since the last update.
state (np.ndarray): State vector of the bounding box.
F (np.ndarray): State transition matrix.
H (np.ndarray): Measurement matrix.
Q (np.ndarray): Process noise covariance matrix.
R (np.ndarray): Measurement noise covariance matrix.
P (np.ndarray): Error covariance matrix.
features (list[np.ndarray]): List of feature vectors.
count_id (int): Class variable to assign unique IDs to each tracker.

Args:
bbox (np.ndarray): Initial bounding box in the form [x1, y1, x2, y2].
feature (Optional[np.ndarray]): Optional initial feature vector.
"""

count_id = 0

@classmethod
def get_next_tracker_id(cls) -> int:
"""
Class method that returns the next available tracker ID.

Returns:
int: The next available tracker ID.
"""
next_id = cls.count_id
cls.count_id += 1
return next_id

def __init__(self, bbox: np.ndarray, feature: Optional[np.ndarray] = None):
# Call the parent class constructor to handle the basic tracker functionality
super().__init__(bbox)

# Initialize features list
self.features: list[np.ndarray] = []
if feature is not None:
self.features.append(feature)
Expand Down
9 changes: 6 additions & 3 deletions trackers/core/deepsort/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def _get_associated_indices(
if combined_dist.size > 0:
row_indices, col_indices = np.where(combined_dist < 1.0)
sorted_pairs = sorted(
zip(row_indices, col_indices), key=lambda x: combined_dist[x[0], x[1]]
zip(map(int, row_indices), map(int, col_indices)),
key=lambda x: combined_dist[x[0], x[1]],
)

used_rows = set()
Expand All @@ -303,8 +304,10 @@ def _get_associated_indices(
used_cols.add(col)
matched_indices.append((row, col))

unmatched_trackers = unmatched_trackers - used_rows
unmatched_detections = unmatched_detections - used_cols
unmatched_trackers = unmatched_trackers - {int(row) for row in used_rows}
unmatched_detections = unmatched_detections - {
int(col) for col in used_cols
}

return matched_indices, unmatched_trackers, unmatched_detections

Expand Down