Ë
    Fêñi´¾  ã            
      ód  — d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	Z	d dl
Zd dlZd dlmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZ d d	lmZmZmZ d d
lm Z   G d„ d«      Z! e!«       Z" G d„ d«      Z# e«        e«       d ed«      dfd„«       «       Z$ ed«      dddddf	 	 	 	 	 	 	 	 	 	 	 d%d„Z%e ejL                  dejN                  ¬«      ddddddddf		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d&d„«       Z( e«       d'd(d„«       Z)d)d*d „Z* e«       d+d,d!„«       Z+ e«       d" ed#«      fd-d$„«       Z,y).é    )ÚannotationsN)ÚCallable)ÚPath)ÚAny)ÚImageÚ	ImageDrawÚ	ImageFont)Ú__version__)ÚIS_COLABÚ	IS_KAGGLEÚLOGGERÚ	TryExceptÚopsÚplt_settingsÚthreaded)Ú
check_fontÚcheck_versionÚis_ascii)Úincrement_pathc                  ó2   — e Zd ZdZd„ Zddd„Zedd„«       Zy)	ÚColorsa¾  Ultralytics color palette for visualization and plotting.

    This class provides methods to work with the Ultralytics color palette, including converting hex color codes to RGB
    values and accessing predefined color schemes for object detection and pose estimation.

    ## Ultralytics Color Palette

    | Index | Color                                                             | HEX       | RGB               |
    |-------|-------------------------------------------------------------------|-----------|-------------------|
    | 0     | <i class="fa-solid fa-square fa-2xl" style="color: #042aff;"></i> | `#042aff` | (4, 42, 255)      |
    | 1     | <i class="fa-solid fa-square fa-2xl" style="color: #0bdbeb;"></i> | `#0bdbeb` | (11, 219, 235)    |
    | 2     | <i class="fa-solid fa-square fa-2xl" style="color: #f3f3f3;"></i> | `#f3f3f3` | (243, 243, 243)   |
    | 3     | <i class="fa-solid fa-square fa-2xl" style="color: #00dfb7;"></i> | `#00dfb7` | (0, 223, 183)     |
    | 4     | <i class="fa-solid fa-square fa-2xl" style="color: #111f68;"></i> | `#111f68` | (17, 31, 104)     |
    | 5     | <i class="fa-solid fa-square fa-2xl" style="color: #ff6fdd;"></i> | `#ff6fdd` | (255, 111, 221)   |
    | 6     | <i class="fa-solid fa-square fa-2xl" style="color: #ff444f;"></i> | `#ff444f` | (255, 68, 79)     |
    | 7     | <i class="fa-solid fa-square fa-2xl" style="color: #cced00;"></i> | `#cced00` | (204, 237, 0)     |
    | 8     | <i class="fa-solid fa-square fa-2xl" style="color: #00f344;"></i> | `#00f344` | (0, 243, 68)      |
    | 9     | <i class="fa-solid fa-square fa-2xl" style="color: #bd00ff;"></i> | `#bd00ff` | (189, 0, 255)     |
    | 10    | <i class="fa-solid fa-square fa-2xl" style="color: #00b4ff;"></i> | `#00b4ff` | (0, 180, 255)     |
    | 11    | <i class="fa-solid fa-square fa-2xl" style="color: #dd00ba;"></i> | `#dd00ba` | (221, 0, 186)     |
    | 12    | <i class="fa-solid fa-square fa-2xl" style="color: #00ffff;"></i> | `#00ffff` | (0, 255, 255)     |
    | 13    | <i class="fa-solid fa-square fa-2xl" style="color: #26c000;"></i> | `#26c000` | (38, 192, 0)      |
    | 14    | <i class="fa-solid fa-square fa-2xl" style="color: #01ffb3;"></i> | `#01ffb3` | (1, 255, 179)     |
    | 15    | <i class="fa-solid fa-square fa-2xl" style="color: #7d24ff;"></i> | `#7d24ff` | (125, 36, 255)    |
    | 16    | <i class="fa-solid fa-square fa-2xl" style="color: #7b0068;"></i> | `#7b0068` | (123, 0, 104)     |
    | 17    | <i class="fa-solid fa-square fa-2xl" style="color: #ff1b6c;"></i> | `#ff1b6c` | (255, 27, 108)    |
    | 18    | <i class="fa-solid fa-square fa-2xl" style="color: #fc6d2f;"></i> | `#fc6d2f` | (252, 109, 47)    |
    | 19    | <i class="fa-solid fa-square fa-2xl" style="color: #a2ff0b;"></i> | `#a2ff0b` | (162, 255, 11)    |

    ## Pose Color Palette

    | Index | Color                                                             | HEX       | RGB               |
    |-------|-------------------------------------------------------------------|-----------|-------------------|
    | 0     | <i class="fa-solid fa-square fa-2xl" style="color: #ff8000;"></i> | `#ff8000` | (255, 128, 0)     |
    | 1     | <i class="fa-solid fa-square fa-2xl" style="color: #ff9933;"></i> | `#ff9933` | (255, 153, 51)    |
    | 2     | <i class="fa-solid fa-square fa-2xl" style="color: #ffb266;"></i> | `#ffb266` | (255, 178, 102)   |
    | 3     | <i class="fa-solid fa-square fa-2xl" style="color: #e6e600;"></i> | `#e6e600` | (230, 230, 0)     |
    | 4     | <i class="fa-solid fa-square fa-2xl" style="color: #ff99ff;"></i> | `#ff99ff` | (255, 153, 255)   |
    | 5     | <i class="fa-solid fa-square fa-2xl" style="color: #99ccff;"></i> | `#99ccff` | (153, 204, 255)   |
    | 6     | <i class="fa-solid fa-square fa-2xl" style="color: #ff66ff;"></i> | `#ff66ff` | (255, 102, 255)   |
    | 7     | <i class="fa-solid fa-square fa-2xl" style="color: #ff33ff;"></i> | `#ff33ff` | (255, 51, 255)    |
    | 8     | <i class="fa-solid fa-square fa-2xl" style="color: #66b2ff;"></i> | `#66b2ff` | (102, 178, 255)   |
    | 9     | <i class="fa-solid fa-square fa-2xl" style="color: #3399ff;"></i> | `#3399ff` | (51, 153, 255)    |
    | 10    | <i class="fa-solid fa-square fa-2xl" style="color: #ff9999;"></i> | `#ff9999` | (255, 153, 153)   |
    | 11    | <i class="fa-solid fa-square fa-2xl" style="color: #ff6666;"></i> | `#ff6666` | (255, 102, 102)   |
    | 12    | <i class="fa-solid fa-square fa-2xl" style="color: #ff3333;"></i> | `#ff3333` | (255, 51, 51)     |
    | 13    | <i class="fa-solid fa-square fa-2xl" style="color: #99ff99;"></i> | `#99ff99` | (153, 255, 153)   |
    | 14    | <i class="fa-solid fa-square fa-2xl" style="color: #66ff66;"></i> | `#66ff66` | (102, 255, 102)   |
    | 15    | <i class="fa-solid fa-square fa-2xl" style="color: #33ff33;"></i> | `#33ff33` | (51, 255, 51)     |
    | 16    | <i class="fa-solid fa-square fa-2xl" style="color: #00ff00;"></i> | `#00ff00` | (0, 255, 0)       |
    | 17    | <i class="fa-solid fa-square fa-2xl" style="color: #0000ff;"></i> | `#0000ff` | (0, 0, 255)       |
    | 18    | <i class="fa-solid fa-square fa-2xl" style="color: #ff0000;"></i> | `#ff0000` | (255, 0, 0)       |
    | 19    | <i class="fa-solid fa-square fa-2xl" style="color: #ffffff;"></i> | `#ffffff` | (255, 255, 255)   |

    !!! note "Ultralytics Brand Colors"

        For Ultralytics brand colors see [https://www.ultralytics.com/brand](https://www.ultralytics.com/brand).
        Please use the official Ultralytics colors for all marketing materials.

    Attributes:
        palette (list[tuple]): List of RGB color tuples for general use.
        n (int): The number of colors in the palette.
        pose_palette (np.ndarray): A specific color palette array for pose estimation with dtype np.uint8.

    Examples:
        >>> from ultralytics.utils.plotting import Colors
        >>> colors = Colors()
        >>> colors(5, True)  # Returns BGR format: (221, 111, 255)
        >>> colors(5, False)  # Returns RGB format: (255, 111, 221)
    c                ó`  — d}|D �cg c]  }| j                  d|› �«      ‘Œ c}| _        t        | j                  «      | _        t	        j
                  g d¢g d¢g d¢g d¢g d¢g d¢g d	¢g d
¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢gt        j                  ¬«      | _        yc c}w )zEInitialize colors as hex = matplotlib.colors.TABLEAU_COLORS.values().)Ú042AFFÚ0BDBEBÚF3F3F3Ú00DFB7Ú111F68ÚFF6FDDÚFF444FÚCCED00Ú00F344ÚBD00FFÚ00B4FFÚDD00BAÚ00FFFFÚ26C000Ú01FFB3Ú7D24FFÚ7B0068ÚFF1B6CÚFC6D2FÚA2FF0Bú#)éÿ   é€   r   )r.   é™   é3   )r.   é²   éf   )éæ   r4   r   )r.   r0   r.   )r0   éÌ   r.   )r.   r3   r.   )r.   r1   r.   )r3   r2   r.   )r1   r0   r.   )r.   r0   r0   )r.   r3   r3   )r.   r1   r1   )r0   r.   r0   )r3   r.   r3   )r1   r.   r1   )r   r.   r   )r   r   r.   )r.   r   r   ©r.   r.   r.   ©ÚdtypeN)Úhex2rgbÚpaletteÚlenÚnÚnpÚarrayÚuint8Úpose_palette)ÚselfÚhexsÚcs      ú\/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/utils/plotting.pyÚ__init__zColors.__init__^   sœ   € ð
ˆð, 8<Ö<°!˜Ÿ™ q¨¨ WÕ-Ò<ˆŒÜ�T—\‘\Ó"ˆŒÜŸH™HâÚÚÚÚÚÚÚÚÚÚÚÚÚÚÚÚÚÚÚð)ô, —(‘(ô/
ˆÕùò =s   ‡B+c                óp   — | j                   t        |«      | j                  z     }|r|d   |d   |d   fS |S )a   Return a color from the palette by index.

        Args:
            i (int | torch.Tensor): Color index.
            bgr (bool, optional): Whether to return BGR format instead of RGB.

        Returns:
            (tuple): RGB or BGR color tuple.
        é   é   r   )r:   Úintr<   )rA   ÚiÚbgrrC   s       rD   Ú__call__zColors.__call__’   s>   € ð �L‰Lœ˜Q› $§&¡&™Ñ)ˆÙ%(��!‘�a˜‘d˜A˜a™DÐ!Ð/¨aÐ/ó    c                ó,   ‡ — t        ˆ fd„dD «       «      S )z?Convert hex color codes to RGB values (i.e. default PIL order).c              3  óN   •K  — | ]  }t        ‰d |z   d |z   dz    d«      –— Œ y­w)rH   rG   é   N©rI   )Ú.0rJ   Úhs     €rD   ú	<genexpr>z!Colors.hex2rgb.<locals>.<genexpr>¢   s+   øè ø€ ÒF°q”S˜˜1˜q™5 1 q¡5¨1¡9Ð-¨r×2ÑFùs   ƒ"%)r   rG   é   )Útuple)rS   s   `rD   r9   zColors.hex2rgbŸ   s   ø€ ô ÓF¸IÔFÓFÐFrM   N©F)rJ   zint | torch.TensorrK   ÚboolÚreturnrV   )rS   ÚstrrY   rV   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rE   rL   Ústaticmethodr9   © rM   rD   r   r      s*   „ ñFòP2
