§
    l¬¸j?
 ã                  óT  — 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	 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! d dl"m#Z# d€d�d„Z$ G d„ d¦  «        Z% e%¦   «         Z& ej'        g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d ¢g d!¢gej(        ¬"¦  «        Z)d‚d#„Z* e*¦   «         Z+e
j,        e
j-        dd$œZ.	 	 	 	 dƒd„d.„Z/ G d/„ d0¦  «        Z0 e¦   «          e¦   «         d1 ed2¦  «        dfd3„¦   «         ¦   «         Z1 ed4¦  «        d5d6d7d7d8fd…dA„Z2e	 	 	 	 	 	 	 	 	 	 	 d†d‡dW„¦   «         Z3 e¦   «         dˆd‰dZ„¦   «         Z4 e¦   «         d[ e¦   «         fdŠd_„¦   «         Z5d‹dŒdg„Z6	 	 	 d�dŽds„Z7 e¦   «         d�d�dw„¦   «         Z8dEddDdxœd‘d„Z9dS )’é    )Ú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_path)Ú
TORCH_1_10é   ç      @ÚsigmaÚintÚtruncateÚfloatÚreturnú
np.ndarrayc                óP  — t          j        | t          ¬¦  «        } t          ||z  dz   ¦  «        }t          j        dt          j        | |dz   ¦  «        |z  dz  z  ¦  «        }||                     ¦   «         z  }t          j        t          j        | |d¬¦  «        |d¬¦  «        S )	aŒ  Smooth a 1D array with a Gaussian kernel (NumPy replacement for scipy.ndimage.gaussian_filter1d).

    Args:
        y (np.ndarray): Input 1D array to smooth.
        sigma (int): Standard deviation of the Gaussian kernel.
        truncate (float): Truncate the kernel at this many standard deviations.

    Returns:
        (np.ndarray): Smoothed 1D array with the same length as the input.
    ©Údtypeç      à?g      à¿é   é   Ú	symmetric©ÚmodeÚvalid)	ÚnpÚasarrayr   r   ÚexpÚarangeÚsumÚconvolveÚpad)Úyr   r   ÚradiusÚkernels        új/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/utils/plotting.pyÚ_gaussian_filter1dr4      s™   € õ 	Œ
�1�EÐ"Ñ"Ô"€AÝ�˜EÑ! CÑ'Ñ(Ô(€FÝŒV�D�BœI v g¨v¸©zÑ:Ô:¸UÑBÀqÑHÑHÑIÔI€FØ
ˆf�jŠj‰lŒlÑ€FåŒ;•r”v˜a ¨kÐ:Ñ:Ô:¸FÈÐQÑQÔQÐQó    c                  ó:   — e Zd ZdZd„ Zddd
„Zedd„¦   «         ZdS )Ú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}ˆ fd„|D ¦   «         ‰ _         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        ¬¦  «        ‰ _        dS )zNInitialize the Ultralytics color palette from a fixed list of hex color codes.)Ú042AFFÚ0BDBEBÚF3F3F3Ú00DFB7Ú111F68ÚFF6FDDÚFF444FÚCCED00Ú00F344ÚBD00FFÚ00B4FFÚDD00BAÚ00FFFFÚ26C000Ú01FFB3Ú7D24FFÚ7B0068ÚFF1B6CÚFC6D2FÚA2FF0Bc                ó@   •— g | ]}‰                      d |› �¦  «        ‘ŒS )ú#)Úhex2rgb)Ú.0ÚcÚselfs     €r3   ú
<listcomp>z#Colors.__init__.<locals>.<listcomp>‹   s+   ø€ Ð<Ð<Ð<°!˜Ÿš W¨ W WÑ-Ô-Ð<Ð<Ð<r5   )éÿ   é€   r   )rT   é™   é3   )rT   é²   éf   )éæ   rZ   r   )rT   rV   rT   )rV   éÌ   rT   )rT   rY   rT   )rT   rW   rT   )rY   rX   rT   )rW   rV   rT   )rT   rV   rV   )rT   rY   rY   )rT   rW   rW   )rV   rT   rV   )rY   rT   rY   )rW   rT   rW   )r   rT   r   )r   r   rT   )rT   r   r   ©rT   rT   rT   r    N)ÚpaletteÚlenÚnr)   ÚarrayÚuint8Úpose_palette)rR   Úhexss   ` r3   Ú__init__zColors.__init__s   sï   ø€ ð
ˆð, =Ð<Ð<Ð<°tÐ<Ñ<Ô<ˆŒÝ�T”\Ñ"Ô"ˆŒÝœHà��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��Ø��ð)õ, ”(ð/
ñ 
ô 
ˆÔÐÐr5   FÚiúint | torch.TensorÚbgrÚboolr   Útuplec                ó|   — | j         t          |¦  «        | j        z           }|r|d         |d         |d         fn|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#   r   )r]   r   r_   )rR   re   rg   rQ   s       r3   Ú__call__zColors.__call__§   s?   € ð ŒL�˜Q™œ $¤&™Ô)ˆØ%(Ð/��!”�a˜”d˜A˜aœDÐ!Ð!¨aÐ/r5   ÚhÚstrc                ó:   ‡ — t          ˆ fd„dD ¦   «         ¦  «        S )z?Convert hex color codes to RGB values (i.e. default PIL order).c              3  óZ   •K  — | ]%}t          ‰d |z   d |z   dz   …         d¦  «        V — Œ&dS )r#   r$   é   N©r   )rP   re   rl   s     €r3   ú	<genexpr>z!Colors.hex2rgb.<locals>.<genexpr>·   sC   øè è € ÐFÐF°q•S˜˜1˜q™5 1 q¡5¨1¡9Ð,Ô-¨rÑ2Ô2ÐFÐFÐFÐFÐFÐFr5   )r   r$   é   ©ri   )rl   s   `r3   rO   zColors.hex2rgb´   s(   ø€ õ ÐFÐFÐFÐF¸IÐFÑFÔFÑFÔFÐFr5   N©F)re   rf   rg   rh   r   ri   )rl   rm   r   ri   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rd   rk   ÚstaticmethodrO   © r5   r3   r7   r7   *   sp   € € € € € ðFð FðP2
ð 2
ð 2
ðh0ð 0ð 0ð 0ð 0ð ðGð Gð Gñ „\ðGð Gð Gr5   r7   )é^   éO   é¢   )rW   é‡   é¼   )rY   éÂ   é¥   )éª   éÜ   é¤   )rZ   éõ   é˜   )rT   éþ   é¾   )rˆ   éà   é‹   )éý   é­   é`   )éô   ém   éC   )éÔ   é=   r}   )éž   r#   éB   r    c                 óœ  — t          j        ddd¦  «        } t          j        |                      t          ¦  «        dd¦  «        }| |z
  dd…df         }t
          |         d|z
  z  t
          |dz            |z  z   }|                     ¦   «                              t           j        ¦  «        dd…ddd	…f                              ddd
¦  «        S )zgBuild the 256x1x3 BGR uint8 Spectral_r LUT for cv2.applyColorMap by linearly interpolating the anchors.ç        g      $@é   r   é	   Nç      ð?r#   éÿÿÿÿr   )	r)   ÚlinspaceÚclipÚastyper   Ú_SPECTRAL_R_ANCHORSÚroundra   Úreshape)Úxsre   ÚfÚrgbs       r3   Ú_spectral_lutr¥   Ð   s²   € å	Œ�S˜$ Ñ	$Ô	$€BÝ
Œ�—	’	�#‘”  1Ñ%Ô%€AØ	ˆa‰����D�Ô€AÝ
˜aÔ
  C¨!¡GÑ
,Õ/BÀ1ÀqÁ5Ô/IÈAÑ/MÑ
M€CØ�9Š9‰;Œ;×Ò�bœhÑ'Ô'¨¨¨¨4¨4¨R¨4¨Ô0×8Ò8¸¸aÀÑCÔCÐCr5   )ÚinfernoÚjetÚspectralr§   Ú	disparityÚdepthÚvminúfloat | NoneÚvmaxÚcmaprm   r'   c           	     ó  — t          j        | t           j        ¬¦  «        }|dk    }|dk    r-t          j        |dt          j        ||d¦  «        z  d¦  «        n|}|�|€Š||         }|dk    r"|j        rt          j        |d¦  «        nd\  }	}
nN|j        rBt          |                     ¦   «         ¦  «        t          |                     ¦   «         ¦  «        fnd\  }	}
|€|	n|}|€|
n|}||k    r|d	z   }t          j	        ||z
  ||z
  z  dd¦  «        }|d
z   
                    t           j        ¦  «        }|dk    rt          nd}|�t          j        ||¦  «        nt          j        |t          |         ¦  «        }d|| <   |S )aŽ  Map a (H, W) metric-depth array to a BGR uint8 colorized image, invalid (<= 0) pixels black.

    Args:
        depth (np.ndarray): (H, W) depth in meters.
        vmin (float, optional): Lower bound of the color range; defaults to the valid-pixel minimum (metric mode) or the
            2nd disparity percentile (disparity mode).
        vmax (float, optional): Upper bound of the color range; defaults to the valid-pixel maximum (metric mode) or the
            98th disparity percentile (disparity mode).
        cmap (str): Colormap, one of "inferno", "jet", "spectral" (matplotlib Spectral_r, near = warm).
        mode (str): "metric" normalizes depth linearly; "disparity" normalizes inverse depth (1/d) between the 2nd and
            98th percentiles for the DepthAnything look (near objects warm, robust to far outliers).

    Returns:
        (np.ndarray): (H, W, 3) BGR uint8 colorized depth.
    r    r   r©   rš   r—   N)r$   éb   ©r—   rš   g�íµ ÷Æ°>rT   r¨   )r)   r*   Úfloat32ÚwhereÚsizeÚ
percentiler   ÚminÚmaxr�   rž   ra   Ú_SPECTRAL_LUTÚcv2ÚapplyColorMapÚ_DEPTH_CMAPS)rª   r«   r­   r®   r'   Údr(   ÚvÚpoolÚloÚhiÚdnÚidxÚlutÚcolors                  r3   Úcolorize_depthrÅ   Ý   s‹  € õ, 	Œ
�5¥¤
Ð+Ñ+Ô+€AØ�ŠE€EØ?CÀ{Ò?RÐ?R�Œ�˜�bœh u¨a°Ñ5Ô5Ñ5°sÑ;Ô;Ð;ÐXY€AØ€|�t�|Ø�ŒxˆØ�;ÒÐØ59´YÐN•R”] 4¨Ñ1Ô1Ð1ÀJ‰FˆB��à?C¼yÐX•e˜DŸHšH™JœJÑ'Ô'­¨t¯xªx©z¬zÑ):Ô):Ð;Ð;Èj‰FˆB�Ø�\ˆrˆr tˆØ�\ˆrˆr tˆØˆt‚|€|Ø�d‰{ˆÝ	Œ�!�d‘(˜t d™{Ñ+¨S°#Ñ	6Ô	6€BØ�‰8×
Ò
�BœHÑ
%Ô
%€CØ :Ò-Ð-�-ˆ-°4€CØ+.¨?�CÔ˜c 3Ñ'Ô'Ð'ÅÔ@QÐRUÕWcÐdhÔWiÑ@jÔ@j€EØ€Eˆ5ˆ&�MØ€Lr5   c                  óÆ   — e Zd ZdZ	 	 	 	 	 dIdJd„ZdKdLd„ZdMdNd„ZdOdPd„ZdQdRd „Z	 	 	 dSdTd)„Z		 	 	 	 	 dUdVd3„Z
dWdXd6„ZdYdZd<„Zd=„ Zd>„ Zd[d?„Zd\d]dB„Zd^d_dE„Zed`dH„¦   «         ZdS )aÚ	Annotatora  Ultralytics Annotator for train/val mosaics and JPGs and predictions annotations.

    Tensor images must be contiguous HWC BGR uint8.

    Attributes:
        im (Image.Image | np.ndarray | torch.Tensor): 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))
    Nú	Arial.ttfFÚabcÚ
