o
    e(                     @   s  d dl mZ d dlmZmZmZmZ d dlZd dl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mZ d dlmZmZ d d	lmZ d d
lmZ G dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$G dd dZ%G dd  d eZ&G d!d" d"Z'G d#d$ d$Z(G d%d& d&eZ)G d'd( d(eZ*dS ))    )sqrt)ListOptionalTupleUnionN)BaseAnnotator)ColorLookupTraceresolve_color)
Detections)
clip_boxesmask_to_polygons)ColorColorPalette)draw_polygon)Positionc                	   @   d   e Zd ZdZe dejfdee	ef de
defddZ	dd	ejd
edeej dejfddZdS )BoundingBoxAnnotatorzS
    A class for drawing bounding boxes on an image using provided detections.
       color	thicknesscolor_lookupc                 C      || _ || _|| _dS )aY  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the bounding box lines.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        Nr   r   r   selfr   r   r    r   J/var/www/myenv/lib/python3.10/site-packages/supervision/annotators/core.py__init__      
zBoundingBoxAnnotator.__init__Nscene
detectionscustom_color_lookupreturnc           
      C   sp   t t|D ]/}|j| t\}}}}t| j|||du r | jn|d}	tj	|||f||f|	
 | jd q|S )a  
        Annotates the given scene with bounding boxes based on the provided detections.

        Args:
            scene (np.ndarray): The image where bounding boxes will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> bounding_box_annotator = sv.BoundingBoxAnnotator()
            >>> annotated_frame = bounding_box_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![bounding-box-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/bounding-box-annotator-example-purple.png)
        Nr   r!   detection_idxr   imgpt1pt2r   r   )rangelenxyxyastypeintr
   r   r   cv2	rectangleas_bgrr   )
r   r    r!   r"   r%   x1y1x2y2r   r   r   r   annotate'   s$   #zBoundingBoxAnnotator.annotateN__name__
__module____qualname____doc__r   defaultr   CLASSr   r   r.   r   npndarrayr   r   r6   r   r   r   r   r      ,    

r   c                	   @   r   )MaskAnnotatorz
    A class for drawing masks on an image using provided detections.

    !!! warning

        This annotator utilizes the `sv.Detections.mask`.
          ?r   opacityr   c                 C   r   an  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        N)r   rD   r   r   r   rD   r   r   r   r   r   g   r   zMaskAnnotator.__init__Nr    r!   r"   r#   c                 C   s   |j du r|S tj|dtjd}tt|jD ]}t| j|||du r'| j	n|d}|j | }|
 ||< qt|| j|d| j d}|tjS )a  
        Annotates the given scene with masks based on the provided detections.

        Args:
            scene (np.ndarray): The image where masks will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> mask_annotator = sv.MaskAnnotator()
            >>> annotated_frame = mask_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![mask-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/mask-annotator-example-purple.png)
        NT)copydtyper$      r   )maskr?   arrayuint8flipargsortarear
   r   r   r1   r/   addWeightedrD   r-   )r   r    r!   r"   colored_maskr%   r   rJ   r   r   r   r6   y   s    
#
zMaskAnnotator.annotater7   r9   r:   r;   r<   r   r=   r   r>   r   r   floatr   r?   r@   r   r   r6   r   r   r   r   rB   ^   ,    


rB   c                	   @   r   )PolygonAnnotatorz
    A class for drawing polygons on an image using provided detections.

    !!! warning

        This annotator utilizes the `sv.Detections.mask`.
    r   r   r   r   c                 C   r   )aT  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the polygon lines.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        Nr   r   r   r   r   r      r   zPolygonAnnotator.__init__Nr    r!   r"   r#   c                 C   sp   |j du r|S tt|D ](}|j | }t| j|||du r | jn|d}t|dD ]}t|||| jd}q)q|S )a  
        Annotates the given scene with polygons based on the provided detections.

        Args:
            scene (np.ndarray): The image where polygons will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> polygon_annotator = sv.PolygonAnnotator()
            >>> annotated_frame = polygon_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![polygon-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/polygon-annotator-example-purple.png)
        Nr$   )rJ   )r    polygonr   r   )	rJ   r*   r+   r
   r   r   r   r   r   )r   r    r!   r"   r%   rJ   r   rV   r   r   r   r6      s*   