ôh0ð òGó ñGrM   r   c                  óÂ   — e Zd ZdZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 dd„Zddd„Zddd„Zddd„Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 dd„Zddd„Z	ddd	„Z
d
„ Zdd„Zddd„Zd d!d„Zed"d„«       Zy)#Ú	Annotatora¿  Ultralytics Annotator for train/val mosaics and JPGs and predictions annotations.

    Attributes:
        im (Image.Image | np.ndarray): The image to annotate.
        pil (bool): Whether to use PIL or cv2 for drawing annotations.
        font (ImageFont.truetype | ImageFont.load_default): Font used for text annotations.
        lw (int): Line width for drawing.
        skeleton (list[list[int]]): Skeleton structure for keypoints.
        limb_color (np.ndarray): Color palette for limbs.
        kpt_color (np.ndarray): Color palette for keypoints.
        dark_colors (set): Set of colors considered dark for text contrast.
        light_colors (set): Set of colors considered light for text contrast.

    Examples:
        >>> from ultralytics.utils.plotting import Annotator
        >>> im0 = cv2.imread("test.png")
        >>> annotator = Annotator(im0, line_width=10)
        >>> annotator.box_label([10, 10, 100, 100], "person", (255, 0, 0))
    Nc                ó  ‡ — t        |«       }t        |t        j                  «      }|xs |xs |‰ _        |xs< t	        t        t        |r|j                  n|j                  «      dz  dz  «      d«      ‰ _	        |s¼|j                  d   dk(  r%t        j                  |t        j                  «      }n…|j                  d   dk(  rEt        j                  t        j                  |t        j                   |ddd…f   «      f«      «      }n.|j                  d   dkD  rt        j                  |ddd…f   «      }‰ j                  �r|r|nt        j"                  |«      ‰ _        ‰ j$                  j&                  dvr ‰ j$                  j)                  d«      ‰ _        t+        j,                  ‰ j$                  d	«      ‰ _        	 t1        |rd
n|«      }|xs8 t	        t        t        ‰ j$                  j                  «      dz  dz  «      d«      }	t3        j4                  t7        |«      |	«      ‰ _        t?        t@        d«      r‘ˆ fd„‰ j8                  _!        n||jD                  jF                  sJ d«       ‚|jH                  jJ                  r|n|jM                  «       ‰ _        t	        ‰ j                  dz
  d«      ‰ _'        ‰ j                  dz  ‰ _(        ddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgg‰ _)        tT        jV                  g d¢   ‰ _,        tT        jV                  g d¢   ‰ _-        h d£‰ _.        h d £‰ _/        y# t:        $ r t3        j<                  «       ‰ _        Y �ŒMw xY w)!zjInitialize the Annotator class with image and line width along with color palette for keypoints and limbs.rG   gú~j¼t“h?rH   .Né   >   ÚRGBÚRGBAre   rf   zArial.Unicode.ttfgìQ¸…ë¡?é   z9.2.0c                ó@   •— ‰j                   j                  | «      dd S )NrG   rU   )ÚfontÚgetbbox)ÚxrA   s    €rD   ú<lambda>z$Annotator.__init__.<locals>.<lambda>ß   s   ø€ ¨d¯i©i×.?Ñ.?ÀÓ.BÀ1ÀQÐ.G€ rM   zOImage not contiguous. Apply np.ascontiguousarray(im) to Annotator input images.rP   é   é   é   é   é   é   é   é	   é
   é   rU   é   )rt   rt   rt   rt   rr   rr   rr   r   r   r   r   r   rP   rP   rP   rP   rP   rP   rP   )rP   rP   rP   rP   rP   r   r   r   r   r   r   rt   rt   rt   rt   rt   rt   >	   ©r   éí   r5   ©rv   r.   é¢   ©éD   éó   r   ©é³   r.   rH   ©é·   éß   r   ©éÝ   éo   r.   ©éë   éÛ   rv   ©r~   r~   r~   ©r.   r.   r   >   ©r   éÀ   é&   ©é/   ém   éü   ©éO   r}   r.   ©éh   r   é{   ©él   é   r.   ©éº   r   r…   ©r.   r   é½   ©r.   é$   é}   ©r.   é*   rU   ©r.   é´   r   ©r–   é   rn   )0r   Ú
isinstancer   ÚpilÚmaxÚroundÚsumÚsizeÚshapeÚlwÚcv2ÚcvtColorÚCOLOR_GRAY2BGRr=   ÚascontiguousarrayÚdstackÚ
zeros_likeÚ	fromarrayÚimÚmodeÚconvertr   ÚDrawÚdrawr   r	   ÚtruetyperZ   ri   Ú	ExceptionÚload_defaultr   Úpil_versionÚgetsizeÚdataÚ
contiguousÚflagsÚ	writeableÚcopyÚtfÚsfÚskeletonÚcolorsr@   Ú
limb_colorÚ	kpt_colorÚdark_colorsÚlight_colors)
rA   r·   Ú
line_widthÚ	font_sizeri   r©   ÚexampleÚ	non_asciiÚinput_is_pilr­   s
   `         rD   rE   zAnnotator.__init__½   s  ø€ ô ! Ó)Ð)ˆ	Ü! "¤e§k¡kÓ2ˆØÒ3˜)Ò3 |ˆŒØÒe¤¤E¬#¹¨b¯gªgÈ2Ï8É8Ó*TÐWXÑ*XÐ[`Ñ*`Ó$aÐcdÓ eˆŒÙØ�x‰x˜‰{˜aÒÜ—\‘\ "¤c×&8Ñ&8Ó9‘Ø—‘˜!‘ Ò!Ü×)Ñ)¬"¯)©)°R¼¿¹ÀrÈ#ÈrÐPQÈrÈ'Á{Ó9SÐ4TÓ*UÓV‘Ø—‘˜!‘˜q’Ü×)Ñ)¨"¨S°"°1°"¨W©+Ó6�Ø�8‹8Ù(‘b¬e¯o©o¸bÓ.AˆDŒGØ�w‰w�|‰| ?Ñ2ØŸ'™'Ÿ/™/¨%Ó0�”Ü!Ÿ™ t§w¡w°Ó7ˆDŒIð5Ü!¹Ñ"5ÈÓM�Ø ÒQ¤C¬¬c°$·'±'·,±,Ó.?À!Ñ.CÀeÑ.KÓ(LÈbÓ$Q�Ü%×.Ñ.¬s°4«y¸$Ó?�”	ô œ[¨'Ô2Û$G�—	‘	Õ!à—7‘7×%Ò%ÐxÐ'xÓxÐ%ØŸH™H×.Ò.‘b°B·G±G³IˆDŒGÜ˜$Ÿ'™' A™+ qÓ)ˆDŒGØ—g‘g ‘kˆDŒGð �ˆHØ�ˆHØ�ˆHØ�ˆHØ�ˆHØ�ˆGØ�ˆGØ�ˆFØ�ˆFØ�ˆFØ�ˆGØ�ˆGØ�ˆFØ�ˆFØ�ˆFØ�ˆFØ�ˆFØ�ˆFØ�ˆFð'
ˆŒô, !×-Ñ-Ò.nÑoˆŒÜ×,Ñ,Ò-eÑfˆŒò

ˆÔò
ˆÕøô] ò 5Ü%×2Ñ2Ó4�—	ð5ús   ÇA/M Í"N Í?N c                óB   — || j                   v ry|| j                  v ry|S )a1  Assign text color based on background color.

        Args:
            color (tuple, optional): The background color of the rectangle for text.
            txt_color (tuple, optional): The fallback color of the text.

        Returns:
            (tuple): Text color for label.

        Examples:
            >>> from ultralytics.utils.plotting import Annotator
            >>> im0 = cv2.imread("test.png")
            >>> annotator = Annotator(im0, line_width=10)
            >>> annotator.get_txt_color(color=(104, 31, 17))  # return (255, 255, 255)
        r¦   r6   )rÌ   rÍ   )rA   ÚcolorÚ	txt_colors      rD   Úget_txt_colorzAnnotator.get_txt_color  s,   € ð  �D×$Ñ$Ñ$ØØ�d×'Ñ'Ñ'Ø àÐrM   c           
     ó¨  — | j                  ||«      }t        |t        j                  «      r|j	                  «       }t        |d   t
        «      }|r|d   D �cg c]  }t        |«      ‘Œ c}nt        |d   «      t        |d   «      f}| j                  �r^|r?| j                  j                  |D �cg c]  }t        |«      ‘Œ c}| j                  |¬«      n'| j                  j                  || j                  |¬«       |rò| j                  j                  |«      \  }}	|d   |	k\  }
|d   | j                  j                   d   |z
  kD  r!| j                  j                   d   |z
  |d   f}| j                  j                  |d   |
r|d   |	z
  n|d   |d   |z   dz   |
r|d   dz   n
|d   |	z   dz   f|¬«       | j                  j#                  |d   |
r|d   |	z
  n|d   f||| j                  ¬«       yy|rGt%        j&                  | j                  t)        j*                  |t        ¬«      gd|| j                  «      nVt%        j                  | j                  |t        |d   «      t        |d	   «      f|| j                  t$        j,                  ¬
«       |�r1t%        j.                  |d| j0                  | j2                  ¬«      d   \  }}	|	d	z  }	|d   |	k\  }
|d   | j                  j4                  d   |z
  kD  r!| j                  j4                  d   |z
  |d   f}|d   |z   |
r|d   |	z
  n|d   |	z   f}t%        j                  | j                  |||dt$        j,                  «       t%        j6                  | j                  ||d   |
r|d   dz
  n
|d   |	z   dz
  fd| j0                  || j2                  t$        j,                  ¬
«       yyc c}w c c}w )ag  Draw a bounding box on an image with a given label.

        Args:
            box (tuple): The bounding box coordinates (x1, y1, x2, y2).
            label (str, optional): The text label to be displayed.
            color (tuple, optional): The background color of the rectangle.
            txt_color (tuple, optional): The color of the text.

        Examples:
            >>> from ultralytics.utils.plotting import Annotator
            >>> im0 = cv2.imread("test.png")
            >>> annotator = Annotator(im0, line_width=10)
            >>> annotator.box_label(box=[10, 20, 30, 40], label="person")
        r   rH   ©ÚwidthÚoutline©Úfill©rÜ   ri   r7   TrG   rd   ©Ú	thicknessÚlineType©Ú	fontScalerß   éÿÿÿÿN)rÖ   r¨   ÚtorchÚTensorÚtolistÚlistrI   r©   r»   ÚpolygonrV   r¯   Ú	rectangleri   rÀ   r·   r­   Útextr°   Ú	polylinesr=   ÚasarrayÚLINE_AAÚgetTextSizerÇ   rÆ   r®   ÚputText)rA   ÚboxÚlabelrÔ   rÕ   Úmulti_pointsÚbÚp1ÚwrS   ÚoutsideÚp2s               rD   Ú	box_labelzAnnotator.box_label.  ss  € ð ×&Ñ& u¨iÓ8ˆ	Ü�cœ5Ÿ<™<Ô(Ø—*‘*“,ˆCä! # a¡&¬$Ó/ˆÙ)5˜c !™fÖ%˜Œc�!�fÓ%¼CÀÀAÁ»KÌÈSÐQRÉVËÐ;UˆØ�8‹8ñ ð �I‰I×ÑØ#&Ö'˜a”�q•Ò'¨t¯w©wÀð ô à#'§9¡9×#6Ñ#6°sÀ$Ç'Á'ÐSXÐ#6Ó#YøÙØ—y‘y×(Ñ(¨Ó/‘��1Ø˜Q™% 1™*�Ø�a‘5˜4Ÿ7™7Ÿ<™<¨™?¨QÑ.Ò.ØŸ™Ÿ™ a™¨1Ñ,¨b°©eÐ3�BØ—	‘	×#Ñ#Ø˜‘U©˜B˜q™E AšI°b¸±e¸RÀ¹UÀQ¹YÈ¹]ÑY`ÈBÈqÉEÐTUÊIÐfhÐijÑfkÐnoÑfoÐrsÑfsÐtØð $ô ð
 —	‘	—‘  1¡±G r¨!¡u¨q¢yÀÀAÁÐGÈÐU^Ðei×enÑen�Õoð ñ ô �M‰MØ—‘œ"Ÿ*™* S´Ô4Ð5°t¸UÀDÇGÁGôä#&§=¡=Ø—‘˜œc # a¡&›k¬3¨s°1©v«;Ð7¸È$Ï'É'Ô\_×\gÑ\gô$øò Ü—‘ u¨a¸4¿7¹7ÈdÏgÉgÔVÐWXÑY‘��1Ø�Q‘�Ø˜Q™% 1™*�Ø�a‘5˜4Ÿ7™7Ÿ=™=¨Ñ+¨aÑ/Ò/ØŸ™Ÿ™ qÑ)¨AÑ-¨r°!©uÐ4�BØ˜‘U˜Q‘Y©W  1¡¨¢	¸"¸Q¹%À!¹)ÐC�Ü—‘˜dŸg™g r¨2¨u°b¼#¿+¹+ÔFÜ—‘Ø—G‘GØØ˜‘U©˜B˜q™E AšI°b¸±e¸a±iÀ!±mÐDØØ—G‘GØØ"Ÿg™gÜ Ÿ[™[ö	ð ùò- &ùò (s   ÁO