line_widthú
int | NoneÚ	font_sizeÚfontrm   Úpilrh   Úexamplec                óL  ‡ — t          |¦  «         }t          |t          j        ¦  «        }t          |t          j        ¦  «        }	|p|p|‰ _        |p=t          t          t          |r|j	        n|j
        ¦  «        dz  dz  ¦  «        d¦  «        ‰ _        |	r˜|j        dk    r&|j
        d         dk    r|j        t          j        k    s(J dt          |j
        ¦  «        › d|j        › d�¦   «         ‚‰ j        s|j        j        dk    r(|                     ¦   «                              ¦   «         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k    rt/          j        |d
dd…f         ¦  «        }‰ j        �r.|r|nt          j        |¦  «        ‰ _        ‰ j        j        dvr‰ j                             d¦  «        ‰ _        t?          j         ‰ j        d¦  «        ‰ _!        	 tE          |rdn|¦  «        }|p9t          t          t          ‰ j        j	        ¦  «        dz  dz  ¦  «        d¦  «        }
tG          j$        tK          |¦  «        |
¦  «        ‰ _&        n(# tN          $ r tG          j(        ¦   «         ‰ _&        Y nw xY wtS          tT          d¦  «        rˆ fd„‰ j&        _+        n‚|	r| ,                    ¦   «         sn|j-        j.        s
J d¦   «         ‚|	s|j/        j0        r|n| 1                    ¦   «         ‰ _        t          ‰ j        d	z
  d	¦  «        ‰ _2        ‰ j        dz  ‰ _3        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‰ _4        tj          j6        g d"¢         ‰ _7        tj          j6        g d#¢         ‰ _8        h d$£‰ _9        h d%£‰ _:        dS )&zjInitialize the Annotator class with image and line width along with color palette for keypoints and limbs.r$   gú~j¼t“h?r   z?Expected HWC uint8 tensor image with 3 channels, but got shape z and dtype ú.ÚcpuFr#   .N>   ÚRGBÚRGBArÓ   rÔ   zArial.Unicode.ttfgìQ¸…ë¡?é   z9.2.0c                óH   •— ‰j                              | ¦  «        dd…         S )Nr$   rs   )rÍ   Úgetbbox)ÚxrR   s    €r3   ú<lambda>z$Annotator.__init__.<locals>.<lambda>H  s    ø€ ¨d¬i×.?Ò.?ÀÑ.BÔ.BÀ1ÀQÀ3Ô.G€ r5   z_Image not contiguous. Apply contiguous() or np.ascontiguousarray(im) to Annotator input images.rp   é   é   é   é   é   é   é   r™   é
   é   rs   é   )r™   r™   r™   r™   rß   rß   rß   r   r   r   r   r   rp   rp   rp   rp   rp   rp   rp   )rp   rp   rp   rp   rp   r   r   r   r   r   r   r™   r™   r™   r™   r™   r™   >	   ©r   éí   r[   ©râ   rT   r~   ©éD   éó   r   ©é³   rT   r#   ©é·   éß   r   ©éÝ   éo   rT   ©éë   éÛ   râ   ©ré   ré   ré   ©rT   rT   r   >   ©r   éÀ   é&   ©é/   r�   éü   ©r}   rè   rT   ©éh   r   é{   ©él   é   rT   ©éº   r   rð   ©rT   r   é½   ©rT   é$   é}   ©rT   é*   rs   ©rT   é´   r   ©rÿ   é   rÛ   );r   Ú
isinstancer   ÚtorchÚTensorrÎ   r·   r    r-   r´   ÚshapeÚlwÚndimr!   ra   ri   ÚdeviceÚtyperÒ   Únumpyr¹   ÚcvtColorÚCOLOR_GRAY2BGRr)   ÚascontiguousarrayÚdstackÚ
zeros_likeÚ	fromarrayÚimr'   Úconvertr   ÚDrawÚdrawr   r	   Útruetyperm   rÍ   Ú	ExceptionÚload_defaultr   Úpil_versionÚgetsizeÚis_contiguousÚdataÚ
contiguousÚflagsÚ	writeableÚcopyÚtfÚsfÚskeletonÚcolorsrb   Ú
limb_colorÚ	kpt_colorÚdark_colorsÚlight_colors)rR   r   rÊ   rÌ   rÍ   rÎ   rÏ   Ú	non_asciiÚinput_is_pilÚinput_is_tensorr´   s   `          r3   rd   zAnnotator.__init__  s\  ø€ õ ! Ñ)Ô)Ð)ˆ	Ý! "¥e¤kÑ2Ô2ˆÝ$ R­¬Ñ6Ô6ˆØÐ3˜)Ð3 |ˆŒØÐe¥¥E­#¸Ð.S¨b¬g¨gÈ2Ì8Ñ*TÔ*TÐWXÑ*XÐ[`Ñ*`Ñ$aÔ$aÐcdÑ eÔ eˆŒØð 	>Ø”7˜a’<�< B¤H¨Q¤K°1Ò$4Ð$4¸¼ÅUÄ[Ò9PÐ9PÐ9PØyÕRWÐXZÔX`ÑRaÔRaÐyÐyÐnpÔnvÐyÐyÐyñ :QÔ9PÐPð Œxð >˜2œ9œ>¨UÒ2Ð2Ø&(§f¢f¡h¤h§n¢nÑ&6Ô&6¸�O�Øð 	7ØŒx˜Œ{˜aÒÐÝ”\ "¥cÔ&8Ñ9Ô9��Ø”˜!” Ò!Ð!ÝÔ)­"¬)°R½¼ÀrÈ#ÈrÐPQÈrÈ'Ä{Ñ9SÔ9SÐ4TÑ*UÔ*UÑVÔV��Ø”˜!”˜q’�ÝÔ)¨"¨S°"°1°"¨W¬+Ñ6Ô6�ØŒ8ñ 	"Ø(ÐA�b�b­e¬o¸bÑ.AÔ.AˆDŒGØŒwŒ| ?Ð2Ð2Øœ'Ÿ/š/¨%Ñ0Ô0�”Ý!œ t¤w°Ñ7Ô7ˆDŒIð5Ý!¸Ð"LÐ"5Ð"5ÈÑMÔM�Ø ÐQ¥C­­c°$´'´,Ñ.?Ô.?À!Ñ.CÀeÑ.KÑ(LÔ(LÈbÑ$QÔ$Q�Ý%Ô.­s°4©y¬y¸$Ñ?Ô?�”	�	øÝð 5ð 5ð 5Ý%Ô2Ñ4Ô4�”	�	�	ð5øøøõ �[¨'Ñ2Ô2ð HØ$GÐ$GÐ$GÐ$G�”	Ô!øà)8ð �2×#Ò#Ñ%Ô%ð ð ¸b¼gÔ>Pð ð Øqñô ÐPð ,ÐP¨r¬xÔ/AÐP�b�bÀrÇwÂwÁyÄyˆDŒGÝ˜$œ' A™+ qÑ)Ô)ˆDŒ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Ð.nÐ.nÔoˆŒÝÔ,Ð-eÐ-eÐ-eÔfˆŒð

ð 

ð 

ˆÔð
ð 
ð 
ˆÔÐÐs   ÉA6K Ë"K6Ë5K6©rU   rU   rU   r\   rÄ   ri   Ú	txt_colorr   c                ó2   — || j         v rdS || j        v rdS |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  r\   )r5  r6  )rR   rÄ   r;  s      r3   Úget_txt_colorzAnnotator.get_txt_color‚  s0   € ð  �DÔ$Ð$Ð$Ø�;Ø�dÔ'Ð'Ð'Ø �=àÐr5   Ú Úlabelc           
     ó¤  — |                       ¦   «          |                      ||¦  «        }t          |t          j        t
          j        f¦  «        r|                     ¦   «         }t          |d         t          ¦  «        }|rd„ |d         D ¦   «         n)t          |d         ¦  «        t          |d         ¦  «        f}| j
        �rX|r,| j                             d„ |D ¦   «         | j        |¬¦  «        n!| j                             || j        |¬¦  «         |�r| j                             |¦  «        \  }}|d         |k    }	|d         | j        j        d         |z
  k    r| j        j        d         |z
  |d         f}| j                             |d         |	r|d         |z
  n|d         |d         |z   dz   |	r|d         dz   n|d         |z   dz   f|¬¦  «         | j                             |d         |	r|d         |z
  n|d         f||| j        ¬¦  «         dS dS |r<t)          j        | j        t          j        |t          ¬¦  «        gd	|| j        ¦  «        nUt)          j        | j        |t          |d
         ¦  «        t          |d         ¦  «        f|| j        t(          j        ¬¦  «         |�rt)          j        |d| j        | j        ¬¦  «        d         \  }}|dz  }|d         |k    }	|d         | j        j        d         |z
  k    r| j        j        d         |z
  |d         f}|d         |z   |	r|d         |z
  n
|d         |z   f}
t)          j        | j        ||
|dt(          j        ¦  «         t)          j        | j        ||d         |	r|d         d
z
  n|d         |z   dz
  fd| j        || j        t(          j        ¬¦  «         dS dS )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   c                ó,   — g | ]}t          |¦  «        ‘ŒS r{   rq   ©rP   Úbs     r3   rS   z'Annotator.box_label.<locals>.<listcomp>®  s   € Ð%Ð%Ð%˜�c�!‰fŒfÐ%Ð%Ð%r5   r#   c                ó,   — g | ]}t          |¦  «        ‘ŒS r{   rt   rB  s     r3   rS   z'Annotator.box_label.<locals>.<listcomp>±  s   € Ð'Ð'Ð'˜a•�q‘”Ð'Ð'Ð'r5   ©ÚwidthÚoutline©Úfill©rI  rÍ   r    Tr$   r   ©Ú	thicknessÚlineType©Ú	fontScalerL  r›   N)Ú	_to_numpyr=  r  r  r  r)   ÚndarrayÚtolistÚlistr   rÎ   r#  Úpolygonr  Ú	rectanglerÍ   r(  r   r´   Útextr¹   Ú	polylinesr*   ÚLINE_AAÚgetTextSizer0  r/  r  ÚputText)rR   Úboxr?  rÄ   r;  Úmulti_pointsÚp1Úwrl   ÚoutsideÚp2s              r3   Ú	box_labelzAnnotator.box_label™  sö  € ð 	�ŠÑÔÐØ×&Ò& u¨iÑ8Ô8ˆ	Ý�c�EœL­"¬*Ð5Ñ6Ô6ð 	Ø—*’*‘,”,ˆCå! # a¤&­$Ñ/Ô/ˆØ)5ÐUÐ%Ð%˜c !œfÐ%Ñ%Ô%Ð%½CÀÀAÄ¹K¼KÍÈSÐQRÌVÉÌÐ;UˆØŒ8ñ &	ð ðZˆDŒI×ÒØ'Ð' 3Ð'Ñ'Ô'¨t¬wÀð ñ ô ð à#'¤9×#6Ò#6°sÀ$Ä'ÐSXÐ#6Ñ#YÔ#YøØñ 
pØ”y×(Ò(¨Ñ/Ô/‘��1Ø˜Qœ% 1š*�Ø�a”5˜4œ7œ<¨œ?¨QÑ.Ò.Ð.Øœœ aœ¨1Ñ,¨b°¬eÐ3�BØ”	×#Ò#Ø˜”U¨Ð;˜B˜qœE A™I˜I°b¸´e¸RÀ¼UÀQ¹YÈ¹]ÐY`ÐLsÈBÈqÌEÐTUÉIÈIÐfhÐijÔfkÐnoÑfoÐrsÑfsÐtØð $ñ ô ð ð
 ”	—’  1¤°GÐ'F r¨!¤u¨q¡y yÀÀAÄÐGÈÐU^ÐeiÔen�ÑoÔoÐoÐoÐoð
pð 
pð ð�CŒMØ”�"œ* SµÐ4Ñ4Ô4Ð5°t¸UÀDÄGñô ð å#&¤=Ø”˜�c # a¤&™kœk­3¨s°1¬v©;¬;Ð7¸È$Ì'Õ\_Ô\gð$ñ $ô $øð ñ Ý” u¨a¸4¼7ÈdÌgÐVÑVÔVÐWXÔY‘��1Ø�Q‘�Ø˜Qœ% 1š*�Ø�a”5˜4œ7œ=¨Ô+¨aÑ/Ò/Ð/Øœœ qÔ)¨AÑ-¨r°!¬uÐ4�BØ˜”U˜Q‘Y¨WÐ C  1¤¨¡	 	¸"¸Q¼%À!¹)ÐC�Ý”˜dœg r¨2¨u°b½#¼+ÑFÔFÐFÝ”Ø”GØØ˜”U¨ÐC˜B˜qœE A™I˜I°b¸´e¸a±iÀ!±mÐDØØ”GØØ"œgÝ œ[ð	ñ 	ô 	ð 	ð 	ð 	ðð r5   r"   Úalphar   c                ó<  — | j         r0t          j        | j        ¦  «                             ¦   «         | _        t          |t          j        ¦  «        rŒ|                      ¦   «          | j                             ¦   «         }t          |¦  «        D ](\  }}||         || 	                    t          ¦  «        <   Œ)t          j        | j        d|z
  ||d¦  «        | _        �n›t          |¦  «        �r‹t          | j        t          j        ¦  «        }|r| j        j        n|j        }t#          j        |d                              |¦  «                             ¦   «         | j        j        dd…         ¦  «        d         dk    }t          j        ||t          j        ¬¦  «        dz  }|dd…ddf         |z  }|                     d¦  «        }t          j        g |j        dd…         ¢d‘R |t          j        ¬¦  «        }	t          j        g |j        dd…         ¢d‘R |t          j        ¬¦  «        }
t5          d|                     ¦   «         d	z  d
z  ¦  «        }t9          |                     |d¦  «        |	                     |¦  «        |
                     |¦  «        ¦  «        D ]=\  }}}t          j        ||z  d|¬¦  «         t          j        d||z  z
  d|¬¦  «         Œ>|r| j        nt          j         | j        ¦  «                             |¦  «                             ¦   «         dz  }||
z  |	z   dz   !                    ¦   «         }|r|n%| "                    ¦   «          #                    ¦   «         | j        dd…<   | j         r|  $                    | j        ¦  «         dS dS )aG  Plot masks on image.

        Args:
            masks (torch.Tensor | np.ndarray): Predicted masks with shape [n, h, w].
            colors (list[list[int]]): BGR colors for predicted masks, [[b, g, r] * n], matching `self.im`.
            alpha (float, optional): Mask transparency: 0.0 fully transparent, 1.0 opaque.
        r#   r   Nr$   r"   )r  r!   g     ào@r   rÕ   i  € )ÚoutrT   )%rÎ   r)   r*   r   r.  r  rQ  rP  Ú	enumeraterž   rh   r¹   ÚaddWeightedr^   r  r  r  r   Úscale_masksÚtor   r  Útensorr²   Ú	unsqueezeÚemptyr·   ÚnumelÚzipÚchunkÚamaxÚprodÚ