#
zPolygonAnnotator.annotater7   r8   r   r   r   r   rU      rT   rU   c                	   @   r   )ColorAnnotatorzN
    A class for drawing box masks on an image using provided detections.
    rC   r   rD   r   c                 C   s   || _ || _|| _dS rE   )r   r   rD   rF   r   r   r   r     r   zColorAnnotator.__init__Nr    r!   r"   r#   c                 C   s   |  }tt|D ].}|j| t\}}}}	t| j|||du r$| jn|d}
t	j
|||f||	f|
 dd q
t	j|| j|d| j dd}|S )a  
        Annotates the given scene with box masks based on the provided detections.

        Args:
            scene (np.ndarray): The image where bounding boxes will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> color_annotator = sv.ColorAnnotator()
            >>> annotated_frame = color_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![box-mask-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/box-mask-annotator-example-purple.png)
        Nr$   r&   rI   r   )gamma)rG   r*   r+   r,   r-   r.   r
   r   r   r/   r0   r1   rP   rD   )r   r    r!   r"   
mask_imager%   r2   r3   r4   r5   r   r   r   r   r6     s,   #zColorAnnotator.annotater7   rR   r   r   r   r   rW     rA   rW   c                	   @   sj   e Zd ZdZe ddejfdee	ef de
dedefdd	Z	
ddejdedeej dejfddZd
S )HaloAnnotatorz
    A class for drawing Halos on an image using provided detections.

    !!! warning

        This annotator utilizes the `sv.Detections.mask`.
    g?(   r   rD   kernel_sizer   c                 C   s   || _ || _|| _|| _dS )a  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
            kernel_size (int): The size of the average pooling kernel used for creating
                the halo.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        N)r   rD   r   r]   )r   r   rD   r]   r   r   r   r   r   b  s   
zHaloAnnotator.__init__Nr    r!   r"   r#   c                 C   s(  |j du r|S tj|tjd}tdg|jd  |jd  |jd |jd }tt|j	D ]%}t
| j|||du r@| jn|d}|j | }t||}| }	|	||< q2t|| j| jf}g d||< t|tj}
| j|
 |
  }|ddddtjf }t|d|  || j  }|S )a  
        Annotates the given scene with halos based on the provided detections.

        Args:
            scene (np.ndarray): The image where masks will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> halo_annotator = sv.HaloAnnotator()
            >>> annotated_frame = halo_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![halo-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/halo-annotator-example-purple.png)
        N)rH   Fr   rI   r$   )r   r   r   )rJ   r?   
zeros_likerL   rK   shapereshaperM   rN   rO   r
   r   r   
logical_orr1   r/   blurr]   cvtColorCOLOR_BGR2GRAYrD   maxnewaxis)r   r    r!   r"   rQ   fmaskr%   r   rJ   	color_bgrgrayalpha
alpha_maskr   r   r   r6   x  s4   
# 

zHaloAnnotator.annotater7   )r9   r:   r;   r<   r   r=   r   r>   r   r   rS   r.   r   r?   r@   r   r   r6   r   r   r   r   r[   Y  s2    


r[   c                   @   sp   e Zd ZdZe dddejfdee	ef de
de
de
d	ef
d
dZ	ddejdedeej dejfddZdS )EllipseAnnotatorzM
    A class for drawing ellipses on an image using provided detections.
    r   i   r   r   start_angle	end_angler   c                 C   "   || _ || _|| _|| _|| _dS )a  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the ellipse lines.
            start_angle (int): Starting angle of the ellipse.
            end_angle (int): Ending angle of the ellipse.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        N)r   r   rn   ro   r   )r   r   r   rn   ro   r   r   r   r   r     
   
zEllipseAnnotator.__init__Nr    r!   r"   r#   c                 C   s   t t|D ]H}|j| t\}}}}t| j|||du r | jn|d}	t|| d |f}
|| }tj	||
t|td| fd| j
| j|	 | jtjd	 q|S )a  
        Annotates the given scene with ellipses based on the provided detections.

        Args:
            scene (np.ndarray): The image where ellipses will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> ellipse_annotator = sv.EllipseAnnotator()
            >>> annotated_frame = ellipse_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![ellipse-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/ellipse-annotator-example-purple.png)
        Nr$   r   gffffff?g        )centeraxesangle
