Skip to content

Polygon Zone

PolygonZone

supervision.detection.tools.polygon_zone.PolygonZone

A class for defining a polygon-shaped zone within a frame for detecting objects.

Warning

Entry and exit counts (:attr:~supervision.detection.tools.polygon_zone.PolygonZone.in_count, :attr:~supervision.detection.tools.polygon_zone.PolygonZone.out_count and their per-class variants) and :meth:~supervision.detection.tools.polygon_zone.PolygonZone.time_in_zone use the tracker_id. Read here to learn how to plug tracking into your inference pipeline. Without a tracker_id the counts stay at zero and dwell time is NaN, while current_count and :attr:~supervision.detection.tools.polygon_zone.PolygonZone.current_count_per_class still work. Detections with a negative tracker_id, which trackers report for tracks they have not confirmed yet, or a non-finite, fractional or non-numeric one never enter or exit, but still count in current_count. A class_id that is not a finite integer counts under the None class key.

Entry and exit counts share their names, their class_id keys and their minimum_crossing_threshold debounce and max_absent_frames eviction with :class:~supervision.detection.line_zone.LineZone, so the same side sequence of a tracked object gives the same counts. As there, a detection with a non-finite anchor, for example one a coord_transform cannot map, has no known side, so it neither crosses nor resets its track's state or dwell. They differ in that a tracker_id repeated within one frame is a single observation, whose side is unknown when its copies disagree, ids that are not finite non-negative integers are skipped rather than used as keys, and frames without a tracker_id emit no warning.

Call :meth:~supervision.detection.tools.polygon_zone.PolygonZone.trigger exactly once per frame for each zone; frame-mode dwell, minimum_crossing_threshold and max_absent_frames count trigger calls, and a tracked object missing from a call is counted absent. Use one PolygonZone per stream (or :meth:~supervision.detection.tools.polygon_zone.PolygonZone.reset between streams); tracker ids from different streams collide.

Attributes:

Name Type Description
polygon

A polygon represented by a numpy array of shape (N, 2), containing the x, y coordinates of the points.

triggering_anchors

Any iterable of positions specifying which anchors of the detections bounding box to consider when deciding on whether the detection fits within the PolygonZone (default: :class:~supervision.geometry.core.Position member BOTTOM_CENTER).

require_all_anchors

If True (default), a detection is considered inside the zone only when every anchor in triggering_anchors is inside. If False, the detection triggers as soon as any anchor is inside. Has no effect when triggering_anchors has a single entry. This is anchor-based, not a true geometric box/polygon intersection test: it fires only when a listed anchor point lands inside the mask. Use mask/IoU-based approaches instead when full-overlap semantics are required.

minimum_crossing_threshold

Number of consecutive observations, that is trigger calls in which it is present, a tracked object must spend on the other side of the zone boundary before an entry or exit is counted.

max_absent_frames int

Number of consecutive trigger calls missing a tracked object at which its entry/exit state is dropped.

current_count

The current count of detected objects within the zone

current_count_per_class dict[int | None, int]

The current count of detected objects within the zone, keyed by class_id (int for classified detections, None for unclassified ones). Classes with no object in the zone are left out.

in_count int

The number of tracked objects that have entered the zone.

out_count int

The number of tracked objects that have exited the zone.

in_count_per_class dict[int | None, int]

The number of tracked objects of each class that have entered the zone, keyed by class_id.

out_count_per_class dict[int | None, int]

The number of tracked objects of each class that have exited the zone, keyed by class_id.

mask

The 2D bool mask for the polygon zone

Example
>>> import numpy as np
>>> import supervision as sv
>>> polygon = np.array([[100, 200], [200, 100], [300, 200], [200, 300]])
>>> polygon_zone = sv.PolygonZone(polygon=polygon)
>>> detections = sv.Detections(
...     xyxy=np.array([[180, 100, 220, 200], [400, 400, 450, 500]])
... )
>>> is_detections_in_zone = polygon_zone.trigger(detections)
>>> is_detections_in_zone
array([ True, False])
>>> polygon_zone.current_count
1
>>> polygon = np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
>>> polygon_zone = sv.PolygonZone(
...     polygon=polygon,
...     triggering_anchors=[sv.Position.TOP_LEFT, sv.Position.BOTTOM_RIGHT],
...     require_all_anchors=False,
... )
>>> detections = sv.Detections(xyxy=np.array([[80, 80, 120, 120]]))
>>> polygon_zone.trigger(detections)
array([ True])

Count tracked objects entering and leaving the zone:

>>> import numpy as np
>>> import supervision as sv
>>> zone = sv.PolygonZone(
...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
... )
>>> outside = sv.Detections(
...     xyxy=np.array([[140.0, 40.0, 160.0, 60.0]]),
...     class_id=np.array([2]),
...     tracker_id=np.array([7]),
... )
>>> inside = sv.Detections(
...     xyxy=np.array([[40.0, 40.0, 60.0, 60.0]]),
...     class_id=np.array([2]),
...     tracker_id=np.array([7]),
... )
>>> for detections in [outside, inside, inside, outside]:
...     _ = zone.trigger(detections)
>>> zone.in_count, zone.out_count
(1, 1)
>>> zone.in_count_per_class
{2: 1}
Source code in src/supervision/detection/tools/polygon_zone.py
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
class PolygonZone:
    """A class for defining a polygon-shaped zone within a frame for detecting objects.

    !!! warning

        Entry and exit counts
        (:attr:`~supervision.detection.tools.polygon_zone.PolygonZone.in_count`,
        :attr:`~supervision.detection.tools.polygon_zone.PolygonZone.out_count`
        and their per-class variants) and
        :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.time_in_zone`
        use the `tracker_id`. Read
        [here](https://trackers.roboflow.com/latest/) to learn how to plug
        tracking into your inference pipeline. Without a `tracker_id` the counts
        stay at zero and dwell time is `NaN`, while `current_count` and
        :attr:`~supervision.detection.tools.polygon_zone.PolygonZone.current_count_per_class`
        still work. Detections with a negative `tracker_id`, which trackers
        report for tracks they have not confirmed yet, or a non-finite,
        fractional or non-numeric one never enter or exit, but still count in
        `current_count`. A `class_id` that is not a finite integer counts under
        the `None` class key.

    Entry and exit counts share their names, their `class_id` keys and their
    `minimum_crossing_threshold` debounce and `max_absent_frames` eviction with
    :class:`~supervision.detection.line_zone.LineZone`, so the same side
    sequence of a tracked object gives the same counts. As there, a detection
    with a non-finite anchor, for example one a `coord_transform` cannot map, has
    no known side, so it neither crosses nor resets its track's state or dwell.
    They differ in that a `tracker_id` repeated within one frame is a single
    observation, whose side is unknown when its copies disagree, ids that are
    not finite non-negative integers are skipped rather than used as keys, and
    frames without a `tracker_id` emit no warning.

    Call :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`
    exactly once per frame for each zone; frame-mode dwell,
    `minimum_crossing_threshold` and `max_absent_frames` count `trigger` calls,
    and a tracked object missing from a call is counted absent. Use one
    `PolygonZone` per stream (or
    :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.reset` between
    streams); tracker ids from different streams collide.

    Attributes:
        polygon: A polygon represented by a numpy array of shape
            `(N, 2)`, containing the `x`, `y` coordinates of the points.
        triggering_anchors: Any iterable of positions specifying
            which anchors of the detections bounding box to consider when deciding on
            whether the detection fits within the PolygonZone
            (default: :class:`~supervision.geometry.core.Position` member
            ``BOTTOM_CENTER``).
        require_all_anchors: If `True` (default), a detection is considered inside
            the zone only when *every* anchor in `triggering_anchors` is inside.
            If `False`, the detection triggers as soon as *any* anchor is inside.
            Has no effect when `triggering_anchors` has a single entry.
            This is anchor-based, not a true geometric box/polygon intersection
            test: it fires only when a listed anchor point lands inside the mask.
            Use mask/IoU-based approaches instead when full-overlap semantics are
            required.
        minimum_crossing_threshold: Number of consecutive observations, that is
            `trigger` calls in which it is present, a tracked object must spend
            on the other side of the zone boundary before an entry or exit is
            counted.
        max_absent_frames: Number of consecutive `trigger` calls missing a
            tracked object at which its entry/exit state is dropped.
        current_count: The current count of detected objects within the zone
        current_count_per_class: The current count of detected objects within
            the zone, keyed by `class_id` (`int` for classified detections,
            `None` for unclassified ones). Classes with no object in the zone
            are left out.
        in_count: The number of tracked objects that have entered the zone.
        out_count: The number of tracked objects that have exited the zone.
        in_count_per_class: The number of tracked objects of each class that
            have entered the zone, keyed by `class_id`.
        out_count_per_class: The number of tracked objects of each class that
            have exited the zone, keyed by `class_id`.
        mask: The 2D bool mask for the polygon zone

    Example:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> polygon = np.array([[100, 200], [200, 100], [300, 200], [200, 300]])
        >>> polygon_zone = sv.PolygonZone(polygon=polygon)
        >>> detections = sv.Detections(
        ...     xyxy=np.array([[180, 100, 220, 200], [400, 400, 450, 500]])
        ... )
        >>> is_detections_in_zone = polygon_zone.trigger(detections)
        >>> is_detections_in_zone
        array([ True, False])
        >>> polygon_zone.current_count
        1

        ```

        ```pycon
        >>> polygon = np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
        >>> polygon_zone = sv.PolygonZone(
        ...     polygon=polygon,
        ...     triggering_anchors=[sv.Position.TOP_LEFT, sv.Position.BOTTOM_RIGHT],
        ...     require_all_anchors=False,
        ... )
        >>> detections = sv.Detections(xyxy=np.array([[80, 80, 120, 120]]))
        >>> polygon_zone.trigger(detections)
        array([ True])

        ```

        Count tracked objects entering and leaving the zone:

        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> zone = sv.PolygonZone(
        ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
        ... )
        >>> outside = sv.Detections(
        ...     xyxy=np.array([[140.0, 40.0, 160.0, 60.0]]),
        ...     class_id=np.array([2]),
        ...     tracker_id=np.array([7]),
        ... )
        >>> inside = sv.Detections(
        ...     xyxy=np.array([[40.0, 40.0, 60.0, 60.0]]),
        ...     class_id=np.array([2]),
        ...     tracker_id=np.array([7]),
        ... )
        >>> for detections in [outside, inside, inside, outside]:
        ...     _ = zone.trigger(detections)
        >>> zone.in_count, zone.out_count
        (1, 1)
        >>> zone.in_count_per_class
        {2: 1}

        ```
    """

    def __init__(
        self,
        polygon: npt.NDArray[np.int64],
        triggering_anchors: Iterable[Position] = (Position.BOTTOM_CENTER,),
        require_all_anchors: bool = True,
        minimum_crossing_threshold: int = 1,
        max_absent_frames: int | None = None,
    ) -> None:
        """Build a zone from a polygon.

        Args:
            polygon: Zone boundary of shape `(N, 2)` holding the `x`, `y`
                coordinates of its vertices.
            triggering_anchors: Which anchors of a detection's bounding box
                decide whether it falls inside the zone.
            require_all_anchors: Whether every triggering anchor must be inside
                for the detection to count, rather than any one of them.
            minimum_crossing_threshold: A tracked object must be seen on the
                other side of the zone boundary for this many consecutive
                observations, that is `trigger` calls in which it is present,
                before its entry or exit is counted. Calls in which the object
                is missing are skipped: they neither extend nor reset the run.
                Raise it to ignore anchors that flicker across the boundary.
                The first side an object is seen on is taken as its starting
                side with no count, so an object that first appears inside the
                zone has not entered it.
            max_absent_frames: Number of consecutive `trigger` calls missing a
                tracked object at which its entry/exit state is dropped, as in
                :class:`~supervision.detection.line_zone.LineZone`. An object
                that returns sooner keeps its state, so a short occlusion does
                not reset it, while dropping bounds memory on long streams. If
                `None`, the limit is the larger of 30 and
                `minimum_crossing_threshold + 1`. Match it to the tracker's
                lost-track buffer. An object that is dropped while inside the
                zone, for example because it left the frame through the zone,
                is not counted as exiting.

        Raises:
            ValueError: If `polygon` is not of shape `(N, 2)`, has fewer than
                `MIN_POLYGON_POINT_COUNT` vertices, `triggering_anchors` is
                empty, or `minimum_crossing_threshold` or `max_absent_frames`
                is less than 1.
        """
        polygon = np.asarray(polygon)
        if polygon.ndim != 2 or polygon.shape[-1] != 2:
            raise ValueError(f"Polygon must have shape (N, 2); got {polygon.shape}.")
        # Fewer than three vertices enclose no area, so the mask below comes out
        # empty or a bare line and the zone silently never triggers. Reject it
        # here rather than let a zone that can never count look like a zone that
        # simply saw nothing. Zero vertices additionally breaks `np.max`.
        if len(polygon) < MIN_POLYGON_POINT_COUNT:
            raise ValueError(
                f"Polygon must have at least {MIN_POLYGON_POINT_COUNT} vertices "
                f"to enclose an area; got {len(polygon)}."
            )

        self.polygon = polygon.astype(int)
        # Materialize once so we can safely accept generators without exhausting them.
        self.triggering_anchors = list(triggering_anchors)
        if not self.triggering_anchors:
            raise ValueError("Triggering anchors cannot be empty.")
        self.require_all_anchors = require_all_anchors
        if minimum_crossing_threshold < 1:
            raise ValueError(
                "minimum_crossing_threshold must be at least 1; "
                f"got {minimum_crossing_threshold}."
            )
        if max_absent_frames is None:
            max_absent_frames = max(30, minimum_crossing_threshold + 1)
        if max_absent_frames < 1:
            raise ValueError("max_absent_frames must be at least 1.")
        self.minimum_crossing_threshold = minimum_crossing_threshold
        self.max_absent_frames: int = max_absent_frames

        self.current_count = 0
        self._current_count_per_class: Counter[int | None] = Counter()
        self._in_count_per_class: Counter[int | None] = Counter()
        self._out_count_per_class: Counter[int | None] = Counter()
        # Side of the boundary (True = inside) each tracker was last confirmed on.
        # Entries and exits are counted against it, never against the raw previous
        # frame, so a sub-threshold flicker cannot become the reference side.
        self._confirmed_inside: dict[int, bool] = {}
        # Consecutive observations a tracker has spent away from its confirmed side.
        self._observations_away: dict[int, int] = {}
        self._frames_absent: dict[int, int] = {}
        # Timestamp at which each track's current stay inside the zone began.
        self._entered_at: dict[int, float] = {}
        # Timestamp of the first observation in each track's current run away from
        # its confirmed side, so a confirmed entry is dated when it really happened.
        self._away_since: dict[int, float] = {}
        self._frame_index = -1
        self._last_timestamp: float | None = None
        # Whether trigger calls pass explicit timestamps; fixed by the first call so
        # seconds and frame counts never mix in one dwell measurement.
        self._uses_explicit_timestamps: bool | None = None

        x_max, y_max = np.max(polygon, axis=0)
        if np.issubdtype(polygon.dtype, np.integer):
            x_max, y_max = int(x_max), int(y_max)
        self.mask = polygon_to_mask(
            polygon=polygon, resolution_wh=(x_max + 2, y_max + 2)
        )

    @property
    def current_count_per_class(self) -> dict[int | None, int]:
        """Objects inside the zone on the last frame, keyed by `class_id`."""
        return dict(self._current_count_per_class)

    @property
    def in_count(self) -> int:
        """Tracked objects that have entered the zone since creation or reset."""
        return sum(self._in_count_per_class.values())

    @property
    def out_count(self) -> int:
        """Tracked objects that have exited the zone since creation or reset."""
        return sum(self._out_count_per_class.values())

    @property
    def in_count_per_class(self) -> dict[int | None, int]:
        """Tracked objects that have entered the zone, keyed by `class_id`."""
        return dict(self._in_count_per_class)

    @property
    def out_count_per_class(self) -> dict[int | None, int]:
        """Tracked objects that have exited the zone, keyed by `class_id`."""
        return dict(self._out_count_per_class)

    def reset(self) -> None:
        """Clear all counts and per-track state so the zone can be reused across
        independent streams. The polygon and configuration are preserved.

        Example:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> zone = sv.PolygonZone(
            ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
            ... )
            >>> detections = sv.Detections(xyxy=np.array([[40.0, 40.0, 60.0, 60.0]]))
            >>> _ = zone.trigger(detections)
            >>> zone.current_count
            1
            >>> zone.reset()
            >>> zone.current_count, zone.in_count, zone.current_count_per_class
            (0, 0, {})

            ```
        """
        self.current_count = 0
        self._current_count_per_class.clear()
        self._in_count_per_class.clear()
        self._out_count_per_class.clear()
        self._confirmed_inside.clear()
        self._observations_away.clear()
        self._frames_absent.clear()
        self._entered_at.clear()
        self._away_since.clear()
        self._frame_index = -1
        self._last_timestamp = None
        self._uses_explicit_timestamps = None

    def time_in_zone(self, detections: Detections) -> npt.NDArray[np.float64]:
        """Report how long each tracked detection has been inside the zone.

        Dwell is measured from the start of each object's current stay to the
        latest
        :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`
        call, in the units of the `timestamp` values passed to it, or in frames
        counted as `trigger` calls when none are passed. A stay starts when an
        entry is confirmed, dated to the first observation of the run that
        confirmed it, or when an object is first seen already inside. It ends
        when an exit is confirmed, so a re-entry starts a new stay, while
        flicker shorter than `minimum_crossing_threshold` and absences shorter
        than `max_absent_frames` do not interrupt it. Detections are matched to
        the zone's state by
        `tracker_id`, so pass the same frame's detections given to
        :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`.

        Args:
            detections: Tracked detections to look up.

        Returns:
            An array aligned with `detections` holding each object's time in the
            zone, or `NaN` for an object that is not confirmed inside (with
            `minimum_crossing_threshold` above 1, an entry or exit takes effect
            only once confirmed), has a missing, negative, non-finite,
            fractional or non-numeric `tracker_id`, or when
            :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`
            was never called.

        Example:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> zone = sv.PolygonZone(
            ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
            ... )
            >>> def frame(x):
            ...     return sv.Detections(
            ...         xyxy=np.array([[x, 40.0, x + 20.0, 60.0]]),
            ...         tracker_id=np.array([7]),
            ...     )
            >>> fps = 2
            >>> for index, x in enumerate([140.0, 40.0, 40.0, 40.0]):
            ...     _ = zone.trigger(frame(x), timestamp=index / fps)
            >>> zone.time_in_zone(frame(40.0))
            array([1.])

            ```
        """
        entered_at = np.full(len(detections), np.nan)
        if self._last_timestamp is None:
            return entered_at
        # Only ids `trigger` could have followed can have state; the rest stay NaN.
        tracker_keys, tracked = _integral_keys(
            detections.tracker_id, len(detections), allow_negative=False
        )
        entered_at[tracked] = [
            self._entered_at.get(key, np.nan) for key in tracker_keys[tracked].tolist()
        ]
        return self._last_timestamp - entered_at

    def trigger(
        self,
        detections: Detections,
        coord_transform: CoordinatesTransformation | None = None,
        timestamp: float | None = None,
    ) -> npt.NDArray[np.bool_]:
        """Determines if the detections are within the polygon zone.

        Anchor points are calculated from original (unclipped) detection boxes to
        avoid per-zone clipping shifting anchor positions. This prevents a single
        detection from being counted in multiple non-overlapping zones due to
        clipping artifacts, although overlapping zones may still legitimately
        contain the same detection. Negative and non-finite anchors are outside;
        a detection with any non-finite anchor is also left out of entry and exit
        counting, as its side of the boundary is unknown, while its track stays
        present, so its state and dwell carry over to the next frame.

        Call this exactly once per frame for each zone; frame-mode dwell,
        `minimum_crossing_threshold` and `max_absent_frames` count `trigger`
        calls, and a tracked object missing from a call is counted absent. Use
        one :class:`~supervision.detection.tools.polygon_zone.PolygonZone` per
        stream (or
        :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.reset`
        between streams); tracker ids from different streams collide. A shared
        zone is not reliably detected: with explicit timestamps it raises only
        when a call's timestamp is earlier than the previous one, so streams
        sharing a monotonic clock mix their ids silently.

        Args:
            detections: The detections to be checked against the polygon zone
            coord_transform: Optional per-frame camera motion. Anchors are mapped
                into the zone's reference frame with
                :meth:`~supervision.geometry.core.CoordinatesTransformation.rel_to_abs`
                before the inside test; anchors mapped outside the zone's bounds
                or to non-finite values are outside.
            timestamp: Time of this frame, used by
                :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.time_in_zone`.
                Pass it on every call, for example `frame_index / fps` for
                seconds in a video or `time.monotonic()` for a live stream, or
                never, to measure time in frames. Equal consecutive timestamps
                are allowed.

        Returns:
            A boolean numpy array indicating
                if each detection is within the polygon zone

        Raises:
            ValueError: If `timestamp` is not finite, exceeds half the largest
                `float64` (about `9e307`) in magnitude, so that dwell times stay
                finite, is earlier than the previous frame's timestamp, or is given
                on some calls but not others since construction or the last
                :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.reset`.

        Example:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> class CameraShift:
            ...     def abs_to_rel(self, points): return points + [50, 0]
            ...     def rel_to_abs(self, points): return points - [50, 0]
            >>> zone = sv.PolygonZone(
            ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
            ... )
            >>> detections = sv.Detections(xyxy=np.array([[120.0, 40.0, 140.0, 60.0]]))
            >>> zone.trigger(detections)
            array([False])
            >>> zone.trigger(detections, coord_transform=CameraShift())
            array([ True])

            ```
        """
        uses_explicit_timestamps = timestamp is not None
        if self._uses_explicit_timestamps not in (None, uses_explicit_timestamps):
            raise ValueError(
                "timestamp must be passed on every trigger call or on none, so "
                "dwell is not measured in mixed units. Call reset() to switch."
            )
        frame_timestamp = (
            float(timestamp) if timestamp is not None else self._frame_index + 1.0
        )
        if not np.isfinite(frame_timestamp):
            raise ValueError(f"timestamp must be finite; got {frame_timestamp}.")
        if abs(frame_timestamp) > _MAX_TIMESTAMP_MAGNITUDE:
            raise ValueError(
                f"timestamp must be at most {_MAX_TIMESTAMP_MAGNITUDE:.4g} in "
                f"magnitude so dwell times stay finite; got {frame_timestamp}."
            )
        if self._last_timestamp is not None and frame_timestamp < self._last_timestamp:
            raise ValueError(
                f"timestamp must not decrease; got {frame_timestamp} after "
                f"{self._last_timestamp}. Call reset() before reusing the zone."
            )

        if len(detections) == 0:
            is_in_zone = np.array([], dtype=bool)
            located = np.array([], dtype=bool)
        else:
            is_in_zone, located = self._is_in_zone(detections, coord_transform)
        # Ids are normalized before any state changes, and never raise, so an
        # unusual tracker or class id cannot leave a half-updated frame behind.
        tracker_keys, tracked = _integral_keys(
            detections.tracker_id, len(detections), allow_negative=False
        )
        class_keys, classified = _integral_keys(
            detections.class_id, len(detections), allow_negative=True
        )
        # The frame is committed only once the inside test has succeeded, so a
        # failing coord_transform leaves the clock where it was.
        self._uses_explicit_timestamps = uses_explicit_timestamps
        self._frame_index += 1
        self._last_timestamp = frame_timestamp
        self._update_counts(
            is_in_zone,
            located,
            tracker_keys,
            tracked,
            class_keys,
            classified,
            frame_timestamp,
        )
        return cast(npt.NDArray[np.bool_], is_in_zone)

    def _is_in_zone(
        self,
        detections: Detections,
        coord_transform: CoordinatesTransformation | None,
    ) -> tuple[npt.NDArray[np.bool_], npt.NDArray[np.bool_]]:
        """Test each detection's triggering anchors against the zone mask.

        Returns:
            Whether each detection is inside the zone, and whether all its anchors
            are finite, which is when its side of the boundary is known.
        """
        anchor_coordinates = np.array(
            [
                detections.get_anchors_coordinates(anchors)
                for anchors in self.triggering_anchors
            ]
        )
        if coord_transform is not None:
            anchor_coordinates = _transform_points(
                anchor_coordinates, coord_transform.rel_to_abs
            )
        # Non-finite anchors test as outside below, but they do not show that the
        # object left, so the caller keeps them out of the crossing state.
        located = np.isfinite(anchor_coordinates).all(axis=(0, 2))
        anchor_coordinates = np.nan_to_num(
            anchor_coordinates, nan=-1.0, posinf=-1.0, neginf=-1.0
        )

        mask_h, mask_w = self.mask.shape
        rounded = np.rint(anchor_coordinates)
        mask_size = np.array([mask_w, mask_h], dtype=np.intp)
        # Test the lower bounds before rounding so a negative subpixel cannot
        # become zero. The rounded coordinates must fit the mask before casting.
        valid_coordinates = (anchor_coordinates >= 0) & (rounded < mask_size)
        in_bounds = valid_coordinates[..., 0] & valid_coordinates[..., 1]
        safe_anchors = np.where(in_bounds[..., None], rounded, 0).astype(np.intp)
        anchor_hits = in_bounds & self.mask[safe_anchors[..., 1], safe_anchors[..., 0]]
        reduce = np.all if self.require_all_anchors else np.any
        is_in_zone = reduce(anchor_hits, axis=0)
        return cast(npt.NDArray[np.bool_], is_in_zone.astype(bool)), located

    def _update_counts(
        self,
        is_in_zone: npt.NDArray[np.bool_],
        located: npt.NDArray[np.bool_],
        tracker_keys: npt.NDArray[np.int64],
        tracked: npt.NDArray[np.bool_],
        class_keys: npt.NDArray[np.int64],
        classified: npt.NDArray[np.bool_],
        timestamp: float,
    ) -> None:
        """Refresh this frame's occupancy counts and advance each track's entry/exit
        state, counting confirmed boundary crossings.

        Takes keys built by
        :func:`~supervision.detection.tools.polygon_zone._integral_keys`, so
        nothing here can raise once the frame is committed. Detections with an
        unusable `class_id` are counted under the `None` key, so the per-class
        counts always add up to `current_count`. A track whose side is unknown
        this frame, because none of its copies is `located` or its copies
        disagree, keeps its state untouched but is not aged as absent.
        """
        self.current_count = int(np.sum(is_in_zone))
        self._current_count_per_class.clear()
        class_ids, counts = np.unique(
            class_keys[is_in_zone & classified], return_counts=True
        )
        self._current_count_per_class.update(
            {int(c): int(n) for c, n in zip(class_ids, counts)}
        )
        unclassified_count = int(np.count_nonzero(is_in_zone & ~classified))
        if unclassified_count:
            self._current_count_per_class[None] = unclassified_count

        # Trackers report a negative id for tracks they have not confirmed yet, and
        # several distinct objects can share it, so such detections get no state,
        # nor do ids that are not finite integers. Every other id seen this frame
        # counts as present, even when its side is unknown, as in LineZone.
        self._age_absent_tracks(present=set(tracker_keys[tracked].tolist()))
        # An id repeated within one frame, for example after merging overlapping
        # sources, is one observation; counting each copy would let a single frame
        # satisfy minimum_crossing_threshold. Copies without a known side are
        # dropped, and copies that disagree leave the side unknown, so the result
        # never depends on row order. The first copy supplies the class.
        observed = np.flatnonzero(tracked & located)
        keys, first_copies, copy_keys = np.unique(
            tracker_keys[observed], return_index=True, return_inverse=True
        )
        copies = np.bincount(copy_keys, minlength=len(keys))
        copies_inside = np.bincount(
            copy_keys, weights=is_in_zone[observed], minlength=len(keys)
        )
        agreed = (copies_inside == 0) | (copies_inside == copies)
        for key, i in zip(keys[agreed].tolist(), observed[first_copies[agreed]]):
            inside = bool(is_in_zone[i])
            # The first observed side seeds the reference; nothing was crossed yet,
            # but an object first seen inside is already dwelling there.
            if key not in self._confirmed_inside:
                self._confirmed_inside[key] = inside
                if inside:
                    self._entered_at[key] = timestamp
                continue
            if inside == self._confirmed_inside[key]:
                self._observations_away.pop(key, None)
                self._away_since.pop(key, None)
                continue

            observations_away = self._observations_away.get(key, 0) + 1
            self._away_since.setdefault(key, timestamp)
            if observations_away < self.minimum_crossing_threshold:
                self._observations_away[key] = observations_away
                continue

            self._observations_away.pop(key, None)
            crossed_at = self._away_since.pop(key)
            self._confirmed_inside[key] = inside
            class_id = int(class_keys[i]) if classified[i] else None
            if inside:
                self._in_count_per_class[class_id] += 1
                self._entered_at[key] = crossed_at
            else:
                self._out_count_per_class[class_id] += 1
                self._entered_at.pop(key, None)

    def _age_absent_tracks(self, present: set[int]) -> None:
        """Advance the absence counter of every known track missing from this frame,
        dropping the state of tracks absent for `max_absent_frames` calls."""
        for key in list(self._confirmed_inside):
            if key in present:
                self._frames_absent.pop(key, None)
                continue
            frames_absent = self._frames_absent.get(key, 0) + 1
            if frames_absent >= self.max_absent_frames:
                del self._confirmed_inside[key]
                self._observations_away.pop(key, None)
                self._away_since.pop(key, None)
                self._entered_at.pop(key, None)
                self._frames_absent.pop(key, None)
            else:
                self._frames_absent[key] = frames_absent

    def occupancy_ratio(self, detections: Detections) -> float:
        """Measure which fraction of the zone's pixels the detections cover.

        Every detection footprint is rasterized onto one boolean canvas and the
        union is intersected with the zone, so overlapping detections are counted
        once. The footprint comes from the first geometry the detections carry,
        in the order
        :attr:`~supervision.detection.core.Detections.area` uses: segmentation
        masks, then oriented boxes stored in
        ``data[ORIENTED_BOX_COORDINATES]``, then axis-aligned boxes. Axis-aligned
        boxes are half-open, matching
        :attr:`~supervision.detection.core.Detections.box_area`, while the zone
        includes the pixels on its vertices, so a box ``[0, 0, 100, 100]`` covers
        exactly a zone with vertices from ``0`` to ``99``.

        The detections must be in the pixel coordinates of the frame the zone's
        polygon was drawn on. No frame size is needed: the union is computed over
        the zone's own
        :attr:`~supervision.detection.tools.polygon_zone.PolygonZone.mask`
        extent, so frame-sized masks are cropped to it, masks of a smaller frame
        are zero-padded, and anything outside the zone is ignored. Camera motion
        compensation is not supported yet: unlike
        :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`,
        this method takes no ``coord_transform``, so map the detections into the
        zone's reference frame before calling it.

        The ratio divides pixel counts, never sums of mask values: a pixel is
        covered when any detection mask is nonzero there, so non-boolean masks, which
        :class:`~supervision.detection.core.Detections` deprecates, treat every
        nonzero value, soft scores included, as foreground. The method leaves
        :attr:`~supervision.detection.tools.polygon_zone.PolygonZone.current_count`
        unchanged.

        Args:
            detections: The detections whose combined footprint is measured.

        Returns:
            The covered share of the zone's pixels, between `0.0` and `1.0`;
                `0.0` for empty `detections`.

        Raises:
            ValueError: If the zone mask is not 2D or has no nonzero pixels, for
                example because the polygon lies entirely at negative coordinates,
                even when `detections` is empty; or if a dense detection mask is
                not of shape `(N, H, W)` or the oriented box corners are not of
                shape `(N, 4, 2)`, with `N` detections, or their finite coordinates
                cannot be represented as `int32` after truncation.

        Example:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> zone = sv.PolygonZone(
            ...     polygon=np.array([[0, 0], [99, 0], [99, 99], [0, 99]])
            ... )
            >>> detections = sv.Detections(
            ...     xyxy=np.array([[0, 0, 50, 100], [25, 0, 75, 100]], dtype=float)
            ... )
            >>> zone.occupancy_ratio(detections)
            0.75
            >>> zone.occupancy_ratio(sv.Detections.empty())
            0.0

            ```
        """
        _validate_shape(np.shape(self.mask), ("H", "W"), "zone mask")
        zone = self.mask.astype(bool)
        zone_pixels = int(np.count_nonzero(zone))
        # Checked before the empty-detections shortcut so that frames without
        # detections cannot hide a zone that has no pixels to cover.
        if zone_pixels == 0:
            raise ValueError(
                "Zone mask covers no pixels, so its occupancy ratio is undefined. "
                "Use a polygon that encloses at least one pixel at non-negative "
                f"coordinates; got a zone mask of shape {zone.shape} with no "
                "nonzero pixels."
            )
        if len(detections) == 0:
            return 0.0
        mask_h, mask_w = zone.shape
        occupied = _detections_to_union_mask(detections, resolution_wh=(mask_w, mask_h))
        return int(np.count_nonzero(occupied & zone)) / zone_pixels