from_numpyÚbyterÒ   r  r  )rR   Úmasksr2  rb  Úoverlayre   ÚmaskÚtensor_imager  ÚmcsÚinv_alpha_masksÚbandsÚmÚmcs_bandÚinv_bandr   s                   r3   rs  zAnnotator.masks×  sD  € ð Œ8ð 	1å”j ¤Ñ)Ô)×.Ò.Ñ0Ô0ˆDŒGÝ�e�RœZÑ(Ô(ð 	BØ�NŠNÑÔÐØ”g—l’l‘n”nˆGÝ$ UÑ+Ô+ð 7ð 7‘��4Ø-3°A¬Y�˜Ÿš¥DÑ)Ô)Ñ*Ð*Ý”o d¤g¨q°5©y¸'À5È!ÑLÔLˆDŒG‰GÝ�‰ZŒZñ 	Bå% d¤g­u¬|Ñ<Ô<ˆLØ'3ÐE�T”W”^�^¸¼ˆFÝ”O E¨$¤K§N¢N°6Ñ$:Ô$:×$@Ò$@Ñ$BÔ$BÀDÄGÄMÐRTÐSTÐRTÔDUÑVÔVÐWXÔYÐ\_Ò_ˆEÝ”\ &°½u¼}ÐMÑMÔMÐPUÑUˆFØ˜A˜A˜A˜t T˜MÔ*¨UÑ2ˆFØ—O’O AÑ&Ô&ˆEÝ”+Ð4 ¤¨A¨a¨CÔ 0Ð4°!Ð4Ð4¸VÍ5Ì=ÐYÑYÔYˆCÝ#œkÐ*@¨E¬K¸¸!¸Ô,<Ð*@¸aÐ*@Ð*@ÈÕW\ÔWdÐeÑeÔeˆOå˜˜5Ÿ;š;™=œ=¨2Ñ-°Ñ6Ñ7Ô7ˆEÝ),¨U¯[ª[¸ÀÑ-BÔ-BÀCÇIÂIÈeÑDTÔDTÐVe×VkÒVkÐlqÑVrÔVrÑ)sÔ)sð ;ð ;Ñ%��8˜XÝ”
˜1˜v™: q¨hÐ7Ñ7Ô7Ð7Ý”
˜1˜q 5™y™=¨!°Ð:Ñ:Ô:Ð:Ð:Ø)ÐH�$”'�'­uÔ/?ÀÄÑ/HÔ/H×LÒLÈVÑTÔT×ZÒZÑ\Ô\Ð_dÑdˆBØ˜Ñ'¨#Ñ-°Ñ4×:Ò:Ñ<Ô<ˆBØ+ÐA˜˜°·²±´·²Ñ1AÔ1AˆDŒG�A�A�A‰JØŒ8ð 	$à�NŠN˜4œ7Ñ#Ô#Ð#Ð#Ð#ð	$ð 	$r5   rT   Úignore_indexr   c                ó2  ‡— |                       ¦   «          | j        r0t          j        | j        ¦  «                             ¦   «         | _        t          j        |¦  «        }t          j        ˆfd„|D ¦   «         | j        j        ¦  «        }t          |¦  «        r|t          j
        ||¦  «                 nt          j        | j        ¦  «        }t          j        | j        d|z
  ||d¦  «        | _        | j        r|                      | j        ¦  «         dS dS )a`  Plot a semantic segmentation mask on the image.

        Args:
            mask (np.ndarray): Semantic mask with shape [h, w] containing integer class indices.
            alpha (float, optional): Mask transparency: 0.0 fully transparent, 1.0 opaque.
            ignore_index (int, optional): Class index to ignore (e.g., 255 for void/ignore).
        c                óZ   •— g | ]'}|‰k    rd nt          t          |¦  «        d¦  «        ‘Œ(S )©r   r   r   T)r2  r   )rP   re   r}  s     €r3   rS   z+Annotator.semantic_mask.<locals>.<listcomp>  s8   ø€ ÐbÐbÐbÐYZ¨¨lÒ):Ð):˜I˜IÅÅsÈ1ÁvÄvÈtÑ@TÔ@TÐbÐbÐbr5   r#   r   N)rP  rÎ   r)   r*   r   r.  Úuniquer`   r!   r^   Úsearchsortedr  r¹   rf  r  )rR   ru  rb  r}  Úidsr]   rt  s      `   r3   Úsemantic_maskzAnnotator.semantic_maskþ  sú   ø€ ð 	�ŠÑÔÐØŒ8ð 	1å”j ¤Ñ)Ô)×.Ò.Ñ0Ô0ˆDŒGÝŒi˜‰oŒoˆÝ”(ÐbÐbÐbÐbÐ^aÐbÑbÔbÐdhÔdkÔdqÑrÔrˆÝ9<¸S¹¼Ð]�'�"œ/¨#¨tÑ4Ô4Ô5Ð5ÅrÄ}ÐUYÔU\ÑG]ÔG]ˆÝ”/ $¤'¨1¨u©9°g¸uÀaÑHÔHˆŒØŒ8ð 	$à�NŠN˜4œ7Ñ#Ô#Ð#Ð#Ð#ð	$ð 	$r5   ç333333ã?r§   r©   rª   r   r®   r'   ÚNonec                ó  — |                       ¦   «          | j        r0t          j        | j        ¦  «                             ¦   «         | _        t          |||¬¦  «        }|j        dd…         | j        j        dd…         k    r7t          j	        || j        j        d         | j        j        d         f¦  «        }t          j
        | j        d|z
  ||d¦  «        | _        | j        r|                      | j        ¦  «         dS dS )aw  Render a colorized depth map blended over the image.

        Args:
            depth (np.ndarray): (H, W) depth in meters.
            alpha (float): Blend factor for the heatmap overlay.
            cmap (str): Colormap, one of "inferno", "jet", "spectral". See `colorize_depth`.
            mode (str): "metric" or "disparity" normalization. See `colorize_depth`.
        )r®   r'   Nr$   r#   r   )rP  rÎ   r)   r*   r   r.  rÅ   r  r¹   Úresizerf  r  )rR   rª   rb  r®   r'   Úheats         r3   Ú	depth_mapzAnnotator.depth_map  sè   € ð 	�ŠÑÔÐØŒ8ð 	1Ý”j ¤Ñ)Ô)×.Ò.Ñ0Ô0ˆDŒGÝ˜e¨$°TÐ:Ñ:Ô:ˆØŒ:�b�q�bŒ>˜TœWœ]¨2¨A¨2Ô.Ò.Ð.Ý”:˜d T¤W¤]°1Ô%5°t´w´}ÀQÔ7GÐ$HÑIÔIˆDÝ”/ $¤'¨1¨u©9°d¸EÀ1ÑEÔEˆŒØŒ8ð 	$Ø�NŠN˜4œ7Ñ#Ô#Ð#Ð#Ð#ð	$ð 	$r5   ©é€  rŒ  Tç      Ð?r  r1   Úkpt_lineÚ
conf_thresr4  útuple | Nonec                ó8  — |�|n| j         }|                      ¦   «          | j        r0t          j        | j        ¦  «                             ¦   «         | _        |j        \  }}|dk    o|dv }	||	z  }t          |¦  «        D ]º\  }
}|p/|	r| j	        |
          
                    ¦   «         nt          |
¦  «        }|d         |d         }}t          |¦  «        dk    r|d         |k     rŒgn|dk    r|dk    rŒut          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    r4||d         dz
  df         }||d         dz
  df         }||k     s||k     rŒ¿nP||d         dz
  dd…f                              ¦   «         r'||d         dz
  dd…f                              ¦   «         s�Œt'          ||z   ¦  «        dk     r�Œ(t          j        | j        |||p| j        |
          
                    ¦   «         t          t          j        | j         dz  ¦  «        ¦  «        t          j        ¬
¦  «         �Œ™| j        r|                      | j        ¦  «         dS dS )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.
        NrÛ   >   r$   r   r   r#   r   r$   r›   )rM  rK  )r  rP  rÎ   r)   r*   r   r.  r  re  r4  rR  r2  r^   r¹   Úcircler   rX  r1  Úanyr¶   Úliner3  Úceilr  )rR   Úkptsr  r1   rŽ  r�  r4  Únkptr  Úis_posere   ÚkÚcolor_kÚx_coordÚy_coordÚskÚpos1Úpos2Úconf1Úconf2s                       r3   r–  zAnnotator.kpts+  s<  € ð0 "Ð-��°4´7ˆØ�ŠÑÔÐØŒ8ð 	1å”j ¤Ñ)Ô)×.Ò.Ñ0Ô0ˆDŒGØ”Z‰
ˆˆdØ˜"’*Ð/ ¨ ˆØ�GÑˆÝ˜d‘O”Oð 	ið 	i‰DˆAˆqØÐYÀ'Ð$X D¤N°1Ô$5×$<Ò$<Ñ$>Ô$>Ð$>ÍvÐVWÉyÌyˆGØ  œt Q q¤T�WˆGÝ�1‰vŒv˜Š{ˆ{Ø�Q”4˜*Ò$Ð$Øð %à˜A’� '¨Q¢, ,ØÝŒJ�t”w¥ W¡¤­s°7©|¬|Ð <¸fÀgÈrÕ\_Ô\gÐhÑhÔhÐhÐhàñ 	Ø”:˜b”>ˆDÝ" 4¤=Ñ1Ô1ð ñ ‘��2Ý˜D " Q¤%¨!¡)¨a Ô0Ñ1Ô1µ3°t¸RÀ¼UÀQ¹YÈ¸NÔ7KÑ3LÔ3LÐM�Ý˜D " Q¤%¨!¡)¨a Ô0Ñ1Ô1µ3°t¸RÀ¼UÀQ¹YÈ¸NÔ7KÑ3LÔ3LÐM�Ø˜1’9�9Ø  " Q¤%¨!¡)¨a Ô0�EØ  " Q¤%¨!¡)¨a Ô0�EØ˜zÒ)Ð)¨U°ZÒ-?Ð-?Ø ð .@à˜r !œu q™y¨"¨1¨"˜}Ô-×1Ò1Ñ3Ô3ð ¸¸RÀ¼UÀQ¹YÈÈÈ¸]Ô8K×8OÒ8OÑ8QÔ8Qð ÙÝ�t˜d‘{Ñ#Ô# aÒ'Ð'ÙÝ”Ø”GØØØÐ< ¤°Ô!3×!:Ò!:Ñ!<Ô!<Ý!¥"¤'¨$¬'°A©+Ñ"6Ô"6Ñ7Ô7Ý œ[ðñ ô ð ñ ð Œ8ð 	$à�NŠN˜4œ7Ñ#Ô#Ð#Ð#Ð#ð	$ð 	$r5   r#   rF  c                ó@   — | j                              ||||¦  «         dS )z"Add rectangle to image (PIL-only).N)r#  rU  )rR   ÚxyrI  rG  rF  s        r3   rU  zAnnotator.rectangleo  s$   € àŒ	×Ò˜B  g¨uÑ5Ô5Ð5Ð5Ð5r5   Útopr{   rV  ÚanchorÚ	box_colorc           
     ó„  — |                       ¦   «          | j        ræ| j                             |¦  «        \  }}|dk    r|dxx         d|z
  z  cc<   |                     d¦  «        D ]˜}|ra| j                             |¦  «        \  }}| j                             |d         |d         |d         |z   dz   |d         |z   dz   f|¬¦  «         | j                             |||| j        ¬¦  «         |dxx         |z  cc<   Œ™dS |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        ¬
¦  «         dS )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.
        Úbottomr#   ú
r   rH  rJ  rN  r   r›   rK  N)rP  rÎ   rÍ   r(  Úsplitr#  rU  rV  r¹   rY  r0  r/  r   rX  rZ  )rR   r£  rV  r;  r¥  r¦  r^  rl   r”  r_  r`  s              r3   rV  zAnnotator.texts  sß  € ð 	�ŠÑÔÐØŒ8ð 	kØ”9×$Ò$ TÑ*Ô*‰DˆAˆqØ˜Ò!Ð!Ø�1��”˜˜Q™‘��‘ØŸ
š
 4Ñ(Ô(ð ð �Øð fàœ9×,Ò,¨TÑ2Ô2‘D�A�qØ”I×'Ò'¨¨A¬°°1´°r¸!´u¸q±yÀ1±}ÀbÈÄeÈaÁiÐRSÁmÐ(TÐ[dÐ'ÑeÔeÐeØ”	—’˜r 4¨i¸d¼i�ÑHÔHÐHØ�1��”˜‘
��‘�ðð ð ð KÝ” t¨Q¸$¼'ÈTÌWÐUÑUÔUÐVWÔX‘��1Ø�Q‘�Ø˜Qœ% 1š*�Ø˜”U˜Q‘Y¨WÐ C  1¤¨¡	 	¸"¸Q¼%À!¹)ÐC�Ý”˜dœg r¨2¨y¸"½c¼kÑJÔJÐJÝŒK˜œ  r¨1¨d¬g°yÈDÌGÕ^aÔ^iÐjÑjÔjÐjÐjÐjr5   c                ó¬   — t          |t          j        ¦  «        r|nt          j        |¦  «        | _        t	          j        | j        ¦  «        | _        dS )z1Update `self.im` from a NumPy array or PIL image.N)r  r   r  r   r   r"  r#  )rR   r   s     r3   r  zAnnotator.fromarray’  s?   € å" 2¥u¤{Ñ3Ô3ÐL�"�"½¼ÈÑ9LÔ9LˆŒÝ”N 4¤7Ñ+Ô+ˆŒ	ˆ	ˆ	r5   c                ó¨   — t          | j        t          j        ¦  «        r2| j                             ¦   «                              ¦   «         | _        dS dS )zIMove a tensor image to CPU only when a CPU drawing operation requires it.N)r  r   r  r  rÒ   r  )rR   s    r3   rP  zAnnotator._to_numpy—  sC   € å�d”g�uœ|Ñ,Ô,ð 	,Ø”g—k’k‘m”m×)Ò)Ñ+Ô+ˆDŒGˆGˆGð	,ð 	,r5   c                ó¢   — |                       ¦   «          t          j        | j        ¦  «        }|rt	          j        |dddd…f         ¦  «        n|S )z-Return annotated image as array or PIL image..Nr›   )rP  r)   r*   r   r   r  )rR   rÎ   r   s      r3   ÚresultzAnnotator.resultœ  sK   € à�ŠÑÔÐÝŒZ˜œÑ Ô ˆØ14Ð<�uŒ˜r # t t¨ t )œ}Ñ-Ô-Ð-¸"Ð<r5   Útitleú
str | Nonec                ó2  — t          j        |                      ¦   «         dddd…f         ¦  «        }t          st          rA	 t          |¦  «         dS # t          $ r"}t          j        d|› �¦  «         Y d}~dS d}~ww xY w| 	                    |¬¦  «         dS )zShow the annotated image..Nr›   z.Unable to display image in Jupyter notebooks: )r¯  )
r   r  r®  r   r   ÚdisplayÚImportErrorr   ÚwarningÚshow)rR   r¯  r   Úes       r3   rµ  zAnnotator.show¢  s·   € åŒ_˜TŸ[š[™]œ]¨3°°°"°¨9Ô5Ñ6Ô6ˆÝð 	!•yð 	!ðUÝ˜‘”���øÝð Uð Uð UÝ”ÐSÐPQÐSÐSÑTÔTÐTÐTÐTÐTÐTÐTÐTøøøøðUøøøð �GŠG˜%ˆGÑ Ô Ð Ð Ð s   ÁA Á
A>ÁA9Á9A>ú	image.jpgÚfilenamec                óT   — t          j        ||                      ¦   «         ¦  «         dS )z'Save the annotated image to 'filename'.N)r¹   Úimwriter®  )rR   r¸  s     r3   ÚsavezAnnotator.save­  s"   € åŒ�H˜dŸkšk™mœmÑ,Ô,Ð,Ð,Ð,r5   Úbboxútuple | listc                ó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{   )r¼  Úx_minÚy_minÚx_maxÚy_maxrF  Úheights          r3   Úget_bbox_dimensionzAnnotator.get_bbox_dimension±  s5   € ð$ &*Ñ"ˆˆu�e˜UØ˜‘ˆØ˜‘ˆØ�f˜e f™nÐ,Ð,r5   )NNrÈ   FrÉ   )
rÊ   rË   rÌ   rË   rÍ   rm   rÎ   rh   rÏ   rm   )r:  r\   )rÄ   ri   r;  ri   r   ri   )r>  r:  r\   )r?  rm   rÄ   ri   r;  ri   )r"   )rb  r   )r"   rT   )rb  r   r}  r   )r…  r§   r©   )
rª   r   rb  r   r®   rm   r'   rm   r   r†  )r‹  NTr�  N)
r  ri   r1   rË   rŽ  rh   r�  r   r4  r�  )NNr#   )rF  r   )r\   r¤  r{   )rV  rm   r;  ri   r¥  rm   r¦  ri   ru   ©N)r¯  r°  )r·  )r¸  rm   )r¼  r½  )rv   rw   rx   ry   rd   r=  ra  rs  r„  rŠ  r–  rU  rV  r  rP  r®  rµ  r»  rz   rÄ  r{   r5   r3   rÇ   rÇ     sÃ  € € € € € ðð ð2 "&Ø $ØØØða
ð a
ð a
ð a
ð a
ðFð ð ð ð ð.<ð <ð <ð <ð <ð|%$ð %$ð %$ð %$ð %$ðN$ð $ð $ð $ð $ð. ØØð$ð $ð $ð $ð $ð8 "Ø!ØØ Ø"&ðB$ð B$ð B$ð B$ð B$ðH6ð 6ð 6ð 6ð 6ðkð kð kð kð kð>,ð ,ð ,ð
,ð ,ð ,ð
=ð =ð =ð =ð	!ð 	!ð 	!ð 	!ð 	!ð-ð -ð -ð -ð -ð ð-ð -ð -ñ „\ð-ð -ð -r5   rÇ   r{   r>  c           	     óè  — ddl m} ddl}ddlm} t          j        d|dz  › d�¦  «         t          |                     ¦   «         dz   ¦  «        }| dd…         } | 	                    | g d	¢¬
¦  «        }	| 
                    dddg¦  «        }
