Skip to content

fix(line_zone): tracker-id reuse across classes #1868

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 2 additions & 6 deletions supervision/detection/line_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(
self.vector = Vector(start=start, end=end)
self.limits = self._calculate_region_of_interest_limits(vector=self.vector)
self.crossing_history_length = max(2, minimum_crossing_threshold + 1)
self.crossing_state_history: Dict[int, Deque[bool]] = defaultdict(
self.crossing_state_history: Dict[Tuple[int, int], Deque[bool]] = defaultdict(
lambda: deque(maxlen=self.crossing_history_length)
)
self._in_count_per_class: Counter = Counter()
Expand Down Expand Up @@ -176,16 +176,12 @@ def trigger(self, detections: Detections) -> Tuple[np.ndarray, np.ndarray]:
continue

tracker_state: bool = has_any_left_trigger[i]
crossing_history = self.crossing_state_history[tracker_id]
crossing_history = self.crossing_state_history[(tracker_id, class_id)]
crossing_history.append(tracker_state)

if len(crossing_history) < self.crossing_history_length:
continue

# TODO: Account for incorrect class_id.
# Most likely this would involve indexing self.crossing_state_history
# with (tracker_id, class_id).

oldest_state = crossing_history[0]
if crossing_history.count(oldest_state) > 1:
continue
Expand Down
2 changes: 1 addition & 1 deletion supervision/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,4 @@ def _merge_tiles_elements(
def _generate_color_image(
shape: Tuple[int, int], color: Tuple[int, int, int]
) -> np.ndarray:
return np.ones(shape[::-1] + (3,), dtype=np.uint8) * color
return np.ones((*shape[::-1], 3), dtype=np.uint8) * color
51 changes: 51 additions & 0 deletions test/detection/test_line_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,54 @@ def test_line_zone_long_horizon_disappearing_detections(
assert crossed_out_list == expected_crossed_out
assert count_in_list == expected_count_in
assert count_out_list == expected_count_out


def test_line_zone_tracker_id_reuse_with_different_classes() -> None:
line_zone = LineZone(start=Point(0, 0), end=Point(10, 0))

# First object with class 0 crosses the line
detections = mock_detections(
xyxy=[[4, 4, 6, 6]],
tracker_id=[0],
class_id=[0],
)
line_zone.trigger(detections)

detections = mock_detections(
xyxy=[[4, -6, 6, -4]],
tracker_id=[0],
class_id=[0],
)
line_zone.trigger(detections)

detections = mock_detections(
xyxy=[[4, 4, 6, 6]],
tracker_id=[0],
class_id=[0],
)
line_zone.trigger(detections)

# Second object reuses tracker id with a different class
detections = mock_detections(
xyxy=[[4, 4, 6, 6]],
tracker_id=[0],
class_id=[1],
)
line_zone.trigger(detections)

detections = mock_detections(
xyxy=[[4, -6, 6, -4]],
tracker_id=[0],
class_id=[1],
)
line_zone.trigger(detections)

detections = mock_detections(
xyxy=[[4, 4, 6, 6]],
tracker_id=[0],
class_id=[1],
)
line_zone.trigger(detections)

assert line_zone.in_count_per_class == {0: 1, 1: 1}
assert line_zone.out_count_per_class == {0: 1, 1: 1}