Attributes

current_count_per_class: dict[int | None, int] property

Objects inside the zone on the last frame, keyed by class_id.

in_count: int property

Tracked objects that have entered the zone since creation or reset.

in_count_per_class: dict[int | None, int] property

Tracked objects that have entered the zone, keyed by class_id.

out_count: int property

Tracked objects that have exited the zone since creation or reset.

out_count_per_class: dict[int | None, int] property

Tracked objects that have exited the zone, keyed by class_id.

Methods:

__init__(polygon: npt.NDArray[np.int64], triggering_anchors: Iterable[Position] = (Position.BOTTOM_CENTER,), require_all_anchors: bool = True, minimum_crossing_threshold: int = 1, max_absent_frames: int | None = None) -> None

Build a zone from a polygon.

Parameters:

Name Type Description Default
polygon
NDArray[int64]

Zone boundary of shape (N, 2) holding the x, y coordinates of its vertices.

required
triggering_anchors
Iterable[Position]

Which anchors of a detection's bounding box decide whether it falls inside the zone.

(BOTTOM_CENTER,)
require_all_anchors
bool

Whether every triggering anchor must be inside for the detection to count, rather than any one of them.

True
minimum_crossing_threshold
int

A tracked object must be seen on the other side of the zone boundary for this many consecutive observations, that is trigger calls in which it is present, before its entry or exit is counted. Calls in which the object is missing are skipped: they neither extend nor reset the run. Raise it to ignore anchors that flicker across the boundary. The first side an object is seen on is taken as its starting side with no count, so an object that first appears inside the zone has not entered it.