|                     dddd¬¦  «        d                              ¦   «         }|d                              |t          j        d||dz   ¦  «        dz
  d¬¦  «        }t!          |¦  «        D ]?}|d         j        |                              d„ t'          |¦  «        D ¦   «         ¦  «         Œ@|d                              d¦  «         dt+          |¦  «        cxk     rdk     r—n n”|d                              t!          t+          |¦  «        ¦  «        ¦  «         |d                              t1          |                     ¦   «         ¦  «        dd¬¦  «         |d                              |d         ¦  «         n|d                              d¦  «         t          j        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 ]N\  }}tE          j#        |¦  «         $                    | %                    ¦   «         dt'          |¦  «        ¬"¦  «         ŒO|d          &                    |¦  «         |d          '                    d#¦  «         |d          (                    |	d$         |	d%         d&|
¬'¦  «         |d                              d$¦  «         |d                              d%¦  «         |d(          (                    |	d)         |	d*         d&|
¬'¦  «         |d(                              d)¦  «         |d(                              d*¦  «         d+D ]-}d,D ](}||         j)        |          *                    d-¦  «         Œ)Œ.|dz  }| +                    |d.¬/¦  «         | ,                    ¦   «          |r ||¦  «         dS dS )0a®  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... r#   i@B )rØ   r0   rF  rÃ  )ÚschemaÚ
white_blueÚwhiteÚbluer$   )rà   rà   T©ÚfigsizeÚtight_layoutr"   çš™™™™™é?)ÚbinsÚrwidthc                ó   — g | ]}|d z  ‘ŒS )rT   r{   )rP   rØ   s     r3   rS   zplot_labels.<locals>.<listcomp>ä  s   € Ð">Ð">Ð">¨q 1 s¡7Ð">Ð">Ð">r5   Ú	instancesé   éZ   rá   )ÚrotationÚfontsizeÚclassesrs   éè  )rÙ  rÙ  r   r    rT   iô  rE  ÚoffrØ   r0   é2   )rÐ  r®   r   rF  rÃ  )r   r#   r$   r   )r¤  ÚrightÚleftr¨  FéÈ   ©Údpi)-Úmatplotlib.pyplotÚpyplotÚpolarsÚmatplotlib.colorsrÇ  r   Úinfor   r·   Ú	DataFrameÚ	from_listÚsubplotsÚravelÚhistr)   rœ   ÚrangeÚpatchesÚ	set_colorr2  Ú
set_ylabelr^   Ú
set_xticksÚset_xticklabelsrS  ÚvaluesÚ	bar_labelÚ
set_xlabelÚcolumn_stackr   r  Úonesra   rm  r   r"  rU  rR  ÚimshowÚaxisÚhist2dÚspinesÚset_visibleÚsavefigÚclose)ÚboxesÚclsÚnamesÚsave_dirÚon_plotÚpltrã  rÇ  ÚncrØ   Úsubplot_3_4_colorÚaxr0   re   ÚimgÚclass_idr[  ÚaÚsÚfnames                       r3   Úplot_labelsr  É  st  € ð $Ð#Ð#Ð#Ð#Ð#Ø€M€M€MØ9Ð9Ð9Ð9Ð9Ð9õ „KÐC h°Ñ&=ÐCÐCÐCÑDÔDÐDÝ	ˆS�WŠW‰YŒY˜‰]Ñ	Ô	€BØ�(�7�(ŒO€EØ×Ò˜Ð'DÐ'DÐ'DÐÑEÔE€Að 0×9Ò9¸,ÈÐRXÐHYÑZÔZÐØ	�Š�a˜ F¸ˆÑ	>Ô	>¸qÔ	A×	GÒ	GÑ	IÔ	I€BØ
ˆ1Œ�
Š
�3�Rœ[¨¨B°°Q±Ñ7Ô7¸#Ñ=Àcˆ
ÑJÔJ€AÝ�2‰YŒYð @ð @ˆØ	ˆ!ŒŒ�QŒ×!Ò!Ð">Ð">µF¸1±I´IÐ">Ñ">Ô">Ñ?Ô?Ð?Ð?Ø€q„E×Ò�[Ñ!Ô!Ð!Ø�3ˆu‰:Œ:ÐÐÒÐ˜ÒÐÐÐÐØ
ˆ1Œ×Ò��s 5™zœzÑ*Ô*Ñ+Ô+Ð+Ø
ˆ1Œ×Ò�d 5§<¢<¡>¤>Ñ2Ô2¸RÈ"ÐÑMÔMÐMØ
ˆ1Œ�Š˜˜!œÑÔÐÐà
ˆ1Œ×Ò˜Ñ#Ô#Ð#ÝŒO˜S 5¨¨¨¨A¨a¨C¨¤=°1Ñ#4Ñ4°c¸EÀ!À!À!ÀQÀqÀSÀ&¼MÈAÑ<MÑ6MÐNÑOÔOÐRVÑV€EÝ
Œ/�"œ' /½¼ÐBÑBÔBÀSÑHÑ
IÔ
I€CÝ˜S  # œY¨¨d¨s¨d¬Ñ4Ô4ð Wð W‰ˆ�#ÝŒ�sÑÔ×%Ò% c§j¢j¡l¤l¸!ÅVÈHÑEUÔEUÐ%ÑVÔVÐVÐVØ€q„E‡L‚L�ÑÔÐØ€q„E‡J‚JˆuÑÔÐà€q„E‡L‚L��3”˜˜3œ bÐ/@€LÑAÔAÐAØ€q„E×Ò�SÑÔÐØ€q„E×Ò�SÑÔÐØ€q„E‡L‚L��7”˜Q˜xœ[¨rÐ8I€LÑJÔJÐJØ€q„E×Ò�WÑÔÐØ€q„E×Ò�XÑÔÐØð /ð /ˆØ3ð 	/ð 	/ˆAØˆqŒEŒL˜ŒO×'Ò'¨Ñ.Ô.Ð.Ð.ð	/ð �|Ñ#€EØ‡K‚K�˜3€KÑÔÐØ‡I‚I�K„K€KØð Øˆ�‰Œˆˆˆðð r5   zim.jpggR¸…ëQð?rá   FTÚfiler   Úgainr/   Úsquarerh   ÚBGRr»  c                ó  — t          | t          j        ¦  «        st          j        | ¦  «        } t	          j        |                      dd¦  «        ¦  «        }|rC|dd…dd…f                              d¦  «        d                              d¦  «        |dd…dd…f<   |dd…dd…f         |z  |z   |dd…dd…f<   t	          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                             dd¬¦  «         t!          t#          |¦  «                             d¦  «        ¦  «        }|	r|
                     d¦  «        n|r|
dddd…f         n|
}t)          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›   rs   Nr$   r#   r   )r   r#   )r   r   )r   r   )r   r$   T©ÚparentsÚexist_okz.jpg.é_   )ÚqualityÚsubsampling)r  r  r  Ú	as_tensorr   Ú	xyxy2xywhÚviewr·   rj  Ú	xywh2xyxyÚlongÚ
clip_boxesr  r   ÚparentÚmkdirrm   r   Úwith_suffixÚsqueezer   r  r»  )Úxyxyr   r  r  r/   r  r  r»  rC  Ú	grayscaleÚcropr£   Úim_saves                r3   Úsave_one_boxr%    sþ  € õF �d�EœLÑ)Ô)ð %ÝŒ˜tÑ$Ô$ˆÝŒ�d—i’i  AÑ&Ô&Ñ'Ô'€AØð 3Ø�Q�Q�Q˜˜˜�U”8—<’< ‘?”? 1Ô%×/Ò/°Ñ2Ô2ˆˆ!ˆ!ˆ!ˆQˆRˆRˆ%‰Ø����A�B�B�Œx˜$‰ Ñ$€A€a€a€aˆˆˆ€e�HÝŒ=˜ÑÔ× Ò Ñ"Ô"€DÝŒ>˜$ ¤Ñ)Ô)€DØ”˜”˜qÒ €IØ�c�$�t”*‰oŒo¥ D¨¤J¡¤Ð/µ°T¸$´Z±´Å3ÀtÈDÄzÁ?Ä?Ð1RÐTvÐTvÐ]`ÐXuÐdmÐXuÐXYÐXYÐsuÐTvÐvÔw€DØð DØŒ×Ò $°ÐÑ6Ô6Ð6Ý•˜tÑ$Ô$×0Ò0°Ñ8Ô8Ñ9Ô9ˆà&/ÐU�$—,’,˜rÑ"Ô"Ð"ÈÐ5U°T¸#¸t¸tÀ¸t¸)´_°_ÐQUˆÝŒ˜Ñ Ô ×%Ò% a°ÀÐ%ÑCÔCÐCØ€Kr5   ú
images.jpgé€  rp   r�  Úlabelsúdict[str, Any]Úimagesú torch.Tensor | np.ndarray | NoneÚpathsúlist[str] | Noner
  rÿ  údict[int, str] | Noner  úCallable | NoneÚmax_sizeÚmax_subplotsr�  Úshow_labelsÚ	show_confúnp.ndarray | Nonec           
     ód  ‡9— |€ t          j        dt           j        ¬¦  «        n|}dD ]‹}|| vrŒ|dk    r/| |         j        dk    r| |                              d¦  «        | |<   t          | |         t          j        ¦  «        r/| |                              ¦   «          	                    ¦   «         | |<   ŒŒ|  
                    dt          j        dt           j        ¬¦  «        ¦  «        }|  
                    d	t          j        |j        t           j        ¬¦  «        ¦  «        }|  
                    d
t          j        dt           j        ¬¦  «        ¦  «        }|  
                    dd¦  «        }|  
                    dt          j        dt           j        ¬¦  «        ¦  «        }|  
                    dt          j        dt           j        ¬¦  «        ¦  «        }|  
                    dt          j        dt           j        ¬¦  «        ¦  «        }|  
                    dt          j        dt           j        ¬¦  «        ¦  «        }|  
                    d|¦  «        }t          |¦  «        rRt          |t          j        ¦  «        r8|                     ¦   «                              ¦   «          	                    ¦   «         }|j        d         }|dk    r9t          j        |dd…dd…f         ¦  «        }t          j        ||fd¬¦  «        }n|dk    r|dd…dd…f         }|j        \  }}}}t%          ||¦  «        }t          j        |dz  ¦  «        Š9t          j        |d         ¦  «        dk    r|dz  }t          j        t-          ‰9|z  ¦  «        t-          ‰9|z  ¦  «        dfdt           j        ¬¦  «        }t/          |¦  «        D ][}t-          ||‰9z  z  ¦  «        t-          ||‰9z  z  ¦  «        }}||                              ddd¦  «        ||||z   …|||z   …dd…f<   Œ\|‰9z  t)          ||¦  «        z  }|dk     r^t3          j        ||z  ¦  «        }t3          j        ||z  ¦  «        }t5          j        |t9          ˆ9fd„||fD ¦   «         ¦  «        ¦  «        }t-          ||z   ‰9z  dz  ¦  «        } t)          | d¦  «        } t;          |t=          | dz  ¦  «        | dt?          |¦  «        ¬¦  «        }!t/          |¦  «        D �]ñ}t-          ||‰9z  z  ¦  «        t-          ||‰9z  z  ¦  «        }}|!                      ||||z   ||z   gddd¬¦  «         |r@|! !                    |dz   |dz   gtE          ||         ¦  «        j#        dd…         d¬ ¦  «         t          |¦  «        dk    �r¥||k    }"||"          $                    d!¦  «        }#|du } |�||"         nd}$t          |¦  «        �r¨||"         }%t          |%¦  «        rg|%dd…dd"…f                              ¦   «         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          j&        |%¦  «        ntK          j'        |%¦  «        }%tQ          |% $                    t           j        ¦  «         )                    ¦   «         ¦  «        D ]�\  }'}(|#|'         }tU          |¦  «        })|r| 
                    ||¦  «        n|}| s|$|'         |	k    rK|$�
|$|'         d(›nd)}*|
r|› nd)}+|+|rd*|*› � +                    ¦   «         nd)z  }+|! ,                    |(|+|)¬+¦  «         Œ�nkt          |#¦  «        r\|#D ]Y}tU          |¦  «        })|r| 
                    ||¦  «        n|}| r|› n|› d*|$d         d(›�}+|! !                    ||g|+|)d,¬-¦  «         ŒZt          |¦  «        �r
||"          -                    ¦   «         },t          |,¦  «        rw|,d%                              ¦   «         d.k    s|,d&                              ¦   «         d.k    r!|,d%xx         |z  cc<   |,d&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<   t/          t          |,¦  «        ¦  «        D ]-}'| s|$|'         |	k    r|! .                    |,|'         |	¬/¦  «         Œ.t          |¦  «        �r'|"j        d         |j        d         k    r!|                     ¦   «         dk    r	||"         }-nn||g         }-|" /                    ¦   «         }.t          j0        d|.dz   ¦  «         1                    |.ddf¦  «        }/|-|/k     $                    t           j        ¦  «        }-t          j2        |!j3        ¦  «         -                    ¦   «         }0t/          t          |-¦  «        ¦  «        D �]}'| s|$|'         |	k    �rtU          |#|'         ¦  «        })|-|'         j        \  }1}2|1|k    s|2|k    rW|-|'          $                    t           j        ¦  «        }3t5          j        |3||f¦  «        }3|3 $                    th          ¦  «        }3n |-|'          $                    th          ¦  «        }3	 |0|||z   …|||z   …dd…f         |3         d0z  t          j5        |)¦  «        d1z  z   |0|||z   …|||z   …dd…f         |3<   �Œ# tl          $ r Y �Œw xY w�Œ |! 7                    |0¦  «         t          |¦  «        �r|t          |¦  «        k     �r||         }3|3j        \  }1}2|1|k    s|2|k    r@t5          j        |3 $                    t           j        ¦  «        ||ft4          j8        ¬2¦  «        }3t          j2        |!j3        ¦  «         -                    ¦   «         }0t;          t          j9        |0|||z   …|||z   …f         ¦  «        dd3¬4¦  «        }4|4 :                    |3d0¬5¦  «         |4j3        |0|||z   …|||z   …f<   |! 7                    |0¦  «         t          |¦  «        �rq|t          |¦  «        k     �r]||         }5|5j        dk    r|5                     d¦  «        }5|5j        \  }6}7|6|k    s|7|k    r@t5          j        |5 $                    t           j        ¦  «        ||ft4          j8        ¬2¦  «        }5t          j2        |!j3        ¦  «         -                    ¦   «         }0t5          j;        t          j9        |0|||z   …|||z   …f         ¦  «        t4          j<        ¦  «        }8t;          |8dd3¬4¦  «        }4|4 =                    |5d1¬5¦  «         t5          j;        |4j3        t4          j>        ¦  «        |0|||z   …|||z   …f<   |! 7                    |0¦  «         �Œó|st          j2        |!j3        ¦  «        S |!j3         ?                    |¦  «         |r ||¦  «         dS dS )6a)  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.
        show_labels (bool): Whether to display class labels.
        show_conf (bool): Whether to display confidence values.

    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
    N)r   r   rŒ  rŒ  r    )	rþ  ÚbboxesÚconfrs  Ú	keypointsÚ	batch_idxr*  r„  rª   rþ  r$   r#   r   r9  r6  r7  rs  r8  r„  rª   r  )r÷  r   r"   rT   c              3  ó<   •K  — | ]}t          |‰z  ¦  «        V — Œd S rÅ  rq   )rP   rØ   Únss     €r3   rr   zplot_images.<locals>.<genexpr>–  s-   øè è € Ð)FÐ)F¸!­#¨a°"©f©+¬+Ð)FÐ)FÐ)FÐ)FÐ)FÐ)Fr5   g{®Gáz„?é   rá   T)rÊ   rÌ   rÎ   rÏ   r\   )rF  rã   é(   )r„   r„   r„   )rV  r;  r   rs   gš™™™™™ñ?.).r   ).r#   r›   z.1fr>  ú ©rÄ   )é@   r@  r@  rU   )r;  r¦  g)\�Âõ(ð?)r�  gš™™™™™Ù?r…  ©ÚinterpolationF)rÊ   rÎ   ©rb  )@r)   Úzerosr²   r  r   r  r  r  rÒ   r  ÚgetÚint64r  ra   r^   r   r  Úconcatenater¶   r•  r·   Úfullr   rë  Ú	transposeÚmathr¹   rˆ  ri   rÇ   r    rm   rU  rV  r   Únamerž   r   Úxywhr2xyxyxyxyr  re  rR  r2  Ústripra  r.  r–  r-   r,   r¡   r*   r   rh   r`   r%  r  ÚINTER_NEARESTr  r„  r  ÚCOLOR_RGB2BGRrŠ  ÚCOLOR_BGR2RGBr»  ):r(  r*  r,  r
  rÿ  r  r0  r1  r»  r�  r2  r3  r™  rþ  r9  r6  Úconfsrs  r–  Úsemantic_masksÚ
depth_mapsrQ   ÚzeroÚbsÚ_rl   r^  Úmosaicre   rØ   r0   ÚscaleÚfsÚ	annotatorrÂ   rØ  r7  rý  Úis_obbÚjr[  rÄ   Ú	conf_textr?  Úkpts_Úimage_masksÚnlÚindexr   ÚmhÚmwru  Úsub_annotatorr¼   ÚdhÚdwÚsub_bgrr;  s:                                                            @r3   Úplot_imagesrh  :  s  ø€ ðZ >D¸^�RŒXÐ&­b¬jÐ9Ñ9Ô9Ð9ÐQW€FØmð 0ð 0ˆØ�Fˆ?ˆ?ØØ�Š:ˆ:˜& œ)œ.¨AÒ-Ð-Ø˜qœ	×)Ò)¨!Ñ,Ô,ˆF�1‰IÝ�f˜Q”i¥¤Ñ.Ô.ð 	0Ø˜qœ	Ÿš™œ×-Ò-Ñ/Ô/ˆF�1‰Iøà
�*Š*�U�BœH Q­b¬hÐ7Ñ7Ô7Ñ
8Ô
8€CØ—
’
˜;­¬°´Å"Ä(Ð(KÑ(KÔ(KÑLÔL€IØ�ZŠZ˜¥"¤(¨1µB´JÐ"?Ñ"?Ô"?Ñ@Ô@€FØ�JŠJ�v˜tÑ$Ô$€EØ�JŠJ�w¥¤¨µ"´(Ð ;Ñ ;Ô ;Ñ<Ô<€EØ�:Š:�k¥2¤8¨AµR´ZÐ#@Ñ#@Ô#@ÑAÔA€DØ—Z’Z µ´¸!Å2Ä8Ð1LÑ1LÔ1LÑMÔM€NØ—’˜G¥R¤X¨aµr´zÐ%BÑ%BÔ%BÑCÔC€JØ�ZŠZ˜˜vÑ&Ô&€Få
ˆ6�{„{ð .•z &­%¬,Ñ7Ô7ð .Ø—’‘”×#Ò#Ñ%Ô%×+Ò+Ñ-Ô-ˆð 	Œ�QŒ€AØˆA‚v€vÝŒ}˜V A A A r¨ r Eœ]Ñ+Ô+ˆÝ” ¨ °QÐ7Ñ7Ô7ˆˆØ	
ˆQŠˆØ˜˜˜˜2˜A˜2˜”ˆà”,�K€Bˆˆ1ˆaÝ	ˆR�Ñ	Ô	€BÝ	Œ��S‘Ñ	Ô	€BÝ	„vˆf�QŒiÑÔ˜AÒÐØ�#‰ˆõ ŒW•c˜"˜q™&‘k”k¥3 r¨A¡v¡;¤;°Ð2°C½r¼xÐHÑHÔH€FÝ�2‰YŒYð Gð GˆÝ�1˜˜R™‘=Ñ!Ô!¥3 q¨A°©F¡|Ñ#4Ô#4ˆ1ˆØ*0°¬)×*=Ò*=¸aÀÀAÑ*FÔ*Fˆˆq�1�q‘5ˆy˜!˜a !™e˜) Q Q QÐ&Ñ'Ð'ð �r‰M�C  1™IœIÑ%€EØˆq‚y€yÝŒI�e˜a‘iÑ Ô ˆÝŒI�e˜a‘iÑ Ô ˆÝ”˜F¥EÐ)FÐ)FÐ)FÐ)FÀÀ1¸vÐ)FÑ)FÔ)FÑ$FÔ$FÑGÔGˆõ 
ˆa�!‰e�r‰\˜DÑ Ñ	!Ô	!€BÝ	ˆR�‰Œ€BÝ˜&­U°2¸±7©^¬^ÀrÈtÕ]`ÐafÑ]gÔ]gÐhÑhÔh€IÝ�2‰YŒYð o$ñ o$ˆÝ�1˜˜R™‘=Ñ!Ô!¥3 q¨A°©F¡|Ñ#4Ô#4ˆ1ˆØ×Ò˜Q  1 q¡5¨!¨a©%Ð0°$¸ÈqÐÑQÔQÐQØð 	eØ�NŠN˜A ™E 1 q¡5˜>µ°U¸1´X±´Ô0CÀCÀRÀCÔ0HÐTcˆNÑdÔdÐdÝˆs‰8Œ8�aŠ<‰<Ø˜q’.ˆCØ˜#”h—o’o eÑ,Ô,ˆGØ˜d�]ˆFØ!&Ð!2�5˜”:�:¸ˆDå�6‰{Œ{ñ `Ø˜sœ�Ý�u‘:”:ð 0Ø˜Q˜Q˜Q   ˜U”|×'Ò'Ñ)Ô)¨SÒ0Ð0Ø˜c A q 6˜kÐ*Ð*Ô*¨aÑ/Ð*Ð*Ñ*Ø˜c A q 6˜kÐ*Ð*Ô*¨aÑ/Ð*Ð*Ñ*Ð*Ø š˜Ø˜c 2 A 2˜g˜˜œ¨%Ñ/˜˜™Ø�f��” Ñ"��‘Ø�f��” Ñ"��‘Øœ Rœ¨AÒ-�Ø5;ÐU�Ô*¨5Ñ1Ô1Ð1ÅÄÈuÑAUÔAU�Ý'¨¯ªµR´XÑ(>Ô(>×(EÒ(EÑ(GÔ(GÑHÔHð Eð E‘F�A�sØ œ
�AÝ" 1™IœI�EØ+0Ð7˜Ÿ	š	 ! Q™œ˜°a�AØð E  a¤¨:Ò!5Ð!5Ø8<Ð8H t¨A¤wÐ$4Ð$4Ð$4Èb˜	Ø*5Ð = 1  ¸2˜Ø¸IÐ!M  Y  ×!6Ò!6Ñ!8Ô!8Ð!8È2ÑM˜Ø!×+Ò+¨C°¸eÐ+ÑDÔDÐDøðEõ �W‘”ð `Ø ð `ð `�AÝ" 1™IœI�EØ+0Ð7˜Ÿ	š	 ! Q™œ˜°a�AØ&,ÐF˜q˜F˜F°QÐ2FÐ2F¸¸a¼Ð2FÐ2FÐ2F�EØ—N’N A q 6¨5¸EÐM^�NÑ_Ô_Ð_Ð_õ �4‰yŒyñ HØ˜Sœ	ŸšÑ(Ô(�Ý�u‘:”:ð 0Ø˜V”}×(Ò(Ñ*Ô*¨dÒ2Ð2°e¸F´m×6GÒ6GÑ6IÔ6IÈTÒ6QÐ6QØ˜f˜˜œ¨Ñ*˜˜™Ø˜f˜˜œ¨Ñ*˜˜™˜Ø š˜Ø˜c 2 A 2˜g˜˜œ¨%Ñ/˜˜™Ø�f��” Ñ"��‘Ø�f��” Ñ"��‘Ý�s 5™zœzÑ*Ô*ð Hð H�AØð H  a¤¨:Ò!5Ð!5Ø!Ÿš u¨Q¤x¸J˜ÑGÔGÐGøõ �5‰zŒzñ (Ø”9˜Q”< 5¤;¨q¤>Ò1Ð1°e·i²i±k´kÀQÒ6FÐ6FØ"'¨¤*�K�Kà"'¨¨¤*�KØŸš™œ�BÝœI a¨¨a©Ñ0Ô0×8Ò8¸"¸aÀ¸ÑDÔD�EØ#.°%Ò#7×"?Ò"?ÅÄ
Ñ"KÔ"K�Kå”Z 	¤Ñ-Ô-×2Ò2Ñ4Ô4�Ý�s ;Ñ/Ô/Ñ0Ô0ð !ñ !�AØð !  a¤¨:Ò!5Ñ!5Ý & w¨q¤zÑ 2Ô 2˜Ø!,¨Q¤Ô!5™˜˜BØ š7˜7 b¨A¢g gØ#.¨q¤>×#8Ò#8½¼Ñ#BÔ#B˜DÝ#&¤:¨d°Q¸°FÑ#;Ô#;˜DØ#'§;¢;­tÑ#4Ô#4˜D˜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°Q°QÐ6Ô7¸Ñ=Ñ=øõ  )ð !ð !ð !Ø ™Dð!øøøñ "6ð ×#Ò# BÑ'Ô'Ð'õ ˆ~ÑÔñ 		$ 1¥s¨>Ñ':Ô':Ò#:Ñ#:Ø! !Ô$ˆDØ”Z‰FˆB�Ø�QŠwˆw˜" š'˜'Ý”z $§+¢+­b¬hÑ"7Ô"7¸!¸Q¸ÍsÔO`ÐaÑaÔa�Ý”˜IœLÑ)Ô)×.Ò.Ñ0Ô0ˆBÝ%¥bÔ&:¸2¸aÀ!ÀaÁ%¸iÈÈQÐQRÉUÈÐ>RÔ;SÑ&TÔ&TÐabÐhmÐnÑnÔnˆMØ×'Ò'¨°CÐ'Ñ8Ô8Ð8Ø'4Ô'7ˆBˆq�1�q‘5ˆy˜!˜a !™e˜)Ð#Ñ$Ø×Ò Ñ#Ô#Ð#õ ˆz‰?Œ?ñ 	$˜q¥3 z¡?¤?Ò2Ñ2Ø˜1”ˆAØŒv˜Š{ˆ{Ø—I’I˜a‘L”L�Ø”W‰FˆB�Ø�QŠwˆw˜" š'˜'Ý”J˜qŸxšx­¬
Ñ3Ô3°a¸°VÍ3ÔK\Ð]Ñ]Ô]�Ý”˜IœLÑ)Ô)×.Ò.Ñ0Ô0ˆBõ ”l¥2Ô#7¸¸1¸qÀ1¹u¸9ÀaÈ!ÈaÉ%ÀiÐ;OÔ8PÑ#QÔ#QÕSVÔSdÑeÔeˆGÝ% g¸!ÀÐGÑGÔGˆMØ×#Ò# A¨SÐ#Ñ1Ô1Ð1Ý'*¤|°MÔ4DÅcÔFWÑ'XÔ'XˆBˆq�1�q‘5ˆy˜!˜a !™e˜)Ð#Ñ$Ø×Ò Ñ#Ô#Ð#ùàð (ÝŒz˜)œ,Ñ'Ô'Ð'Ø„L×Ò�eÑÔÐØð Øˆ�‰Œˆˆˆðð s   ë
Alì
l*ì)l*úpath/to/results.csvÚdirc           
     ó  — ddl m} ddl}| rt          | ¦  «        j        nt          |¦  «        }t          |                     d¦  «        ¦  «        }t          |¦  «        s J d|                     ¦   «         › d�¦   «         ‚g g }}d\  }	}
