o
    eU                      @   s  d dl Z d dl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 	d,dejdeded	e	d
edejfddZ	d,dejded	e	d
edejf
ddZdejded	e	dejfddZ	d,dejdejd	e	d
edejf
ddZe	 dddejdfdejdedede	dedededed ee	 dejfd!d"Zdejd#eeejf d$ededejf
d%d&Zd'eeef defd(d)Zd'eeef defd*d+ZdS )-    N)OptionalTupleUnion)Color)PointRect   scenestartendcolor	thicknessreturnc                 C   s$   t j| | | | |d | S )a  
    Draws a line on a given scene.

    Parameters:
        scene (np.ndarray): The scene on which the line will be drawn
        start (Point): The starting point of the line
        end (Point): The end point of the line
        color (Color): The color of the line
        thickness (int): The thickness of the line

    Returns:
        np.ndarray: The scene with the line drawn on it
    r   )cv2lineas_xy_int_tupleas_bgr)r	   r
   r   r   r    r   E/var/www/myenv/lib/python3.10/site-packages/supervision/draw/utils.py	draw_line   s   r   rectc                 C   s(   t j| |j |j | |d | S )ao  
    Draws a rectangle on an image.

    Parameters:
        scene (np.ndarray): The scene on which the rectangle will be drawn
        rect (Rect): The rectangle to be drawn
        color (Color): The color of the rectangle
        thickness (int): The thickness of the rectangle border

    Returns:
        np.ndarray: The scene with the rectangle drawn on it
    r   r   	rectangletop_leftr   bottom_rightr   )r	   r   r   r   r   r   r   draw_rectangle%   s   r   c                 C   s&   t | |j |j | d | S )a7  
    Draws a filled rectangle on an image.

    Parameters:
        scene (np.ndarray): The scene on which the rectangle will be drawn
        rect (Rect): The rectangle to be drawn
        color (Color): The color of the rectangle

    Returns:
        np.ndarray: The scene with the rectangle drawn on it
    r   r	   r   r   r   r   r   draw_filled_rectangle>   s   r   polygonc                 C   s   t j| |gd| |d | S )a  Draw a polygon on a scene.

    Parameters:
        scene (np.ndarray): The scene to draw the polygon on.
        polygon (np.ndarray): The polygon to be drawn, given as a list of vertices.
        color (Color): The color of the polygon.
        thickness (int, optional): The thickness of the polygon lines, by default 2.

    Returns:
        np.ndarray: The scene with the polygon drawn on it.
    T)isClosedr   r   )r   	polylinesr   )r	   r    r   r   r   r   r   draw_polygonT   s   r#   g      ?   
   texttext_anchor
text_color
text_scaletext_thicknesstext_padding	text_fontbackground_colorc	              
   C   s   t j||||dd \}	}
t|j|	d  |j|
d  |	|
d|}|dur-t| ||d} t j| ||j|	d  |j|
d  f||| |t j	d | S )a  
    Draw text with background on a scene.

    Parameters:
        scene (np.ndarray): A 2-dimensional numpy ndarray representing an image or scene
        text (str): The text to be drawn.
        text_anchor (Point): The anchor point for the text, represented as a
            Point object with x and y attributes.
        text_color (Color, optional): The color of the text. Defaults to black.
        text_scale (float, optional): The scale of the text. Defaults to 0.5.
        text_thickness (int, optional): The thickness of the text. Defaults to 1.
        text_padding (int, optional): The amount of padding to add around the text
            when drawing a rectangle in the background. Defaults to 10.
        text_font (int, optional): The font to use for the text.
            Defaults to cv2.FONT_HERSHEY_SIMPLEX.
        background_color (Color, optional): The color of the background rectangle,
            if one is to be drawn. Defaults to None.

    Returns:
        np.ndarray: The input scene with the text drawn on it.

    Examples:
        ```python
        >>> scene = np.zeros((100, 100, 3), dtype=np.uint8)
        >>> text_anchor = Point(x=50, y=50)
        >>> scene = draw_text(scene=scene, text="Hello, world!",text_anchor=text_anchor)
        ```
    )r&   fontFace	fontScaler   r   r   )xywidthheightNr   )imgr&   orgr.   r/   r   r   lineType)
r   getTextSizer   r0   r1   padr   putTextr   LINE_AA)r	   r&   r'   r(   r)   r*   r+   r,   r-   
text_widthtext_height	text_rectr   r   r   	draw_texth   s>   '
r>   imageopacityc           	   	   C   s  t |trtj|std| dt|tj}d|  kr'dks,t	d t	d|j
dk sL|jdk sL|j
|j | jd ksL|j|j | jd krPt	dt||j|jf}|jd	 d
krl|dddddf ntj|j|jf|jdd }t|| }| |j|j|j |j
|j
|j f }|tjd }td|dtjf  | |dtjf |ddddddf   }|| |j|j|j |j
|j
|j f< | S )a"  
    Draws an image onto a given scene with specified opacity and dimensions.

    Args:
        scene (np.ndarray): Background image where the new image will be drawn.
        image (Union[str, np.ndarray]): Image to draw.
        opacity (float): Opacity of the image to be drawn.
        rect (Rect): Rectangle specifying where to draw the image.

    Returns:
        np.ndarray: The updated scene.

    Raises:
        FileNotFoundError: If the image path does not exist.
        ValueError: For invalid opacity or rectangle dimensions.
    zImage path ('z') does not exist.g        g      ?z$Opacity must be between 0.0 and 1.0.r   r$   zInvalid rectangle dimensions.r      N   )dtype   g     o@.)
isinstancestrospathexistsFileNotFoundErrorr   imreadIMREAD_UNCHANGED
ValueErrorr0   r1   r2   shaper3   resizenponesrC   convertScaleAbsastypefloat32newaxis)	r	   r?   r@   r   alpha_channelalpha_scaled	scene_roialpha_floatblended_roir   r   r   
draw_image   s8   


(&(r[   resolution_whc                 C   s   t | d S )a  
    Calculate a dynamic font scale based on the resolution of an image.

    Parameters:
         resolution_wh (Tuple[int, int]): A tuple representing the width and height
                 of the image.

    Returns:
         float: The calculated font scale factor.
    gMbP?min)r\   r   r   r   calculate_dynamic_text_scale   s   r_   c                 C   s<   t | }|dk r
dS |dk rdS |dk rdS |dk rdS dS )a  
    Calculate a dynamic line thickness based on the resolution of an image.

    Parameters:
        resolution_wh (Tuple[int, int]): A tuple representing the width and height
                of the image.

    Returns:
        int: The calculated line thickness in pixels.
    i  r   i  i8  ip  rA   r]   )r\   min_dimensionr   r   r    calculate_dynamic_line_thickness   s   ra   )r   )rG   typingr   r   r   r   numpyrP   supervision.draw.colorr   supervision.geometry.corer   r   ndarrayintr   r   r   r#   blackFONT_HERSHEY_SIMPLEXrF   floatr>   r[   r_   ra   r   r   r   r   <module>   s    


	

F
>