1
max_absent_frames
int | None

Number of consecutive trigger calls missing a tracked object at which its entry/exit state is dropped, as in :class:~supervision.detection.line_zone.LineZone. An object that returns sooner keeps its state, so a short occlusion does not reset it, while dropping bounds memory on long streams. If None, the limit is the larger of 30 and minimum_crossing_threshold + 1. Match it to the tracker's lost-track buffer. An object that is dropped while inside the zone, for example because it left the frame through the zone, is not counted as exiting.

None

Raises:

Type Description
ValueError

If polygon is not of shape (N, 2), has fewer than MIN_POLYGON_POINT_COUNT vertices, triggering_anchors is empty, or minimum_crossing_threshold or max_absent_frames is less than 1.

Source code in src/supervision/detection/tools/polygon_zone.py
def __init__(
    self,
    polygon: npt.NDArray[np.int64],
    triggering_anchors: Iterable[Position] = (Position.BOTTOM_CENTER,),
    require_all_anchors: bool = True,
    minimum_crossing_threshold: int = 1,
    max_absent_frames: int | None = None,
) -> None:
    """Build a zone from a polygon.

    Args:
        polygon: Zone boundary of shape `(N, 2)` holding the `x`, `y`
            coordinates of its vertices.
        triggering_anchors: Which anchors of a detection's bounding box
            decide whether it falls inside the zone.
        require_all_anchors: Whether every triggering anchor must be inside
            for the detection to count, rather than any one of them.
        minimum_crossing_threshold: A tracked object must be seen on the
            other side of the zone boundary for this many consecutive
            observations, that is `trigger` calls in which it is present,
            before its entry or exit is counted. Calls in which the object
            is missing are skipped: they neither extend nor reset the run.
            Raise it to ignore anchors that flicker across the boundary.
            The first side an object is seen on is taken as its starting
            side with no count, so an object that first appears inside the
            zone has not entered it.
        max_absent_frames: Number of consecutive `trigger` calls missing a
            tracked object at which its entry/exit state is dropped, as in
            :class:`~supervision.detection.line_zone.LineZone`. An object
            that returns sooner keeps its state, so a short occlusion does
            not reset it, while dropping bounds memory on long streams. If
            `None`, the limit is the larger of 30 and
            `minimum_crossing_threshold + 1`. Match it to the tracker's
            lost-track buffer. An object that is dropped while inside the
            zone, for example because it left the frame through the zone,
            is not counted as exiting.

    Raises:
        ValueError: If `polygon` is not of shape `(N, 2)`, has fewer than
            `MIN_POLYGON_POINT_COUNT` vertices, `triggering_anchors` is
            empty, or `minimum_crossing_threshold` or `max_absent_frames`
            is less than 1.
    """
    polygon = np.asarray(polygon)
    if polygon.ndim != 2 or polygon.shape[-1] != 2:
        raise ValueError(f"Polygon must have shape (N, 2); got {polygon.shape}.")
    # Fewer than three vertices enclose no area, so the mask below comes out
    # empty or a bare line and the zone silently never triggers. Reject it
    # here rather than let a zone that can never count look like a zone that
    # simply saw nothing. Zero vertices additionally breaks `np.max`.
    if len(polygon) < MIN_POLYGON_POINT_COUNT:
        raise ValueError(
            f"Polygon must have at least {MIN_POLYGON_POINT_COUNT} vertices "
            f"to enclose an area; got {len(polygon)}."
        )

    self.polygon = polygon.astype(int)
    # Materialize once so we can safely accept generators without exhausting them.
    self.triggering_anchors = list(triggering_anchors)
    if not self.triggering_anchors:
        raise ValueError("Triggering anchors cannot be empty.")
    self.require_all_anchors = require_all_anchors
    if minimum_crossing_threshold < 1:
        raise ValueError(
            "minimum_crossing_threshold must be at least 1; "
            f"got {minimum_crossing_threshold}."
        )
    if max_absent_frames is None:
        max_absent_frames = max(30, minimum_crossing_threshold + 1)
    if max_absent_frames < 1:
        raise ValueError("max_absent_frames must be at least 1.")
    self.minimum_crossing_threshold = minimum_crossing_threshold
    self.max_absent_frames: int = max_absent_frames

    self.current_count = 0
    self._current_count_per_class: Counter[int | None] = Counter()
    self._in_count_per_class: Counter[int | None] = Counter()
    self._out_count_per_class: Counter[int | None] = Counter()
    # Side of the boundary (True = inside) each tracker was last confirmed on.
    # Entries and exits are counted against it, never against the raw previous
    # frame, so a sub-threshold flicker cannot become the reference side.
    self._confirmed_inside: dict[int, bool] = {}
    # Consecutive observations a tracker has spent away from its confirmed side.
    self._observations_away: dict[int, int] = {}
    self._frames_absent: dict[int, int] = {}
    # Timestamp at which each track's current stay inside the zone began.
    self._entered_at: dict[int, float] = {}
    # Timestamp of the first observation in each track's current run away from
    # its confirmed side, so a confirmed entry is dated when it really happened.
    self._away_since: dict[int, float] = {}
    self._frame_index = -1
    self._last_timestamp: float | None = None
    # Whether trigger calls pass explicit timestamps; fixed by the first call so
    # seconds and frame counts never mix in one dwell measurement.
    self._uses_explicit_timestamps: bool | None = None

    x_max, y_max = np.max(polygon, axis=0)
    if np.issubdtype(polygon.dtype, np.integer):
        x_max, y_max = int(x_max), int(y_max)
    self.mask = polygon_to_mask(
        polygon=polygon, resolution_wh=(x_max + 2, y_max + 2)
    )