t          |¦  «        D �]“\  }}	 | 
                    |                     ¦   «         d¬¦  «        }|dk    �r|j        D ]5}d|v r|                     |¦  «         Œd	|v r|                     |¦  «         Œ6t          |¦  «        d
z  t          |¦  «        dz   d
z  }}|d|…         |d|…         z   ||d…         z   ||d…         z   }|                     d
t          |¦  «        dz   d
z  t          |¦  «        d
z   dfd¬¦  «        \  }	}
|
                     ¦   «         }
|
d                              t          |¦  «        d
z  dk    ¦  «         |                     |j        d         ¦  «                             ¦   «                              ¦   «         }t          |¦  «        D ]Ã\  }}|                     |¦  «                             ¦   «                              ¦   «                              d¦  «        }|
|                              ||d|j        d
d¬¦  «         |
|                              |t/          |d¬¦  «        ddd
¬¦  «         |
|                              |d¬¦  «         ŒÄ�Œc# t2          $ r%}t5          j        d|› d|› �¦  «         Y d}~�Œ�d}~ww xY w|
�Y|
d                              ¦   «          |dz  }|	                     |d¬¦  «         |                     ¦   «          |r ||¦  «         dS dS dS ) aá  Plot training results from a results CSV file. The function supports various types of data including detection,
    instance segmentation, semantic segmentation, depth estimation, classification, and pose estimation. 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   Nzresults*.csvzNo results.csv files found in z, nothing to plot.)NN)Úinfer_schema_lengthÚlossÚmetricr$   r#   rÞ   TrÌ  r›   r   rÑ   rà   )Úmarkerr?  Ú	linewidthÚ
markersizer   )r   ú:Úsmooth)r?  rp  rÕ   ©r×  zPlotting error for z: zresults.pngrÞ  rß  )rá  râ  rã  r   r  rS  Úglobr^   Úresolvere  Úread_csvÚ
read_bytesÚcolumnsÚappendrè  ré  rú  ÚselectÚto_numpyÚflattenrž   ÚplotÚstemr4   Ú	set_titler%  r   ÚerrorÚlegendrû  rü  )r  rj  r  r  Úplr   ÚfilesÚ	loss_keysÚmetric_keysÚfigr  re   r£   r*  rQ   Úloss_midÚ
metric_midry  rØ   r\  r0   r¶  r
  s                          r3   Úplot_resultsrŠ    sÆ  € ð $Ð#Ð#Ð#Ð#Ð#ØÐÐÐà$(Ð7�t�D‰zŒzÔ Ð ­d°3©i¬i€HÝ�—’˜~Ñ.Ô.Ñ/Ô/€EÝˆu‰:Œ:Ð^Ð^Ð^¸×8HÒ8HÑ8JÔ8JÐ^Ð^Ð^Ñ^Ô^ˆ:à ˆ{€IØ�G€CˆÝ˜%Ñ Ô ð 9ñ 9‰ˆˆ1ð	9Ø—;’;˜qŸ|š|™~œ~À4�;ÑHÔHˆDØ�AŠv‰vØœð .ð .�AØ �{�{Ø!×(Ò(¨Ñ+Ô+Ð+Ð+Ø! Q˜˜Ø#×*Ò*¨1Ñ-Ô-Ð-øÝ'*¨9¡~¤~¸Ñ':½SÀÑ=MÔ=MÐPQÑ=QÐVWÑ<W˜*�à˜i˜x˜iÔ(¨;°{¸