Â2Oc                óî  — | j                   r2t        j                  | j                  «      j	                  «       | _        |€–t        |t        j                  «      sJ d«       ‚| j                  j	                  «       }t        |«      D ]   \  }}||   ||j                  t        «      <   Œ" t        j                  | j                  d|z
  ||d«      | _        �nwt        |t        j                  «      sJ d«       ‚t        |«      dk(  rN|j                  ddd«      j!                  «       j#                  «       j%                  «       dz  | j                  dd y|j&                  |j&                  k7  r|j)                  |j&                  «      }| j                  j*                  dd \  }	}
|s¦t-        j.                  |d   j1                  «       |	|
f«      d   dkD  }t        j2                  | j                  «      j)                  |j&                  «      j                  ddd«      j5                  d«      j!                  «       j1                  «       d	z  }t        j6                  ||j&                  t        j8                  ¬
«      d	z  }|dd…ddf   }|j;                  d«      }|||z  z  }d||z  z
  j=                  d«      }|j?                  d¬«      j@                  }|j5                  dg¬«      j                  ddd«      j!                  «       }||d   z  |z   }|dz  jC                  «       j#                  «       j%                  «       | j                  dd | j                   r| jE                  | j                  «       yy)aâ  Plot masks on image.

        Args:
            masks (torch.Tensor | np.ndarray): Predicted masks with shape [n, h, w].
            colors (list[list[int]]): Colors for predicted masks, [[r, g, b] * n].
            im_gpu (torch.Tensor | None): Image on GPU with shape [3, h, w], range [0, 1].
            alpha (float, optional): Mask transparency: 0.0 fully transparent, 1.0 opaque.
            retina_masks (bool, optional): Whether to use high resolution masks or not.
        Nz9`masks` must be a np.ndarray if `im_gpu` is not provided.rH   r   z7'masks' must be a torch.Tensor if 'im_gpu' is provided.rG   r.   ç      à?g     ào@)Údevicer8   rd   ©Údim)Údimsrã   )#r©   r=   rì   r·   rÅ   r¨   ÚndarrayÚ	enumerateÚastyperX   r°   ÚaddWeightedrä   rå   r;   ÚpermuterÂ   ÚcpuÚnumpyrû   Útor®   r   Úscale_masksÚfloatÚ
from_numpyÚflipÚtensorÚfloat32Ú	unsqueezeÚcumprodrª   ÚvaluesÚbyter¶   )rA   ÚmasksrÉ   Úim_gpuÚalphaÚretina_masksÚoverlayrJ   ÚmaskÚihÚiwÚmasks_colorÚinv_alpha_masksÚmcss                 rD   r  zAnnotator.masksk  sÎ  € ð �8Š8ä—j‘j §¡Ó)×.Ñ.Ó0ˆDŒGØˆ>Ü˜e¤R§Z¡ZÔ0ÐmÐ2mÓmÐ0Ø—g‘g—l‘l“nˆGÜ$ UÓ+ò 7‘��4Ø-3°A©Y�˜Ÿ™¤DÓ)Ò*ð7ä—o‘o d§g¡g¨q°5©y¸'À5È!ÓLˆDŽGä˜e¤U§\¡\Ô2ÐmÐ4mÓmÐ2Ü�5‹z˜QŠØ#Ÿ^™^¨A¨q°!Ó4×?Ñ?ÓA×EÑEÓG×MÑMÓOÐRUÑU�—‘™�
ØØ�}‰} §¡Ò,ØŸ™ 5§<¡<Ó0�à—W‘W—]‘] 2 AÐ&‰FˆB�ÙäŸ™¨¨d©×(9Ñ(9Ó(;¸bÀ"¸XÓFÀqÑIÈCÑO�ô ×$Ñ$ T§W¡WÓ-×0Ñ0°·±Ó>×FÑFÀqÈ!ÈQÓO×TÑTÐUVÓW×bÑbÓd×jÑjÓlÐotÑtð ô —\‘\ &°·±ÄUÇ]Á]ÔSÐV[Ñ[ˆFØšA˜t T˜MÑ*ˆFØ—O‘O AÓ&ˆEØ 6¨E¡>Ñ2ˆKØ  5¨5¡=Ñ0×9Ñ9¸!Ó<ˆOØ—/‘/ a�/Ó(×/Ñ/ˆCà—[‘[ q c�[Ó*×2Ñ2°1°a¸Ó;×FÑFÓHˆFØ˜o¨bÑ1Ñ1°CÑ7ˆFØ  3™,×,Ñ,Ó.×2Ñ2Ó4×:Ñ:Ó<ˆD�G‰G‘AˆJØ�8Š8à�N‰N˜4Ÿ7™7Õ#ð rM   c                óÄ  — |�|n| j                   }| j                  r2t        j                  | j                  «      j                  «       | _        |j                  \  }}|dk(  xr |dv }	||	z  }t        |«      D ]µ  \  }
}|xs* |	r| j                  |
   j                  «       n
t        |
«      }|d   |d   }}||d   z  dk7  sŒJ||d   z  dk7  sŒVt        |«      dk(  r|d   }||k  rŒot        j                  | j                  t        |«      t        |«      f||dt        j                  ¬	«       Œ· |�r…|j                  d   }t        | j                   «      D �]]  \  }
}t        ||d   dz
  df   «      t        ||d   dz
  df   «      f}t        ||d   dz
  df   «      t        ||d   dz
  df   «      f}|dk(  r%||d   dz
  df   }||d   dz
  df   }||k  s||k  rŒ‰|d   |d   z  dk(  s|d   |d   z  dk(  s|d   dk  s|d   dk  rŒ¶|d   |d   z  dk(  s|d   |d   z  dk(  s|d   dk  s|d   dk  rŒãt        j"                  | j                  |||xs | j$                  |
   j                  «       t        t        j&                  | j                   dz  «      «      t        j                  ¬
«       �Œ` | j                  r| j)                  | j                  «       yy)a„  Plot keypoints on the image.

        Args:
            kpts (torch.Tensor): Keypoints, shape [17, 3] (x, y, confidence).
            shape (tuple, optional): Image shape (h, w).
            radius (int, optional): Keypoint radius.
            kpt_line (bool, optional): Draw lines between keypoints.
            conf_thres (float, optional): Confidence threshold.
            kpt_color (tuple, optional): Keypoint color.

        Notes:
            - `kpt_line=True` currently only supports human pose plotting.
            - Modifies self.im in-place.
            - If self.pil is True, converts image to numpy array and back to PIL.
        Nrn   >   rG   rd   r   rH   rd   rG   rã   )rà   rÞ   )r¯   r©   r=   rì   r·   rÅ   r®   r   rË   ræ   rÉ   r;   r°   ÚcirclerI   rí   rÈ   ÚlinerÊ   Úceilr¶   )rA   Úkptsr®   ÚradiusÚkpt_lineÚ
conf_thresrË   ÚnkptÚndimÚis_poserJ   ÚkÚcolor_kÚx_coordÚy_coordÚconfÚskÚpos1Úpos2Úconf1Úconf2s                        rD   r   zAnnotator.kpts�  sû  € ð0 "Ð-‘°4·7±7ˆØ�8Š8ä—j‘j §¡Ó)×.Ñ.Ó0ˆDŒGØ—Z‘Z‰
ˆˆdØ˜"‘*Ò/ ¨ ˆØ�GÑˆÜ˜d“Oò 	m‰DˆAˆqØÒYÁ' D§N¡N°1Ñ$5×$<Ñ$<Ô$>ÌvÐVWËyˆGØ  ™t Q q¡T�WˆGØ˜˜q™Ñ! QÓ&¨7°U¸1±XÑ+=ÀÓ+BÜ�q“6˜Q’;Ø˜Q™4�DØ˜jÒ(Ø Ü—
‘
˜4Ÿ7™7¤S¨£\´3°w³<Ð$@À&È'ÐSUÔ`c×`kÑ`kÖlð	mò Ø—:‘:˜b‘>ˆDÜ" 4§=¡=Ó1ó ‘��2Ü˜D " Q¡%¨!¡)¨a Ñ0Ó1´3°t¸RÀ¹UÀQ¹YÈ¸NÑ7KÓ3LÐM�Ü˜D " Q¡%¨!¡)¨a Ñ0Ó1´3°t¸RÀ¹UÀQ¹YÈ¸NÑ7KÓ3LÐM�Ø˜1’9Ø  " Q¡%¨!¡)¨a Ñ0�EØ  " Q¡%¨!¡)¨a Ñ0�EØ˜zÒ)¨U°ZÒ-?Ø Ø˜‘7˜U 1™XÑ%¨Ò*¨d°1©g¸¸a¹Ñ.@ÀAÒ.EÈÈaÉÐSTÊÐX\Ð]^ÑX_ÐbcÒXcØØ˜‘7˜U 1™XÑ%¨Ò*¨d°1©g¸¸a¹Ñ.@ÀAÒ.EÈÈaÉÐSTÊÐX\Ð]^ÑX_ÐbcÒXcØÜ—‘Ø—G‘GØØØÒ< §¡°Ñ!3×!:Ñ!:Ó!<Ü!¤"§'¡'¨$¯'©'°A©+Ó"6Ó7Ü Ÿ[™[÷ðð( �8Š8à�N‰N˜4Ÿ7™7Õ#ð rM   c                ó@   — | j                   j                  ||||«       y)z"Add rectangle to image (PIL-only).N)r»   ré   )rA   ÚxyrÜ   rÚ   rÙ   s        rD   ré   zAnnotator.rectangleà  s   € à�	‰	×Ñ˜B  g¨uÕ5rM   c           
     ó|  — | j                   rÙ| j                  j                  |«      \  }}|dk(  r|dxx   d|z
  z  cc<   |j                  d«      D ]‘  }|rW| j                  j                  |«      \  }}| j                  j                  |d   |d   |d   |z   dz   |d   |z   dz   f|¬«       | j                  j                  |||| j                  ¬«       |dxx   |z  cc<   Œ“ y|rŒt        j                  |d| j                  | j                  ¬«      d   \  }}|dz  }|d   |k\  }	|d   |z   |	r|d   |z
  n|d   |z   f}