occupancy_ratio(detections: Detections) -> float

Measure which fraction of the zone's pixels the detections cover.

Every detection footprint is rasterized onto one boolean canvas and the union is intersected with the zone, so overlapping detections are counted once. The footprint comes from the first geometry the detections carry, in the order :attr:~supervision.detection.core.Detections.area uses: segmentation masks, then oriented boxes stored in data[ORIENTED_BOX_COORDINATES], then axis-aligned boxes. Axis-aligned boxes are half-open, matching :attr:~supervision.detection.core.Detections.box_area, while the zone includes the pixels on its vertices, so a box [0, 0, 100, 100] covers exactly a zone with vertices from 0 to 99.

The detections must be in the pixel coordinates of the frame the zone's polygon was drawn on. No frame size is needed: the union is computed over the zone's own :attr:~supervision.detection.tools.polygon_zone.PolygonZone.mask extent, so frame-sized masks are cropped to it, masks of a smaller frame are zero-padded, and anything outside the zone is ignored. Camera motion compensation is not supported yet: unlike :meth:~supervision.detection.tools.polygon_zone.PolygonZone.trigger, this method takes no coord_transform, so map the detections into the zone's reference frame before calling it.

The ratio divides pixel counts, never sums of mask values: a pixel is covered when any detection mask is nonzero there, so non-boolean masks, which :class:~supervision.detection.core.Detections deprecates, treat every nonzero value, soft scores included, as foreground. The method leaves :attr:~supervision.detection.tools.polygon_zone.PolygonZone.current_count unchanged.

Parameters:

Name Type Description Default
detections
Detections

The detections whose combined footprint is measured.

required

Returns:

Type Description
float

The covered share of the zone's pixels, between 0.0 and 1.0; 0.0 for empty detections.

Raises:

Type Description
ValueError

If the zone mask is not 2D or has no nonzero pixels, for example because the polygon lies entirely at negative coordinates, even when detections is empty; or if a dense detection mask is not of shape (N, H, W) or the oriented box corners are not of shape (N, 4, 2), with N detections, or their finite coordinates cannot be represented as int32 after truncation.

Example
>>> import numpy as np
>>> import supervision as sv
>>> zone = sv.PolygonZone(
...     polygon=np.array([[0, 0], [99, 0], [99, 99], [0, 99]])
... )
>>> detections = sv.Detections(
...     xyxy=np.array([[0, 0, 50, 100], [25, 0, 75, 100]], dtype=float)
... )
>>> zone.occupancy_ratio(detections)
0.75
>>> zone.occupancy_ratio(sv.Detections.empty())
0.0
Source code in src/supervision/detection/tools/polygon_zone.py
def occupancy_ratio(self, detections: Detections) -> float:
    """Measure which fraction of the zone's pixels the detections cover.

    Every detection footprint is rasterized onto one boolean canvas and the
    union is intersected with the zone, so overlapping detections are counted
    once. The footprint comes from the first geometry the detections carry,
    in the order
    :attr:`~supervision.detection.core.Detections.area` uses: segmentation
    masks, then oriented boxes stored in
    ``data[ORIENTED_BOX_COORDINATES]``, then axis-aligned boxes. Axis-aligned
    boxes are half-open, matching
    :attr:`~supervision.detection.core.Detections.box_area`, while the zone
    includes the pixels on its vertices, so a box ``[0, 0, 100, 100]`` covers
    exactly a zone with vertices from ``0`` to ``99``.

    The detections must be in the pixel coordinates of the frame the zone's
    polygon was drawn on. No frame size is needed: the union is computed over
    the zone's own
    :attr:`~supervision.detection.tools.polygon_zone.PolygonZone.mask`
    extent, so frame-sized masks are cropped to it, masks of a smaller frame
    are zero-padded, and anything outside the zone is ignored. Camera motion
    compensation is not supported yet: unlike
    :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`,
    this method takes no ``coord_transform``, so map the detections into the
    zone's reference frame before calling it.

    The ratio divides pixel counts, never sums of mask values: a pixel is
    covered when any detection mask is nonzero there, so non-boolean masks, which
    :class:`~supervision.detection.core.Detections` deprecates, treat every
    nonzero value, soft scores included, as foreground. The method leaves
    :attr:`~supervision.detection.tools.polygon_zone.PolygonZone.current_count`
    unchanged.

    Args:
        detections: The detections whose combined footprint is measured.

    Returns:
        The covered share of the zone's pixels, between `0.0` and `1.0`;
            `0.0` for empty `detections`.

    Raises:
        ValueError: If the zone mask is not 2D or has no nonzero pixels, for
            example because the polygon lies entirely at negative coordinates,
            even when `detections` is empty; or if a dense detection mask is
            not of shape `(N, H, W)` or the oriented box corners are not of
            shape `(N, 4, 2)`, with `N` detections, or their finite coordinates
            cannot be represented as `int32` after truncation.

    Example:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> zone = sv.PolygonZone(
        ...     polygon=np.array([[0, 0], [99, 0], [99, 99], [0, 99]])
        ... )
        >>> detections = sv.Detections(
        ...     xyxy=np.array([[0, 0, 50, 100], [25, 0, 75, 100]], dtype=float)
        ... )
        >>> zone.occupancy_ratio(detections)
        0.75
        >>> zone.occupancy_ratio(sv.Detections.empty())
        0.0

        ```
    """
    _validate_shape(np.shape(self.mask), ("H", "W"), "zone mask")
    zone = self.mask.astype(bool)
    zone_pixels = int(np.count_nonzero(zone))
    # Checked before the empty-detections shortcut so that frames without
    # detections cannot hide a zone that has no pixels to cover.
    if zone_pixels == 0:
        raise ValueError(
            "Zone mask covers no pixels, so its occupancy ratio is undefined. "
            "Use a polygon that encloses at least one pixel at non-negative "
            f"coordinates; got a zone mask of shape {zone.shape} with no "
            "nonzero pixels."
        )
    if len(detections) == 0:
        return 0.0
    mask_h, mask_w = zone.shape
    occupied = _detections_to_union_mask(detections, resolution_wh=(mask_w, mask_h))
    return int(np.count_nonzero(occupied & zone)) / zone_pixels