startAngleendAngler   r   lineType)r*   r+   r,   r-   r.   r
   r   r   r/   ellipsern   ro   r1   r   LINE_4)r   r    r!   r"   r%   r2   r3   r4   r5   r   rr   widthr   r   r   r6     s0   #zEllipseAnnotator.annotater7   r8   r   r   r   r   rl     s8    

rl   c                	   @   sj   e Zd ZdZe ddejfdee	ef de
de
defdd	Z	
ddejdedeej dejfddZd
S )BoxCornerAnnotatorzP
    A class for drawing box corners on an image using provided detections.
          r   r   corner_lengthr   c                 C      || _ || _|| _|| _dS )a  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the corner lines.
            corner_length (int): Length of each corner line.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        N)r   r   r~   r   )r   r   r   r~   r   r   r   r   r        
zBoxCornerAnnotator.__init__Nr    r!   r"   r#   c              	   C   s   t t|D ]q}|j| t\}}}}t| j|||du r | jn|d}	||f||f||f||fg}
|
D ]B\}}||krA|| j n|| j }t	j
|||f||f|	 | jd ||kr`|| j n|| j }t	j
|||f||f|	 | jd q4q|S )a  
        Annotates the given scene with box corners based on the provided detections.

        Args:
            scene (np.ndarray): The image where box corners will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> corner_annotator = sv.BoxCornerAnnotator()
            >>> annotated_frame = corner_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![box-corner-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/box-corner-annotator-example-purple.png)
        Nr$   )r   )r*   r+   r,   r-   r.   r
   r   r   r~   r/   liner1   r   )r   r    r!   r"   r%   r2   r3   r4   r5   r   cornersxyx_endy_endr   r   r   r6   -  s,   #
zBoxCornerAnnotator.annotater7   r8   r   r   r   r   r{     s2    

r{   c                	   @   r   )CircleAnnotatorzK
    A class for drawing circle on an image using provided detections.
    r   r   r   r   c                 C   r   )aR  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the circle line.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        Nr   r   r   r   r   r   n  s   
zCircleAnnotator.__init__Nr    r!   r"   r#   c                 C   s   t t|D ]K}|j| t\}}}}|| d || d f}	t||	d  d ||	d  d  }
t| j|||du r>| jn|d}t	j
||	t|
| | jd q|S )a  
        Annotates the given scene with circles based on the provided detections.

        Args:
            scene (np.ndarray): The image where box corners will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> circle_annotator = sv.CircleAnnotator()
            >>> annotated_frame = circle_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```


        ![circle-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/circle-annotator-example-purple.png)
        r   r   rI   Nr$   )r'   rr   radiusr   r   )r*   r+   r,   r-   r.   r   r
   r   r   r/   circler1   r   )r   r    r!   r"   r%   r2   r3   r4   r5   rr   distancer   r   r   r   r6     s(   $$zCircleAnnotator.annotater7   r8   r   r   r   r   r   i  s,    

r   c                	   @   sl   e Zd ZdZe dejej	fde
eef dededefddZ		dd
ejdedeej dejfddZd	S )DotAnnotatorzh
    A class for drawing dots on an image at specific coordinates based on provided
    detections.
    r|   r   r   positionr   c                 C   r   )a  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            radius (int): Radius of the drawn dots.
            position (Position): The anchor position for placing the dot.
            color_lookup (ColorLookup): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        N)r   r   r   r   )r   r   r   r   r   r   r   r   r     r   zDotAnnotator.__init__Nr    r!   r"   r#   c                 C   s|   |j | jd}tt|D ].}t| j|||du r| jn|d}t||df t||df f}t	||| j
| d q|S )a  
        Annotates the given scene with dots based on the provided detections.

        Args:
            scene (np.ndarray): The image where dots will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> dot_annotator = sv.DotAnnotator()
            >>> annotated_frame = dot_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![dot-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/dot-annotator-example-purple.png)
        anchorNr$   r   rI   rX   )get_anchors_coordinatesr   r*   r+   r
   r   r   r.   r/   r   r   r1   )r   r    r!   r"   xyr%   r   rr   r   r   r   r6     s   # zDotAnnotator.annotater7   r9   r:   r;   r<   r   r=   r   CENTERr   r>   r   r   r.   r   r?   r@   r   r   r6   r   r   r   r   r     s2    

r   c                   @   s   e Zd ZdZe e dddej	e
jfdeeef dededed	ed
ede
fddZedeeef deeef dedeeeeef fddZ		ddejdedee deej dejf
ddZdS )LabelAnnotatorzN
    A class for annotating labels on an image using provided detections.
    rC   rI   
   r   