°{Ô+CÑCÀiÐPXÐPYÐPYÔFZÑZÐ]hÐisÐitÐitÔ]uÑuð ð Ÿ,š, q­3¨w©<¬<¸!Ñ+;ÀÑ*AÍCÐPWÉLÌLÐ[\ÑL\Ð^_ÐK`Ðos˜,ÑtÔt‘��RØ—X’X‘Z”Z�Ø�2”×"Ò"¥3 w¡<¤<°!Ñ#3°qÒ#8Ñ9Ô9Ð9Ø—’˜DœL¨œOÑ,Ô,×5Ò5Ñ7Ô7×?Ò?ÑAÔAˆAÝ! 'Ñ*Ô*ð 0ð 0‘��1Ø—K’K ‘N”N×+Ò+Ñ-Ô-×5Ò5Ñ7Ô7×>Ò>¸wÑGÔG�Ø�1”—
’
˜1˜a¨°1´6ÀQÐST�
ÑUÔUÐUØ�1”—
’
˜1Õ0°¸!Ð<Ñ<Ô<¸cÈÐ]^�
Ñ_Ô_Ð_Ø�1”—’ ¨B�Ñ/Ô/Ð/Ð/ñ	0øõ
 ð 	9ð 	9ð 	9ÝŒLÐ7¨qÐ7Ð7°AÐ7Ð7Ñ8Ô8Ð8Ð8Ð8Ð8Ð8Ñ8øøøøð	9øøøà	€~Ø
ˆ1Œ�Š‰ŒˆØ˜=Ñ(ˆØ�Š�E˜sˆÑ#Ô#Ð#Ø�	Š	‰ŒˆØð 	ØˆG�E‰NŒNˆNˆNˆNð €~ð
	ð 	s   Â IK<Ë<
L+ÌL&Ì&L+ÚfitnessÚscoresÚdictÚkeyc                ó„  — ddl m} t          |                      ¦   «         ¦  «        t	          | ¦  «        z  }|                     t          dt	          | ¦  «        dz  ¦  «        dfd¬¦  «        \  }}|                     t          t	          | ¦  «        ¦  «        t          |                      ¦   «         ¦  «        d¬	¦  «         | 
                    |d
dd|d›�¬¦  «         |                     t          t	          | ¦  «        ¦  «        ¦  «         |                     t          | ¦  «        d¬¦  «         |                     |¦  «         |                     dt	          | ¦  «        › d�¦  «         |                     ¦   «          t!          |¦  «        dz  }|                     |d¬¦  «         |                     |¦  «         |S )aQ  Plot per-dataset metrics from a multi-dataset training run as a bar chart with the cross-dataset mean.

    Args:
        scores (dict): Mapping of dataset name to its scalar metric value.
        key (str): Name of the plotted metric, used as the y-axis label.
        save_dir (str | Path): Directory to save the 'multitrain_results.png' figure.

    Returns:
        (Path): Path to the saved figure.

    Examples:
        >>> from ultralytics.utils.plotting import plot_multitrain_results
        >>> plot_multitrain_results({"coco8": 0.61, "dota8": 0.48}, key="metrics/mAP50-95(B)")
    r   Ng      @gÍÌÌÌÌÌÜ?rã   TrÌ  z#042AFFr?  Úorangeú--zmean = z.3f©rÄ   Ú	linestyler?  rÕ  )rÖ  zMultiTrainer results across z	 datasetszmultitrain_results.pngrÞ  rß  )rá  râ  r-   rñ  r^   rè  r·   Úbarrë  rS  Úaxhlinerï  rð  rî  r€  r‚  r   rû  rü  )rŒ  rŽ  r   r  Úmeanr‡  r  r
  s           r3   Úplot_multitrain_resultsr—  M  sƒ  € ð  $Ð#Ð#Ð#Ð#Ð#åˆv�}Š}‰ŒÑÔ¥# f¡+¤+Ñ-€DØ�lŠl¥C¨­S°©[¬[¸4Ñ-?Ñ$@Ô$@À!Ð#DÐSWˆlÑXÔX�G€CˆØ‡F‚F�5•�V‘”ÑÔ�t F§M¢M¡O¤OÑ4Ô4¸I€FÑFÔFÐFØ‡J‚Jˆt˜8¨tÐ;OÀTÐ;OÐ;OÐ;O€JÑPÔPÐPØ‡M‚M•%�˜F™œÑ$Ô$Ñ%Ô%Ð%Ø×Ò•t˜F‘|”|¨bÐÑ1Ô1Ð1Ø‡M‚M�#ÑÔÐØ‡L‚LÐFµ°F±´ÐFÐFÐFÑGÔGÐGØ‡I‚I�K„K€KÝ�‰NŒNÐ5Ñ5€EØ‡K‚K�˜3€KÑÔÐØ‡I‚Iˆc�N„N€NØ€Lr5   é   ÚviridisrÏ  ÚnonerÐ  rb  Ú
edgecolorsc                óà   ‡ ‡‡‡	‡
— ddl m} t          j        ‰ ‰|¬¦  «        \  ŠŠ	Š
ˆˆˆ ˆ	ˆ
fd„t	          t          ‰ ¦  «        ¦  «        D ¦   «         }|                     ‰ ‰||||¬¦  «         dS )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)rÐ  c                ó$  •— g | ]Œ}‰t          j        t          j        ‰|         ‰d ¬¦  «        dz
  d‰j        d         dz
  ¦  «        t          j        t          j        ‰|         ‰d ¬¦  «        dz
  d‰j        d         dz
  ¦  «        f         ‘Œ�S )F)rÜ  r#   r   )r)   r�   Údigitizer  )rP   re   r£   rê  r½   ÚxedgesÚyedgess     €€€€€r3   rS   z%plt_color_scatter.<locals>.<listcomp>‚  s¤   ø€ ð ð ð ð
 ð	 	ÝŒG•B”K  !¤ f°EÐ:Ñ:Ô:¸QÑ>ÀÀ4Ä:ÈaÄ=ÐSTÑCTÑUÔUÝŒG•B”K  !¤ f°EÐ:Ñ:Ô:¸QÑ>ÀÀ4Ä:ÈaÄ=ÐSTÑCTÑUÔUðWô	
ðð ð r5   )rQ   r®   rb  r›  )rá  râ  r)   Úhistogram2drë  r^   Úscatter)r½   r£   rÐ  r®   rb  r›  r  r2  rê  rŸ  r   s   ``      @@@r3   Úplt_color_scatterr£  n  sª   øøøøø€ ð  $Ð#Ð#Ð#Ð#Ð#õ œ>¨!¨Q°TÐ:Ñ:Ô:Ñ€Dˆ&�&ðð ð ð ð ð ð ð õ
 •s˜1‘v”v‘”ðñ ô €Fð ‡K‚K��1˜ T°À:€KÑNÔNÐNÐNÐNr5   rs   Úimgsútorch.TensorÚpredsúlist[torch.Tensor]ú
str | PathÚgtútorch.Tensor | NoneÚtitlesÚ
max_imagesr†  c                ó~	  — d„ |D ¦   «         }| j         dd…         \  }}g }t          t          | j         d         |¦  «        ¦  «        D �]œ}	| |	                              ¦   «                              ¦   «                              ¦   «                              dd¦  «                             ¦   «         dz                       t          j
        ¦  «                             ddd¦  «        }
t          j        |
t          j        ¦  «        g}|��b|j        dk    r
||	df         n||	         }||dk             }|                     ¦   «         r!t	          |                     ¦   «         ¦  «        nd	}|                     ¦   «         r!t	          |                     ¦   «         ¦  «        nd
}t%          |t&          j        ¦  «        rJ|                     ¦   «                              ¦   «                              ¦   «                              ¦   «         nt          j        |t          j        ¦  «        }|                     t          j        t3          |||d¬¦  «        ||ft          j        ¬¦  «        ¦  «         ndx}}|D �]L}|j        dk    r
||	df         n||	         }t%          |t&          j        ¦  «        rJ|                     ¦   «                              ¦   «                              ¦   «                              ¦   «         nt          j        |t          j        ¦  «        }||}}|�|€Z||dk             }|j        rBt	          |                     ¦   «         ¦  «        t	          |                     ¦   «         ¦  «        fnd\  }}|                     t          j        t3          |||d¬¦  «        ||ft          j        ¬¦  «        ¦  «         �ŒN|                     t          j        |¦  «        ¦  «         �Œžt          j        |¦  «        }|ržt          j        d|j         d         dfdt          j
        ¬¦  «        }t?          |¦  «        D ]I\  }}t          j         |tC          |¦  «        ||z  dz   dft          j"        dddt          j#        ¦  «         ŒJt          j        ||g¦  «        }t          j$        tC          |¦  «        |¦  «         dS )a¼  Write a depth panel grid: one row per image, columns RGB | GT (if provided) | one per entry of ``preds``.

    All depth columns share the GT valid-pixel range per row, so a scale error between GT and any prediction shows up
    directly as a color mismatch. Panels are resized to the RGB image size, so predictions at head stride need no prior
    interpolation.

    Args:
        imgs (torch.Tensor): (B,3,H,W) float image tensor in [0,1].
        preds (list): List of (B,1,H,W) or (B,H,W) predicted depth tensors; each adds one column.
        fname (str | Path): Output image path.
        gt (torch.Tensor, optional): (B,1,H,W) or (B,H,W) ground-truth depth in meters (pixels <= 0 invalid, drawn
            black). Used for the GT column and to set the shared color scale.
        titles (list, optional): List of column labels, drawn in a 24 px header strip. None keeps the strip-free layout.
        max_images (int): Maximum number of rows.
    c                óR   — g | ]$}|j         d k    r|                     d¦  «        n|‘Œ%S )r   r#   )r  rj  )rP   Úps     r3   rS   z%plot_depth_panels.<locals>.<listcomp>¥  s0   € ÐAÐAÐA°a˜qœv¨š{˜{ˆQ�[Š[˜‰^Œ^ˆ^°ÐAÐAÐAr5   éþÿÿÿNr   r#   rT   r$   rs   r—   rš   rn  r&   rA  r±   é   r   r    rÛ   r"   r€  )%r  rë  r¶   Údetachr   rÒ   Úclampr  rž   r)   ra   rI  r¹   r  rO  r  rl  r·   r  r  r  r*   r²   rz  rˆ  rÅ   rN  r´   ÚhstackÚvstackrH  re  rZ  rm   ÚFONT_HERSHEY_SIMPLEXrX  rº  )r¤  r¦  r
  r©  r«  r¬  rl   r^  Úrowsre   r¤   ÚpanelsÚgÚgvr«   r­   r¼   r¯  r¿   rÀ   ÚdvÚgridrM  r\  Úts                            r3   Úplot_depth_panelsr¾  Ž  s  € ð. BÐA¸5ÐAÑAÔA€EØŒ:�b�c�cŒ?�D€A€qØ€DÝ•3�t”z !”} jÑ1Ô1Ñ2Ô2ð 'ñ 'ˆØ�AŒw�~Š~ÑÔ×%Ò%Ñ'Ô'×+Ò+Ñ-Ô-×3Ò3°A°qÑ9Ô9×?Ò?ÑAÔAÀCÑG×OÒOÕPRÔPXÑYÔY×cÒcÐdeÐghÐjkÑlÔlˆÝ”,˜s¥CÔ$5Ñ6Ô6Ð7ˆà‰>ØœG qšL˜L��1�a�4”�¨b°¬eˆAØ�1�q’5”ˆBØ&(§h¢h¡j¤jÐ9•5˜Ÿš™œ‘?”?�?°cˆDØ&(§h¢h¡j¤jÐ9•5˜Ÿš™œ‘?”?�?°cˆDÝ4>¸qÅ%Ä,Ñ4OÔ4OÐn�—’‘
”
× Ò Ñ"Ô"×&Ò&Ñ(Ô(×.Ò.Ñ0Ô0Ð0ÕUWÔU_Ð`aÕceÔcmÑUnÔUnˆAØ�MŠMÝ”
�>¨!¨T°4¸hÐGÑGÔGÈ!ÈQÈÕ_bÔ_pÐqÑqÔqñô ð ð ð
 ÐˆD�4àð 	yñ 	yˆAØœ6 Qš;˜;��!�Q�$”�¨A¨a¬DˆAÝ4>¸qÅ%Ä,Ñ4OÔ4OÐn�—’‘
”
× Ò Ñ"Ô"×&Ò&Ñ(Ô(×.Ò.Ñ0Ô0Ð0ÕUWÔU_Ð`aÕceÔcmÑUnÔUnˆAØ˜4�ˆBØˆz˜R˜ZØ�q˜1’u”X�Ø?A¼wÐV�% §¢¡¤™/œ/­5°·²±´©?¬?Ð;Ð;ÈJ‘��BØ�MŠM�#œ*¥^°A°r¸2ÀHÐ%MÑ%MÔ%MÐPQÐSTÈvÕehÔevÐwÑwÔwÑxÔxÐxÑxà�Š•B”I˜fÑ%Ô%Ñ&Ô&Ð&Ñ&ÝŒ9�T‰?Œ?€DØð (Ý”˜˜TœZ¨œ]¨AÐ.°½2¼8ÐDÑDÔDˆÝ˜fÑ%Ô%ð 	rð 	r‰DˆAˆqÝŒK˜�s 1™vœv¨¨A©°©	°2 ½Ô8PÐRUÐW`ÐbcÕehÔepÑqÔqÐqÐqÝŒy˜% ˜Ñ'Ô'ˆÝ„K•�E‘
”
˜DÑ!Ô!Ð!Ð!Ð!r5   útune_results.ndjsonÚresults_fileÚexclude_zero_fitness_pointsc                óˆ  ‡‡‡‡‡ — ddl ŠddlmŠ  ˆ fd„}t          | ¦  «        } t	          | d¬¦  «        5 }ˆfd„|D ¦   «         }ddd¦  «         n# 1 swxY w Y   |sdS t          |d                              di ¦  «        ¦  «        Št          j        ˆfd„|D ¦   «         t          ¬	¦  «        }t          |¦  «         |dd…df         }t          d¦  «        }|r|dk    }||         ||         }}t          |¦  «        dk    rt          j        d
¦  «         dS |                     ¦   «         }t          d¦  «        D ]^}	|                     ¦   «         |                     ¦   «         }}
|
d|z  z
  }||k    }|                     ¦   «         r n||         ||         }}Œ_t          j        |¦  «        }t)          j        t          ‰¦  «        dz  ¦  «        }‰                      dd¬¦  «         t/          ‰¦  «        D ]É\  }}|dd…|dz   f         }||         }‰                      |||dz   ¦  «         t3          ||ddd¬¦  «         ‰                      ||                     ¦   «         dd¬¦  «         ‰                      |› d|d›�ddi¬¦  «         ‰                      dd¬¦  «         ||z  dk    r‰                      g ¦  «         ŒÊ ||                      d ¦  «        ¦  «         tA          |t          ¦  «        r|nd!„ tC          ||¦  «        D ¦   «         ŠtE          d"„ ‰D ¦   «         ¦  «        }‰  #                    dd#d$t7          d%t          |¦  «        d&z  ¦  «        fd'd(id¬)¦  «        \  }	\  }}t          j        d*„ t/          ‰d¦  «        D ¦   «         ¦  «        }tI          |                     ¦   «         ¦  «        Š| %                    ||d+d,d-d.¬/¦  «         |                     |t          j&         '                    |¦  «        d0d1d2¬3¦  «         | (                    |d         d4d5d6|d         d7›�¬8¦  «         | %                    |‰         |‰         d9d:d;d<¬=¦  «         | )                    d>d.d?¬@¦  «         | *                    dA¬B¦  «         | +                    ¦   «          |�rnt          j        ˆfdC„|D ¦   «         ¦  «        }t          j        ˆˆfdD„|D ¦   «         ¦  «        }t          j,        ||z
  ¦  «        }t          j-        t          |¦  «        ¦  «        }| .                    |||         ||         dE¬F¦  «         | %                    ||         |dGd4dH¬I¦  «         | %                    ||         |dGd:dJ|‰         › �¬I¦  «         | /                    |¦  «         | 0                    t          j        |¦  «        |         d¬K¦  «         | )                    dLd?¬M¦  «         | *                    dNdA¬O¦  «         | +                    ¦   «          n| 1                    ¦   «           ||                      dP¦  «        ¦  «         dS )Qaa  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   Nc                óŒ   •— ‰                      | d¬¦  «         ‰                     ¦   «          t          j        d| › �¦  «         dS )z#Save one matplotlib plot to 'file'.rÞ  rß  zSaved N)rû  rü  r   rå  )r  r  s    €r3   Ú_save_one_filez)plot_tune_results.<locals>._save_one_fileÛ  sB   ø€ à�Š�D˜cˆÑ"Ô"Ð"Ø�	Š	‰ŒˆÝŒ�O˜T�O�OÑ$Ô$Ð$Ð$Ð$r5   zutf-8)Úencodingc                ób   •— g | ]+}|                      ¦   «         ¯‰                     |¦  «        ‘Œ,S r{   )rM  Úloads)rP   r”  Újsons     €r3   rS   z%plot_tune_results.<locals>.<listcomp>ã  s2   ø€ ÐBÐBÐB¨°T·Z²Z±\´\ÐB�4—:’:˜dÑ#Ô#ÐBÐBÐBr5   Úhyperparametersc                ó^   •‡— g | ](Š‰                      d d¦  «        gˆfd„‰D ¦   «         z   ‘Œ)S )r‹  r—   c                óx   •— g | ]6}‰                      d i ¦  «                              |t          j        ¦  «        ‘Œ7S )rÉ  )rE  r)   Únan)rP   r™  Úrs     €r3   rS   z0plot_tune_results.<locals>.<listcomp>.<listcomp>é  s;   ø€ Ð#^Ð#^Ð#^ÐTU A§E¢EÐ*;¸RÑ$@Ô$@×$DÒ$DÀQÍÌÑ$OÔ$OÐ#^Ð#^Ð#^r5   ©rE  )rP   rÍ  Úkeyss    @€r3   rS   z%plot_tune_results.<locals>.<listcomp>é  sG   øø€ ÐpÐpÐpÐcdˆ!�%Š%�	˜3Ñ