reset() -> None

Clear all counts and per-track state so the zone can be reused across independent streams. The polygon and configuration are preserved.

Example
>>> import numpy as np
>>> import supervision as sv
>>> zone = sv.PolygonZone(
...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
... )
>>> detections = sv.Detections(xyxy=np.array([[40.0, 40.0, 60.0, 60.0]]))
>>> _ = zone.trigger(detections)
>>> zone.current_count
1
>>> zone.reset()
>>> zone.current_count, zone.in_count, zone.current_count_per_class
(0, 0, {})
Source code in src/supervision/detection/tools/polygon_zone.py
def reset(self) -> None:
    """Clear all counts and per-track state so the zone can be reused across
    independent streams. The polygon and configuration are preserved.

    Example:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> zone = sv.PolygonZone(
        ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
        ... )
        >>> detections = sv.Detections(xyxy=np.array([[40.0, 40.0, 60.0, 60.0]]))
        >>> _ = zone.trigger(detections)
        >>> zone.current_count
        1
        >>> zone.reset()
        >>> zone.current_count, zone.in_count, zone.current_count_per_class
        (0, 0, {})

        ```
    """
    self.current_count = 0
    self._current_count_per_class.clear()
    self._in_count_per_class.clear()
    self._out_count_per_class.clear()
    self._confirmed_inside.clear()
    self._observations_away.clear()
    self._frames_absent.clear()
    self._entered_at.clear()
    self._away_since.clear()
    self._frame_index = -1
    self._last_timestamp = None
    self._uses_explicit_timestamps = None

time_in_zone(detections: Detections) -> npt.NDArray[np.float64]

Report how long each tracked detection has been inside the zone.

Dwell is measured from the start of each object's current stay to the latest :meth:~supervision.detection.tools.polygon_zone.PolygonZone.trigger call, in the units of the timestamp values passed to it, or in frames counted as trigger calls when none are passed. A stay starts when an entry is confirmed, dated to the first observation of the run that confirmed it, or when an object is first seen already inside. It ends when an exit is confirmed, so a re-entry starts a new stay, while flicker shorter than minimum_crossing_threshold and absences shorter than max_absent_frames do not interrupt it. Detections are matched to the zone's state by tracker_id, so pass the same frame's detections given to :meth:~supervision.detection.tools.polygon_zone.PolygonZone.trigger.

Parameters:

Name Type Description Default
detections
Detections

Tracked detections to look up.

required

Returns:

Type Description
NDArray[float64]

An array aligned with detections holding each object's time in the

NDArray[float64]

zone, or NaN for an object that is not confirmed inside (with

NDArray[float64]

minimum_crossing_threshold above 1, an entry or exit takes effect

NDArray[float64]

only once confirmed), has a missing, negative, non-finite,

NDArray[float64]

fractional or non-numeric tracker_id, or when

NDArray[float64]

meth:~supervision.detection.tools.polygon_zone.PolygonZone.trigger

NDArray[float64]

was never called.

Example
>>> import numpy as np
>>> import supervision as sv
>>> zone = sv.PolygonZone(
...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
... )
>>> def frame(x):
...     return sv.Detections(
...         xyxy=np.array([[x, 40.0, x + 20.0, 60.0]]),
...         tracker_id=np.array([7]),
...     )
>>> fps = 2
>>> for index, x in enumerate([140.0, 40.0, 40.0, 40.0]):
...     _ = zone.trigger(frame(x), timestamp=index / fps)
>>> zone.time_in_zone(frame(40.0))
array([1.])
Source code in src/supervision/detection/tools/polygon_zone.py
def time_in_zone(self, detections: Detections) -> npt.NDArray[np.float64]:
    """Report how long each tracked detection has been inside the zone.

    Dwell is measured from the start of each object's current stay to the
    latest
    :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`
    call, in the units of the `timestamp` values passed to it, or in frames
    counted as `trigger` calls when none are passed. A stay starts when an
    entry is confirmed, dated to the first observation of the run that
    confirmed it, or when an object is first seen already inside. It ends
    when an exit is confirmed, so a re-entry starts a new stay, while
    flicker shorter than `minimum_crossing_threshold` and absences shorter
    than `max_absent_frames` do not interrupt it. Detections are matched to
    the zone's state by
    `tracker_id`, so pass the same frame's detections given to
    :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`.

    Args:
        detections: Tracked detections to look up.

    Returns:
        An array aligned with `detections` holding each object's time in the
        zone, or `NaN` for an object that is not confirmed inside (with
        `minimum_crossing_threshold` above 1, an entry or exit takes effect
        only once confirmed), has a missing, negative, non-finite,
        fractional or non-numeric `tracker_id`, or when
        :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`
        was never called.

    Example:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> zone = sv.PolygonZone(
        ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
        ... )
        >>> def frame(x):
        ...     return sv.Detections(
        ...         xyxy=np.array([[x, 40.0, x + 20.0, 60.0]]),
        ...         tracker_id=np.array([7]),
        ...     )
        >>> fps = 2
        >>> for index, x in enumerate([140.0, 40.0, 40.0, 40.0]):
        ...     _ = zone.trigger(frame(x), timestamp=index / fps)
        >>> zone.time_in_zone(frame(40.0))
        array([1.])

        ```
    """
    entered_at = np.full(len(detections), np.nan)
    if self._last_timestamp is None:
        return entered_at
    # Only ids `trigger` could have followed can have state; the rest stay NaN.
    tracker_keys, tracked = _integral_keys(
        detections.tracker_id, len(detections), allow_negative=False
    )
    entered_at[tracked] = [
        self._entered_at.get(key, np.nan) for key in tracker_keys[tracked].tolist()
    ]
    return self._last_timestamp - entered_at

trigger(detections: Detections, coord_transform: CoordinatesTransformation | None = None, timestamp: float | None = None) -> npt.NDArray[np.bool_]

Determines if the detections are within the polygon zone.

Anchor points are calculated from original (unclipped) detection boxes to avoid per-zone clipping shifting anchor positions. This prevents a single detection from being counted in multiple non-overlapping zones due to clipping artifacts, although overlapping zones may still legitimately contain the same detection. Negative and non-finite anchors are outside; a detection with any non-finite anchor is also left out of entry and exit counting, as its side of the boundary is unknown, while its track stays present, so its state and dwell carry over to the next frame.

Call this exactly once per frame for each zone; frame-mode dwell, minimum_crossing_threshold and max_absent_frames count trigger calls, and a tracked object missing from a call is counted absent. Use one :class:~supervision.detection.tools.polygon_zone.PolygonZone per stream (or :meth:~supervision.detection.tools.polygon_zone.PolygonZone.reset between streams); tracker ids from different streams collide. A shared zone is not reliably detected: with explicit timestamps it raises only when a call's timestamp is earlier than the previous one, so streams sharing a monotonic clock mix their ids silently.

Parameters:

Name Type Description Default
detections
Detections

The detections to be checked against the polygon zone

required
coord_transform
CoordinatesTransformation | None

Optional per-frame camera motion. Anchors are mapped into the zone's reference frame with :meth:~supervision.geometry.core.CoordinatesTransformation.rel_to_abs before the inside test; anchors mapped outside the zone's bounds or to non-finite values are outside.

None
timestamp
float | None