t        j
                  | j                  ||
|d	t        j                  «       t        j                  | j                  ||d| j                  || j                  t        j                  ¬
«       y)aƒ  Add text to an image using PIL or cv2.

        Args:
            xy (list[int]): Top-left coordinates for text placement.
            text (str): Text to be drawn.
            txt_color (tuple, optional): Text color.
            anchor (str, optional): Text anchor position ('top' or 'bottom').
            box_color (tuple, optional): Box background color with optional alpha.
        ÚbottomrH   ú
r   rÛ   rÝ   rá   rd   rã   rÞ   N)r©   ri   rÀ   Úsplitr»   ré   rê   r°   rî   rÇ   rÆ   r·   rí   rï   )rA   r2  rê   rÕ   ÚanchorÚ	box_colorrõ   rS   r  rö   r÷   s              rD   rê   zAnnotator.textä  sŒ  € ð �8Š8Ø—9‘9×$Ñ$ TÓ*‰DˆAˆqØ˜Ò!Ø�1“˜˜Q™‘“ØŸ
™
 4Ó(ò �ÙàŸ9™9×,Ñ,¨TÓ2‘D�A�qØ—I‘I×'Ñ'¨¨A©°°1±°r¸!±u¸q±yÀ1±}ÀbÈÁeÈaÁiÐRSÁmÐ(TÐ[dÐ'ÔeØ—	‘	—‘˜r 4¨i¸d¿i¹i�ÔHØ�1“˜‘
”ññ Ü—‘ t¨Q¸$¿'¹'ÈTÏWÉWÔUÐVWÑX‘��1Ø�Q‘�Ø˜Q™% 1™*�Ø˜‘U˜Q‘Y©W  1¡¨¢	¸"¸Q¹%À!¹)ÐC�Ü—‘˜dŸg™g r¨2¨y¸"¼c¿k¹kÔJÜ�K‰K˜Ÿ™  r¨1¨d¯g©g°yÈDÏGÉGÔ^a×^iÑ^iÖjrM   c                ó¸   — t        |t        j                  «      r|nt        j                  |«      | _        t	        j
                  | j                  «      | _        y)z1Update `self.im` from a NumPy array or PIL image.N)r¨   r   r¶   r·   r   rº   r»   )rA   r·   s     rD   r¶   zAnnotator.fromarray  s5   € ä" 2¤u§{¡{Ô3‘"¼¿¹ÈÓ9LˆŒÜ—N‘N 4§7¡7Ó+ˆ�	rM   c                ó‚   — t        j                  | j                  «      }|rt        j                  |dddd…f   «      S |S )z-Return annotated image as array or PIL image..Nrã   )r=   rì   r·   r   r¶   )rA   r©   r·   s      rD   ÚresultzAnnotator.result  s6   € ä�Z‰Z˜Ÿ™Ó ˆÙ14Œu�‰˜r #¡t¨ t )™}Ó-Ð<¸"Ð<rM   c                ó*  — t        j                  t        j                  | j                  «      dddd…f   «      }t
        st        r	 t        |«       y|j                  |¬«       y# t        $ r"}t        j                  d|› �«       Y d}~yd}~ww xY w)zShow the annotated image..Nrã   z.Unable to display image in Jupyter notebooks: )Útitle)r   r¶   r=   rì   r·   r   r   ÚdisplayÚImportErrorr   ÚwarningÚshow)rA   r=  r·   Úes       rD   rA  zAnnotator.show  sx   € ä�_‰_œRŸZ™Z¨¯©Ó0°±d¸°d°Ñ;Ó<ˆÝ•yðUÜ˜•ð �G‰G˜%ˆGÕ øô ò UÜ—‘Ð!OÐPQÈsÐS×TÑTûðUús   ÁA' Á'	BÁ0BÂBc                ój   — t        j                  |t        j                  | j                  «      «       y)z'Save the annotated image to 'filename'.N)r°   Úimwriter=   rì   r·   )rA   Úfilenames     rD   ÚsavezAnnotator.save  s   € ä�‰�HœbŸj™j¨¯©Ó1Õ2rM   c                ó4   — | \  }}}}||z
  }||z
  }||||z  fS )an  Calculate the dimensions and area of a bounding box.

        Args:
            bbox (tuple | list): Bounding box coordinates in the format (x_min, y_min, x_max, y_max).

        Returns:
            width (float): Width of the bounding box.
            height (float): Height of the bounding box.
            area (float): Area enclosed by the bounding box.

        Examples:
            >>> from ultralytics.utils.plotting import Annotator
            >>> im0 = cv2.imread("test.png")
            >>> annotator = Annotator(im0, line_width=10)
            >>> annotator.get_bbox_dimension(bbox=[10, 20, 30, 40])
        r`   )ÚbboxÚx_minÚy_minÚx_maxÚy_maxrÙ   Úheights          rD   Úget_bbox_dimensionzAnnotator.get_bbox_dimension  s5   € ð$ &*Ñ"ˆˆu�e˜UØ˜‘ˆØ˜‘ˆØ�f˜e f™nÐ,Ð,rM   )NNz	Arial.ttfFÚabc)
rÎ   ú
int | NonerÏ   rP  ri   rZ   r©   rX   rÐ   rZ   )©r/   r/   r/   r6   )rÔ   rV   rÕ   rV   rY   rV   )Ú rQ  r6   )rñ   rZ   rÔ   rV   rÕ   rV   )Nrú   F)r  ztorch.Tensorr  r  r  rX   ))é€  rS  NTç      Ð?N)
r®   rV   r!  rP  r"  rX   r#  r  rË   ztuple | None)NNrH   )rÙ   rI   )r6   Útopr`   )rê   rZ   rÕ   rV   r7  rZ   r8  rV   rW   ©N)r=  z
str | None)z	image.jpg)rE  rZ   )rH  ztuple | list)r[   r\   r]   r^   rE   rÖ   rø   r  r   ré   rê   r¶   r;  rA  rF  r_   rN  r`   rM   rD   rb   rb   ¨   sé   „ ñð. "&Ø $ØØØðX
ð ðX
ð ð	X
ð
 ðX
ð ðX
ð óX
ôtô.;ôz0$ðj "Ø!ØØ Ø"&ðA$ð ðA$ð ð	A$ð
 ðA$ð ðA$ð  óA$ôF6ôkò<,ó
=ô
	!ô3ð ò-ó ñ-rM   rb   r`   rR  c           	     óX  — ddl m} ddl}ddlm} t        j                  d|dz  › d�«       t        |j                  «       dz   «      }| dd } |j                  | g d	¢¬
