Extend LineZone to filter out miscounts

This commit is contained in:
Grzegorz Klimaszewski
2024-09-23 17:36:32 +02:00
parent f8397bdce9
commit cc640dc890
2 changed files with 290 additions and 6 deletions
+46 -6
View File
@@ -1,5 +1,6 @@
from collections import deque
import warnings
from typing import Dict, Iterable, Optional, Tuple
from typing import Deque, Dict, Iterable, List, Optional, Tuple
import cv2
import numpy as np
@@ -67,6 +68,7 @@ class LineZone:
Position.BOTTOM_LEFT,
Position.BOTTOM_RIGHT,
),
max_linger: int = 1,
):
"""
Args:
@@ -77,10 +79,16 @@ class LineZone:
to consider when deciding on whether the detection
has passed the line counter or not. By default, this
contains the four corners of the detection's bounding box
max_linger: An integer indicating the number of consequtive frames
detections should stay away from the line after crossing it
to consider crossing completed. This configuration option
is useful when dealing with unstable bounding boxes or when
detections may linger on the line
"""
self.vector = Vector(start=start, end=end)
self.limits = self.calculate_region_of_interest_limits(vector=self.vector)
self.tracker_state: Dict[str, bool] = {}
self.max_linger = max(1, max_linger)
self.crossing_state: Dict[str, Deque[bool]] = {}
self.in_count: int = 0
self.out_count: int = 0
self.triggering_anchors = triggering_anchors
@@ -178,14 +186,24 @@ class LineZone:
continue
tracker_state = has_any_left_trigger[i]
if tracker_id not in self.tracker_state:
self.tracker_state[tracker_id] = tracker_state
if tracker_id not in self.crossing_state:
self.crossing_state[tracker_id] = deque([tracker_state], maxlen=self.max_linger)
continue
if self.tracker_state.get(tracker_id) == tracker_state:
crossing_state = self.crossing_state[tracker_id]
prev_frame_tracker_state = crossing_state[-1]
if self.max_linger == 1 and prev_frame_tracker_state == tracker_state:
continue
self.tracker_state[tracker_id] = tracker_state
crossing_in_progress = crossing_state.count(True) != 0 and crossing_state.count(False) != 0
crossing_state.appendleft(tracker_state)
all_on_same_side = crossing_state.count(not tracker_state) == 0
if not all_on_same_side:
continue
else:
if self.max_linger > 1 and not crossing_in_progress:
continue
if tracker_state:
self.in_count += 1
crossed_in[i] = True
@@ -193,6 +211,28 @@ class LineZone:
self.out_count += 1
crossed_out[i] = True
if self.max_linger == 1:
return crossed_in, crossed_out
this_frame_trackers = set(detections.tracker_id)
for tracker_id in list(self.crossing_state.keys()):
if tracker_id in this_frame_trackers:
continue
crossing_state = self.crossing_state[tracker_id]
crossing_in_progress = crossing_state.count(True) != 0 and crossing_state.count(False) != 0
if not crossing_in_progress:
continue
tracker_state = crossing_state[0]
crossing_state.appendleft(tracker_state)
all_on_same_side = crossing_state.count(not tracker_state) == 0
if not all_on_same_side:
continue
if tracker_state:
self.in_count += 1
else:
self.out_count += 1
return crossed_in, crossed_out
+244
View File
@@ -303,6 +303,19 @@ def test_line_zone_one_detection_default_anchors(
[False, True, False, True],
[False, False, True, False],
),
( # Scrape line, left side, center anchor (along line point), then hold position
Vector(Point(0, 0), Point(10, 0)),
[
[-2, 4, 2, 6],
[-2, 4 - 10, 2, 6 - 10],
[-2, 4, 2, 6],
[-2, 4 - 10, 2, 6 - 10],
[-2, 4 - 10, 2, 6 - 10],
],
[Position.CENTER],
[False, True, False, True, False],
[False, False, True, False, False],
),
( # Scrape line, right side, corner anchors
Vector(Point(0, 0), Point(10, 0)),
[
@@ -477,3 +490,234 @@ def test_line_zone_multiple_detections(
assert crossed_in_list == expected_crossed_in
assert crossed_out_list == expected_crossed_out
@pytest.mark.parametrize(
"vector, xyxy_sequence, triggering_anchors, max_linger, expected_crossed_in, "
"expected_crossed_out",
[
( # Detection lingers around line, all crosses counted
Vector(Point(0, 0), Point(10, 0)),
[
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4 - 10, 3, 6],
],
[
Position.TOP_LEFT,
],
1,
[False, True, False, True, False],
[False, False, True, False, False],
),
( # Detection lingers around line, only final cross counted
Vector(Point(0, 0), Point(10, 0)),
[
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4 - 10, 3, 6],
],
[
Position.TOP_LEFT,
],
2,
[False, False, False, False, True],
[False, False, False, False, False],
),
( # Detection lingers around line for a long time
Vector(Point(0, 0), Point(10, 0)),
[
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4 - 10, 3, 6],
],
[
Position.TOP_LEFT,
],
2,
[False] * 12 + [True],
[False] * 13,
),
( # Detection lingers around line, longer cycle
Vector(Point(0, 0), Point(10, 0)),
[
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4, 3, 6],
[2, 4, 3, 6],
[2, 4, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4 - 10, 3, 6],
[2, 4 - 10, 3, 6],
],
[
Position.TOP_LEFT,
],
4,
[False] * 8 + [True],
[False] * 9,
),
],
)
def test_line_zone_one_detection_long_horizon(
vector: Vector,
xyxy_sequence: List[List[float]],
triggering_anchors: List[Position],
max_linger: int,
expected_crossed_in: List[bool],
expected_crossed_out: List[bool],
) -> None:
line_zone = LineZone(
start=vector.start, end=vector.end, triggering_anchors=triggering_anchors, max_linger=max_linger,
)
crossed_in_list = []
crossed_out_list = []
for i, bbox in enumerate(xyxy_sequence):
detections = mock_detections(
xyxy=[bbox],
tracker_id=[0],
)
crossed_in, crossed_out = line_zone.trigger(detections)
crossed_in_list.append(crossed_in[0])
crossed_out_list.append(crossed_out[0])
assert (
crossed_in_list == expected_crossed_in
), f"expected {expected_crossed_in}, got {crossed_in_list}"
assert (
crossed_out_list == expected_crossed_out
), f"expected {expected_crossed_out}, got {crossed_out_list}"
@pytest.mark.parametrize(
"vector, xyxy_sequence, anchors, max_linger, expected_crossed_in, "
"expected_crossed_out, expected_count_in, expected_count_out, exception",
[
( # One stays, one crosses, one disappears before crossing, max_linger==1
Vector(Point(0, 0), Point(10, 0)),
[
[[4, 4, 6, 6], [4, 4, 6, 6], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
],
[
Position.TOP_LEFT,
],
1,
[[False, False, False], [False, True, False], [False, False], [False, True], [False, False]],
[[False, False, False], [False, False, False], [False, True], [False, False], [False, False]],
[0, 1, 1, 2, 2],
[0, 0, 1, 1, 1],
DoesNotRaise(),
),
( # One stays, one crosses, one disappears immediately after crossing, max_linger==1
Vector(Point(0, 0), Point(10, 0)),
[
[[4, 4, 6, 6], [4, 4, 6, 6], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
],
[
Position.TOP_LEFT,
],
1,
[[False, False, False], [False, True, False], [False, False, True,], [False, True], [False, False]],
[[False, False, False], [False, False, False], [False, True, False], [False, False], [False, False]],
[0, 1, 2, 3, 3],
[0, 0, 1, 1, 1],
DoesNotRaise(),
),
( # One stays, one crosses, one disappears before crossing, max_linger==2
Vector(Point(0, 0), Point(10, 0)),
[
[[4, 4, 6, 6], [4, 4, 6, 6], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
],
[
Position.TOP_LEFT,
],
2,
[[False, False, False], [False, False, False], [False, False], [False, False], [False, True]],
[[False, False, False], [False, False, False], [False, False], [False, False], [False, False]],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
DoesNotRaise(),
),
( # One stays, one crosses, one disappears immediately after crossing, max_linger==2
Vector(Point(0, 0), Point(10, 0)),
[
[[4, 4, 6, 6], [4, 4, 6, 6], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10], [4, 4, 6, 6]],
[[4, 4, 6, 6], [4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
[[4, 4, 6, 6], [4, 4 - 10, 6, 6 - 10]],
],
[
Position.TOP_LEFT,
],
2,
[[False, False, False], [False, False, False], [False, False, False,], [False, False], [False, True]],
[[False, False, False], [False, False, False], [False, False, False], [False, False], [False, False]],
[0, 0, 0, 1, 2],
[0, 0, 0, 0, 0],
DoesNotRaise(),
),
],
)
def test_line_zone_long_horizon_disappearing_detections(
vector: Vector,
xyxy_sequence: List[List[Optional[List[float]]]],
anchors: List[Position],
max_linger: int,
expected_crossed_in: List[List[bool]],
expected_crossed_out: List[List[bool]],
expected_count_in: List[int],
expected_count_out: List[int],
exception: Exception,
) -> None:
with exception:
line_zone = LineZone(
start=vector.start, end=vector.end, triggering_anchors=anchors, max_linger=max_linger
)
crossed_in_list = []
crossed_out_list = []
count_in_list = []
count_out_list = []
for bboxes in xyxy_sequence:
detections = mock_detections(
xyxy=bboxes,
tracker_id=[i for i in range(0, len(bboxes))],
)
crossed_in, crossed_out = line_zone.trigger(detections)
crossed_in_list.append(list(crossed_in))
crossed_out_list.append(list(crossed_out))
count_in_list.append(line_zone.in_count)
count_out_list.append(line_zone.out_count)
assert crossed_in_list == expected_crossed_in
assert crossed_out_list == expected_crossed_out
assert count_in_list == expected_count_in
assert count_out_list == expected_count_out