Time of this frame, used by :meth:~supervision.detection.tools.polygon_zone.PolygonZone.time_in_zone. Pass it on every call, for example frame_index / fps for seconds in a video or time.monotonic() for a live stream, or never, to measure time in frames. Equal consecutive timestamps are allowed.

None

Returns:

Type Description
NDArray[bool_]

A boolean numpy array indicating if each detection is within the polygon zone

Raises:

Type Description
ValueError

If timestamp is not finite, exceeds half the largest float64 (about 9e307) in magnitude, so that dwell times stay finite, is earlier than the previous frame's timestamp, or is given on some calls but not others since construction or the last :meth:~supervision.detection.tools.polygon_zone.PolygonZone.reset.

Example
>>> import numpy as np
>>> import supervision as sv
>>> class CameraShift:
...     def abs_to_rel(self, points): return points + [50, 0]
...     def rel_to_abs(self, points): return points - [50, 0]
>>> zone = sv.PolygonZone(
...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
... )
>>> detections = sv.Detections(xyxy=np.array([[120.0, 40.0, 140.0, 60.0]]))
>>> zone.trigger(detections)
array([False])
>>> zone.trigger(detections, coord_transform=CameraShift())
array([ True])
Source code in src/supervision/detection/tools/polygon_zone.py
def trigger(
    self,
    detections: Detections,
    coord_transform: CoordinatesTransformation | None = None,
    timestamp: float | None = None,
) -> npt.NDArray[np.bool_]:
    """Determines if the detections are within the polygon zone.

    Anchor points are calculated from original (unclipped) detection boxes to
    avoid per-zone clipping shifting anchor positions. This prevents a single
    detection from being counted in multiple non-overlapping zones due to
    clipping artifacts, although overlapping zones may still legitimately
    contain the same detection. Negative and non-finite anchors are outside;
    a detection with any non-finite anchor is also left out of entry and exit
    counting, as its side of the boundary is unknown, while its track stays
    present, so its state and dwell carry over to the next frame.

    Call this exactly once per frame for each zone; frame-mode dwell,
    `minimum_crossing_threshold` and `max_absent_frames` count `trigger`
    calls, and a tracked object missing from a call is counted absent. Use
    one :class:`~supervision.detection.tools.polygon_zone.PolygonZone` per
    stream (or
    :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.reset`
    between streams); tracker ids from different streams collide. A shared
    zone is not reliably detected: with explicit timestamps it raises only
    when a call's timestamp is earlier than the previous one, so streams
    sharing a monotonic clock mix their ids silently.

    Args:
        detections: The detections to be checked against the polygon zone
        coord_transform: Optional per-frame camera motion. Anchors are mapped
            into the zone's reference frame with
            :meth:`~supervision.geometry.core.CoordinatesTransformation.rel_to_abs`
            before the inside test; anchors mapped outside the zone's bounds
            or to non-finite values are outside.
        timestamp: Time of this frame, used by
            :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.time_in_zone`.
            Pass it on every call, for example `frame_index / fps` for
            seconds in a video or `time.monotonic()` for a live stream, or
            never, to measure time in frames. Equal consecutive timestamps
            are allowed.

    Returns:
        A boolean numpy array indicating
            if each detection is within the polygon zone

    Raises:
        ValueError: If `timestamp` is not finite, exceeds half the largest
            `float64` (about `9e307`) in magnitude, so that dwell times stay
            finite, is earlier than the previous frame's timestamp, or is given
            on some calls but not others since construction or the last
            :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.reset`.

    Example:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> class CameraShift:
        ...     def abs_to_rel(self, points): return points + [50, 0]
        ...     def rel_to_abs(self, points): return points - [50, 0]
        >>> zone = sv.PolygonZone(
        ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
        ... )
        >>> detections = sv.Detections(xyxy=np.array([[120.0, 40.0, 140.0, 60.0]]))
        >>> zone.trigger(detections)
        array([False])
        >>> zone.trigger(detections, coord_transform=CameraShift())
        array([ True])

        ```
    """
    uses_explicit_timestamps = timestamp is not None
    if self._uses_explicit_timestamps not in (None, uses_explicit_timestamps):
        raise ValueError(
            "timestamp must be passed on every trigger call or on none, so "
            "dwell is not measured in mixed units. Call reset() to switch."
        )
    frame_timestamp = (
        float(timestamp) if timestamp is not None else self._frame_index + 1.0
    )
    if not np.isfinite(frame_timestamp):
        raise ValueError(f"timestamp must be finite; got {frame_timestamp}.")
    if abs(frame_timestamp) > _MAX_TIMESTAMP_MAGNITUDE:
        raise ValueError(
            f"timestamp must be at most {_MAX_TIMESTAMP_MAGNITUDE:.4g} in "
            f"magnitude so dwell times stay finite; got {frame_timestamp}."
        )
    if self._last_timestamp is not None and frame_timestamp < self._last_timestamp:
        raise ValueError(
            f"timestamp must not decrease; got {frame_timestamp} after "
            f"{self._last_timestamp}. Call reset() before reusing the zone."
        )

    if len(detections) == 0:
        is_in_zone = np.array([], dtype=bool)
        located = np.array([], dtype=bool)
    else:
        is_in_zone, located = self._is_in_zone(detections, coord_transform)
    # Ids are normalized before any state changes, and never raise, so an
    # unusual tracker or class id cannot leave a half-updated frame behind.
    tracker_keys, tracked = _integral_keys(
        detections.tracker_id, len(detections), allow_negative=False
    )
    class_keys, classified = _integral_keys(
        detections.class_id, len(detections), allow_negative=True
    )
    # The frame is committed only once the inside test has succeeded, so a
    # failing coord_transform leaves the clock where it was.
    self._uses_explicit_timestamps = uses_explicit_timestamps
    self._frame_index += 1
    self._last_timestamp = frame_timestamp
    self._update_counts(
        is_in_zone,
        located,
        tracker_keys,
        tracked,
        class_keys,
        classified,
        frame_timestamp,
    )
    return cast(npt.NDArray[np.bool_], is_in_zone)

PolygonZoneAnnotator

supervision.detection.tools.polygon_zone.PolygonZoneAnnotator

A class for annotating a polygon-shaped zone within a frame with a count of detected objects.

Attributes:

Name Type Description
zone

The polygon zone to be annotated

color

The color to draw the polygon lines, default is white

thickness

The thickness of the polygon lines, default is 2

text_color

The color of the text on the polygon, default is black

text_scale

The scale of the text on the polygon, default is 0.5

text_thickness

The thickness of the text on the polygon, default is 1

text_padding

The padding around the text on the polygon, default is 10

font

The font type for the text on the polygon, default is cv2.FONT_HERSHEY_SIMPLEX

center

The center of the polygon for text placement

display_in_zone_count

Show the label of the zone or not. Default is True

opacity

The opacity of zone filling when drawn on the scene. Default is 0

