§
    >²¸jT*  ã                   óÊ   — d dl mZ d dlmZmZ d dlZd dlmZ d dl	m
Z
 d dl	mZ d dlmZmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ  G d„ d¦  «        Z G d„ d¦  «        ZdS )é    )ÚIterable)ÚAnyÚcastN)Ú
Detections)Ú_cv2)ÚMIN_POLYGON_POINT_COUNTÚpolygon_to_mask)ÚColor)Údraw_filled_polygonÚdraw_polygonÚ	draw_text)ÚPosition)Úget_polygon_centerc            	       ó˜   — e Zd ZdZej        fdfdej        ej	                 de
e         deddfd„Zd	edej        ej                 fd
„ZdS )ÚPolygonZoneav	  
    A class for defining a polygon-shaped zone within a frame for detecting objects.

    !!! warning

        PolygonZone uses the `tracker_id`. Read
        [here](/latest/trackers/) to learn how to plug
        tracking into your inference pipeline.

    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: (sv.Position.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.
        current_count: The current count of detected objects within the zone
        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])

        ```
    TÚpolygonÚtriggering_anchorsÚrequire_all_anchorsÚreturnNc                 ó2  — t          j        |¦  «        }|j        dk    s|j        d         dk    rt	          d|j        › d�¦  «        ‚t          |¦  «        t          k     r(t	          dt          › dt          |¦  «        › d�¦  «        ‚|                     t          ¦  «        | _	        t          |¦  «        | _        | j        st	          d¦  «        ‚|| _        d| _        t          j        |d¬	¦  «        \  }}t          ||dz   |dz   f¬
¦  «        | _        dS )aŽ  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.

        Raises:
            ValueError: If `polygon` is not of shape `(N, 2)`, has fewer than
                `MIN_POLYGON_POINT_COUNT` vertices, or `triggering_anchors` is
                empty.
        é   éÿÿÿÿz$Polygon must have shape (N, 2); got ú.zPolygon must have at least z" vertices to enclose an area; got z#Triggering anchors cannot be empty.r   ©Úaxis)r   Úresolution_whN)ÚnpÚasarrayÚndimÚshapeÚ
ValueErrorÚlenr   ÚastypeÚintr   Úlistr   r   Úcurrent_countÚmaxr	   Úmask)Úselfr   r   r   Úx_maxÚy_maxs         úx/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/detection/tools/polygon_zone.pyÚ__init__zPolygonZone.__init__N   s2  € õ* ”*˜WÑ%Ô%ˆØŒ<˜1ÒÐ ¤¨bÔ 1°QÒ 6Ð 6ÝÐTÀGÄMÐTÐTÐTÑUÔUÐUõ
 ˆw‰<Œ<Õ1Ò1Ð1Ýð;Õ.Eð ;ð ;Ý+.¨w©<¬<ð;ð ;ð ;ñô ð ð
 —~’~¥cÑ*Ô*ˆŒå"&Ð'9Ñ":Ô":ˆÔØÔ&ð 	DÝÐBÑCÔCÐCØ#6ˆÔ àˆÔå”v˜g¨AÐ.Ñ.Ô.‰ˆˆuÝ#Ø¨E°A©I°u¸q±yÐ+Að
ñ 
ô 
ˆŒ	ˆ	ˆ	ó    Ú
detectionsc                 óH  ‡— t          ‰¦  «        dk    rJd| _        t          t          j        t
          j                 t          j        g t          ¬¦  «        ¦  «        S t          j        ˆfd„| j	        D ¦   «         ¦  «        }| j
        j        \  }}|dd…dd…df         |dd…dd…df         }}|dk    |dk    z  ||k     z  ||k     z  }t          j        |d|dz
  ¦  «        }t          j        |d|dz
  ¦  «        }	|| j
        |	|f         z  }
| j        rt
          j        nt
          j        } ||
d¬¦  «        }t!          t          j        |¦  «        ¦  «        | _        t          t          j        t
          j                 |                     t          ¦  «        ¦  «        S )a‰  
        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.

        Args:
            detections: The detections to be checked against the polygon zone

        Returns:
            A boolean numpy array indicating
                if each detection is within the polygon zone
        r   )Údtypec                 óŽ   •— g | ]A}t          j        ‰                     |¦  «        ¦  «                             t          ¦  «        ‘ŒBS © )r   ÚrintÚget_anchors_coordinatesr#   r$   )Ú.0Úanchorsr/   s     €r,   ú
<listcomp>z'PolygonZone.trigger.<locals>.<listcomp>”   sO   ø€ ð ð ð àõ ”˜
×:Ò:¸7ÑCÔCÑDÔD×KÒKÍCÑPÔPðð ð r.   Né   r   )r"   r&   r   ÚnptÚNDArrayr   Úbool_ÚarrayÚboolr   r(   r    Úclipr   ÚallÚanyr$   Úsumr#   )r)   r/   Úall_anchorsÚmask_hÚmask_wÚxÚyÚ	in_boundsÚx_safeÚy_safeÚanchor_hitsÚreduceÚ
is_in_zones    `           r,   ÚtriggerzPolygonZone.trigger~   s�  ø€ õ" ˆz‰?Œ?˜aÒÐØ!"ˆDÔÝ�œ¥B¤HÔ-­r¬x¸Å$Ð/GÑ/GÔ/GÑHÔHÐHå”hðð ð ð à#Ô6ðñ ô ñ
ô 
ˆð œœ‰ˆ�Ø˜1˜1˜1˜a˜a˜a ˜7Ô# [°°°°A°A°A°q°Ô%9ˆ1ˆØ˜!’V  Q¢Ñ'¨1¨vª:Ñ6¸!¸fº*ÑEˆ	Ý”˜˜A˜v¨™zÑ*Ô*ˆÝ”˜˜A˜v¨™zÑ*Ô*ˆØ $¤)¨F°F¨NÔ";Ñ;ˆØÔ3Ð?•”�½¼ˆØ�V˜K¨aÐ0Ñ0Ô0ˆ
Ý ¥¤¨
Ñ!3Ô!3Ñ4Ô4ˆÔÝ•C”K¥¤Ô)¨:×+<Ò+<½TÑ+BÔ+BÑCÔCÐCr.   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚBOTTOM_CENTERr:   r;   r   Úint64r   r>   r-   r   r<   rN   r3   r.   r,   r   r      s®   € € € € € ð8ð 8ðz 3;Ô2HÐ1JØ$(ð	.
ð .
à”˜RœXÔ&ð.
ð % XÔ.ð.
ð "ð	.
ð
 
ð.
ð .
ð .
ð .
ð`%D *ð %D°´¸R¼XÔ1Fð %Dð %Dð %Dð %Dð %Dð %Dr.   r   c                   ó²   — e Zd ZdZej        dej        dddddfded	ed
edede	dedede
de	ddfd„Z	 ddej        e         dedz  dej        e         fd„ZdS )ÚPolygonZoneAnnotatoraº  
    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)

        ```
    r   g      à?r9   é
   Tr   ÚzoneÚcolorÚ	thicknessÚ
text_colorÚ
text_scaleÚtext_thicknessÚtext_paddingÚdisplay_in_zone_countÚopacityr   Nc
                 óÚ   — || _         || _        || _        || _        || _        || _        || _        t          j        | _	        t          |j        ¬¦  «        | _        || _        |	| _        d S )N)r   )rX   rY   rZ   r[   r\   r]   r^   Úcv2ÚFONT_HERSHEY_SIMPLEXÚfontr   r   Úcenterr_   r`   )
r)   rX   rY   rZ   r[   r\   r]   r^   r_   r`   s
             r,   r-   zPolygonZoneAnnotator.__init__È   sh   € ð ˆŒ	ØˆŒ
