
    ^jj/                       d dl mZ d dlZd dl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mZ ej                  df	 	 	 	 	 	 	 	 	 	 	 ddZej                  df	 	 	 	 	 	 	 	 	 ddZej                  d	f	 	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 	 	 	 	 ddZej                  df	 	 	 	 	 	 	 	 	 ddZej                  d	f	 	 	 	 	 	 	 	 	 ddZej,                  dd	dej.                  df	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZddZddZy)    )annotationsN)cast)Color)PointRect   c                    t        j                  | |j                         |j                         |j                         |       | S )ai  
    Draws a line on a given scene.

    Args:
        scene: The scene on which the line will be drawn
        start: The starting point of the line
        end: The end point of the line
        color: The color of the line, defaults to Color.ROBOFLOW
        thickness: The thickness of the line

    Returns:
        The scene with the line drawn on it
    	thickness)cv2lineas_xy_int_tupleas_bgr)scenestartendcolorr   s        a/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/draw/utils.py	draw_liner      s>    ( HH L    c                    t        j                  | |j                  j                         |j                  j                         |j                         |       | S )a;  
    Draws a rectangle on an image.

    Args:
        scene: The scene on which the rectangle will be drawn
        rect: The rectangle to be drawn
        color: The color of the rectangle
        thickness: The thickness of the rectangle border

    Returns:
        The scene with the rectangle drawn on it
    r
   )r   	rectangletop_leftr   bottom_rightr   )r   rectr   r   s       r   draw_rectangler   ,   sH    $ MM%%'))+ Lr      c                   |dk(  rYt        j                  | |j                  j                         |j                  j                         |j                         d       | S | j                         }t        j                  ||j                  j                         |j                  j                         |j                         d       t        j                  ||| d|z
  d|        | S )aL  
    Draws a filled rectangle on an image.

    Args:
        scene: The scene on which the rectangle will be drawn
        rect: The rectangle to be drawn
        color: The color of the rectangle
        opacity: The opacity of rectangle when drawn on the scene.

    Returns:
        The scene with the rectangle drawn on it
    r   r   gammadst)r   r   r   r   r   r   copyaddWeighted)r   r   r   opacityscene_with_annotationss        r   draw_filled_rectangler'   H   s    $ !|MM))+--/LLN	
( L "'"MM))+--/LLN	
 	"GUAKqe	
 Lr   c                   |j                         \  }}}}||z
  ||z
  }	}t        |t        ||	      dz        }||z   |f||z
  |ff|||z   f|||z
  ffg}
||z   ||z   f||z
  ||z   f||z   ||z
  f||z
  ||z
  fg}|
D ]0  }t        j                  | |d   |d   |j	                         d       2 |D ]*  }t        j
                  | |||j	                         d       , | S )a`  
    Draws a rounded rectangle on an image.

    Args:
        scene: The image on which the rounded rectangle will be drawn.
        rect: The rectangle to be drawn.
        color: The color of the rounded rectangle.
        border_radius: The radius of the corner rounding.

    Returns:
        The image with the rounded rectangle drawn on it.
    r   r   r   r   )imgpt1pt2r   r   )r)   centerradiusr   r   )as_xyxy_int_tupleminr   r   r   circle)r   r   r   border_radiusx1y1x2y2widthheightrectangle_coordinatescircle_centerscoordinatesr,   s                 r   draw_rounded_rectangler;   r   sI   $ ++-NBBGR"W6Es5&'9Q'>?M }
b	!B$6#;<
b= 	!B](:#;<
 
m	R-/0	m	R-/0	m	R-/0	m	R-/0	N - 
AA,,.	

 ! 


 ,,.	

 Lr   c                X    t        j                  | |gd|j                         |       | S )aj  Draw a polygon on a scene.

    Args:
        scene: The scene to draw the polygon on.
        polygon: The polygon to be drawn, given as a list of vertices.
        color: The color of the polygon. Defaults to Color.ROBOFLOW.
        thickness: The thickness of the polygon lines, by default 2.

    Returns:
        The scene with the polygon drawn on it.
    T)isClosedr   r   )r   	polylinesr   )r   polygonr   r   s       r   draw_polygonr@      s*    " MMy4u||~ Lr   c                   |dk(  r)t        j                  | |g|j                                | S | j                         }t        j                  ||g|j                                t        j                  ||| d|z
  d|        | S )am  Draw a filled polygon on a scene.

    Args:
        scene: The scene to draw the polygon on.
        polygon: The polygon to be drawn, given as a list of vertices.
        color: The color of the polygon. Defaults to Color.ROBOFLOW.
        opacity: The opacity of polygon when drawn on the scene.

    Returns:
        The scene with the polygon drawn on it.
    r   )r   r   r    )r   fillPolyr   r#   r$   )r   r?   r   r%   r&   s        r   draw_filled_polygonrC      su    " !|UWIU\\^< L "'+gYellnM"GUAKqe	
 Lr   g      ?
   c	           
     l   t        j                  ||||      d   \  }	}
|j                         \  }}t        ||	dz  z
  ||
dz  z
  |	|
      j	                  |      }|t        | ||      } t        j                  | |||	dz  z
  ||
dz  z   f|||j                         |t         j                         | S )a  
    Draw text with background on a scene.

    Args:
        scene: A numpy ndarray representing the image, typically of shape
            (H, W, 3) for a color BGR image or (H, W) for grayscale,
            with dtype uint8.
        text: The text to be drawn.
        text_anchor: The anchor point for the text, represented as a
            Point object with x and y attributes.
        text_color: The color of the text. Defaults to black.
        text_scale: The scale of the text. Defaults to 0.5.
        text_thickness: The thickness of the text. Defaults to 1.
        text_padding: The amount of padding to add around the text
            when drawing a rectangle in the background. Defaults to 10.
        text_font: The font to use for the text.
            Defaults to cv2.FONT_HERSHEY_SIMPLEX.
        background_color: The color of the background rectangle,
            if one is to be drawn. Defaults to None.

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

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.geometry.core import Point
        >>> from supervision.draw.utils import draw_text
        >>> 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
        ... )
        >>> scene.shape
        (100, 100, 3)

        ```
    )textfontFace	fontScaler   r   r   )xyr6   r7   )r   r   r   )r)   rF   orgrG   rH   r   r   lineType)	r   getTextSizer   r   padr'   putTextr   LINE_AA)r   rF   text_anchor