«      }	|j                  dddg«      }
|j                  dddd¬«      d   j                  «       }|d   j                  |t        j                  d||dz   «      dz
  d¬«      }t!        |«      D ]=  }|d   j"                  |   j%                  t'        |«      D �	cg c]  }	|	dz  ‘Œ	 c}	«       Œ? |d   j)                  d«       dt+        |«      cxk  rdk  ron nl|d   j-                  t!        t+        |«      «      «       |d   j/                  t1        |j3                  «       «      dd¬«       |d   j5                  |d   «       n|d   j7                  d«       t        j8                  d| dd…dd…f   dz  z
  d| dd…dd…f   dz  z   g«      dz  } t;        j<                  t        j>                  dt        j@                  ¬«      dz  «      }tC        |dd  | dd  «      D ]C  \  }}tE        jF                  |«      jI                  |jK                  «       dt'        |«      ¬!«       ŒE |d   jM                  |«       |d   jO                  d"«       |d   jQ                  	d#   |	d$   d%|
¬&«       |d   j7                  d#«       |d   j)                  d$«       |d'   jQ                  |	d(   |	d)   d%|
¬&«       |d'   j7                  d(«       |d'   j)                  d)«       d*D ]*  }d+D ]#  }||   jR                  |   jU                  d,«       Œ% Œ, |dz  }|jW                  |d-¬.«       |jY                  «        |r	 ||«       yyc c}	w )/a®  Plot training labels including class histograms and box statistics.

    Args:
        boxes (np.ndarray): Bounding box coordinates in format [x, y, width, height].
        cls (np.ndarray): Class indices.
        names (dict, optional): Dictionary mapping class indices to class names.
        save_dir (Path, optional): Directory to save the plot.
        on_plot (Callable, optional): Function to call after plot is saved.
    r   N)ÚLinearSegmentedColormapzPlotting labels to z
labels.jpgz... rH   i@B )rk   ÚyrÙ   rM  )ÚschemaÚ
white_blueÚwhiteÚbluerG   )rs   rs   T©ÚfigsizeÚtight_layoutrú   çš™™™™™é?)ÚbinsÚrwidthr.   Ú	instancesé   éZ   ru   )ÚrotationÚfontsizeÚclassesrU   éè  )rj  rj  rd   r7   iô  rØ   Úoffrk   rY  é2   )rb  Úcmaprd   rÙ   rM  >   r   rH   rG   rd   >   rU  ÚleftÚrightr4  FéÈ   ©Údpi)-Úmatplotlib.pyplotÚpyplotÚpolarsÚmatplotlib.colorsrX  r   ÚinforI   rª   Ú	DataFrameÚ	from_listÚsubplotsÚravelÚhistr=   ÚlinspaceÚrangeÚpatchesÚ	set_colorrÉ   Ú
set_ylabelr;   Ú
set_xticksÚset_xticklabelsrç   r  Ú	bar_labelÚ
set_xlabelÚcolumn_stackr   r¶   Úonesr?   Úzipr   rº   ré   ræ   ÚimshowÚaxisÚhist2dÚspinesÚset_visibleÚsavefigÚclose)ÚboxesÚclsÚnamesÚsave_dirÚon_plotÚpltru  rX  Úncrk   Úsubplot_3_4_colorÚaxrY  rJ   ÚimgÚclass_idrð   ÚaÚsÚfnames                       rD   Úplot_labelsrž  3  sj  € õ $ÛÝ9ô ‡K�KÐ% h°Ñ&=Ð%>¸dÐCÔDÜ	ˆS�W‰W‹Y˜‰]Ó	€BØ�(�7ˆO€EØ×Ñ˜Ò'DÐÓE€Að 0×9Ñ9¸,ÈÐRXÐHYÓZÐØ	�‰�a˜ F¸ˆÓ	>¸qÑ	A×	GÑ	GÓ	I€BØ
ˆ1‰�
‰
�3œRŸ[™[¨¨B°°Q±Ó7¸#Ñ=Àcˆ
ÓJ€AÜ�2‹Yò @ˆØ	ˆ!‰�‰�Q‰×!Ñ!´F¸1³IÖ">¨q 1 s£7Ò">Õ?ð@à€q�E×Ñ�[Ô!ØŒ3ˆu‹:Ô˜ÕØ
ˆ1‰×Ñœœs 5›zÓ*Ô+Ø
ˆ1‰×Ñœd 5§<¡<£>Ó2¸RÈ"ÐÔMØ
ˆ1‰�‰˜˜!™Õà
ˆ1‰×Ñ˜Ô#Ü�O‰O˜S 5ª¨A¨a¨C¨¡=°1Ñ#4Ñ4°c¸EÂ!ÀQÀqÀSÀ&¹MÈAÑ<MÑ6MÐNÓOÐRVÑV€EÜ
�/‰/œ"Ÿ'™' /¼¿¹ÔBÀSÑHÓ
I€CÜ˜S  #˜Y¨¨d¨s¨Ó4ò W‰ˆ�#Ü�‰�sÓ×%Ñ% c§j¡j£l¸!ÄVÈHÓEUÐ%ÕVðWà€q�E‡L�L�ÔØ€q�E‡J�JˆuÔà€q�E‡L�L��3‘˜˜3™ bÐ/@€LÔAØ€q�E×Ñ�SÔØ€q�E×Ñ�SÔØ€q�E‡L�L��7‘˜Q˜x™[¨rÐ8I€LÔJØ€q�E×Ñ�WÔØ€q�E×Ñ�XÔØò /ˆØ3ò 	/ˆAØˆq‰E�L‰L˜‰O×'Ñ'¨Õ.ñ	/ð/ð �|Ñ#€EØ‡K�K�˜3€KÔØ‡I�I„KÙÙ��ð ùò9 #?s   ÄN'
zim.jpggR¸…ëQð?ru   FTc                ó˜  — t        | t        j                  «      st        j                  | «      } t	        j
                  | j                  dd«      «      }|r5|dd…dd…f   j                  d«      d   j                  d«      |dd…dd…f<   |dd…dd…f   |z  |z   |dd…dd…f<   t	        j                  |«      j                  «       } t	        j                  | |j                  «      } |j                  d   dk(  }	|t        | d   «      t        | d   «      …t        | d	   «      t        | d
   «      …dd|s|	rdnd…f   }
|rˆ|j                  j                  dd¬«       t!        t#        |«      j%                  d«      «      }|	r|
j'                  d«      n|r
|
dddd…f   n|
}
t)        j*                  |
«      j-                  |dd¬«       |
S )aM  Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop.

    This function takes a bounding box and an image, and then saves a cropped portion of the image according to the
    bounding box. Optionally, the crop can be squared, and the function allows for gain and padding adjustments to the
    bounding box.

    Args:
        xyxy (torch.Tensor | list): A tensor or list representing the bounding box in xyxy format.
        im (np.ndarray): The input image.
        file (Path, optional): The path where the cropped image will be saved.
        gain (float, optional): A multiplicative factor to increase the size of the bounding box.
        pad (int, optional): The number of pixels to add to the width and height of the bounding box.
        square (bool, optional): If True, the bounding box will be transformed into a square.
        BGR (bool, optional): If True, the image will be returned in BGR format, otherwise in RGB.
        save (bool, optional): If True, the cropped image will be saved to disk.

    Returns:
        (np.ndarray): The cropped image.

    Examples:
        >>> from ultralytics.utils.plotting import save_one_box
        >>> xyxy = [50, 50, 150, 150]
        >>> im = cv2.imread("image.jpg")
        >>> cropped_im = save_one_box(xyxy, im, file="cropped.jpg", square=True)
    rã   rU   NrG   rH   r   )r   rH   )r   rd   )r   r   )r   rG   T)ÚparentsÚexist_okz.jpg.é_   )ÚqualityÚsubsampling)r¨   rä   rå   Ústackr   Ú	xyxy2xywhÚviewrª   r  Ú	xywh2xyxyÚlongÚ
clip_boxesr®   rI   ÚparentÚmkdirrZ   r   Úwith_suffixÚsqueezer   r¶   rF  )Úxyxyr·   ÚfileÚgainÚpadÚsquareÚBGRrF  ró   Ú	grayscaleÚcropÚfs               rD   Úsave_one_boxr¸  n  s‘  € ôF �dœEŸL™LÔ)Ü�{‰{˜4Ó ˆÜ�‰�d—i‘i  AÓ&Ó'€AÙØ’Q˜™�U‘8—<‘< “? 1Ñ%×/Ñ/°Ó2ˆŠ!ˆQ‰Rˆ%‰Ø’�A‘B�‰x˜$‰ Ñ$€A‚aˆ‰€e�HÜ�=‰=˜Ó× Ñ Ó"€DÜ�>‰>˜$ §¡Ó)€DØ—‘˜‘˜qÑ €IØŒc�$�t‘*‹o¤ D¨¡J£Ð/´°T¸$±Z³Ä3ÀtÈDÁzÃ?Ð1RÑTvÑ]`ÑdmÑXYÐsuÐTvÐvÑw€DÙØ�‰×Ñ $°ÐÔ6Ü”˜tÓ$×0Ñ0°Ó8Ó9ˆá#,ˆt�|‰|˜BÔÁS°$°s¹D¸b¸D°y²/ÈdˆÜ�‰˜Ó×"Ñ" 1¨b¸aÐ"Ô@Ø€KrM   )r   rd   rS  rS  r7   z
images.jpgi€  rP   rT  c
           	     ó  ‡/— dD ]w  }