Ô
Ð	 Ð#^Ð#^Ð#^Ð#^ÐY]Ð#^Ñ#^Ô#^Ñ	^ÐpÐpÐpr5   r    z@No valid fitness values to plot (all iterations may have failed)r   r"   )rá   rá   TrÌ  r#   r™  rÏ  rš  )r®   rb  r›  zk+rÜ   )rq  z = z.3gr´   r™   )ÚfontdictÚbothrà   )r÷  Ú	labelsizeztune_scatter_plots.pngc                ó   — g | ]	\  }}|¯|‘Œ
S r{   r{   )rP   rÍ  Úkeeps      r3   rS   z%plot_tune_results.<locals>.<listcomp>  s"   € Ð@rÐ@rÐ@rÁwÀqÈ$ÐmqÐ@rÀÐ@rÐ@rÐ@rr5   c                óD   — h | ]}|                      d i ¦  «        D ]}|’ŒŒS )ÚdatasetsrÎ  )rP   rÍ  r™  s      r3   ú	<setcomp>z$plot_tune_results.<locals>.<setcomp>  s3   € ÐNÐNÐN˜Q¸¿º¸jÈ"Ñ8MÔ8MÐNÐN°1�qÐNÐNÐNÐNr5   r$   rp   rÞ   gìQ¸…ëÑ?Úwidth_ratios)r#   gš™™™™™õ?)rÍ  Úgridspec_kwÚconstrained_layoutc                ó@   — g | ]\  }}|                      d |¦  «        ‘ŒS )Ú	iterationrÎ  )rP   re   rÍ  s      r3   rS   z%plot_tune_results.<locals>.<listcomp>  s*   € ÐXÐXÐX±T°Q¸˜1Ÿ5š5 ¨aÑ0Ô0ÐXÐXÐXr5   é   z0.45g      è?Ú	Iteration)r	  rÄ   rb  r?  z#2563ebg      @zBest so far)rÄ   rp  r?  z#dc2626r‘  zInitial z.4fr’  rÕ  z#16a34arã   ÚBest)r	  rÄ   Úzorderr?  zFitness ProgressÚFitness)r¯  ÚxlabelÚylabelgš™™™™™É?rC  c                ó˜   •— g | ]F}‰d                                di ¦  «                              |i ¦  «                              dd¦  «        ‘ŒGS )r   rÖ  r‹  r—   rÎ  )rP   r™  Úplot_recordss     €r3   rS   z%plot_tune_results.<locals>.<listcomp>!  sO   ø€ ÐqÐqÐqÐcd˜L¨œO×/Ò/°
¸BÑ?Ô?×CÒCÀAÀrÑJÔJ×NÒNÈyÐZ]Ñ^Ô^ÐqÐqÐqr5   c                ó˜   •— g | ]F}‰‰                               d i ¦  «                              |i ¦  «                              dd¦  «        ‘ŒGS )rÖ  r‹  r—   rÎ  )rP   r™  Úbest_idxrå  s     €€r3   rS   z%plot_tune_results.<locals>.<listcomp>"  sQ   ø€ ÐuÐuÐuÐgh˜ hÔ/×3Ò3°JÀÑCÔC×GÒGÈÈ2ÑNÔN×RÒRÐS\Ð^aÑbÔbÐuÐuÐur5   z0.8r?  r±  ÚInitial)r	  rÄ   r?  zBest aggregate iteration rt  z8Per-Dataset Fitness: Initial vs Best Aggregate Iteration)r¯  râ  rØ   )r÷  rb  ztune_fitness.png)2rÈ  rá  râ  r   ÚopenrS  rE  r)   r`   r   r^   Úslicer   r´  r.  rë  r–  ÚstdÚallÚargmaxrJ  r•  Úfigurere  Úsubplotr£  r~  r·   r¯  Útick_paramsÚyticksÚ	with_namer  rm  Úsortedrè  r   r¢  ÚmaximumÚ
accumulater•  Úsetr¼  r‚  Úargsortr,   ÚhlinesÚ
set_yticksÚset_yticklabelsÚset_axis_off)!rÀ  rÁ  rÄ  r£   ÚrecordsrØ   Úall_fitnessÚ	zero_maskr‹  rV  r–  rë  Úlower_boundru  r\  r_   re   r™  r½   ÚmurÖ  Úax1Úax2Ú
iterationsÚinitialÚbestÚorderr0   rç  rÈ  rÏ  rå  r  s!                               @@@@@r3   Úplot_tune_resultsr  Ì  sÕ  øøøøø€ ð €K€K€Kà#Ð#Ð#Ð#Ð#Ð#ð%ð %ð %ð %ð %õ ˜Ñ%Ô%€LÝ	ˆl WÐ	-Ñ	-Ô	-ð C°ØBÐBÐBÐB°ÐBÑBÔBˆðCð Cð Cñ Cô Cð Cð Cð Cð Cð Cð Cøøøð Cð Cð Cð Càð Øˆå�˜”
—’Ð0°"Ñ5Ô5Ñ6Ô6€DÝ
ŒØpÐpÐpÐpÐhoÐpÑpÔpÝð	ñ 	ô 	€Aõ ˆ�F„F€FØ�A�A�A�q�D”'€KÝ�d‘”€IØ"ð >Ø !’Oˆ	Ø˜9œ {°9Ô'=ˆ;ˆÝ
ˆ;ÑÔ˜1ÒÐÝŒÐYÑZÔZÐZØˆØ×ÒÑ Ô €Gå�1‰XŒXð ,ð ,ˆØ—L’L‘N”N G§K¢K¡M¤MˆcˆØ˜Q ™W‘nˆØ˜+Ò%ˆØ�8Š8‰:Œ:ð 	ØˆEØ�t”W˜g dœmˆ7ˆˆÝ
Œ	�'ÑÔ€AÝŒ	•#�d‘)”)˜sÑ"Ñ#Ô#€AØ‡J‚J�x¨d€JÑ3Ô3Ð3Ý˜$‘”ð 	ð 	‰ˆˆ1Øˆaˆaˆa��Q‘ˆhŒKˆØˆqŒTˆØ�Š�A�q˜!˜a™%Ñ Ô Ð Ý˜!˜W¨9¸CÈFÐSÑSÔSÐSØ�Š��W—[’[‘]”] D°RˆÑ8Ô8Ð8Ø�	Š	�QÐ#Ð#˜2Ð#Ð#Ð#¨v°q¨kˆ	Ñ:Ô:Ð:Ø�Š˜V¨qˆÑ1Ô1Ð1Øˆq‰5�AŠ:ˆ:Ø�JŠJ�r‰NŒNˆNøØ€N�<×)Ò)Ð*BÑCÔCÑDÔDÐDõ )¨µEÑ:Ô:Ðr�7�7Ð@rÐ@rÕRUÐV]Ð_hÑRiÔRiÐ@rÑ@rÔ@r€LÝÐNÐN ,ÐNÑNÔNÑOÔO€HØ—L’LØ	Ø	Ø•S˜�C ™MœM¨DÑ0Ñ1Ô1Ð2Ø# YÐ/Øð !ñ ô �M€A�zˆˆSõ ”ÐXÐX½YÀ|ÐUVÑ=WÔ=WÐXÑXÔXÑYÔY€JÝ�;×%Ò%Ñ'Ô'Ñ(Ô(€HØ‡K‚K�
˜K¨2°VÀ4È{€KÑ[Ô[Ð[Ø‡H‚HˆZ�œ×.Ò.¨{Ñ;Ô;À9ÐX[Ðcp€HÑqÔqÐqØ‡K‚K�˜A” i¸4ÐGfÐR]Ð^_ÔR`ÐGfÐGfÐGf€KÑgÔgÐgØ‡K‚K�
˜8Ô$ k°(Ô&;¸rÈÐ[\Ðdj€KÑkÔkÐkØ‡G‚GÐ$¨[À€GÑKÔKÐKØ‡H‚H�3€HÑÔÐØ‡J‚J�L„L€LØñ Ý”(ÐqÐqÐqÐqÐhpÐqÑqÔqÑrÔrˆÝŒxÐuÐuÐuÐuÐuÐltÐuÑuÔuÑvÔvˆÝ”
˜4 '™>Ñ*Ô*ˆÝŒI•c˜(‘m”mÑ$Ô$ˆØ�
Š
�1�g˜e”n d¨5¤k¸ˆ
Ñ?Ô?Ð?Ø�Š�G˜E”N A¨°9ÀIˆÑNÔNÐNØ�Š�D˜”K  b°	ÐAsÐ]gÐhpÔ]qÐAsÐAsˆÑtÔtÐtØ�Š�qÑÔÐØ×Ò�BœH XÑ.Ô.¨uÔ5ÀÐÑBÔBÐBØ�ŠÐPÐYbˆÑcÔcÐcØ�Š�c ˆÑ%Ô%Ð%Ø�
Š
‰Œˆˆà×ÒÑÔÐØ€N�<×)Ò)Ð*<Ñ=Ô=Ñ>Ô>Ð>Ð>Ð>s   µAÁAÁA)r7  rØ  Útopkr   ú	list[str]r   r7  r  r   c               ó¤  ‡$‡%‡&— g g cŠ$Š&ˆ$fd„}	ˆ&fd„}
| j         j         d         }dx|_        |_        |                     |	¦  «        |                     |
¦  «        g}t
          rt          j        d¦  «        nt          j	        ¦   «         5  t          j
        ¦   «         5  	 |                     ¦   «                              d¦  «        } | |g|¢R i |¤Ž}|D ]}|                     ¦   «          Œn# |D ]}|                     ¦   «          Œw xY wt          j        ‰&d¦  «        }|�qt          j        |t          j        |j        ¬¦  «                             ¦   «         }||d	k    ||j        d
         k     z           }t'          |¦  «        r|dd…|f         }|                     d
¦  «        }|                     ¦   «         |k    ||                     d
d¬¦  «        k    z  }t-          t/          |                     d
¦  «                             ¦   «         ¦  «        |¦  «        }t/          |                     d
¦  «                             ¦   «         ¦  «        |k    rMt3          j        d|› dt/          |                     d
¦  «                             ¦   «         ¦  «        › d�¦  «         t          j        ||j        ¬¦  «        |                     d
d¬¦  «                             d
¬¦  «        z  }|                     | t=          d¦  «        ¦  «                             d
d¬¦  «                              d
|¦  «        }d}tC          |¦  «        D �]—}g }t          j"         #                    |                      d
|dd…||d
z   …f         ¦  «                             ¦   «         ‰$||d
z
  k     ¬¦  «        }tI          ‰$|¦  «        D ]ã\  }}|                     ¦   «                              d	¬¦  «        |                     ¦   «         z                       d
d¬¦  «        }|                     d	¬¦  «        }t          j%        j&         '                    ||j        dd…         dd¬¦  «        }| (                    ||                     dd¬¦  «                             d¬¦  «        z  ¦  «         Œät          j)        |¦  «                             d	¦  «        }|€|nt          j*        ||¦  «        }�Œ™	 ddd¦  «         n# 1 swxY w Y   ddd¦  «         n# 1 swxY w Y   | +                    d
¦  «        dz   ,                    ¦   «          -                    ¦   «          .                    ¦   «         }| /                    ¦   «         dd…dd…f                              ¦   «         }| 0                    dd¬¦  «        |                     dd¬¦  «        }}||z
  ||z
                       d¬¦  «        z  dz   ,                    ¦   «          1                    d	ddd
¦  «         -                    ¦   «          .                    ¦   «         dddd…f         }| 2                    dd¬¦  «         tI          |||¦  «        D ]á\  }} }!tg          |ti          |!¦  «        j5        › d�z  ¦  «        }"tm          j7        | j        d         dk    r| n| ddd
…f          8                    dd¦  «        ¦  «        } ts          j:        ts          j;        |tr          j<        ¦  «        d| dd	¦  «        }#ts          j=        t}          |"¦  «        |#¦  «         t3          j?        d|"› d�¦  «         Œâˆ%fd „Š% ‰%|¦  «        S )!aN  Run inference and save a class activation heatmap for each image of the batch.

    LayerCAM weights each head-input position by its positive gradient toward the predicted class score. Each prediction
    and head level is normalized independently before taking their element-wise maximum, preventing stronger predictions
    or levels from hiding weaker ones.

    Args:
        model (torch.nn.Module): AutoBackend wrapping a PyTorch model.
        im (torch.Tensor): Preprocessed images of shape (B, 3, H, W).
        paths (list[str]): Source path of each image of the batch, used to name the saved overlays.
        save_dir (Path): Directory to save the overlays in.
        *args (Any): Additional positional arguments passed to the model forward.
        conf (float): Score threshold a prediction must pass to contribute, falling back to the single best prediction
            for images where nothing passes it, so that a near miss can still be inspected.
        classes (int | list[int], optional): Only let these class ids contribute, as in the predict `classes` filter.
        topk (int): Maximum number of predictions to explain per image, each one costing a backward pass.
        **kwargs (Any): Additional keyword arguments passed to the model forward.

    Returns:
        (Any): Model predictions, detached from the autograd graph.
    c                ó”   •— |d         }‰                      d„ t          |t          t          f¦  «        r|n|gD ¦   «         ¦  «         dS )zbCapture the feature maps entering the head, before heads like WorldDetect overwrite them in place.r   c              3  ó0   K  — | ]}|j         d k    ¯|V — ŒdS )rs   N)r  )rP   r  s     r3   rr   z9class_activation_map.<locals>.pre_hook.<locals>.<genexpr>W  s)   è è € Ð[Ð[˜!ÈqÌvÐYZÊ{È{�AÈ{È{È{È{Ð[Ð[r5   N)Úextendr  rS  ri   )ÚmoduleÚinputsrØ   Úactss      €r3   Úpre_hookz&class_activation_map.<locals>.pre_hookT  sL   ø€ à�1ŒIˆØ�ŠÐ[Ð[¥Z°µD½%°=Ñ%AÔ%AÐ J  ÈÀsÐ[Ñ[Ô[Ñ[Ô[Ð[Ð[Ð[r5   c                óŽ  •— t          |t          ¦  «        r|d         n|}t          |t          ¦  «        r&|                     | j        rdnd|¦  «        d         }n:t          |t          ¦  «        r#|d         d                              dd¦  «        }n|}‰                      |j        g |j        dd…         ¢d‘R Ž ¦  «         dS )z*Capture the class logits leaving the head.r#   Úone2oneÚone2manyrŒ  r›   r$   N)	r  ri   r�  rE  Úend2endrI  rz  r¡   r  )r  r  ÚoutputÚrawr	  rŒ  s        €r3   Úhookz"class_activation_map.<locals>.hookY  sÅ   ø€ å% f­eÑ4Ô4Ð@ˆf�QŒiˆi¸&ˆÝ�c�4Ñ Ô ð 	Ø—’ V¤^ÐC˜	˜	¸ÀSÑIÔIÈ(ÔSˆAˆAÝ˜�UÑ#Ô#ð 	Ø�A”�r”
×$Ò$ Q¨Ñ*Ô*ˆAˆAàˆAØ�Š�i�a”iÐ1 ¤¨¨!¨¤Ð1¨bÐ1Ð1Ð1Ñ2Ô2Ð2Ð2Ð2r5   r›   NFTr$   )r!   r  r   r#   )ÚkeepdimzExplaining the z( strongest predictions per image out of rÑ   )r  )r¶   z-inf)Ú
descending)Úretain_graphÚbilinear)r'   Úalign_corners)r$   r   gH¯¼šò×z>rT   r   .r  z_cam.jpgr"   zSaving z... (LayerCAM)c                óN  •— t          | t          j        ¦  «        r|                      ¦   «         S t          | t          ¦  «        r ˆfd„|                      ¦   «         D ¦   «         S t          | t          t          f¦  «        r$ t          | ¦  «        ˆfd„| D ¦   «         ¦  «        S | S )z?Detach tensors in nested model outputs from the autograd graph.c                ó.   •— i | ]\  }}| ‰|¦  «        “ŒS r{   r{   )rP   r™  r½   r²  s      €r3   ú
<dictcomp>z8class_activation_map.<locals>.detach.<locals>.<dictcomp>œ  s'   ø€ Ð7Ð7Ð7¡T Q¨�A�v�v˜a‘y”yÐ7Ð7Ð7r5   c              3  ó.   •K  — | ]} ‰|¦  «        V — Œd S rÅ  r{   )rP   r½   r²  s     €r3   rr   z7class_activation_map.<locals>.detach.<locals>.<genexpr>ž  s+   øè è € Ð0Ð0¨˜6˜6 !™9œ9Ð0Ð0Ð0Ð0Ð0Ð0r5   )	r  r  r  r²  r�  ÚitemsrS  ri   r  )rØ   r²  s    €r3   r²  z$class_activation_map.<locals>.detach—  sŸ   ø€ å�a�œÑ&Ô&ð 	Ø—8’8‘:”:ÐÝ�a�ÑÔð 	8Ø7Ð7Ð7Ð7¨Q¯WªW©Y¬YÐ7Ñ7Ô7Ð7Ý�a�$¥˜Ñ'Ô'ð 	1Ø•4˜‘7”7Ð0Ð0Ð0Ð0¨aÐ0Ñ0Ô0Ñ0Ô0Ð0Øˆr5   )@Úmodelr  ÚshapesÚregister_forward_pre_hookÚregister_forward_hookr   r  Úinference_modeÚ
contextlibÚnullcontextÚenable_gradÚcloneÚrequires_grad_ÚremoveÚcatr  r  r  r}  r^   ro  Úsigmoidr¶   r   r-   r   r´  r,   r³  Úmasked_fillr   r÷  Úgatherrë  ÚautogradÚgradrm  ÚnnÚ
functionalÚinterpolaterz  Ústackrô  r   rr  rÒ   r  r²  ÚaminÚpermuter  r   r   r  r)   r  Úrepeatr¹   rf  rº   ÚCOLORMAP_JETrº  rm   rå  )'r#  r   r,  r   r7  rØ  r  ÚargsÚkwargsr  r  ÚheadÚhandlesr¦  Úhandler	  rþ  rÔ  r_   Úrankr  Úcamr™  ÚlevelsÚgradsr  r¹  rQ   ÚlevelÚimsr¿   rÀ   r  r¯  r£   Úheatmapr  r²  rŒ  s'                                       @@@r3   Úclass_activation_maprH  2  sœ  øøø€ ð@ �r€L€Dˆ&ð\ð \ð \ð \ð \ð
	3ð 	3ð 	3ð 	3ð 	3ð Œ;Ô˜RÔ €DØ#Ð#€D„J�”Ø×-Ò-¨hÑ7Ô7¸×9SÒ9SÐTXÑ9YÔ9YÐZ€Gõ )3Ð	P�Ô	˜eÑ	$Ô	$Ð	$½
Ô8NÑ8PÔ8Pð  Fð  FÕRWÔRcÑReÔReð  Fð  Fð	 Ø—’‘”×*Ò*¨4Ñ0Ô0ˆBØ�E˜"Ð.˜tÐ.Ð.Ð. vÐ.Ð.ˆEà!ð  ð  �Ø—’‘”��ð ø˜'ð  ð  �Ø—’‘”��ð øøøåŒI�f˜aÑ Ô ˆØÐÝ”/ 'µ´ÀAÄHÐMÑMÔM×UÒUÑWÔWˆCØ�s˜a’x C¨!¬'°!¬*Ò$4Ñ5Ô6ˆCÝ�3‰xŒxð Ø�a�a�a˜�f”I�Ø�FŠF�1‰IŒIˆØ—	’	‘”˜tÒ#¨¨Q¯VªV°A¸t¨VÑ-DÔ-DÒ(DÑEˆÝ•�D—H’H˜Q‘K”K×$Ò$Ñ&Ô&Ñ'Ô'¨Ñ.Ô.ˆÝˆt�xŠx˜‰{Œ{×ÒÑ!Ô!Ñ"Ô" QÒ&Ð&ÝŒNÐr¨QÐrÐrÕX[Ð\`×\dÒ\dÐefÑ\gÔ\g×\lÒ\lÑ\nÔ\nÑXoÔXoÐrÐrÐrÑsÔsÐsÝŒ|˜A a¤hÐ/Ñ/Ô/°$·(²(¸1Àd°(Ñ2KÔ2K×2QÒ2QÐVWÐ2QÑ2XÔ2XÑXˆØ—’˜t˜e¥U¨6¡]¤]Ñ3Ô3×;Ò;¸AÈ$Ð;ÑOÔO×VÒVÐWXÐZ^Ñ_Ô_ˆØˆÝ�q‘”ð 	Fñ 	FˆAØˆFÝ”N×'Ò'¨¯ª°°E¸!¸!¸!¸QÀÀQÁ¸Y¸,Ô4GÑ(HÔ(H×(LÒ(LÑ(NÔ(NÐPTÐcdÐghÐklÑglÒclÐ'ÑmÔmˆEÝ˜D %Ñ(Ô(ð Pð P‘��1Ø—W’W‘Y”Y—_’_¨�_Ñ+Ô+¨a¯gªg©i¬iÑ7×<Ò<¸QÈÐ<ÑMÔM�Ø—G’G �G‘N”N�Ý”HÔ'×3Ò3°A°r´xÀÀÀ´|È*ÐdiÐ3ÑjÔj�Ø—’˜a !§&¢&¨¸ &Ñ">Ô">×"DÒ"DÈÐ"DÑ"NÔ"NÑNÑOÔOÐOÐOõ ”K Ñ'Ô'×,Ò,¨QÑ/Ô/ˆEØ˜;�%�%­E¬M¸#¸uÑ,EÔ,EˆC‰Cð	Fð+ Fð  Fð  Fñ  Fô  Fð  Fð  Fð  Fð  Fð  Fð  Føøøð  Fð  Fð  Fð  Fð  Fð  Fð  Fñ  Fô  Fð  Fð  Fð  Fð  Fð  Fð  Føøøð  Fð  Fð  Fð  FðD �;Š;�q‰>Œ>˜CÑ×
%Ò
%Ñ
'Ô
'×
+Ò
+Ñ
-Ô
-×
3Ò
3Ñ
5Ô
5€CØ
�)Š)‰+Œ+�a�a�a˜˜!˜�eÔ
×
"Ò
"Ñ
$Ô
$€CØ�XŠX�f dˆXÑ+Ô+¨S¯XªX°fÀd¨XÑ-KÔ-Kˆ€BØ�"‰H˜˜b™Ÿš¨D˜Ñ1Ô1Ñ1°CÑ7×
=Ò
=Ñ
?Ô
?×
GÒ
GÈÈ1ÈaÐQRÑ
SÔ
S×
WÒ
WÑ
YÔ
Y×
_Ò
_Ñ
aÔ
aÐbeÐgkÐgkÐikÐgkÐbkÔ
l€CØ‡N‚N˜4¨$€NÑ/Ô/Ð/Ý˜˜c 5Ñ)Ô)ð 1ð 1‰	ˆˆ3�Ý˜8­¨a©¬¬Ð&?Ð&?Ð&?Ñ?Ñ@Ô@ˆÝÔ"¨#¬)°A¬,¸!Ò*;Ð*; 3 3ÀÀSÈ"È1È"ÀWÄ×ATÒATÐUVÐXYÑAZÔAZÑ[Ô[ˆÝ”/¥#Ô"3°AµsÔ7GÑ"HÔ"HÈ#ÈsÐTWÐYZÑ[Ô[ˆÝŒ•C˜‘F”F˜GÑ$Ô$Ð$ÝŒÐ/˜aÐ/Ð/Ð/Ñ0Ô0Ð0Ð0ðð ð ð ð ð ˆ6�%‰=Œ=ÐsO   ÂTÂS/Â!3C.ÃS/Ã.D	Ä	OS/Ó#TÓ/S3	Ó3TÓ6S3	Ó7TÔT
ÔT
)r   r   )r   r   r   r   r   r   )r   r   )NNr§   r©   )rª   r   r«   r¬   r­   r¬   r®   rm   r'   rm   r   r   )r  r   r  r   r/   r   r  rh   r  rh   r»  rh   )NNr&  NNr'  rp   Tr�  TT)r(  r)  r*  r+  r,  r-  r
  rm   rÿ  r.  r  r/  r0  r   r1  r   r»  rh   r�  r   r2  rh   r3  rh   r   r4  )ri  r>  N)r  rm   rj  rm   r  r/  )rŒ  r�  rŽ  rm   )r˜  r™  rÏ  rš  )rÐ  r   r®   rm   rb  r   r›  rm   )NNrs   )r¤  r¥  r¦  r§  r
  r¨  r©  rª  r«  r-  r¬  r   r   r†  )r¿  T)rÀ  rm   rÁ  rh   )r   r¥  r,  r	  r   r   r7  r   r  r   r   r   ):Ú
__future__r   r(  rJ  Ú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   Úultralytics.utils.torch_utilsr   r4   r7   r2  r`   r²   rŸ   r¥   r¸   ÚCOLORMAP_INFERNOr;  r»   rÅ   rÇ   r  r%  rh  rŠ  r—  r£  r¾  r  rH  r{   r5   r3   ú<module>rS     ss  ðð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à 
€
€
€
Ø Ð Ð Ð Ø €€€Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ø *Ð *Ð *Ð *Ð *Ð *à aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aØ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HØ 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4ðRð Rð Rð Rð Rð&MGð MGð MGð MGð MGñ MGô MGð MGð` 
ˆ‰Œ€ð �b”hàˆˆØˆˆØˆˆØˆˆØˆˆØˆˆØˆˆØˆˆØˆˆØˆˆØˆˆðð Œ*ðñ ô Ð ð$Dð Dð Dð Dð �‘”€ØÔ/¸Ô8HÐVZÐ[Ð[€ð
 ØØØð(ð (ð (ð (ð (ðV~-ð ~-ð ~-ð ~-ð ~-ñ ~-ô ~-ð ~-ðB €�„Ø€�„Ø"$¨t¨t°B©x¬xÀð 6ð 6ð 6ñ „ñ „ð6ðx ��h‘”ØØØØØð3ð 3ð 3ð 3ð 3ðl 
ð 04Ø"ØØ#'Ø#ØØØØØØðVð Vð Vð Vñ 
„ðVðr €�„ð5ð 5ð 5ð 5ñ „ð5ðp €�„Ø5>ÈÈÉÌð ð ð ð ñ „ðð@Oð Oð Oð Oð OðH #Ø#Øð;"ð ;"ð ;"ð ;"ð ;"ð| €�„ðb?ð b?ð b?ð b?ñ „ðb?ðV ØØðoð oð oð oð oð oð oð or5   