text_color
text_scaletext_thicknesstext_padding	text_fontbackground_color
text_widthtext_heighttext_anchor_xtext_anchor_y	text_rects                 r   	draw_textr]      s    b "oo 	
 	J $/#>#>#@ M=
*/
)
+*
*	
 
c,  #%i/?
 KKZ1_,mkQ>N.NO! 	 Lr   c           	        t        |t              rRt        j                  j	                  |      st        d| d      t        j                  |t        j                        }d|cxk  rdk  st        d       t        d      t        |j                        }t        |j                        }t        |j                        }t        |j                        }|dk  s/|dk  s*||z   | j                  d   kD  s||z   | j                  d   kD  rt        d      t        j                   |||f      }t#        t$        j&                  t(        j*                     |      }|j                  d	   d
k(  r|dddddf   n%t)        j,                  ||f|j.                        dz  }t        j0                  ||z        }	| |||z   |||z   f   }
|	j3                  t(        j4                        dz  }t        j0                  d|dt(        j6                  f   z
  |
z  |dt(        j6                  f   |ddddddf   z  z         }|| |||z   |||z   f<   | S )a  
    Draws an image onto a given scene with specified opacity and dimensions.

    Args:
        scene: Background image where the new image will be drawn.
        image: Image to draw, either a file path or an already-loaded image array.
        opacity: Opacity of the image to be drawn.
        rect: Rectangle specifying where to draw the image.

    Returns:
        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
ValueErrorintrI   rJ   r6   r7   shaperesizer   nptNDArraynpuint8onesra   convertScaleAbsastypefloat32newaxis)r   imager%   r   rect_xrect_y
rect_widthrect_heightalpha_channelalpha_scaled	scene_roialpha_floatblended_rois                r   
draw_imager   -  s5   0 %ww~~e$#mE7:L$MNN

5#"6"67 ' S ?@@ !?@@[F[FTZZJdkk"K 	
A:JQ/K%++a.0899 JJuz;78ERXX&.E ;;q>Q 	aAgWWk:.ekkBSH 
 &&}w'>?L fv33Vfz>Q5QQRI%%bjj1E9K%%	
[bjj)	)Y6
c2::o
&q!RaRx
8	9K JUE&6K'
'&:2E)E
EFLr   c                    t        |       dz  S )a)  
    Calculate optimal font scale based on image resolution. Adjusts font scale
    proportionally to the smallest dimension of the given image resolution for
    consistent readability.

    Args:
        resolution_wh: A tuple of `(width, height)` of the image in pixels.

    Returns:
        Recommended font scale factor.

    Examples:
        ```pycon
        >>> import supervision as sv
        >>> sv.calculate_optimal_text_scale((1920, 1080))
        1.08
        >>> sv.calculate_optimal_text_scale((640, 480))
        0.48

        ```
    gMbP?r/   resolution_whs    r   calculate_optimal_text_scaler   s  s    , }$$r   c                "    t        |       dk  ryy)a1  
    Calculate optimal line thickness based on image resolution. Adjusts the line
    thickness for readability depending on the smallest dimension of the provided
    image resolution.

    Args:
        resolution_wh: A tuple of `(width, height)` of the image in pixels.

    Returns:
        Recommended line thickness in pixels.

    Examples:
        ```pycon
        >>> import supervision as sv
        >>> sv.calculate_optimal_line_thickness((1920, 1080))
        4
        >>> sv.calculate_optimal_line_thickness((640, 480))
        2

        ```
    i8  r   r_   r   r   s    r    calculate_optimal_line_thicknessr     s    , =D r   )r   npt.NDArray[np.uint8]r   r   r   r   r   r   r   rl   returnr   )
r   r   r   r   r   r   r   rl   r   r   )
r   r   r   r   r   r   r%   floatr   r   )
r   r   r   r   r   r   r1   rl   r   r   )
r   r   r?   npt.NDArray[np.int_]r   r   r   rl   r   r   )
r   r   r?   r   r   r   r%   r   r   r   )r   r   rF   rd   rQ   r   rR   r   rS   r   rT   rl   rU   rl   rV   rl   rW   zColor | Noner   r   )
r   r   rx   zstr | npt.NDArray[np.uint8]r%   r   r   r   r   r   )r   tuple[int, int]r   r   )r   r   r   rl   )
__future__r   re   typingr   r   numpyrq   numpy.typingro   supervision.draw.colorr   supervision.geometry.corer   r   ROBOFLOWr   r   r'   r;   r@   rC   BLACKFONT_HERSHEY_SIMPLEXr]   r   r   r    r   r   <module>r      sl   " 	  
   ( 1 >>  
 	
  B >>	 
  	
 > >>	' '
' ' 	'
 'T1 1
1 1 	1
 1n >>	 !  	
 4 >>	 !  	
 B --%)P P
P P 	P
 P P P P #P PfC C&C C 	C
 CL%2r   