text_color
text_scaletext_thicknesstext_paddingtext_positionr   c                 C   s.   || _ || _|| _|| _|| _|| _|| _dS )a  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating the text background.
            text_color (Color): The color to use for the text.
            text_scale (float): Font scale for the text.
            text_thickness (int): Thickness of the text characters.
            text_padding (int): Padding around the text within its background box.
            text_position (Position): Position of the text relative to the detection.
                Possible values are defined in the `Position` enum.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        N)r   r   r   r   r   text_anchorr   )r   r   r   r   r   r   r   r   r   r   r   r     s   
zLabelAnnotator.__init__center_coordinatestext_whr   r#   c                 C   s   | \}}|\}}|t jkr||| || |fS |t jkr&|| || ||fS |t jkr;||d  || ||d  |fS |t jksE|t jkr[||d  ||d  ||d  ||d  fS |t jkrj|||| || fS |t jkry|| |||| fS |t jkr||d  |||d  || fS d S )Nr   )	r   TOP_LEFT	TOP_RIGHT
TOP_CENTERr   CENTER_OF_MASSBOTTOM_LEFTBOTTOM_RIGHTBOTTOM_CENTER)r   r   r   center_xcenter_ytext_wtext_hr   r   r   resolve_text_background_xyxy-  s:   













z+LabelAnnotator.resolve_text_background_xyxyNr    r!   labelsr"   c                 C   sL  t j}|j| jdt}t|D ]\}}t| j|||du r!| j	n|d}	|du s1t
|t
|kr7|j|  n|| }
t j|
|| j| jdd \}}|d| j  }|d| j  }| jt|||f| jd}|d | j }|d | j | }t j||d |d f|d |d	 f|	 t jd
 t j||
||f|| j| j | jt jd q|S )a;  
        Annotates the given scene with labels based on the provided detections.

        Args:
            scene (np.ndarray): The image where labels will be drawn.
            detections (Detections): Object detections to annotate.
            labels (List[str]): Optional. Custom labels for each detection.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
            >>> annotated_frame = label_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![label-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/label-annotator-example-purple.png)
        r   Nr$   )textfontFace	fontScaler   r   r   )r   r   r   rI      r&   )r'   r   orgr   r   r   r   rw   )r/   FONT_HERSHEY_SIMPLEXr   r   r-   r.   	enumerater
   r   r   r+   class_idgetTextSizer   r   r   r   tupler0   r1   FILLEDputTextr   as_rgbLINE_AA)r   r    r!   r   r"   fontanchors_coordinatesr%   r   r   r   r   r   text_w_paddedtext_h_paddedtext_background_xyxytext_xtext_yr   r   r   r6   T  sl   %

zLabelAnnotator.annotate)NN)r9   r:   r;   r<   r   r=   r   blackr   r   r   r>   r   rS   r.   r   staticmethodr   r   r?   r@   r   r   strr   r6   r   r   r   r   r   	  s^    



*r   c                   @   :   e Zd ZdZddefddZdejdedejfd	d
Z	dS )BlurAnnotatorzM
    A class for blurring regions in an image using provided detections.
    r}   r]   c                 C   
   || _ dS )zp
        Args:
            kernel_size (int): The size of the average pooling kernel used for blurring.
        N)r]   )r   r]   r   r   r   r        
zBlurAnnotator.__init__r    r!   r#   c                 C   s|   |j dd \}}t|j||fdt}|D ]$\}}}}	|||	||f }
t|
| j| jf}
|
|||	||f< q|S )a>  
        Annotates the given scene by blurring regions based on the provided detections.

        Args:
            scene (np.ndarray): The image where blurring will be applied.
            detections (Detections): Object detections to annotate.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> blur_annotator = sv.BlurAnnotator()
            >>> annotated_frame = circle_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![blur-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/blur-annotator-example-purple.png)
        Nr   r,   resolution_wh)r_   r   r,   r-   r.   r/   rb   r]   )r   r    r!   image_heightimage_widthclipped_xyxyr2   r3   r4   r5   roir   r   r   r6     s    