|
| vrŒ|
dk(  r)| |
   j                   dk(  r| |
   j                  d«      | |
<   t        | |
   t        j                  «      sŒT| |
   j                  «       j                  «       | |
<   Œy | j                  dt        j                  dt        j                  ¬«      «      }| j                  dt        j                  |j                  t        j                  ¬«      «      }| j                  dt        j                  dt        j                  ¬«      «      }| j                  d	d
«      }| j                  dt        j                  dt        j                  ¬«      «      }| j                  dt        j                  dt        j                  ¬«      «      }| j                  d|«      }t        |«      rFt        |t        j                  «      r,|j                  «       j                  «       j                  «       }|j                  d   }|dk(  r8t        j                   |d
d
…d
d…f   «      }t        j"                  ||fd¬«      }n|dkD  r|d
d
…d
d…f   }|j                  \  }}}}t%        ||«      }t        j&                  |dz  «      Š/t        j(                  |d   «      dk  r|dz  }t        j*                  t-        ‰/|z  «      t-        ‰/|z  «      dfdt        j                  ¬«      }t/        |«      D ]L  }t-        ||‰/z  z  «      t-        ||‰/z  z  «      }}||   j1                  ddd«      ||||z   …|||z   …d
d
…f<   ŒN |‰/z  t)        ||«      z  }|dk  rZt3        j&                  ||z  «      }t3        j&                  ||z  «      }t5        j6                  |t9        ˆ/fd„||fD «       «      «      }t-        ||z   ‰/z  dz  «      }t)        |d«      }t;        |t=        |dz  «      |dt?        |«      ¬«      }t/        |«      D �]  }t-        ||‰/z  z  «      t-        ||‰/z  z  «      }}|jA                  ||||z   ||z   gd
dd¬«       |r5|jC                  |dz   |dz   gtE        ||   «      jF                  d
d d¬«       t        |«      dkD  sŒ‹||k(  }||   jI                  d«      }|d
u } |�||   nd
} t        |«      �rQ||   }!t        |!«      rU|!d
d
…d
d…f   j)                  «       d k  r#|!d!ddgfxx   |z  cc<   |!d!ddgfxx   |z  cc<   n|dk  r|!d!d
d…fxx   |z  cc<   |!d"xx   |z  cc<   |!d#xx   |z  cc<   |!j                  d$   dk(  }"|"rtK        jL                  |!«      ntK        jN                  |!«      }!tQ        |!jI                  t        j                  «      jS                  «       «      D ]Z  \  }#}$||#   }tU        |«      }%|r|j                  ||«      n|}| s	| |#   |	kD  sŒ7| r|› n
|› d%| |#   d&›�}&|jW                  |$|&|%¬'«       Œ\ nZt        |«      rO|D ]J  }tU        |«      }%|r|j                  ||«      n|}| r|› n
|› d%| d   d&›�}&|jC                  ||g|&|%d(¬)«       ŒL t        |«      rÃ||   jY                  «       }'t        |'«      rQ|'d"   j)                  «       d*k  s|'d#   j)                  «       d*k  r|'d"xx   |z  cc<   |'d#xx   |z  cc<   n
|dk  r|'|z  }'|'d"xx   |z  cc<   |'d#xx   |z  cc<   t/        t        |'«      «      D ]#  }#| s	| |#   |	kD  sŒ|j[                  |'|#   |	¬+«       Œ% t        |«      s�ŒC|j                  d   |j                  d   k(  r|j)                  «       dk  r||   }(nc||g   }(|j]                  «       })t        j^                  d|)dz   «      ja                  |)ddf«      }*|(|*k(  jI                  t        j                  «      }(t        jb                  |jd                  «      jY                  «       }+t/        t        |(«      «      D ]è  }#| s	| |#   |	kD  sŒtU        ||#   «      }%|(|#   j                  \  },}-|,|k7  s|-|k7  rP|(|#   jI                  t        j                  «      }.t5        j6                  |.||f«      }.|.jI                  tf        «      }.n|(|#   jI                  tf        «      }.	 |+|||z   …|||z   …d
d
…f   |.   d,z  t        jh                  |%«      d-z  z   |+|||z   …|||z   …d
d
…f   |.<   Œê |jm                  |+«       �Œ |st        jb                  |jd                  «      S |jd                  jo                  |«       |r	 ||«       y
y
# tj        $ r Y �ŒSw xY w).a¬  Plot image grid with labels, bounding boxes, masks, and keypoints.

    Args:
        labels (dict[str, Any]): Dictionary containing detection data with keys like 'cls', 'bboxes', 'conf', 'masks',
            'keypoints', 'batch_idx', 'img'.
        images (torch.Tensor | np.ndarray): Batch of images to plot. Shape: (batch_size, channels, height, width).
        paths (list[str] | None): List of file paths for each image in the batch.
        fname (str): Output filename for the plotted image grid.
        names (dict[int, str] | None): Dictionary mapping class indices to class names.
        on_plot (Callable | None): Callback function to be called after saving the plot.
        max_size (int): Maximum size of the output image grid.
        max_subplots (int): Maximum number of subplots in the image grid.
        save (bool): Whether to save the plotted image grid to a file.
        conf_thres (float): Confidence threshold for displaying detections.

    Returns:
        (np.ndarray | None): Plotted image grid as a numpy array if save is False, None otherwise.

    Notes:
        This function supports both tensor and numpy array inputs. It will automatically
        convert tensor inputs to numpy arrays for processing.

        Channel Support:
        - 1 channel: Grayscale
        - 2 channels: Third channel added as zeros
        - 3 channels: Used as-is (standard RGB)
        - 4+ channels: Cropped to first 3 channels
    >   r‘  r+  r  ÚbboxesÚimagesÚ	batch_idxÚ	keypointsr‘  rG   rH   r   r7   r¼  rº  r+  Nr  r½  r™  )rŠ  rd   rú   r.   c              3  ó:   •K  — | ]  }t        |‰z  «      –— Œ y ­wrV  rQ   )rR   rk   Únss     €rD   rT   zplot_images.<locals>.<genexpr>ù  s   øè ø€ Ò)F¸!¬#¨a°"©f¯+Ñ)Fùs   ƒg{®Gáz„?é   ru   T)rÎ   rÏ   r©   rÐ   r6   )rÙ   rw   é(   )éÜ   rÂ  rÂ  )rê   rÕ   rI   rU   gš™™™™™ñ?.).r   ).rH   rã   ú z.1f)rÔ   )é@   rÄ  rÄ  r/   )rÕ   r8  g)\�Âõ(ð?)r#  gš™™™™™Ù?g333333ã?)8r%  r®  r¨   rä   rå   r  r  Úgetr=   ÚzerosÚint64r®   r  r?   r;   r  rµ   ÚconcatenateÚminr  rª   ÚfullrI   r~  Ú	transposeÚmathr°   ÚresizerV   rb   r«   rZ   ré   rê   r   Únamer  r   Úxywhr2xyxyxyxyr¨  r   ræ   rÉ   rø   rÅ   r   r¬   ÚarangeÚreshaperì   r·   rX   r>   r½   r¶   rF  )0Úlabelsr»  Úpathsr�  r’  r”  Úmax_sizeÚmax_subplotsrF  r#  r'  r‘  r¼  rº  Úconfsr  r   rC   ÚzeroÚbsÚ_rS   rõ   ÚmosaicrJ   rk   rY  ÚscaleÚfsÚ	annotatorÚidxri  r+  r�  Úis_obbÚjrð   rÔ   rñ   Úkpts_Úimage_masksÚnlÚindexr·   ÚmhÚmwr  r¿  s0                                                  @rD   Úplot_imagesrç  ¤  sÕ  ø€ ðR Tò 0ˆØ�F‰?ØØ�Š:˜& ™)Ÿ.™.¨AÒ-Ø˜q™	×)Ñ)¨!Ó,ˆF�1‰IÜ�f˜Q‘i¤§¡Õ.Ø˜q™	Ÿ™›×-Ñ-Ó/ˆF�1ŠIð0ð �*‰*�UœBŸH™H Q¬b¯h©hÔ7Ó
8€CØ—
‘
˜;¬¯©°·±Ä"Ç(Á(Ô(KÓL€IØ�Z‰Z˜¤"§(¡(¨1´B·J±JÔ"?Ó@€FØ�J‰J�v˜tÓ$€EØ�J‰J�w¤§¡¨´"·(±(Ô ;Ó<€EØ�:‰:�k¤2§8¡8¨A´R·Z±ZÔ#@ÓA€DØ�Z‰Z˜˜vÓ&€Fä
ˆ6„{”z &¬%¯,©,Ô7Ø—‘“×#Ñ#Ó%×+Ñ+Ó-ˆð 	�‰�Q‰€AØˆA‚vÜ�}‰}˜V¢A r¨ r E™]Ó+ˆÜ—‘ ¨ °QÔ7‰Ø	
ˆQŠØš˜2˜A˜2˜‘ˆà—,‘,�K€Bˆˆ1ˆaÜ	ˆR�Ó	€BÜ	�‰��S‘Ó	€BÜ	‡v�vˆf�Q‰iÓ˜AÒØ�#‰ˆô �W‰W”c˜"˜q™&“k¤3 r¨A¡v£;°Ð2°C¼r¿x¹xÔH€FÜ�2‹Yò GˆÜ�1˜˜R™‘=Ó!¤3 q¨A°©F¡|Ó#4ˆ1ˆØ*0°©)×*=Ñ*=¸aÀÀAÓ*Fˆˆq�1�q‘5ˆy˜!˜a !™e˜)¢QÐ&Ò'ðGð
 �r‰MœC  1›IÑ%€EØˆq‚yÜ�I‰I�e˜a‘iÓ ˆÜ�I‰I�e˜a‘iÓ ˆÜ—‘˜F¤EÓ)FÀÀ1¸vÔ)FÓ$FÓGˆô 
ˆa�!‰e�r‰\˜DÑ Ó	!€BÜ	ˆR�‹€BÜ˜&¬U°2¸±7«^ÀrÈtÔ]`ÐafÓ]gÔh€IÜ�2‹Yó P(ˆÜ�1˜˜R™‘=Ó!¤3 q¨A°©F¡|Ó#4ˆ1ˆØ×Ñ˜Q  1 q¡5¨!¨a©%Ð0°$¸ÈqÐÔQÙØ�N‰N˜A ™E 1 q¡5˜>´°U¸1±X³×0CÑ0CÀCÀRÐ0HÐTcˆNÔdÜˆs‹8�a‹<Ø˜q‘.ˆCØ˜#‘h—o‘o eÓ,ˆGØ˜d�]ˆFØ!&Ð!2�5˜’:¸ˆDä�6�{Ø˜s™�Ü�u”:ØšQ   ˜U‘|×'Ñ'Ó)¨SÒ0Ø˜c A q 6˜kÓ*¨aÑ/Ó*Ø˜c A q 6˜kÓ*¨aÑ/Ô*Ø šØ˜c 2 A 2˜g›¨%Ñ/›Ø�f“ Ñ"“Ø�f“ Ñ"“ØŸ™ R™¨AÑ-�Ù5;œ×*Ñ*¨5Ô1ÄÇÁÈuÓAU�Ü'¨¯©´R·X±XÓ(>×(EÑ(EÓ(GÓHò E‘F�A�sØ ™
�AÜ" 1›I�EÙ+0˜Ÿ	™	 ! Qœ°a�AÙ  a¡¨:Ó!5Ù*0 1¡#¸¸¸1¸TÀ!¹WÀS¸MÐ6J˜Ø!×+Ñ+¨C°¸eÐ+ÕDñEô �W”Ø ò `�AÜ" 1›I�EÙ+0˜Ÿ	™	 ! Qœ°a�AÙ&,˜q™c°Q°C°q¸¸a¹À¸Ð2F�EØ—N‘N A q 6¨5¸EÐM^�NÕ_ð	`ô �4ŒyØ˜S™	Ÿ™Ó(�Ü�u”:Ø˜V‘}×(Ñ(Ó*¨dÒ2°e¸F±m×6GÑ6GÓ6IÈTÒ6QØ˜f›¨Ñ*›Ø˜f›¨Ñ*œØ šØ ™˜Ø�f“ Ñ"“Ø�f“ Ñ"“Üœs 5›zÓ*ò H�AÙ  a¡¨:Ó!5Ø!Ÿ™ u¨Q¡x¸J˜ÕGðHô
 �5ŽzØ—9‘9˜Q‘< 5§;¡;¨q¡>Ò1°e·i±i³kÀQÒ6FØ"'¨¡*‘Kà"'¨¨¡*�KØŸ™›�BÜŸI™I a¨¨a©Ó0×8Ñ8¸"¸aÀ¸ÓD�EØ#.°%Ñ#7×"?Ñ"?ÄÇ
Á
Ó"K�Kä—Z‘Z 	§¡Ó-×2Ñ2Ó4�Üœs ;Ó/Ó0ò !�AÙ  a¡¨:Ó!5Ü & w¨q¡zÓ 2˜Ø!,¨Q¡×!5Ñ!5™˜˜BØ š7 b¨A¢gØ#.¨q¡>×#8Ñ#8¼¿¹Ó#B˜DÜ#&§:¡:¨d°Q¸°FÓ#;˜DØ#'§;¡;¬tÓ#4™Dà#.¨q¡>×#8Ñ#8¼Ó#>˜Dð!à " 1 q¨1¡u 9¨a°!°a±%¨iºÐ#:Ñ ;¸DÑ AÀCÑ GÌ"Ï(É(ÐSXË/Ð\_ÑJ_Ñ _ð ˜q 1 q¡5˜y¨!¨a°!©e¨)²QÐ6Ñ7¸Ò=ð!ð  ×#Ñ# BÖ'ðaP(ñb Ü�z‰z˜)Ÿ,™,Ó'Ð'Ø‡L�L×Ñ�eÔÙÙ��ð øô  )ò !Ú ð!ús   ãAe9å9	fæfc           
     ót  — ddl m} ddl}ddlm} | rt        | «      j                  n