Example
>>> import numpy as np
>>> import supervision as sv
>>> polygon = np.array([[100, 200], [200, 100], [300, 200], [200, 300]])
>>> polygon_zone = sv.PolygonZone(polygon=polygon)
>>> zone_annotator = sv.PolygonZoneAnnotator(zone=polygon_zone, thickness=2)
>>> scene = np.zeros((400, 400, 3), dtype=np.uint8)
>>> annotated_scene = zone_annotator.annotate(scene=scene)
>>> annotated_scene.shape
(400, 400, 3)
Source code in src/supervision/detection/tools/polygon_zone.py
class PolygonZoneAnnotator:
    """A class for annotating a polygon-shaped zone within a frame with a count of
    detected objects.

    Attributes:
        zone: The polygon zone to be annotated
        color: The color to draw the polygon lines, default is white
        thickness: The thickness of the polygon lines, default is 2
        text_color: The color of the text on the polygon, default is black
        text_scale: The scale of the text on the polygon, default is 0.5
        text_thickness: The thickness of the text on the polygon, default is 1
        text_padding: The padding around the text on the polygon, default is 10
        font: The font type for the text on the polygon,
            default is cv2.FONT_HERSHEY_SIMPLEX
        center: The center of the polygon for text placement
        display_in_zone_count: Show the label of the zone or not. Default is True
        opacity: The opacity of zone filling when drawn on the scene. Default is 0

    Example:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> polygon = np.array([[100, 200], [200, 100], [300, 200], [200, 300]])
        >>> polygon_zone = sv.PolygonZone(polygon=polygon)
        >>> zone_annotator = sv.PolygonZoneAnnotator(zone=polygon_zone, thickness=2)
        >>> scene = np.zeros((400, 400, 3), dtype=np.uint8)
        >>> annotated_scene = zone_annotator.annotate(scene=scene)
        >>> annotated_scene.shape
        (400, 400, 3)

        ```
    """

    def __init__(
        self,
        zone: PolygonZone,
        color: Color = Color.WHITE,
        thickness: int = 2,
        text_color: Color = Color.BLACK,
        text_scale: float = 0.5,
        text_thickness: int = 1,
        text_padding: int = 10,
        display_in_zone_count: bool = True,
        opacity: float = 0,
    ) -> None:
        self.zone = zone
        self.color = color
        self.thickness = thickness
        self.text_color = text_color
        self.text_scale = text_scale
        self.text_thickness = text_thickness
        self.text_padding = text_padding
        self.font = cv2.FONT_HERSHEY_SIMPLEX
        self.center = get_polygon_center(polygon=zone.polygon)
        self.display_in_zone_count = display_in_zone_count
        self.opacity = opacity

    def annotate(
        self,
        scene: npt.NDArray[Any],
        label: str | None = None,
        coord_transform: CoordinatesTransformation | None = None,
    ) -> npt.NDArray[Any]:
        """Annotates the polygon zone within a frame with a count of detected objects.

        Args:
            scene: The image on which the polygon zone will be annotated
            label: A label for the count of detected objects
                within the polygon zone (default: None)
            coord_transform: Optional per-frame camera motion, the same one
                passed to
                :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`.
                The zone's vertices, and its count label when shown, are mapped
                into the current frame with
                :meth:`~supervision.geometry.core.CoordinatesTransformation.abs_to_rel`
                and rounded to the nearest pixel, where drawing without a transform
                truncates. If any of them maps to a non-finite coordinate or one
                beyond `2**20` pixels, the zone is not drawn on this frame.

        Returns:
            The image with the polygon zone and count of detected objects

        Raises:
            ValueError: If `coord_transform` does not return one point per input
                point.

        Example:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> class CameraShift:
            ...     def abs_to_rel(self, points): return points + [50, 0]
            ...     def rel_to_abs(self, points): return points - [50, 0]
            >>> zone = sv.PolygonZone(
            ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
            ... )
            >>> zone_annotator = sv.PolygonZoneAnnotator(
            ...     zone=zone, display_in_zone_count=False
            ... )
            >>> scene = np.zeros((120, 200, 3), dtype=np.uint8)
            >>> annotated_scene = zone_annotator.annotate(
            ...     scene=scene, coord_transform=CameraShift()
            ... )
            >>> annotated_scene[50, 150].tolist()
            [255, 255, 255]
            >>> annotated_scene[50, 100].tolist()
            [0, 0, 0]

            ```
        """
        polygon = self.zone.polygon
        center = self.center
        if coord_transform is not None:
            reference_points = polygon
            if self.display_in_zone_count:
                reference_points = np.vstack([polygon, [[center.x, center.y]]])
            mapped = _map_to_drawing_coordinates(reference_points, coord_transform)
            if mapped is None:
                # Alias the result like the drawn path: a copy when filling.
                return scene if self.opacity == 0 else scene.copy()
            mapped_pixels = np.rint(mapped).astype(np.int64)
            polygon = mapped_pixels[: len(self.zone.polygon)]
            if self.display_in_zone_count:
                center_x, center_y = mapped_pixels[-1].tolist()
                center = Point(x=center_x, y=center_y)

        if self.opacity == 0:
            annotated_frame = draw_polygon(
                scene=scene,
                polygon=polygon,
                color=self.color,
                thickness=self.thickness,
            )
        else:
            annotated_frame = draw_filled_polygon(
                scene=scene.copy(),
                polygon=polygon,
                color=self.color,
                opacity=self.opacity,
            )
            annotated_frame = draw_polygon(
                scene=annotated_frame,
                polygon=polygon,
                color=self.color,
                thickness=self.thickness,
            )

        if self.display_in_zone_count:
            annotated_frame = draw_text(
                scene=annotated_frame,
                text=str(self.zone.current_count) if label is None else label,
                text_anchor=center,
                background_color=self.color,
                text_color=self.text_color,
                text_scale=self.text_scale,
                text_thickness=self.text_thickness,
                text_padding=self.text_padding,
                text_font=self.font,
            )

        return cast(npt.NDArray[Any], annotated_frame)

Methods:

annotate(scene: npt.NDArray[Any], label: str | None = None, coord_transform: CoordinatesTransformation | None = None) -> npt.NDArray[Any]

Annotates the polygon zone within a frame with a count of detected objects.

Parameters:

Name Type Description Default
scene
NDArray[Any]

The image on which the polygon zone will be annotated

required
label
str | None

A label for the count of detected objects within the polygon zone (default: None)

None
coord_transform
CoordinatesTransformation | None

Optional per-frame camera motion, the same one passed to :meth:~supervision.detection.tools.polygon_zone.PolygonZone.trigger. The zone's vertices, and its count label when shown, are mapped into the current frame with :meth:~supervision.geometry.core.CoordinatesTransformation.abs_to_rel and rounded to the nearest pixel, where drawing without a transform truncates. If any of them maps to a non-finite coordinate or one beyond 2**20 pixels, the zone is not drawn on this frame.

None

Returns:

Type Description
NDArray[Any]

The image with the polygon zone and count of detected objects

Raises:

Type Description
ValueError

If coord_transform does not return one point per input point.

Example
>>> import numpy as np
>>> import supervision as sv
>>> class CameraShift:
...     def abs_to_rel(self, points): return points + [50, 0]
...     def rel_to_abs(self, points): return points - [50, 0]
>>> zone = sv.PolygonZone(
...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
... )
>>> zone_annotator = sv.PolygonZoneAnnotator(
...     zone=zone, display_in_zone_count=False
... )
>>> scene = np.zeros((120, 200, 3), dtype=np.uint8)
>>> annotated_scene = zone_annotator.annotate(
...     scene=scene, coord_transform=CameraShift()
... )
>>> annotated_scene[50, 150].tolist()
[255, 255, 255]
>>> annotated_scene[50, 100].tolist()
[0, 0, 0]
Source code in src/supervision/detection/tools/polygon_zone.py
def annotate(
    self,
    scene: npt.NDArray[Any],
    label: str | None = None,
    coord_transform: CoordinatesTransformation | None = None,
) -> npt.NDArray[Any]:
    """Annotates the polygon zone within a frame with a count of detected objects.

    Args:
        scene: The image on which the polygon zone will be annotated
        label: A label for the count of detected objects
            within the polygon zone (default: None)
        coord_transform: Optional per-frame camera motion, the same one
            passed to
            :meth:`~supervision.detection.tools.polygon_zone.PolygonZone.trigger`.
            The zone's vertices, and its count label when shown, are mapped
            into the current frame with
            :meth:`~supervision.geometry.core.CoordinatesTransformation.abs_to_rel`
            and rounded to the nearest pixel, where drawing without a transform
            truncates. If any of them maps to a non-finite coordinate or one
            beyond `2**20` pixels, the zone is not drawn on this frame.

    Returns:
        The image with the polygon zone and count of detected objects

    Raises:
        ValueError: If `coord_transform` does not return one point per input
            point.

    Example:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> class CameraShift:
        ...     def abs_to_rel(self, points): return points + [50, 0]
        ...     def rel_to_abs(self, points): return points - [50, 0]
        >>> zone = sv.PolygonZone(
        ...     polygon=np.array([[0, 0], [100, 0], [100, 100], [0, 100]])
        ... )
        >>> zone_annotator = sv.PolygonZoneAnnotator(
        ...     zone=zone, display_in_zone_count=False
        ... )
        >>> scene = np.zeros((120, 200, 3), dtype=np.uint8)
        >>> annotated_scene = zone_annotator.annotate(
        ...     scene=scene, coord_transform=CameraShift()
        ... )
        >>> annotated_scene[50, 150].tolist()
        [255, 255, 255]
        >>> annotated_scene[50, 100].tolist()
        [0, 0, 0]

        ```
    """
    polygon = self.zone.polygon
    center = self.center
    if coord_transform is not None:
        reference_points = polygon
        if self.display_in_zone_count:
            reference_points = np.vstack([polygon, [[center.x, center.y]]])
        mapped = _map_to_drawing_coordinates(reference_points, coord_transform)
        if mapped is None:
            # Alias the result like the drawn path: a copy when filling.
            return scene if self.opacity == 0 else scene.copy()
        mapped_pixels = np.rint(mapped).astype(np.int64)
        polygon = mapped_pixels[: len(self.zone.polygon)]
        if self.display_in_zone_count:
            center_x, center_y = mapped_pixels[-1].tolist()
            center = Point(x=center_x, y=center_y)

    if self.opacity == 0:
        annotated_frame = draw_polygon(
            scene=scene,
            polygon=polygon,
            color=self.color,
            thickness=self.thickness,
        )
    else:
        annotated_frame = draw_filled_polygon(
            scene=scene.copy(),
            polygon=polygon,
            color=self.color,
            opacity=self.opacity,
        )
        annotated_frame = draw_polygon(
            scene=annotated_frame,
            polygon=polygon,
            color=self.color,
            thickness=self.thickness,
        )

    if self.display_in_zone_count:
        annotated_frame = draw_text(
            scene=annotated_frame,
            text=str(self.zone.current_count) if label is None else label,
            text_anchor=center,
            background_color=self.color,
            text_color=self.text_color,
            text_scale=self.text_scale,
            text_thickness=self.text_thickness,
            text_padding=self.text_padding,
            text_font=self.font,
        )

    return cast(npt.NDArray[Any], annotated_frame)

Comments