Ø"ˆŒØ$ˆŒØ$ˆŒØ,ˆÔØ(ˆÔÝÔ,ˆŒ	Ý(°´Ð>Ñ>Ô>ˆŒØ%:ˆÔ"ØˆŒˆˆr.   ÚsceneÚlabelc                 ó,  — | j         dk    r(t          || j        j        | j        | j        ¬¦  «        }n`t          |                     ¦   «         | j        j        | j        | j         ¬¦  «        }t          || j        j        | j        | j        ¬¦  «        }| j        rVt          ||€t          | j        j        ¦  «        n|| j        | j        | j        | j        | j        | j        | j        ¬¦	  «	        }t%          t&          j        t*                   |¦  «        S )a†  
        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)

        Returns:
            The image with the polygon zone and count of detected objects
        r   )rf   r   rY   rZ   )rf   r   rY   r`   N)	rf   ÚtextÚtext_anchorÚbackground_colorr[   r\   r]   r^   Ú	text_font)r`   r   rX   r   rY   rZ   r   Úcopyr_   r   Ústrr&   re   r[   r\   r]   r^   rd   r   r:   r;   r   )r)   rf   rg   Úannotated_frames       r,   ÚannotatezPolygonZoneAnnotator.annotateà   s  € ð Œ<˜1ÒÐÝ*ØØœ	Ô)Ø”jØœ.ð	ñ ô ˆOˆOõ 2Ø—j’j‘l”lØœ	Ô)Ø”jØœð	ñ ô ˆOõ +Ø%Øœ	Ô)Ø”jØœ.ð	ñ ô ˆOð Ô%ð 	Ý'Ø%Ø5:°]•S˜œÔ0Ñ1Ô1Ð1ÈØ œKØ!%¤Øœ?Øœ?Ø#Ô2Ø!Ô.Øœ)ð
ñ 
ô 
ˆOõ •C”K¥Ô$ oÑ6Ô6Ð6r.   )N)rO   rP   rQ   rR   r
   ÚWHITEÚBLACKr   r$   Úfloatr>   r-   r:   r;   r   rn   rp   r3   r.   r,   rV   rV   ¦   s  € € € € € ðð ðH ”{ØØ!œKØØØØ&*Øðð àðð ðð ð	ð
 ðð ðð ðð ðð  $ðð ðð 
ðð ð ð ð2 <@ð07ð 07Ø”[ Ô%ð07Ø.1°D©jð07à	Œ�SÔ	ð07ð 07ð 07ð 07ð 07ð 07r.   rV   )Úcollections.abcr   Útypingr   r   Únumpyr   Únumpy.typingr:   Úsupervisionr   r   rb   Ú&supervision.detection.utils.convertersr   r	   Úsupervision.draw.colorr
   Úsupervision.draw.utilsr   r   r   Úsupervision.geometry.corer   Úsupervision.geometry.utilsr   r   rV   r3   r.   r,   ú<module>r~      sd  ðØ $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð à "Ð "Ð "Ð "Ð "Ð "Ø #Ð #Ð #Ð #Ð #Ð #ðð ð ð ð ð ð ð ð )Ð (Ð (Ð (Ð (Ð (Ø OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OØ .Ð .Ð .Ð .Ð .Ð .Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9ðPDð PDð PDð PDð PDñ PDô PDð PDðfj7ð j7ð j7ð j7ð j7ñ j7ô j7ð j7ð j7ð j7r.   