Ë
    ¼Ó^j4  ã                  óÀ   — d dl mZ d dlmZ d dlmZmZ d dl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 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y)é    )Úannotations)ÚIterable)ÚAnyÚcastN)Ú
Detections)Úpolygon_to_mask)ÚColor)Údraw_filled_polygonÚdraw_polygonÚ	draw_text)ÚPosition)Úget_polygon_centerc                  ó@   — e Zd ZdZej
                  ff	 	 	 dd„Zdd„Zy)Ú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: A list 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,)).
        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

        ```
    c                óü   — |j                  t        «      | _        || _        t	        | j                  «      st        d«      ‚d| _        t        j                  |d¬«      \  }}t        ||dz   |dz   f¬«      | _
        y )Nz#Triggering anchors cannot be empty.r   ©Úaxisé   )ÚpolygonÚresolution_wh)ÚastypeÚintr   Útriggering_anchorsÚlistÚ
ValueErrorÚcurrent_countÚnpÚmaxr   Úmask)Úselfr   r   Úx_maxÚy_maxs        ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/detection/tools/polygon_zone.pyÚ__init__zPolygonZone.__init__8   sp   € ð
 —~‘~¤cÓ*ˆŒØ"4ˆÔÜ�D×+Ñ+Ô,ÜÐBÓCÐCàˆÔä—v‘v˜g¨AÔ.‰ˆˆuÜ#Ø¨E°A©I°u¸q±yÐ+Aô
ˆ�	ó    c           
     óê  — t        |«      dk(  r"d| _        t        j                  g t        ¬«      S t        j                  | j
                  D �cg c]9  }t        j                  |j                  |«      «      j                  t        «      ‘Œ; c}«      }| 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
  «      }
t        j                  || j                  |
|	f   z  d¬«      }t        t        j                  |«      «      | _        |j                  t        «      S c c}w )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   )ÚdtypeNé   r   )Úlenr   r   ÚarrayÚboolr   ÚceilÚget_anchors_coordinatesr   r   r   ÚshapeÚclipÚallÚsum)r    Ú
detectionsÚanchorsÚall_anchorsÚmask_hÚmask_wÚxÚyÚ	in_boundsÚx_safeÚy_safeÚ
is_in_zones               r#   ÚtriggerzPolygonZone.triggerI   sD  € ô" ˆz‹?˜aÒØ!"ˆDÔÜ—8‘8˜B¤dÔ+Ð+ä—h‘hð  $×6Ñ6öàô —‘˜
×:Ñ:¸7ÓCÓD×KÑKÌCÕPòó
ˆð Ÿ™Ÿ™‰ˆ�Øš1ša ˜7Ñ# [²²A°q°Ñ%9ˆ1ˆØ˜!‘V  Q¡Ñ'¨1¨v©:Ñ6¸!¸f¹*ÑEˆ	Ü—‘˜˜A˜v¨™zÓ*ˆÜ—‘˜˜A˜v¨™zÓ*ˆÜ—V‘V˜I¨¯	©	°&¸&°.Ñ(AÑAÈÔJˆ
Ü ¤§¡¨
Ó!3Ó4ˆÔØ× Ñ ¤Ó&Ð&ùòs   Á>E0N)r   znpt.NDArray[np.int64]r   zIterable[Position])r2   r   Úreturnznpt.NDArray[np.bool_])Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚBOTTOM_CENTERr$   r=   © r%   r#   r   r      s2   „ ñ#ðP 3;×2HÑ2HÐ1Jð
à&ð
ð /ó
ô"#'r%   r   c                  óŠ   — e Zd ZdZej
                  dej                  dddddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z	 d	 	 	 	 	 dd
„Zy	)Ú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
    r   g      à?r(   é
   Tr   c
                óâ   — || _         || _        || _        || _        || _        || _        || _        t        j                  | _	        t        |j                  ¬«      | _        || _        |	| _        y )N)r   )ÚzoneÚcolorÚ	thicknessÚ
text_colorÚ
text_scaleÚtext_thicknessÚtext_paddingÚcv2ÚFONT_HERSHEY_SIMPLEXÚfontr   r   ÚcenterÚdisplay_in_zone_countÚopacity)
r    rI   rJ   rK   rL   rM   rN   rO   rT   rU   s
             r#   r$   zPolygonZoneAnnotator.__init__ƒ   sc   € ð ˆŒ	ØˆŒ
Ø"ˆŒØ$ˆŒØ$ˆŒØ,ˆÔØ(ˆÔÜ×,Ñ,ˆŒ	Ü(°·±Ô>ˆŒØ%:ˆÔ"Øˆ�r%   Nc                óØ  — | j                   dk(  r8t        || j                  j                  | j                  | j
                  ¬«      }n|t        |j                  «       | j                  j                  | j                  | j                   ¬«      }t        || j                  j                  | j                  | j
                  ¬«      }| j                  r{t        ||€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   )Úscener   rJ   rK   )rW   r   rJ   rU   )	rW   ÚtextÚtext_anchorÚbackground_colorrL   rM   rN   rO   Ú	text_font)rU   r   rI   r   rJ   rK   r
   ÚcopyrT   r   Ústrr   rS   rL   rM   rN   rO   rR   r   ÚnptÚNDArrayr   )r    rW   ÚlabelÚannotated_frames       r#   ÚannotatezPolygonZoneAnnotator.annotate›   s  € ð �<‰<˜1ÒÜ*ØØŸ	™	×)Ñ)Ø—j‘jØŸ.™.ô	‰Oô 2Ø—j‘j“lØŸ	™	×)Ñ)Ø—j‘jØŸ™ô	ˆOô +Ø%ØŸ	™	×)Ñ)Ø—j‘jØŸ.™.ô	ˆOð ×%Ò%Ü'Ø%Ø5:°]”S˜Ÿ™×0Ñ0Ô1ÈØ ŸK™KØ!%§¡ØŸ?™?ØŸ?™?Ø#×2Ñ2Ø!×.Ñ.ØŸ)™)ô
ˆOô ”C—K‘K¤Ñ$ oÓ6Ð6r%   )rI   r   rJ   r	   rK   r   rL   r	   rM   ÚfloatrN   r   rO   r   rT   r+   rU   rc   )N)rW   únpt.NDArray[Any]r`   z
str | Noner>   rd   )	r?   r@   rA   rB   r	   ÚWHITEÚBLACKr$   rb   rD   r%   r#   rF   rF   o   s©   „ ñð, —{‘{ØØ!ŸK™KØØØØ&*Øðàðð ðð ð	ð
 ðð ðð ðð ðð  $ðð óð2 <@ð07Ø%ð07Ø.8ð07à	ô07r%   rF   )Ú
__future__r   Úcollections.abcr   Útypingr   r   rP   Únumpyr   Únumpy.typingr^   Úsupervisionr   Ú&supervision.detection.utils.convertersr   Úsupervision.draw.colorr	   Úsupervision.draw.utilsr
   r   r   Úsupervision.geometry.corer   Úsupervision.geometry.utilsr   r   rF   rD   r%   r#   ú<module>rr      sE   ðÝ "å $ß ã 
Û Ý å "Ý BÝ (ß OÑ OÝ .Ý 9÷Z'ñ Z'÷z\7ò \7r%   