t        |«      }t        |j                  d«      «      }t        |«      sJ d|j                  «       › d�«       ‚g g }	}d\  }
}t        |«      D �]µ  \  }}	 |j                  |d¬«      }|dk(  r²|j                  D ].  }d	|v r|j                  |«       Œd
|v sŒ|	j                  |«       Œ0 t        |«      dz  t        |	«      dz  }}|d| |	d| z   ||d z   |	|d z   }|j                  dt        |«      dz  t        |«      dz   dfd¬«      \  }
}|j!                  «       }|j#                  |j                  d   «      j%                  «       j'                  «       }t        «      D ]œ  \  }}|j#                  |«      j%                  «       j'                  «       j)                  d«      }||   j+                  ||d|j,                  dd¬«       ||   j+                  | ||d¬«      ddd¬«       ||   j/                  |d¬«       Œž �Œ¸ |�G|d   j7                  «        |dz  }|
j9                  |d¬«       |j;                  «        |r	 ||«       yyy# t0        $ r&}t3        j4                  d|› d|› �«       Y d}~�Œ-d}~ww xY w) a¤  Plot training results from a results CSV file. The function supports various types of data including
    segmentation, pose estimation, and classification. Plots are saved as 'results.png' in the directory where the
    CSV is located.

    Args:
        file (str, optional): Path to the CSV file containing the training results.
        dir (str, optional): Directory where the CSV file is located if 'file' is not provided.
        on_plot (Callable, optional): Callback function to be executed after plotting. Takes filename as an argument.

    Examples:
        >>> from ultralytics.utils.plotting import plot_results
        >>> plot_results("path/to/results.csv")
    r   N©Úgaussian_filter1dzresults*.csvzNo results.csv files found in z, nothing to plot.)NN)Úinfer_schema_lengthÚlossÚmetricrG   rq   Tr^  r  ú.rs   )Úmarkerrñ   Ú	linewidthÚ
markersizerd   ©Úsigmaú:Úsmooth)rñ   rð  rg   )rh  zPlotting error for z: rH   zresults.pngrp  rq  )rs  rt  ru  Úscipy.ndimagerê  r   r«  rç   Úglobr;   Úresolver   Úread_csvÚcolumnsÚappendrz  r{  ÚselectÚto_numpyÚflattenr  ÚplotÚstemÚ	set_titler½   r   ÚerrorÚlegendrŽ  r�  )r°  Údirr”  r•  Úplrê  r“  ÚfilesÚ	loss_keysÚmetric_keysÚfigr˜  rJ   r·  rÁ   rC   Úloss_midÚ
metric_midrú  rk   rà  rY  rB  r�  s                           rD   Úplot_resultsr  W  s¶  € õ $ÛÝ/á$(Œt�D‹z× Ò ¬d°3«i€HÜ�—‘˜~Ó.Ó/€EÜˆuŒ:Ð^Ð7¸×8HÑ8HÓ8JÐ7KÐK]Ð^Ó^ˆ:à ˆ{€IØ�G€CˆÜ˜%Ó ó 9‰ˆˆ1ð	9Ø—;‘;˜q°d�;Ó;ˆDØ�AŠvØŸ™ò .�AØ ‘{Ø!×(Ñ(¨Õ+Ø! QšØ#×*Ñ*¨1Õ-ð	.ô
 (+¨9£~¸Ñ':¼CÀÓ<LÐPQÑ<Q˜*�à˜i˜xÐ(¨;°{¸
Ð+CÑCÀiÐPXÐPYÐFZÑZÐ]hÐisÐitÐ]uÑuð ð Ÿ,™, q¬#¨g«,¸!Ñ*;ÄcÈ'ÃlÐUVÑFVÐXYÐEZÐim˜,Ón‘��RØ—X‘X“Z�Ø—‘˜DŸL™L¨™OÓ,×5Ñ5Ó7×?Ñ?ÓAˆAÜ! 'Ó*ò 0‘��1Ø—K‘K “N×+Ñ+Ó-×5Ñ5Ó7×>Ñ>¸wÓG�Ø�1‘—
‘
˜1˜a¨°1·6±6ÀQÐST�
ÔUØ�1‘—
‘
˜1Ñ/°¸Ô;¸SÈÐ\]�
Ô^Ø�1‘—‘ ¨B�Õ/ò	0ð!9ð. 
€~Ø
ˆ1‰�‰ŒØ˜=Ñ(ˆØ�‰�E˜sˆÔ#Ø�	‰	ŒÙÙ�E�Nð ð øô ò 	9Ü�L‰LÐ.¨q¨c°°A°3Ð7×8Ò8ûð	9ús    ÂAJÃE,JÊ	J7ÊJ2Ê2J7c                ó¤  — ddl m} t        j                  | ||¬«      \  }}}	t	        t        | «      «      D �
cg c]u  }
|t        t        j                  | |
   |d¬«      dz
  |j                  d   dz
  «      t        t        j                  ||
   |	d¬«      dz
  |j                  d   dz
  «      f   ‘Œw }}
|j                  | |||||¬«       yc c}
w )a)  Plot a scatter plot with points colored based on a 2D histogram.

    Args:
        v (array-like): Values for the x-axis.
        f (array-like): Values for the y-axis.
        bins (int, optional): Number of bins for the histogram.
        cmap (str, optional): Colormap for the scatter plot.
        alpha (float, optional): Alpha for the scatter plot.
        edgecolors (str, optional): Edge colors for the scatter plot.

    Examples:
        >>> v = np.random.rand(100)
        >>> f = np.random.rand(100)
        >>> plt_color_scatter(v, f)
    r   N)rb  T)ro  rH   )rC   rm  r  Ú
edgecolors)
rs  rt  r=   Úhistogram2dr~  r;   rÉ  Údigitizer®   Úscatter)Úvr·  rb  rm  r  r  r•  r|  ÚxedgesÚyedgesrJ   rÉ   s               rD   Úplt_color_scatterr  �  sÏ   € õ  $ô Ÿ>™>¨!¨Q°TÔ:Ñ€Dˆ&�&ô ”s˜1“v“öð
 ð	 	Ü”—‘˜A˜a™D &°Ô5¸Ñ9¸4¿:¹:Àa¹=È1Ñ;LÓMÜ”—‘˜A˜a™D &°Ô5¸Ñ9¸4¿:¹:Àa¹=È1Ñ;LÓMðOó	
ð€Fð ð ‡K�K��1˜ T°À:€KÕNùòs   ¹A:Cc                óþ  ‡— ddl }ddlmŠ ddlm} ˆfd„}t        | «      } t        | d¬«      5 }|D �cg c]$  }|j                  «       sŒ|j                  |«      ‘Œ& }}ddd«       syt        |d   j                  di «      «      }t        j                  |D �	�
cg c]T  }	|	j                  dd	«      g|D �
cg c]2  }
|	j                  di «      j                  |
t        j                  «      ‘Œ4 c}
z   ‘ŒV c}
}	t        ¬
«      }t        |«       |dd…df   }t!        d«      }|r|dkD  }||   ||   }}t        |«      dk(  rt#        j$                  d«       y|j'                  «       }t)        d«      D ]K  }|j+                  «       |j-                  «       }}|d|z  z
  }||k\  }|j/                  «       r n||   ||   }}ŒM t        j0                  |«      }t3        j4                  t        |«      dz  «      }‰j7                  dd¬«       t9        |«      D ]§  \  }}
