diff --git a/supervision/detection/line_counter.py b/supervision/detection/line_counter.py index ca5f2a11..78dee65e 100644 --- a/supervision/detection/line_counter.py +++ b/supervision/detection/line_counter.py @@ -53,6 +53,44 @@ class LineZone: self.out_count: int = 0 self.triggering_anchors = triggering_anchors + @staticmethod + def calculate_region_of_interest_limits(vector: Vector) -> Tuple[Vector, Vector]: + magnitude = vector.magnitude + + if magnitude == 0: + raise ValueError("The magnitude of the vector cannot be zero.") + + delta_x = vector.end.x - vector.start.x + delta_y = vector.end.y - vector.start.y + + unit_vector_x = delta_x / magnitude + unit_vector_y = delta_y / magnitude + + perpendicular_vector_x = -unit_vector_y + perpendicular_vector_y = unit_vector_x + + start_region_limit = Vector( + start=vector.start, + end=Point( + x=vector.start.x + perpendicular_vector_x, + y=vector.start.y + perpendicular_vector_y + ) + ) + end_region_limit = Vector( + start=vector.end, + end=Point( + x=vector.end.x - perpendicular_vector_x, + y=vector.end.y - perpendicular_vector_y + ) + ) + return start_region_limit, end_region_limit + + @staticmethod + def is_point_in_limits(point: Point, limits: Tuple[Vector, Vector]) -> bool: + cross_product_1 = limits[0].cross_product(point) + cross_product_2 = limits[1].cross_product(point) + return (cross_product_1 > 0) == (cross_product_2 > 0) + def is_point_in_line_range(self, point: Point) -> bool: """ Check if the given point is within the line's range. diff --git a/test/detection/test_line_counter.py b/test/detection/test_line_counter.py new file mode 100644 index 00000000..c1231b0d --- /dev/null +++ b/test/detection/test_line_counter.py @@ -0,0 +1,72 @@ +from contextlib import ExitStack as DoesNotRaise +from typing import Tuple, Optional + +import pytest + +from supervision import LineZone +from supervision.geometry.core import Vector, Point + + +@pytest.mark.parametrize( + "vector, expected_result, exception", + [ + ( + Vector(start=Point(x=0.0, y=0.0), end=Point(x=0.0, y=0.0)), + None, + pytest.raises(ValueError) + ), + ( + Vector(start=Point(x=1.0, y=1.0), end=Point(x=1.0, y=1.0)), + None, + pytest.raises(ValueError) + ), + ( + Vector(start=Point(x=0.0, y=0.0), end=Point(x=0.0, y=4.0)), + ( + Vector(start=Point(x=0.0, y=0.0), end=Point(x=-1.0, y=0.0)), + Vector(start=Point(x=0.0, y=4.0), end=Point(x=1.0, y=4.0)) + ), + DoesNotRaise(), + ), + ( + Vector(Point(0.0, 0.0), Point(4.0, 0.0)), + ( + Vector(start=Point(x=0.0, y=0.0), end=Point(x=0.0, y=1.0)), + Vector(start=Point(x=4.0, y=0.0), end=Point(x=4.0, y=-1.0)) + ), + DoesNotRaise(), + ), + ( + Vector(Point(0.0, 0.0), Point(3.0, 4.0)), + ( + Vector(start=Point(x=0, y=0), end=Point(x=-0.8, y=0.6)), + Vector(start=Point(x=3, y=4), end=Point(x=3.8, y=3.4)) + ), + DoesNotRaise(), + ), + ( + Vector(Point(0.0, 0.0), Point(4.0, 3.0)), + ( + Vector(start=Point(x=0, y=0), end=Point(x=-0.6, y=0.8)), + Vector(start=Point(x=4, y=3), end=Point(x=4.6, y=2.2)) + ), + DoesNotRaise(), + ), + ( + Vector(Point(0.0, 0.0), Point(3.0, -4.0)), + ( + Vector(start=Point(x=0, y=0), end=Point(x=0.8, y=0.6)), + Vector(start=Point(x=3, y=-4), end=Point(x=2.2, y=-4.6)) + ), + DoesNotRaise(), + ) + ] +) +def test_calculate_region_of_interest_limits( + vector: Vector, + expected_result: Optional[Tuple[Vector, Vector]], + exception: Exception +) -> None: + with exception: + result = LineZone.calculate_region_of_interest_limits(vector=vector) + assert result == expected_result