
    ^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vvgA.u#EAIuqy+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88Bd++hh  $66 
::7CDKKCP
 1a7#[Aq%91!VQ'1v:6!f*E	Avz*Avz*VVI		&&.(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;2H2H1J
&
 /
"#'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*		))jj..	O 2jjl		))jj	O +%		))jj..	O %%'%5:]S001 KK!%????#22!..))
O CKK$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    , {{!KK&*  	
      $ 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%   