|dd…|dz   f   }||   }‰j;                  |||dz   «       t=        ||ddd¬«       ‰j?                  ||jA                  «       dd¬«       ‰jC                  |
› d|d›�ddi¬«       ‰jE                  dd¬ «       ||z  dk7  sŒ—‰jG                  g «       Œ©  || jI                  d!«      «       t)        dt        |«      dz   «      }‰j7                  d"d¬«       tK        |D �	�
ch c]  }	|	j                  d#i «      D ]  }
|
’Œ Œ c}
}	«      D ]—  }t        j                  |D �	cg c]B  }	|	j                  d#i «      j                  |i «      j                  dt        j                  «      ‘ŒD c}	t        ¬
«      }|rtM        |t         «      s||   }‰j?                  ||d$d%d|¬&«       Œ™ ‰j?                  | ||d¬'«      d(d)d*d+¬,«       ‰jC                  d-«       ‰jO                  d.«       ‰jQ                  d/«       ‰jS                  d«       ‰jU                  «         || jI                  d0«      «       yc c}w # 1 sw Y   �ŒÿxY wc c}
w c c}
}	w c c}
}	w c c}	w )1aa  Plot the evolution results stored in a tuning NDJSON file.

    Args:
        results_file (str, optional): Path to the NDJSON file containing the tuning results.
        exclude_zero_fitness_points (bool, optional): Don't include points with zero fitness in tuning plots.

    Examples:
        >>> plot_tune_results("path/to/tune_results.ndjson")
    r   Nré  c                ó|   •— ‰j                  | d¬«       ‰j                  «        t        j                  d| › �«       y)z#Save one matplotlib plot to 'file'.rp  rq  zSaved N)rŽ  r�  r   rw  )r°  r•  s    €rD   Ú_save_one_filez)plot_tune_results.<locals>._save_one_fileÀ  s.   ø€ à�‰�D˜cˆÔ"Ø�	‰	ŒÜ�‰�f˜T˜F�OÕ$rM   zutf-8)ÚencodingÚhyperparametersÚfitnessg        r7   z@No valid fitness values to plot (all iterations may have failed)rd   rú   )ru   ru   Tr^  rH   Úviridisra  Únone)rm  r  r  zk+ro   )rñ  z = z.3gr­   rt   )ÚfontdictÚbothrs   )rŠ  Ú	labelsizeztune_scatter_plots.png)ru   rq   ÚdatasetsÚorw   )rñ  r  rñ   rò  rô  z0.35zsmoothed meanrG   )rÔ   rñ   rð  zFitness vs IterationÚ	IterationÚFitnessztune_fitness.png)+Újsonrs  rt  rö  rê  r   ÚopenÚstripÚloadsrç   rÅ  r=   r>   Únanr  r;   Úslicer   r@  rÅ   r~  ÚmeanÚstdÚallÚargmaxrÌ  r  Úfigurer   Úsubplotr  rÿ  rª   r=  Útick_paramsÚyticksÚ	with_nameÚsortedr¨   ÚxlabelÚylabelÚgridr  )Úresults_fileÚexclude_zero_fitness_pointsr%  rê  r  r·  r  ÚrecordsÚkeysÚrr'  rk   Úall_fitnessÚ	zero_maskr  rÙ  r+  r,  Úlower_boundr  rà  r<   rJ   r  ÚmuÚdatasetrY  r•  s                              @rD   Úplot_tune_resultsrB  °  s
  ø€ ó å#Ý/ô%ô ˜Ó%€LÜ	ˆl WÔ	-ð C°Ø01ÖB¨°T·Z±Zµ\�4—:‘:˜dÕ#ÐBˆÐB÷CáØä�˜‘
—‘Ð0°"Ó5Ó6€DÜ
�‰Øho×pÐcdˆ!�%‰%�	˜3Ó
Ð	 ÐY]Ö#^ÐTU A§E¡EÐ*;¸RÓ$@×$DÑ$DÀQÌÏÉÕ$OÒ#^Ó	^ÓpÜô	€Aô ˆ„FØ’A�q�D‘'€KÜ�d“€IÙ"Ø !‘Oˆ	Ø˜9™ {°9Ñ'=ˆ;ˆÜ
ˆ;Ó˜1ÒÜ�‰ÐYÔZØØ×ÑÓ €Gä�1‹Xò ,ˆØ—L‘L“N G§K¡K£MˆcˆØ˜Q ™W‘nˆØ˜+Ñ%ˆØ�8‰8Œ:ÙØ�t‘W˜g d™mˆ7‰ð,ô 	�	‰	�'Ó€AÜ�	‰	”#�d“)˜sÑ"Ó#€AØ‡J�J�x¨d€JÔ3Ü˜$“ò 	‰ˆˆ1ØŠa��Q‘ˆh‰KˆØˆq‰TˆØ�‰�A�q˜!˜a™%Ô Ü˜!˜W¨9¸CÈFÕSØ�‰��W—[‘[“] D°RˆÔ8Ø�	‰	�Q�C�s˜2˜c˜(Ð#¨v°q¨kˆ	Ô:Ø�‰˜V¨qˆÔ1Øˆq‰5�A‹:Ø�J‰J�r�Nð	ñ �<×)Ñ)Ð*BÓCÔDô 	ˆa”�[Ó! AÑ%Ó&€AØ‡J�J�w¨T€JÔ2Ü g×M °q·u±u¸ZÈÓ7LÒM°!š1ÐM˜1ÓMÓNò DˆÜ�H‰HÐ]dÖeÐXY�a—e‘e˜J¨Ó+×/Ñ/°¸Ó<×@Ñ@ÀÌBÏFÉFÕSÒeÔmrÔsˆÙ&¬z¸)ÄUÔ/KØ�)‘ˆAØ�‰��A�s q°¸7ˆÕCð	Dð
 ‡H�HˆQÑ! +°QÔ7¸ÀFÐRaÐmn€HÔoØ‡I�IÐ$Ô%Ø‡J�Jˆ{ÔØ‡J�JˆyÔØ‡H�HˆT„NØ‡J�J„LÙ�<×)Ñ)Ð*<Ó=Õ>ùòq C÷Cñ Cüò $_ùÓpùóN NùÚesG   ¯Q´QÁ
QÁQÂQ.
Â77Q)Ã.Q.
Ì
 Q4
ÍAQ:
ÑQÑQ&Ñ)Q.
é    zruns/detect/expc           	     óš  — ddl m} dD ]  }||v sŒ y t        | t        j                  «      �r�| j
                  \  }}}	}
|	dkD  �r…|
dkD  �r~|d|› d|j                  dd«      d   › d	�z  }t        j                  | d   j                  «       |d¬
«      }t        ||«      }|j                  t        j                  |dz  «      dd¬«      \  }}|j                  «       }|j                  dd¬«       t        |«      D ];  }||   j!                  ||   j#                  «       «       ||   j%                  d«       Œ= t'        j(                  d|› d|› d|› d�«       |j+                  |dd¬«       |j-                  «        t/        j0                  t3        |j5                  d«      «      | d   j                  «       j7                  «       «       yyyy)ab  Visualize feature maps of a given model module during inference.

    Args:
        x (torch.Tensor): Features to be visualized.
        module_type (str): Module type.
        stage (int): Module stage within the model.
        n (int, optional): Maximum number of feature maps to plot.
        save_dir (Path, optional): Directory to save results.
    r   N>   ÚOBBÚPoseÚDetectÚSegmentÚClassifyÚRTDETRDecoderrH   ÚstagerÙ  rî  rã   z_features.pngrü   rs   T)r`  gš™™™™™©?)ÚwspaceÚhspacerk  zSaving z... (ú/ú)i,  Útight)rr  Úbbox_inchesz.npy)rs  rt  r¨   rä   rå   r®   ÚrsplitÚchunkr  rÉ  rz  rÌ  r  r{  Úsubplots_adjustr~  r‰  r®  rŠ  r   rw  rŽ  r�  r=   rF  rZ   r­  r  )rk   Úmodule_typerK  r<   r“  r•  ÚmrÙ  ÚchannelsrM  rÙ   r·  Úblocksr˜  rJ   s                  rD   Úfeature_visualizationrY    s   € õ $àNò ˆØ�ÒÙðô �!”U—\‘\Õ"Ø%&§W¡WÑ"ˆˆ8�V˜UØ�A‹:˜% !›)Ø˜U 5 '¨¨;×+=Ñ+=¸cÀ1Ó+EÀbÑ+IÐ*JÈ-ÐXÑXˆAä—[‘[  1¡§¡£¨X¸1Ô=ˆFÜ�A�xÓ ˆAØ—L‘L¤§¡¨1¨q©5Ó!1°1À4�LÓH‰EˆAˆrØ—‘“ˆBØ×Ñ t°DÐÔ9Ü˜1“Xò "�Ø�1‘—‘˜V A™Y×.Ñ.Ó0Ô1Ø�1‘—
‘
˜5Õ!ð"ô �K‰K˜' !  E¨!¨¨A¨h¨Z°qÐ9Ô:Ø�K‰K˜˜s°ˆKÔ8Ø�I‰IŒKÜ�G‰G”C˜Ÿ™ fÓ-Ó.°°!±·±³
×0@Ñ0@Ó0BÕCð $ˆ:ð #rM   )r°  r   r±  r  r²  rI   r³  rX   r´  rX   rF  rX   )rÒ  zdict[str, Any]r»  ztorch.Tensor | np.ndarrayrÓ  zlist[str] | Noner�  rZ   r’  zdict[int, str] | Noner”  úCallable | NonerÔ  rI   rÕ  rI   rF  rX   r#  r  rY   znp.ndarray | None)zpath/to/results.csvrR  N)r°  rZ   r  rZ   r”  rZ  )é   r  ra  r  )rb  rI   rm  rZ   r  r  r  rZ   )ztune_results.ndjsonT)r8  rZ   r9  rX   )rU  rZ   rK  rI   r<   rI   r“  r   )-Ú
__future__r   rÌ  Úcollections.abcr   Úpathlibr   Útypingr   r°   r  r=   rä   ÚPILr   r   r	   r
   r¿   Úultralytics.utilsr   r   r   r   r   r   r   Úultralytics.utils.checksr   r   r   Úultralytics.utils.filesr   r   rÉ   rb   rž  r¸  rÆ  r  rç  r  r  rB  rY  r`   rM   rD   ú<module>rd     sû  ðõ #ã Ý $Ý Ý ã 
Û Û ß +Ñ +Ý *ç a× aÑ aß HÑ HÝ 2÷MGñ MGñ` 
‹€÷H-ñ H-ñV ƒÙƒØ"$©t°B«xÀò 6ó ó ð6ñx �h“ØØØØØð3ð ð3ð ð	3ð
 
ð3ð ð3ð 
ð3ð ó3ðl 
ð )1¨¯©Ð1AÈÏÉÔ(TØ"ØØ#'Ø#ØØØØðoØðoà%ðoð ðoð ð	oð
 !ðoð ðoð ðoð ðoð ðoð ðoð òoó 
ðoñd ƒó5ó ð5ôpOñ@ ƒóO?ó ðO?ñd ƒØDFÑY]Ð^oÓYpó  Dó ñ DrM   