zBlurAnnotator.annotateN)r}   
r9   r:   r;   r<   r.   r   r?   r@   r   r6   r   r   r   r   r         r   c                   @   sr   e Zd ZdZe ejddej	fde
eef dedededef
d	d
Z	ddejdedeej dejfddZdS )TraceAnnotatora+  
    A class for drawing trace paths on an image based on detection coordinates.

    !!! warning

        This annotator utilizes the `sv.Detections.tracker_id`. Read
        [here](https://supervision.roboflow.com/trackers/) to learn how to plug
        tracking into your inference pipeline.
       r   r   r   trace_lengthr   r   c                 C   s$   || _ t||d| _|| _|| _dS )aT  
        Args:
            color (Union[Color, ColorPalette]): The color to draw the trace, can be
                a single color or a color palette.
            position (Position): The position of the trace.
                Defaults to `CENTER`.
            trace_length (int): The maximum length of the trace in terms of historical
                points. Defaults to `30`.
            thickness (int): The thickness of the trace lines. Defaults to `2`.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        )max_sizer   N)r   r	   tracer   r   )r   r   r   r   r   r   r   r   r   r     s   
zTraceAnnotator.__init__Nr    r!   r"   r#   c                 C   s   | j | tt|D ]8}t|j| }t| j|||du r!| jn|d}| j j	|d}t|dkrDt
j||tjgd| | jd}q|S )a  
        Draws trace paths on the frame based on the detection coordinates provided.

        Args:
            scene (np.ndarray): The image on which the traces will be drawn.
            detections (Detections): The detections which include coordinates for
                which the traces will be drawn.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv
            >>> from ultralytics import YOLO

            >>> model = YOLO('yolov8x.pt')

            >>> trace_annotator = sv.TraceAnnotator()

            >>> video_info = sv.VideoInfo.from_video_path(video_path='...')
            >>> frames_generator = sv.get_video_frames_generator(source_path='...')
            >>> tracker = sv.ByteTrack()

            >>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
            ...    for frame in frames_generator:
            ...        result = model(frame)[0]
            ...        detections = sv.Detections.from_ultralytics(result)
            ...        detections = tracker.update_with_detections(detections)
            ...        annotated_frame = trace_annotator.annotate(
            ...            scene=frame.copy(),
            ...            detections=detections)
            ...        sink.write_frame(frame=annotated_frame)
            ```

        ![trace-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/trace-annotator-example-purple.png)
        Nr$   )
tracker_idrI   F)r   r   )r   putr*   r+   r.   r   r
   r   r   getr/   	polylinesr-   r?   int32r1   r   )r   r    r!   r"   r%   r   r   r   r   r   r   r6     s,   .zTraceAnnotator.annotater7   r   r   r   r   r   r     s8    

r   c                   @   s\   e Zd ZdZejdddddfdeded	ed
ededefddZde	j
dede	j
fddZdS )HeatMapAnnotatorz
    A class for drawing heatmaps on an image based on provided detections.
    Heat accumulates over time and is drawn as a semi-transparent overlay
    of blurred circles.
    g?r\      r   }   r   rD   r   r]   top_huelow_huec                 C   s.   || _ || _|| _|| _d| _|| _|| _dS )a  
        Args:
            position (Position): The position of the heatmap. Defaults to
                `BOTTOM_CENTER`.
            opacity (float): Opacity of the overlay mask, between 0 and 1.
            radius (int): Radius of the heat circle.
            kernel_size (int): Kernel size for blurring the heatmap.
            top_hue (int): Hue at the top of the heatmap. Defaults to 0 (red).
            low_hue (int): Hue at the bottom of the heatmap. Defaults to 125 (blue).
        N)r   rD   r   r]   	heat_maskr   r   )r   r   rD   r   r]   r   r   r   r   r   r   Z  s   
zHeatMapAnnotator.__init__r    r!   r#   c                 C   sH  | j du rt|jdd | _ t|jdd }|| jD ]}t|t|d t|d f| j	dd q || j  | _ | j 
 }| j||  | j| j   }|tj}| jdurft|| j| jf}t|j}||d< d|d< d|d	< t|tjtj}t| j tjtjdk}t|| j|d| j d| ||< |S )
a0  
        Annotates the scene with a heatmap based on the provided detections.

        Args:
            scene (np.ndarray): The image where the heatmap will be drawn.
            detections (Detections): Object detections to annotate.

        Returns:
            Annotated image.

        Example:
            ```python
            >>> import supervision as sv
            >>> from ultralytics import YOLO

            >>> model = YOLO('yolov8x.pt')

            >>> heat_map_annotator = sv.HeatMapAnnotator()

            >>> video_info = sv.VideoInfo.from_video_path(video_path='...')
            >>> frames_generator = get_video_frames_generator(source_path='...')

            >>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
            ...    for frame in frames_generator:
            ...        result = model(frame)[0]
            ...        detections = sv.Detections.from_ultralytics(result)
            ...        annotated_frame = heat_map_annotator.annotate(
            ...            scene=frame.copy(),
            ...            detections=detections)
            ...        sink.write_frame(frame=annotated_frame)
            ```

        ![heatmap-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/heat-map-annotator-example-purple.png)
        Nr   r   rI   rX   ).r      ).rI   ).r   )r   r?   zerosr_   r   r   r/   r   r.   r   rG   r   re   r   r-   rL   r]   rb   rc   COLOR_HSV2BGRCOLOR_GRAY2BGRrP   rD   )r   r    r!   rJ   r   temphsvr   r   r   r6   u  s*   
%*

zHeatMapAnnotator.annotateN)r9   r:   r;   r<   r   r   rS   r.   r   r?   r@   r   r6   r   r   r   r   r   S  s,    
r   c                   @   r   )PixelateAnnotatorzO
    A class for pixelating regions in an image using provided detections.
       
pixel_sizec                 C   r   )zQ
        Args:
            pixel_size (int): The size of the pixelation.
        N)r   )r   r   r   r   r   r     r   zPixelateAnnotator.__init__r    r!   r#   c                 C   s   |j dd \}}t|j||fdt}|D ]:\}}}}	|||	||f }
tj|
dd| j d| j d}tj||
j d |
j d ftjd}||||	||f< q|S )a\  
        Annotates the given scene by pixelating regions based on the provided
            detections.

        Args:
            scene (np.ndarray): The image where pixelating will be applied.
            detections (Detections): Object detections to annotate.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> pixelate_annotator = sv.PixelateAnnotator()
            >>> annotated_frame = pixelate_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![pixelate-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/pixelate-annotator-example-10.png)
        Nr   r   rI   )srcdsizefxfyr   )r   r   interpolation)	r_   r   r,   r-   r.   r/   resizer   INTER_NEAREST)r   r    r!   r   r   r   r2   r3   r4   r5   r   scaled_up_roiscaled_down_roir   r   r   r6     s$   !
zPixelateAnnotator.annotateN)r   r   r   r   r   r   r     r   r   c                   @   sr   e Zd ZdZe ddejej	fde
eef dedededef
dd	Z	
ddejdedeej dejfddZd
S )TriangleAnnotatorzt
    A class for drawing triangle markers on an image at specific coordinates based on
    provided detections.
    r   r   baseheightr   r   c                 C   rp   )a  
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            base (int): The base width of the triangle.
            height (int): The height of the triangle.
            position (Position): The anchor position for placing the triangle.
            color_lookup (ColorLookup): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        N)r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r     rq   zTriangleAnnotator.__init__Nr    r!   r"   r#   c           
      C   s   |j | jd}tt|D ]M}t| j|||du r| jn|d}t||df t||df }}t	|| j
d  || j g|| j
d  || j g||ggtj}	t||	g|  q|S )a  
        Annotates the given scene with triangles based on the provided detections.

        Args:
            scene (np.ndarray): The image where triangles will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            np.ndarray: The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> triangle_annotator = sv.TriangleAnnotator()
            >>> annotated_frame = triangle_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![triangle-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/triangle-annotator-example.png)
        r   Nr$   r   rI   r   )r   r   r*   r+   r
   r   r   r.   r?   rK   r   r   r   r/   fillPolyr1   )
r   r    r!   r"   r   r%   r   tip_xtip_yverticesr   r   r   r6     s(   #"	zTriangleAnnotator.annotater7   )r9   r:   r;   r<   r   r=   r   r   r   r>   r   r   r.   r   r?   r@   r   r   r6   r   r   r   r   r     s8    

r   )+mathr   typingr   r   r   r   r/   numpyr?   supervision.annotators.baser   supervision.annotators.utilsr   r	   r
   supervision.detection.corer   supervision.detection.utilsr   r   supervision.draw.colorr   r   supervision.draw.utilsr   supervision.geometry.corer   r   rB   rU   rW   r[   rl   r{   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s6    NSVR`ZVSM (9j^B