Ë
    Fêñiè’  ã                  ó"  — d dl mZ d dlZd dlZd dlZd dl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mZ d dlZd dlZd dl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m Z m!Z!m"Z"m#Z#m$Z$ d d
l%m&Z&m'Z'm(Z( d dl)m*Z*m+Z+m,Z, d dl-m.Z. dZ/h d£Z0h d£Z1de0› de1› �Z2d%d„Z3	 d&	 	 	 	 	 	 	 	 	 d'd„Z4d(d„Z5d)d„Z6d*d„Z7d+d„Z8d,d„Z9	 d-	 	 	 	 	 	 	 	 	 d.d„Z:	 d/	 	 	 	 	 	 	 	 	 d.d„Z;	 d/	 	 	 	 	 	 	 d0d„Z<d1d„Z=d2d„Z>d3d4d„Z?d5d6d„Z@ G d „ d!«      ZAd7d8d"„ZBd9d#„ZCd:d$„ZDy);é    )ÚannotationsN)Ú
ThreadPool)ÚPath)Ú
is_tarfile)ÚAny)ÚImageÚImageOps)Úcheck_class_names)Ú
ASSETS_URLÚDATASETS_DIRÚLOGGERÚNUM_THREADSÚROOTÚSETTINGS_FILEÚTQDMÚYAMLÚ	clean_urlÚcolorstrÚemojisÚis_dir_writeable)Ú
check_fileÚ
check_fontÚis_ascii)ÚdownloadÚsafe_downloadÚ
unzip_file)Úsegments2boxeszJSee https://docs.ultralytics.com/datasets for dataset formatting guidance.>   ÚbmpÚdngÚjp2ÚjpgÚmpoÚpngÚtifÚavifÚheicÚheifÚjpegÚtiffÚwebpÚjpeg2000>   ÚtsÚasfÚaviÚgifÚm4vÚmkvÚmovÚmp4ÚmpgÚwmvÚmpegÚwebmzSupported formats are:
images: z	
videos: c           	     ó"  — t         j                  › dt         j                  › �t         j                  › dt         j                  › �}}| D �cg c]9  }|j                  |j                  |d«      «      j                  dd«      d   dz   ‘Œ; c}S c c}w )zaConvert image paths to label paths by replacing 'images' with 'labels' and extension with '.txt'.ÚimagesÚlabelsé   ú.r   z.txt)ÚosÚsepÚjoinÚrsplit)Ú	img_pathsÚsaÚsbÚxs       úX/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/data/utils.pyÚimg2label_pathsrF   <   so   € ä—‘ˆx�vœbŸf™f˜XÐ&¬2¯6©6¨(°&¼¿¹¸Ð(Aˆ€BØIRÖSÀAˆB�G‰G�A—H‘H˜R “OÓ$×+Ñ+¨C°Ó3°AÑ6¸Ó?ÒSÐSùÒSs   Á>Bc           	     ó¼  — | st        j                  |› d�«       yt        j                  | t	        |t        | «      «      «      } g }g }g }| D ]Ü  }	 t        j                  «       }	t        j                  |«      j                  }
|j                  t        j                  «       |	z
  dz  «       |j                  |
«       t        j                  «       }	t        |d«      5 }|j                  «       }ddd«       t        j                  «       |	z
  }|dkD  r|j                  |
dz  |z  «       ŒÞ |st        j                  |› d�«       yt        j                   |«      }t        |«      dkD  rt        j"                  |d¬	«      nd}d
t        j                   |«      dz  d›d�}d|d›d|d›d�}|rHt        j                   |«      }t        |«      dkD  rt        j"                  |d¬	«      nd}d|d›d|d›d�}nd}||k  s|k  r t        j$                  |› d|› |› |› d�«       yt        j                  |› d|› |› |› d�«       y# 1 sw Y   �ŒTxY w# t        $ r Y �Œw xY w)aw  Check dataset file access speed and provide performance feedback.

    This function tests the access speed of dataset files by measuring ping (stat call) time and read speed. It samples
    up to `max_files` files from the provided list and warns if access times exceed the threshold.

    Args:
        files (list[str]): List of file paths to check for access speed.
        threshold_ms (float, optional): Threshold in milliseconds for ping time warnings.
        threshold_mb (float, optional): Threshold in megabytes per second for read speed warnings.
        max_files (int, optional): The maximum number of files to check.
        prefix (str, optional): Prefix string to add to log messages.

    Examples:
        >>> from pathlib import Path
        >>> image_files = list(Path("dataset/images").glob("*.jpg"))
        >>> check_file_speeds(image_files, threshold_ms=15)
    z%Image speed checks: No files to checkNiè  Úrbr   i   z*Image speed checks: failed to access filesr;   )Úddofz, size: i   ú.1fz KBzping: õ   Â±z msz, read: z MB/sÚ u   Fast image access âœ… (ú)zSlow image access detected (z‹). Use local storage instead of remote/mounted storage for better performance. See https://docs.ultralytics.com/guides/model-training-tips/)r   ÚwarningÚrandomÚsampleÚminÚlenÚtimeÚperf_counterr=   ÚstatÚst_sizeÚappendÚopenÚreadÚ	ExceptionÚnpÚmeanÚstdÚinfo)ÚfilesÚthreshold_msÚthreshold_mbÚ	max_filesÚprefixÚ
ping_timesÚ
file_sizesÚread_speedsÚfÚstartÚ	file_sizeÚfile_objÚ_Ú	read_timeÚavg_pingÚstd_pingÚsize_msgÚping_msgÚ	avg_speedÚ	std_speedÚ	speed_msgs                        rE   Úcheck_file_speedsrt   B   s\  € ñ( Ü�‰˜&˜Ð!FÐGÔHØô �M‰M˜%¤ Y´°E³
Ó!;Ó<€Eð €JØ€JØ€Kàò ˆð	ä×%Ñ%Ó'ˆEÜŸ™ ›
×*Ñ*ˆIØ×Ñœt×0Ñ0Ó2°UÑ:¸dÑBÔCØ×Ñ˜iÔ(ô ×%Ñ%Ó'ˆEÜ�a˜“ð $ (Ø—M‘M“O�÷$ä×)Ñ)Ó+¨eÑ3ˆIØ˜1Š}Ø×"Ñ" 9°Ñ#8¸9Ñ#DÔEøðñ$ Ü�‰˜&˜Ð!KÐLÔMØô �w‰w�zÓ"€HÜ-0°«_¸qÒ-@Œr�v‰v�j qÕ)Àa€HØœ"Ÿ'™' *Ó-°Ñ9¸#Ð>¸cÐB€HØ˜ �~ R¨° ~°SÐ9€HáÜ—G‘G˜KÓ(ˆ	Ü36°{Ó3CÀaÒ3G”B—F‘F˜;¨QÕ/ÈQˆ	Ø˜y¨˜o¨R°	¸#¨¸eÐD‰	àˆ	à�,Ò )¨lÒ":Ü�‰�v�hÐ5°h°ZÀ	¸{È8È*ÐTUÐVÕWä�‰ØˆhÐ2°8°*¸Y¸KÈÀzð RKð Lõ	
÷9$ñ $ûô
 ò 	Úð	ús+   ÁBIÃIÃ/;IÉI	ÉIÉ	IÉIc                óV  — d}| D ]%  }	 |t        j                  |«      j                  z  }Œ' t	        d«      j                  t        |«      j                  «       «      }|j                  dj                  | «      j                  «       «       |j                  «       S # t        $ r Y Œ¢w xY w)z>Return a single hash value of a list of paths (files or dirs).r   ÚhashlibrL   )r=   rU   rV   ÚOSErrorÚ
__import__Úsha256ÚstrÚencodeÚupdater?   Ú	hexdigest)ÚpathsÚsizeÚpÚhs       rE   Úget_hashr‚   �   s”   € à€DØò ˆð	Ø”B—G‘G˜A“J×&Ñ&Ñ&‰Dðô
 	�9Ó×$Ñ$¤S¨£Y×%5Ñ%5Ó%7Ó8€AØ‡H�HˆR�W‰W�U‹^×"Ñ"Ó$Ô%Ø�;‰;‹=Ðøô	 ò 	Ùð	ús   ‰"BÂ	B(Â'B(c                óÆ   — | j                   }| j                  dk(  r5	 | j                  «       x}r |j                  dd«      }|dv r
|d   |d   f}|S |S # t        $ r Y |S w xY w)zReturn exif-corrected PIL size.ÚJPEGi  N>   é   é   r;   r   )r   ÚformatÚgetexifÚgetrZ   )ÚimgÚsÚexifÚrotations       rE   Ú	exif_sizerŽ   œ   sz   € à�‰€AØ
‡z�z�VÒð	Ø—{‘{“}Ð$ˆtÐ$ØŸ8™8 C¨Ó.�Ø˜vÑ%Ø˜!™˜a ™d˜
�Að €Hˆ1€Høô ò 	ØØ€Hð	ús   �2A Á	A ÁA c                óà  — | \  \  }}}d\  }}}	 t        j                  |«      }|j                  «        t        |«      }|d   |d   f}|d   dkD  |d   dkD  z  sJ d|› d�«       ‚|j                  j                  «       t        v sJ d|j                  › dt        › �«       ‚|j                  j                  «       d	v r|t        |d
«      5 }	|	j                  dd«       |	j                  «       dk7  rBt        j                  t        j                  |«      «      j                  |ddd¬«       |› |› d�}ddd«       d}||f|||fS # 1 sw Y   ŒxY w# t        $ r}
d}|› |› d|
› �}Y d}
~
Œ-d}
~
ww xY w)zVerify one image.)r   r   rL   r;   r   é	   úimage size ú <10 pixelszInvalid image format ú. >   r!   r(   rH   éþÿÿÿé   ó   ÿÙr„   éd   ©ÚsubsamplingÚqualityú!: corrupt JPEG restored and savedNú : ignoring corrupt image/label: )r   rX   ÚverifyrŽ   r‡   ÚlowerÚIMG_FORMATSÚFORMATS_HELP_MSGÚseekrY   r	   Úexif_transposeÚsaverZ   )ÚargsÚim_fileÚclsrc   ÚnfÚncÚmsgÚimÚshaperg   Úes              rE   Úverify_imager­   ª   sŒ  € à!Ñ�N€Wˆc�Fà�K€BˆˆCðFÜ�Z‰Z˜Ó ˆØ
�	‰	ŒÜ˜"“ˆØ�q‘˜5 ™8Ð$ˆØ�a‘˜1‘  q¡¨A¡Ò.ÐP°+¸e¸WÀKÐ0PÓPÐ.Ø�y‰y�‰Ó ¤KÑ/ÐhÐ3HÈÏÉÈÐSUÔVfÐUgÐ1hÓhÐ/Ø�9‰9�?‰?Ó Ñ/Ü�g˜tÓ$ð P¨Ø—‘�r˜1”Ø—6‘6“8˜{Ò*Ü×+Ñ+¬E¯J©J°wÓ,?Ó@×EÑEÀgÈvÐcdÐnqÐEÔrØ#˜H W IÐ-NÐO�C÷	Pð
 ˆð �Sˆ>˜2˜r 3Ð&Ð&÷Pð Pûô ò FØˆØ�˜˜	Ð!AÀ!ÀÐE�ûðFús1   �B9E Ã	A(EÄ1
E ÅEÅE Å	E-ÅE(Å(E-c                ó   — | \  }}}}}}}}dddddg df\  }	}
}}}}}	 t        j                  |«      }|j                  «        t        |«      }|d   |d   f}|d   dkD  |d   dkD  z  sJ d|› d�«       ‚|j                  j                  «       t        v sJ d|j                  › d	t        › �«       ‚|j                  j                  «       d
v r|t        |d«      5 }|j                  dd«       |j                  «       dk7  rBt        j                  t        j                  |«      «      j                  |ddd¬«       |› |› d�}ddd«       t        j                  j                  |«      �ròd}
t        |d¬«      5 }|j                  «       j!                  «       j#                  «       D �cg c]  }t%        |«      sŒ|j'                  «       ‘Œ  }}t)        d„ |D «       «      r®|s¬t+        j,                  |D �cg c]  }|d   ‘Œ	 c}t*        j.                  ¬«      }|D �cg c]:  }t+        j,                  |dd t*        j.                  ¬«      j1                  dd«      ‘Œ< }}t+        j2                  |j1                  dd«      t5        |«      fd«      }t+        j,                  |t*        j.                  ¬«      }ddd«       t%        «      x}�r`|rN|j6                  d   d||z  z   k(  sJ dd||z  z   › d�«       ‚|dd…dd…f   j1                  d|«      dd…dd…f   }n5|j6                  d   dk(  sJ d|j6                  d   › d�«       ‚|dd…dd…f   }|j9                  «       dk  sJ d||dkD     › �«       ‚|j;                  «       dk\  sJ d ||dk     › �«       ‚|rdn|dd…df   j9                  «       }||k  sJ d!t=        |«      › d"|› d#|dz
  › �«       ‚t+        j>                  |dd$¬%«      \  }}t%        |«      |k  r—||   }|r|D �cg c]  }||   ‘Œ	 }}|› |› d&|t%        |«      z
  › d'�}ngd}t+        j@                  d|rd||z  z   ndft*        j.                  ¬«      }n3d}	t+        j@                  d|rd||z  z   ndft*        j.                  ¬«      }|r€|dd…dd…f   j1                  d||«      }|dk(  r_t+        jB                  |d(   dk  |d)   dk  z  d*d+«      jE                  t*        j.                  «      }t+        j2                  ||d,   gd¬-«      }|dd…dd…f   }||||||	|
|||f
S # 1 sw Y   �ŒèxY wc c}w c c}w c c}w # 1 sw Y   �Œ”xY wc c}w # tF        $ r!}d}|› |› d.|› �}ddddd|	|
|||g
cY d}~S d}~ww xY w)/zVerify one image-label pair.r   rL   Nr;   r�   r‘   r’   zinvalid image format r“   >   r!   r(   rH   r”   r•   r–   r„   r—   r˜   r›   úutf-8©Úencodingc              3  ó8   K  — | ]  }t        |«      d kD  –— Œ y­w)r…   N)rR   ©Ú.0rD   s     rE   ú	<genexpr>z%verify_image_label.<locals>.<genexpr>Ü   s   è ø€ Ò. a”s˜1“v •zÑ.ùs   ‚©Údtypeéÿÿÿÿé   zlabels require z columns eachzlabels require 5 columns, z columns detectedg)\�Âõ(ð?z,non-normalized or out of bounds coordinates g{®Gáz„¿z$negative class labels or coordinate zLabel class z exceeds dataset class count z. Possible class labels are 0-T)ÚaxisÚreturn_indexú: z duplicate labels removed).r   ).r;   g        ç      ð?).N)rº   rœ   )$r   rX   r�   rŽ   r‡   rž   rŸ   r    r¡   rY   r	   r¢   r£   r=   ÚpathÚisfileÚstripÚ
splitlinesrR   ÚsplitÚanyr[   ÚarrayÚfloat32ÚreshapeÚconcatenater   r«   ÚmaxrQ   ÚintÚuniqueÚzerosÚwhereÚastyperZ   )r¤   r¥   Úlb_filerc   ÚkeypointÚnum_clsÚnkptÚndimÚ
single_clsÚnmr§   Úner¨   r©   ÚsegmentsÚ	keypointsrª   r«   rg   rD   ÚlbÚclassesÚnlÚpointsÚmax_clsrk   ÚiÚkpt_maskr¬   s                                rE   Úverify_image_labelrß   Ã   s�  € àJNÑG€GˆW�f˜h¨°°t¸Zà/0°!°Q¸¸2¸rÀ4Ð/GÑ,€BˆˆB��C˜ 9ð@Cä�Z‰Z˜Ó ˆØ
�	‰	ŒÜ˜"“ˆØ�q‘˜5 ™8Ð$ˆØ�a‘˜1‘  q¡¨A¡Ò.ÐP°+¸e¸WÀKÐ0PÓPÐ.Ø�y‰y�‰Ó ¤KÑ/ÐhÐ3HÈÏÉÈÐSUÔVfÐUgÐ1hÓhÐ/Ø�9‰9�?‰?Ó Ñ/Ü�g˜tÓ$ð P¨Ø—‘�r˜1”Ø—6‘6“8˜{Ò*Ü×+Ñ+¬E¯J©J°wÓ,?Ó@×EÑEÀgÈvÐcdÐnqÐEÔrØ#˜H W IÐ-NÐO�C÷	Pô �7‰7�>‰>˜'Õ"ØˆBÜ�g¨Ô0ð 4°AØ)*¯©«¯©Ó)9×)DÑ)DÓ)FÖQ AÌ#ÈaÍ&�a—g‘g•iÐQ�ÐQÜÑ.¨2Ô.Ô.¹Ü Ÿh™h°bÖ'9°¨¨!«Ò'9ÄÇÁÔL�GØZ\Ö]ÐUV¤§¡¨¨1¨2¨´b·j±jÔ A× IÑ IÈ"ÈaÕ PÐ]�HÐ]ÜŸ™¨¯©¸¸QÓ)?ÄÐPXÓAYÐ(ZÐ\]Ó^�BÜ—X‘X˜b¬¯
©
Ô3�÷4ô ˜“Wˆ}ˆr‰}ÙØŸ8™8 A™;¨1¨t°d©{©?Ò;Ðo¸ÐPQÐTXÐ[_ÑT_ÑP_ÐNaÐanÐ=oÓoÐ;Ø¢ 1¡2 ™Y×.Ñ.¨r°4Ó8º¸B¸Q¸B¸Ñ?‘FàŸ8™8 A™;¨!Ò+ÐhÐ/IÈ"Ï(É(ÐSTÉ+ÈÐVgÐ-hÓhÐ+Ø¢ 1¡2 ™Y�Fà—z‘z“| tÒ+ÐsÐ/[Ð\bÐciÐlpÑcpÑ\qÐ[rÐ-sÓsÐ+Ø—v‘v“x 5Ò(ÐaÐ,PÐQSÐTVÐY^ÑT^ÑQ_ÐP`Ð*aÓaÐ(ñ  *™!¨r²!°Q°$©x¯|©|«~�Ø Ò(ð Ø"¤3 w£< .Ð0MÈgÈYð W3Ø3:¸Q±;°-ðAóÐ(ô —y‘y ¨!¸$Ô?‘��1Ü�q“6˜B’;Ø˜A™�BÙØ9:Ö#;°A H¨Q£KÐ#;˜Ð#;Ø#˜H W I¨R°´S¸³V±¨}Ð<UÐV‘Cà�Ü—X‘X˜q±x 1 t¨d¡{¢?ÀQÐGÌrÏzÉzÔZ‘àˆBÜ—‘˜1±8˜q 4¨$¡;šÀÐCÌ2Ï:É:ÔVˆBÙØš1˜a™b˜5™	×)Ñ)¨"¨d°DÓ9ˆIØ�qŠyÜŸ8™8 Y¨vÑ%6¸Ñ%:¸yÈÑ?PÐSTÑ?TÑ$UÐWZÐ\_Ó`×gÑgÔhj×hrÑhrÓs�ÜŸN™N¨I°xÀ	Ñ7JÐ+KÐRTÔU�	Ø’�2�A�2�‰YˆØ˜˜E 8¨Y¸¸BÀÀBÈÐKÐK÷gPñ Püò Rùâ'9ùÚ]÷	4ñ 4üò8 $<øô ò CØˆØ�˜˜	Ð!AÀ!ÀÐEˆØ�d˜D $¨¨b°"°b¸"¸cÐBÕBûðCúsŽ   žB9U ÃA(T%Ä?7U Å6/UÆ%T2Æ6T2Ç)UÇ1T7
Ç=UÈ?T<ÉAUÊ/EU Ð UÐDU Ô%T/Ô*U Ô2UÕUÕU Õ	U=ÕU8Õ2U=Õ8U=c                ó4  — ddl m} ddlm} t	        j
                  t        j                  | «      «      }|j                  dd \  }}g }t        |d¬«      5 }	|	D ]f  }
t        t        |
j                  «       «      \  }}}}}||dz  z
  |z  }||dz  z
  |z  }||z  }||z  } |j                  ||||t        |«      f«       Œh 	 ddd«       |j                  d«      \  }}|D ]ˆ  \  }}}}}t        d„  ||d	«      D «       «      }|j!                  ||f||d|d
¬«      }|j#                  |«       d|d   z  d|d   z  z   d|d   z  z   }|j%                  ||dz
  ||   |dk  rdnd|¬«       ŒŠ |j'                  |«       |j)                  «        y# 1 sw Y   ŒÌxY w)aÔ  Visualize YOLO annotations (bounding boxes and class labels) on an image.

    This function reads an image and its corresponding annotation file in YOLO format, then draws bounding boxes around
    detected objects and labels them with their respective class names. The bounding box colors are assigned based on
    the class ID, and the text color is dynamically adjusted for readability, depending on the background color's
    luminance.

    Args:
        image_path (str): Path to the image file to annotate. The file must be readable by PIL.
        txt_path (str): Path to the annotation file in YOLO format, which should contain one line per object.
        label_map (dict[int, str]): A dictionary that maps class IDs (integers) to class labels (strings).

    Examples:
        >>> label_map = {0: "cat", 1: "dog", 2: "bird"}  # Should include all annotated classes
        >>> visualize_image_annotations("path/to/image.jpg", "path/to/annotations.txt", label_map)
    r   N)Úcolorsr•   r¯   r°   r;   c              3  ó&   K  — | ]	  }|d z  –— Œ y­w)éÿ   N© )r´   Úcs     rE   rµ   z.visualize_image_annotations.<locals>.<genexpr>-  s   è ø€ Ò< !�a˜#•gÑ<ùs   ‚FÚnone)Ú	linewidthÚ	edgecolorÚ	facecolorg¼–�z6Ë?g¥,Cëâæ?g]mÅþ²{²?r¹   g      à?ÚwhiteÚblack)ÚcolorÚbackgroundcolor)Úmatplotlib.pyplotÚpyplotÚultralytics.utils.plottingrá   r[   rÄ   r   rX   r«   ÚmapÚfloatrÂ   rW   rÉ   ÚsubplotsÚtupleÚ	RectangleÚ	add_patchÚtextÚimshowÚshow)Ú
image_pathÚtxt_pathÚ	label_mapÚpltrá   rŠ   Ú
img_heightÚ	img_widthr   ÚfileÚlineÚclass_idÚx_centerÚy_centerÚwidthÚheightrD   ÚyÚwr�   rk   ÚaxÚlabelrì   ÚrectÚ	luminances                             rE   Úvisualize_image_annotationsr    sº  € õ" $å1ä
�(‰(”5—:‘:˜jÓ)Ó
*€CØŸI™I b q˜MÑ€J�	Ø€KÜ	ˆh Ô	)ð <¨TØò 	<ˆDÜ:=¼eÀTÇZÁZÃ\Ó:RÑ7ˆH�h ¨%°Ø˜E A™IÑ%¨Ñ2ˆAØ˜F Q™JÑ&¨*Ñ4ˆAØ˜	Ñ!ˆAØ˜Ñ#ˆAØˆK×Ñ  1 a¨¬C°«MÐ:Õ;ñ	<÷<ð �L‰L˜‹O�E€A€rØ(ò rÑˆˆ1ˆa��EÜÑ<¡v¨e°UÓ';Ô<Ó<ˆØ�}‰}˜a ˜V Q¨°QÀ%ÐSYˆ}ÓZˆØ
�‰�TÔØ˜U 1™XÑ%¨°°q±Ñ(9Ñ9¸FÀUÈ1ÁXÑ<MÑMˆ	Ø
�‰��1�q‘5˜) EÑ*¸YÈº_±'ÐRYÐkpˆÕqðrð ‡I�Iˆc„NØ‡H�H…J÷!<ð <ús   ÁA,FÆFc                óX  — t        j                  | t         j                  ¬«      }t        j                  |t         j                  ¬«      }|j                  |j                  d   ddf«      }t        j                  |||¬«       | d   |z  | d   |z  }}t        j                  |||f«      S )a]  Convert a list of polygons to a binary mask of the specified image size.

    Args:
        imgsz (tuple[int, int]): The size of the image as (height, width).
        polygons (list[np.ndarray]): A list of polygons. Each polygon is a 1D array of coordinates with length M, where
            M % 2 = 0 (alternating x, y values).
        color (int, optional): The color value to fill in the polygons on the mask.
        downsample_ratio (int, optional): Factor by which to downsample the mask.

    Returns:
        (np.ndarray): A binary mask of the specified image size with the polygons filled in.
    r¶   r   r¸   r•   )rì   r;   )
r[   rË   Úuint8ÚasarrayÚint32rÆ   r«   Úcv2ÚfillPolyÚresize)ÚimgszÚpolygonsrì   Údownsample_ratioÚmaskÚnhÚnws          rE   Úpolygon2maskr  6  sŽ   € ô �8‰8�E¤§¡Ô*€DÜ�z‰z˜(¬"¯(©(Ô3€HØ×Ñ §¡°Ñ!2°B¸Ð :Ó;€HÜ‡L�L��x uÕ-Ø�A‰hÐ*Ñ*¨E°!©HÐ8HÑ,Hˆ€Bä�:‰:�d˜R ˜HÓ%Ð%ó    c                óŠ   — t        j                  |D �cg c]   }t        | |j                  d«      g||«      ‘Œ" c}«      S c c}w )a`  Convert a list of polygons to a set of binary masks of the specified image size.

    Args:
        imgsz (tuple[int, int]): The size of the image as (height, width).
        polygons (list[np.ndarray]): A list of polygons. Each polygon is an array of coordinates that can be reshaped to
            (-1, 2) as (x, y) point pairs.
        color (int): The color value to fill in the polygons on the masks.
        downsample_ratio (int, optional): Factor by which to downsample each mask.

    Returns:
        (np.ndarray): A set of binary masks of the specified image size with the polygons filled in.
    r¸   )r[   rÄ   r  rÆ   )r  r  rì   r  rD   s        rE   Úpolygons2masksr  N  s9   € ô �8‰8Ð\dÖeÐWX”\ %¨!¯)©)°B«-¨¸%ÐAQÕRÒeÓfÐfùÒes   ”%A c                ó   — t        j                  | d   |z  | d   |z  ft        |«      dkD  rt         j                  nt         j                  ¬«      }g }g }|D ]j  }t        | |j                  d«      g|d¬«      }|j                  |j                  |j                  «      «       |j                  |j                  «       «       Œl t        j                  |«      }t        j                  | «      }t        j                  |«      |   }t        t        |«      «      D ]-  }	||	   |	dz   z  }||z   }t        j                  |d|	dz   ¬«      }Œ/ ||fS )z:Return a downsampled overlap mask and sorted area indices.r   r;   rã   r¶   r¸   )r  rì   )Úa_minÚa_max)r[   rË   rR   r  r  r  rÆ   rW   rÍ   r·   Úsumr  ÚargsortrÄ   ÚrangeÚclip)
r  rÖ   r  ÚmasksÚareasÚmsÚsegmentr  ÚindexrÝ   s
             rE   Úpolygons2masks_overlapr+  `  s6  € ô �H‰HØ	ˆq‰Ð%Ñ	% u¨Q¡xÐ3CÑ'CÐDÜ˜h›-¨#Ò-Œb�hŠh´2·8±8ô€Eð €EØ	€BØò !ˆÜØØ�_‰_˜RÓ Ð!Ø-Øô	
ˆð 	�	‰	�$—+‘+˜eŸk™kÓ*Ô+Ø�‰�T—X‘X“ZÕ ð!ô �J‰J�uÓ€EÜ�J‰J˜�vÓ€EÜ	�‰�"‹�eÑ	€BÜ”3�x“=Ó!ò 5ˆØ�!‰u˜˜A™‰ˆØ˜‘ˆÜ—‘˜ Q¨a°!©eÔ4‰ð5ð �%ˆ<Ðr  c                ó�  — t        | j                  d«      «      xs t        | j                  d«      «      }|sJ d| j                  «       › d�«       ‚t	        |«      dkD  r)|D �cg c]  }|j
                  | j
                  k(  sŒ|‘Œ  }}t	        |«      dk(  s'J d| j                  «       › dt	        |«      › d|› �«       ‚|d   S c c}w )	a½  Find and return the YAML file associated with a Detect, Segment or Pose dataset.

    This function searches for a YAML file at the root level of the provided directory first, and if not found, it
    performs a recursive search. It prefers YAML files that have the same stem as the provided path.

    Args:
        path (Path): The directory path to search for the YAML file.

    Returns:
        (Path): The path of the found YAML file.
    z*.yamlzNo YAML file found in 'ú'r;   zExpected 1 YAML file in 'z', but found z.
r   )ÚlistÚglobÚrglobÚresolverR   Ústem)r¾   r_   rg   s      rE   Úfind_dataset_yamlr3  }  s¾   € ô �—‘˜8Ó$Ó%ÒC¬¨d¯j©j¸Ó.BÓ)C€EÙÐ=Ð+¨D¯L©L«NÐ+;¸1Ð=Ó=ˆ5Ü
ˆ5ƒz�A‚~Ø!Ö9�q Q§V¡V¨t¯y©yÓ%8’Ð9ˆÐ9Üˆu‹:˜Š?ÐkÐ7¸¿¹»Ð7GÀ}ÔUXÐY^ÓU_ÐT`Ð`cÐdiÐcjÐkÓkˆ?Ø�‰8€Oùò :s   Á$CÂCc                ó¼   — t        | «      }|j                  d«      s|j                  d«      r.d|v r*ddl}ddlm} |j                   |t        | «      «      «      S | S )zAConvert an NDJSON dataset or Platform dataset URI to YOLO format.z.ndjsonzul://z
/datasets/r   N)Úconvert_ndjson_to_yolo)rz   ÚendswithÚ
startswithÚasyncioÚultralytics.data.converterr5  Úrunr   )ÚdataÚdata_strr8  r5  s       rE   Ú convert_ndjson_to_yolo_if_neededr=  ‘  sS   € ä�4‹y€HØ×Ñ˜Ô#¨×(;Ñ(;¸GÔ(DÈÐYaÑIaÛåEà�{‰{Ñ1´*¸TÓ2BÓCÓDÐDØ€Kr  c           
     ó
  ‡— t        t        | «      «      }|j                  «       rt        |«      }d}t	        j
                  |«      st        |«      r3t        |t        dd¬«      }t        t        |z  «      }|j                  d}}t        j                  |d¬«      ŠdD ]S  }|‰vsŒ|dk7  sd‰vrt        t        | › d	|› d
�«      «      ‚t        j                  d«       ‰j!                  d«      ‰d<   ŒU d‰vrd‰vrt        t        | › d�«      «      ‚d‰v rDd‰v r@t#        ‰d   «      ‰d   k7  r,t        t        | › dt#        ‰d   «      › d‰d   › d�«      «      ‚d‰vr#t%        ‰d   «      D �cg c]  }d|› �‘Œ	 c}‰d<   nt#        ‰d   «      ‰d<   t'        ‰d   «      ‰d<   ‰j)                  dd«      ‰d<   t        |xs8 ‰j)                  d«      xs% t        ‰j)                  dd«      «      j                  «      }|j+                  «       s'|j-                  «       st        |z  j/                  «       }|‰d<   dD ]¹  }‰j)                  |«      sŒt1        ‰|   t2        «      rb|‰|   z  j/                  «       }|j+                  «       s-‰|   j5                  d«      r|‰|   dd z  j/                  «       }t3        |«      ‰|<   ŒŠ‰|   D �cg c]  }t3        ||z  j/                  «       «      ‘Œ  c}‰|<   Œ» ˆfd„dD «       \  }	}
|	�rÅt1        |	t6        «      r|	n|	gD �cg c]  }t        |«      j/                  «       ‘Œ }	}t9        d„ |	D «       «      �syt;        | «      }t        j<                  d«       d|› dt?        d„ |	D «       «      › d �}|
r|rt        j                  |«       n|d!t        › d"t@        › d �z  }tC        |«      ‚tE        jD                  «       }d}|
j5                  d#«      r$|
jG                  d$«      rt        |
t        d¬%«       n^|
j5                  d&«      r?t        j<                  d'|
› d(�«       tI        jJ                  |
jM                  «       d¬)«       ntO        |
d*‰i«       d+tQ        tE        jD                  «       |z
  d,«      › d-�}|d.v rd/|› d0tS        d1t        «      › �nd2|› d3�}
t        j<                  d4|
› d5�«       tU        tW        ‰d   «      rd6«       ‰S d7«       ‰S c c}w c c}w c c}w )8am  Download, verify, and/or unzip a dataset if not found locally.

    This function checks the availability of a specified dataset, and if not found, it has the option to download and
    unzip the dataset. It then reads and parses the accompanying YAML data, ensuring key requirements are met and also
    resolves paths related to the dataset.

    Args:
        dataset (str): Path to the dataset or dataset descriptor (like a YAML file).
        autodownload (bool, optional): Whether to automatically download the dataset if not found.

    Returns:
        (dict[str, Any]): Parsed dataset information and paths.
    rL   TF©ÚdirÚunzipÚdelete)Úappend_filename)ÚtrainÚvalrE  Ú
validationú 'uE   :' key missing â�Œ.
'train' and 'val' are required in all data YAMLs.zBrenaming data YAML 'validation' key to 'val' to match YOLO format.Únamesr¨   uI    key missing â�Œ.
 either 'names' or 'nc' are required in all data YAMLs.z 'names' length z
 and 'nc: z' must match.Úclass_Úchannelsé   r¾   Ú	yaml_file)rD  rE  ÚtestÚminivalz../Nc              3  ó@   •K  — | ]  }‰j                  |«      –— Œ y ­w©N)r‰   )r´   rD   r;  s     €rE   rµ   z$check_det_dataset.<locals>.<genexpr>à  s   øè ø€ Ò7˜aˆd�h‰h�q�kÑ7ùs   ƒ)rE  r   c              3  ó<   K  — | ]  }|j                  «       –— Œ y ­wrP  ©Úexistsr³   s     rE   rµ   z$check_det_dataset.<locals>.<genexpr>ã  s   è ø€ Ò+ !�1—8‘8—:Ñ+ùs   ‚z	Dataset 'z"' images not found, missing path 'c              3  óB   K  — | ]  }|j                  «       rŒ|–— Œ y ­wrP  rR  r³   s     rE   rµ   z$check_det_dataset.<locals>.<genexpr>æ  s   è ø€ ÒHjÈqÐ_`×_gÑ_gÕ_iÌÑHjùs   ‚˜r-  z%
Note dataset download directory is 'z'. You can update this in 'Úhttpú.zip)Úurlr@  rB  zbash zRunning z ...©ÚcheckÚyamlú(r;   zs)>   Nr   u   success âœ… z, saved to Úboldzfailure u    â�ŒzDataset download ú
z	Arial.ttfzArial.Unicode.ttf),r   r   Úis_dirr3  ÚzipfileÚ
is_zipfiler   r   r   Úparentr   ÚloadÚSyntaxErrorr   r   rN   ÚpoprR   r$  r
   r‰   rS  Úis_absoluter1  Ú
isinstancerz   r7  r.  Úallr   r^   Únextr   ÚFileNotFoundErrorrS   r6  Ú
subprocessr:  rÂ   ÚexecÚroundr   r   r   )ÚdatasetÚautodownloadr   Úextract_dirÚnew_dirÚkrÝ   r¾   rD   rE  r‹   ÚnameÚmÚtÚrÚdtr;  s                   @rE   Úcheck_det_datasetrw  �  s»  ø€ ô ”
˜7Ó#Ó$€DØ‡{�{„}Ü  Ó&ˆð €KÜ×Ñ˜$Ô¤:¨dÔ#3Ü ¬,¸dÈ5ÔQˆÜ ¤°Ñ!7Ó8ˆØ$(§K¡K°�\ˆô �9‰9�T¨4Ô0€Dð ò 1ˆØ�DŠ=Ø�EŠz˜\°Ñ5Ü!Ü˜g˜Y b¨¨Ð+qÐrÓsóð ô �N‰NÐ_Ô`ØŸ(™( <Ó0ˆD�ŠKð1ð �dÑ˜t¨4Ñ/Üœ& G 9Ð,vÐ!wÓxÓyÐyØ�$�˜4 4™<¬C°°W±Ó,>À$ÀtÁ*Ò,LÜœ& G 9Ð,<¼SÀÀgÁÓ=OÐ<PÐPZÐ[_Ð`dÑ[eÐZfÐfsÐ!tÓuÓvÐvØ�dÑÜ/4°T¸$±ZÓ/@ÖA¨!˜6 ! šÒAˆˆWŠä˜˜g™Ó'ˆˆT‰
ä% d¨7¡mÓ4€Dˆ�MØ—x‘x 
¨AÓ.€DˆÑô �ÒY˜tŸx™x¨Ó/ÒY´4¸¿¹ÀÈbÓ8QÓ3R×3YÑ3YÓZ€DØ�;‰;Œ= ×!1Ñ!1Ô!3Ü˜tÑ#×,Ñ,Ó.ˆð €Dˆ�LØ.ò GˆØ�8‰8�A�;Ü˜$˜q™'¤3Ô'Ø˜D ™G‘^×,Ñ,Ó.�Ø—x‘x”z d¨1¡g×&8Ñ&8¸Ô&?Ø  Q¡¨¨ Ñ+×4Ñ4Ó6�AÜ˜a›&��Q’à>BÀ1¹gÖF¸œ3  q¡×1Ñ1Ó3Õ4ÒF��Q’ðGó 8Ð#6Ô7�F€CˆÚ
Ü2<¸SÄ$Ô2G©3ÈcÈUÖT QŒt�A‹w�‰Õ ÐTˆÐTÜÑ+ sÔ+Õ+Ü˜WÓ%ˆDÜ�K‰K˜ŒOØ˜D˜6Ð!CÄDÑHjÐTWÔHjÓDjÐCkÐklÐmˆAÙ‘\Ü—‘˜qÕ!àÐ=¼l¸^ÐKfÔgtÐfuÐuvÐwÑw�Ü'¨Ó*Ð*Ü—	‘	“ˆAØˆAØ�|‰|˜FÔ#¨¯
©
°6Ô(:Ü !¬¸dÖCØ—‘˜gÔ&Ü—‘˜h q c¨Ð.Ô/Ü—‘˜qŸw™w›y°Ö5ä�Q˜ ˜Ô'Ø”Uœ4Ÿ9™9›;¨™?¨AÓ.Ð/¨rÐ2ˆBØRSÐW`ÑR`�,˜r˜d +¬h°v¼|Ó.LÐ-MÑNÐhpÐqsÐptÐtxÐfyˆAÜ�K‰KÐ+¨A¨3¨bÐ1Ô2Üœh t¨G¡}Ô5ˆ{ÔOà€Kð <OÔOà€Kùòg Bùò, Gùò
 Us   Å0S2Ë#S7Ì! S<c                óÈ	  — t        | «      j                  d«      rt        | t        dd¬«      } n8t        | «      j	                  d«      rt        | «      }t        |t        dd¬«      } t        | «      } | j                  «       r| nt        | z  j                  «       }|j                  «       sõ|j                  dk7  rt        d| › d�«      ‚t        j                  d«       t        j                  d	|› d
�«       t        j                  «       }t        | «      dk(  r*t        j                   dt        t"        dz  «      gd¬«       n!t%        t&        › d| › d�|j(                  ¬«       t        j                  dt        j                  «       |z
  d›dt+        d|«      › d�«       |dz  }|j                  «       s t        j                  d|› �«       t-        |j/                  d«      «      t-        |j/                  d«      «      z   x}r8ddlm} t        j                  dt5        |«      › d�«        ||d¬ «      }|dz  }nt        j6                  d!|› d"�«       |d#z  j9                  «       r|d#z  n1|d$z  j9                  «       r|d$z  n|d%z  j9                  «       r|d%z  nd&}|d'z  j9                  «       r|d'z  nd&}	|d#k(  r|st        j                  d(«       |	}n|d'k(  r|	st        j                  d)«       |}	t5        |dz  j;                  d*«      D �
cg c]  }
|
j                  «       sŒ|
‘Œ c}
«      }|dz  j=                  «       D �
cg c]  }
|
j                  «       sŒ|
j>                  ‘Œ! }}
tA        tC        tE        |«      «      «      }|||	d+œjG                  «       D �]%  \  }}t+        |› d,�«      › d-|› d.�}|€t        j                  |«       Œ3|j/                  d/«      D �cg c](  }|j                  d0d& jI                  «       tJ        v sŒ'|‘Œ* }}t5        |«      }t5        |D �ch c]  }|j(                  ’Œ c}«      }|dk(  r5|dk(  rtM        | › d1|› d2�«      ‚t        j                  |› d3|› d4|› d5�«       ŒÝ||k7  r&t        j6                  |› d3|› d4|› d6|› d7|› d8�
«       �Œt        j                  |› d3|› d4|› d9�«       �Œ( |||	||d:d;œS c c}
w c c}
w c c}w c c}w )<a¨  Check a classification dataset such as Imagenet.

    This function accepts a `dataset` name and attempts to retrieve the corresponding dataset information. If the
    dataset is not found locally, it attempts to download the dataset from the internet and save it locally.

    Args:
        dataset (str | Path): The name of the dataset.
        split (str, optional): The split of the dataset. Either 'val', 'test', or ''.

    Returns:
        (dict[str, Any]): A dictionary containing the following keys:

            - 'train' (Path): The directory path containing the training set of the dataset.
            - 'val' (Path): The directory path containing the validation set of the dataset.
            - 'test' (Path): The directory path containing the test set of the dataset.
            - 'nc' (int): The number of classes in the dataset.
            - 'names' (dict[int, str]): A dictionary of class names in the dataset.
    )zhttp:/zhttps:/TFr?  )rV  z.tarz.gzrL   zSClassification datasets must be a directory (data="path/to/dir") not a file (data="z7"), See https://docs.ultralytics.com/datasets/classify/z Dataset not found, missing path z, attempting download...ÚimagenetÚbashzdata/scripts/get_imagenet.shrX  ú/rV  )r@  u   Dataset download success âœ… (rJ   zs), saved to r\  r]  rD  z#Dataset 'split=train' not found at z*.jpgz*.pngr   )Úsplit_classify_datasetzFound z1 images in subdirectories. Attempting to split...gš™™™™™é?)Útrain_ratiozNo images found in z or its subdirectories.rE  rF  ÚvalidNrM  z:Dataset 'split=val' not found, using 'split=test' instead.z:Dataset 'split=test' not found, using 'split=val' instead.Ú*©rD  rE  rM  ú:ú ú...ú*.*r;   rG  z:' no training images foundz found z images in z classes (no images found)z classes (requires z classes, not rM   u    classes âœ… rK  )rD  rE  rM  r¨   rH  rJ  )'rz   r7  r   r   r6  r   r   r^  r1  ÚsuffixÚ
ValueErrorr   r^   rN   rS   rj  r:  r   r   r   ra  r   r.  r0  Úultralytics.data.splitr|  rR   ÚerrorrS  r/  Úiterdirrr  ÚdictÚ	enumerateÚsortedÚitemsrž   rŸ   ri  )rm  rÂ   r   Údata_dirrt  Ú	train_setÚimage_filesr|  Úval_setÚtest_setrD   r¨   rH  rq  Úvrc   r¾   r_   r§   Únds                       rE   Úcheck_cls_datasetr•  ý  s–  € ô( ˆ7ƒ|×ÑÐ4Ô5Ü ¬\ÀÈeÔT‰Ü	ˆW‹×	Ñ	Ð6Ô	7Ü˜'Ó"ˆÜ ¬,¸dÈ5ÔQˆä�7‹m€GØ"Ÿ>™>Ô+‘´,ÀÑ2H×RÑRÓT€HØ�?‰?ÔØ�?‰?˜bÒ ÜØeÐfmÐenð oFð Fóð ô 	�‰�BŒÜ�‰Ð9¸(¸ÐC[Ð\Ô]Ü�I‰I‹KˆÜˆw‹<˜:Ò%Ü�N‰N˜F¤C¬Ð/MÑ(MÓ$NÐOÐW[Ö\äœ
�| 1 W I¨TÐ2¸¿¹ÕHÜ�‰Ð4´T·Y±Y³[À1±_ÀSÐ4IÈÔW_Ð`fÐhpÓWqÐVrÐrtÐuÔvØ˜7Ñ"€IØ×ÑÔÜ�‰Ð<¸Y¸KÐHÔIÜ˜xŸ~™~¨gÓ6Ó7¼$¸x¿~¹~ÈgÓ?VÓ:WÑWÐWˆ;ÐWÝEä�K‰K˜&¤ [Ó!1Ð 2Ð2cÐdÔeÙ-¨hÀCÔHˆHØ  7Ñ*‰Iä�L‰LÐ.¨x¨jÐ8OÐPÔQð �uÑ×$Ñ$Ô&ð 	�5Òð �|Ñ#×+Ñ+Ô-ð ˜Ò$ð �wÑ×&Ñ&Ô(ð ˜Òàð ð &.°Ñ%6×$>Ñ$>Ô$@ˆx˜&Ò Àd€HØ�‚~™gÜ�‰ÐSÔTØ‰Ø	�&Š¡Ü�‰ÐSÔTØˆä	˜( WÑ,×2Ñ2°3Ó7ÖF�A¸1¿8¹8½:ŠaÒFÓ	G€BØ&¨Ñ0×9Ñ9Ó;ÖJ˜¸q¿x¹x½zˆQ�V‹VÐJ€EÐJÜ”œ6 %›=Ó)Ó*€Eð $¨G¸XÑF×LÑLÓNó P‰ˆˆ1Ü ˜s !˜WÓ%Ð& a¨ s¨#Ð.ˆØˆ9Ü�K‰K˜Õà&'§g¡g¨e£nÖ_˜d¸¿¹ÀAÀB¸×8MÑ8MÓ8OÔS^Ò8^’TÐ_ˆEÐ_Ü�U“ˆBÜ¨eÖ4 d�d—k“kÒ4Ó5ˆBØ�QŠwØ˜’<Ü+¨w¨i°r¸!¸Ð<WÐ,XÓYÐYä—N‘N f X¨W°R°D¸ÀBÀ4ÐGaÐ#bÕcØ�r’Ü—‘ ˜x w¨r¨d°+¸b¸TÐATÐUWÐTXÐXfÐgiÐfjÐjkÐlÖmä—‘˜v˜h g¨b¨T°¸R¸DÀÐNÖOð!Pð$  w¸ÈÐUZÐhiÑjÐjùò/ GùÚJùò `ùâ4s*   ÌSÌ'SÍSÍSÏ$(SÐSÐ'S
c                  óF   — e Zd ZdZdd	d„Zed
d„«       Zdd„Zddd„Zdd„Z	y)ÚHUBDatasetStatsaD  A class for generating HUB dataset JSON and `-hub` dataset directory.

    Args:
        path (str): Path to data.yaml or data.zip (with data.yaml inside data.zip).
        task (str): Dataset task. Options are 'detect', 'segment', 'pose', 'classify', 'obb'.
        autodownload (bool): Attempt to download dataset if not found locally.

    Attributes:
        task (str): Dataset task type.
        hub_dir (Path): Directory path for HUB dataset files.
        im_dir (Path): Directory path for compressed images.
        stats (dict): Statistics dictionary containing dataset information.
        data (dict): Dataset configuration data.

    Methods:
        get_json: Return dataset JSON for Ultralytics HUB.
        process_images: Compress images for Ultralytics HUB.

    Examples:
        >>> from ultralytics.data.utils import HUBDatasetStats
        >>> stats = HUBDatasetStats("path/to/coco8.zip", task="detect")  # detect dataset
        >>> stats = HUBDatasetStats("path/to/coco8-seg.zip", task="segment")  # segment dataset
        >>> stats = HUBDatasetStats("path/to/coco8-pose.zip", task="pose")  # pose dataset
        >>> stats = HUBDatasetStats("path/to/dota8.zip", task="obb")  # OBB dataset
        >>> stats = HUBDatasetStats("path/to/imagenet10.zip", task="classify")  # classification dataset
        >>> stats.get_json(save=True)
        >>> stats.process_images()

    Notes:
        Download *.zip files from https://github.com/ultralytics/hub/tree/main/example_datasets
        i.e. https://github.com/ultralytics/hub/raw/main/example_datasets/coco8.zip for coco8.zip.
    c                óˆ  — t        |«      j                  «       }t        j                  d|› d�«       || _        | j                  dk(  rt        |«      }t        |«      }||d<   n`| j                  t        |«      «      \  }}}	 t        j                  |«      }d|d<   t        j                  ||«       t        ||«      }||d<   t        |d   › d�«      | _        | j                  d	z  | _        t        |d
   «      t!        |d
   j#                  «       «      dœ| _        || _        y# t        $ r}	t        d«      |	‚d}	~	ww xY w)zInitialize class.z Starting HUB dataset checks for z....Úclassifyr¾   rL   zerror/HUB/dataset_stats/initNz-hubr9   rH  )r¨   rH  )r   r1  r   r^   Útaskr   r•  Ú_unzipr   rb  r£   rw  rZ   Úhub_dirÚim_dirrR   r.  ÚvaluesÚstatsr;  )
Úselfr¾   rš  rn  Ú	unzip_dirr;  rk   rŽ  Ú	yaml_pathr¬   s
             rE   Ú__init__zHUBDatasetStats.__init__  s,  € ä�D‹z×!Ñ!Ó#ˆÜ�‰Ð6°t°f¸DÐAÔBàˆŒ	Ø�9‰9˜
Ò"Ü" 4Ó(ˆIÜ$ YÓ/ˆDØ$ˆD�ŠLà%)§[¡[´°d³Ó%<Ñ"ˆAˆx˜ðGä—y‘y Ó+�Ø!��V‘Ü—	‘	˜) TÔ*Ü(¨°LÓA�Ø'��V‘ô ˜t F™|˜n¨DÐ1Ó2ˆŒØ—l‘l XÑ-ˆŒÜ  W¡Ó.¼¸dÀ7¹m×>RÑ>RÓ>TÓ9UÑVˆŒ
Øˆ�	øô ò GÜÐ >Ó?ÀQÐFûðGús   ÂAD' Ä'	EÄ0D<Ä<Ec                óØ   — t        | «      j                  d«      sdd| fS t        | | j                  ¬«      }|j	                  «       sJ d| › d|› d�«       ‚dt        |«      t        |«      fS )	zUnzip data.zip.rV  FN)r¾   zError unzipping z, z6 not found. path/to/abc.zip MUST unzip to path/to/abc/T)rz   r6  r   ra  r^  r3  )r¾   r¡  s     rE   r›  zHUBDatasetStats._unzipš  sx   € ô �4‹y×!Ñ! &Ô)Ø˜$ Ð$Ð$Ü˜t¨$¯+©+Ô6ˆ	Ø×ÑÔ!ð 	
Ø˜t˜f B y kÐ1gÐhó	
Ð!ð ”S˜“^Ô%6°yÓ%AÐAÐAr  c                ó\   — t        || j                  t        |«      j                  z  «       y)z)Save a compressed image for HUB previews.N)Úcompress_one_imager�  r   rr  )r   rg   s     rE   Ú_hub_opszHUBDatasetStats._hub_ops¥  s   € ä˜1˜dŸk™k¬D°«G¯L©LÑ8Õ9r  c                óR  ‡ — ˆ fd„}dD �]+  }d‰ j                   |<   ‰ j                  j                  |«      }|€Œ1t        |«      j	                  d«      D �cg c](  }|j
                  dd j                  «       t        v sŒ'|‘Œ* }}|sŒ‰ j                  dk(  róddl	m
}  |‰ j                  |   «      }	t        j                  t        |	j                  «      «      j                  t         «      }
|	j"                  D ]  }|
|d   xx   dz  cc<   Œ t        |	«      |
j%                  «       d	œt        |	«      d|
j%                  «       d
œ|	j"                  D ��cg c]  \  }}t        |«      j&                  |i‘Œ c}}dœ‰ j                   |<   �Œ�ddlm}  |‰ j                  |   ‰ j                  ‰ j                  ¬«      }	t        j,                  t/        |	j0                  t        |	«      d¬«      D �cg c]J  }t        j2                  |d   j                  t         «      j5                  «       ‰ j                  d   ¬«      ‘ŒL c}«      }
t!        |
j7                  «       «      |
j7                  d«      j%                  «       d	œt        |	«      t!        t        j8                  |
dk(  d«      j7                  «       «      |
dkD  j7                  d«      j%                  «       d
œt;        |	j<                  |	j0                  «      D ��cg c]"  \  }}t        |«      j&                   ||«      i‘Œ$ c}}dœ‰ j                   |<   �Œ. |rŠ‰ j>                  jA                  dd¬«       ‰ j>                  dz  }tC        jD                  d|jG                  «       › d�«       tI        |dd¬«      5 }tK        jL                  ‰ j                   |«       ddd«       |r5tC        jD                  tK        jN                  ‰ j                   dd¬«      «       ‰ j                   S c c}w c c}}w c c}w c c}}w # 1 sw Y   ŒbxY w)z(Return dataset JSON for Ultralytics HUB.c                óð  •— ‰	j                   dk(  r| d   }nœ‰	j                   dv r!| d   D �cg c]  }|j                  «       ‘Œ }}nm‰	j                   dk(  rE| d   j                  \  }}}t        j                  | d   | d   j                  |||z  «      fd«      }nt        d‰	j                   › d	�«      ‚t        | d
   |«      }|D ��cg c]  \  }}t        |d   «      gd„ |D «       ¢‘Œ c}}S c c}w c c}}w )z:Update labels to integer class and 4 decimal place floats.ÚdetectÚbboxes>   Úobbr)  rÖ   Úposer×   r;   zUndefined dataset task=r<   r¦   r   c              3  óF   K  — | ]  }t        t        |«      d «      –— Œ y­w)é   N)rl  rò   r³   s     rE   rµ   z;HUBDatasetStats.get_json.<locals>._round.<locals>.<genexpr>¸  s   è ø€ Ò!E¸¤%¬¨a«°!×"4Ñ!Eùs   ‚!)	rš  Úflattenr«   r[   rÇ   rÆ   r†  ÚziprÉ   )
r:   ÚcoordinatesrD   ÚnÚnkr”  Úzippedrå   rÛ   r   s
            €rE   Ú_roundz(HUBDatasetStats.get_json.<locals>._round¬  sû   ø€ à�y‰y˜HÒ$Ø$ XÑ.‘Ø—‘Ð0Ñ0Ø4:¸:Ñ4FÖG¨q˜qŸy™y�{ÐG�ÑGØ—‘˜fÒ$Ø" ;Ñ/×5Ñ5‘	��2�rÜ Ÿn™n¨f°XÑ.>ÀÀ{Ñ@S×@[Ñ@[Ð\]Ð_aÐdfÑ_fÓ@gÐ-hÐjkÓl‘ä Ð#:¸4¿9¹9¸+ÀQÐ!GÓHÐHÜ˜ ™¨Ó4ˆFØX^×_É9È1Èf”S˜˜1™“YÐFÑ!E¸fÔ!EÒFÓ_Ð_ùò Hùó `s   ¬C-Ã"C2r€  Nr„  r;   r™  r   )ÚImageFolder)ÚtotalÚ	per_class)r¸  Ú
unlabelledr¹  )Úinstance_statsÚimage_statsr:   ©ÚYOLODataset)Úimg_pathr;  rš  Ú
Statistics©r¸  Údescr¦   r¨   )Ú	minlengthT©ÚparentsÚexist_okz
stats.jsonzSaving rƒ  r  r¯   r°   r•   F)ÚindentÚ	sort_keys)(rŸ  r;  r‰   r   r0  r…  rž   rŸ   rš  Útorchvision.datasetsr·  r[   rË   rR   rÙ   rÍ   rÉ   ÚimgsÚtolistrr  Úultralytics.datar¾  rÄ   r   r:   Úbincountr°  r"  rg  r±  Úim_filesrœ  Úmkdirr   r^   r1  rX   ÚjsonÚdumpÚdumps)r   r£   Úverboser¶  rÂ   r¾   rg   r_   r·  rm  rD   rª   rq  r“  r¾  r
  Ú
stats_paths   `                rE   Úget_jsonzHUBDatasetStats.get_json©  sT  ø€ ô	`ð ,ó ,	ˆEØ $ˆD�J‰J�uÑØ—9‘9—=‘= Ó'ˆDð ˆ|ØÜ $ T£
× 0Ñ 0°Ó 7Ö_˜1¸1¿8¹8ÀAÀB¸<×;MÑ;MÓ;OÔS^Ò;^’QÐ_ˆEÐ_ÙØð �y‰y˜JÒ&Ý<á% d§i¡i°Ñ&6Ó7�ä—H‘HœS §¡Ó1Ó2×9Ñ9¼#Ó>�Ø!Ÿ,™,ò "�BØ�b˜‘e“H ‘M”Hð"ô 14°G³È1Ï8É8Ë:Ñ&VÜ-0°«\ÈÐYZ×YaÑYaÓYcÑ#dØ=D¿\¹\×J±T°Q¸¤ Q£§¡¨aÒ0ÓJñ%�—
‘
˜5Ó!õ 9á%¨t¯y©y¸Ñ/?ÀdÇiÁiÐVZ×V_ÑV_Ô`�Ü—H‘Hô &*¨'¯.©.ÄÀGÃÐS_Ô%`öà!ô Ÿ™ E¨%¡L×$7Ñ$7¼Ó$<×$DÑ$DÓ$FÐRV×R[ÑR[Ð\`ÑRaÖbòó�ô 14°A·E±E³G³È1Ï5É5ÐQRË8Ï?É?ÓK\Ñ&]ä!$ W£Ü&)¬"¯&©&°°a±¸Ó*;×*?Ñ*?Ó*AÓ&BØ&'¨!¡e§[¡[°£^×%:Ñ%:Ó%<ñ$ô
 FIÈ×IYÑIYÐ[b×[iÑ[iÓEj×k¹T¸QÀ¤ Q£§¡©f°Q«iÒ8Ókñ%�—
‘
˜5Ó!ðI,	ñ^ Ø�L‰L×Ñ t°dÐÔ;ØŸ™¨Ñ4ˆJÜ�K‰K˜' *×"4Ñ"4Ó"6Ð!7°sÐ;Ô<Ü�j #°Ô8ð )¸AÜ—	‘	˜$Ÿ*™* aÔ(÷)áÜ�K‰KœŸ
™
 4§:¡:°aÀ5ÔIÔJØ�z‰zÐùòa `ùó" Kùòùó l÷)ð )ús+   Á(PÁ?PÅ!PÇ.AP
Ë;'PÎ!PÐP&c                óü  — ddl m} | j                  j                  dd¬«       dD ]›  }| j                  j                  |«      €Œ || j                  |   | j                  ¬«      }t        t        «      5 }t        |j                  | j                  |j                  «      t        |«      |› d�¬	«      D ]  }Œ 	 ddd«       Œ� t        j                  d
| j                  › �«       | j                  S # 1 sw Y   ŒÕxY w)z$Compress images for Ultralytics HUB.r   r½  TrÄ  r€  N)r¿  r;  z imagesrÁ  zDone. All images saved to )rÌ  r¾  r�  rÏ  r;  r‰   r   r   r   Úimapr§  rÎ  rR   r   r^   )r   r¾  rÂ   rm  Úpoolrk   s         rE   Úprocess_imageszHUBDatasetStats.process_imagesó  sã   € å0à�‰×Ñ $°ÐÔ6Ø+ò 	ˆEØ�y‰y�}‰}˜UÓ#Ð+ØÙ!¨4¯9©9°UÑ+;À$Ç)Á)ÔLˆGÜœKÓ(ð ¨DÜ˜dŸi™i¨¯©°w×7GÑ7GÓHÔPSÐT[ÓP\ÐfkÐelÐlsÐctÔuò �AØñ÷ð ð		ô 	�‰Ð0°·±°Ð>Ô?Ø�{‰{Ð÷	ð ús   Á5AC2Ã2C;	N)z
coco8.yamlrª  F)r¾   rz   rš  rz   rn  Úbool)r¾   r   Úreturnztuple[bool, str, Path])rg   rz   )FF)r£   rÚ  rÓ  rÚ  rÛ  rŠ  )rÛ  r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r£  Ústaticmethodr›  r§  rÕ  rÙ  rä   r  rE   r—  r—  ]  s4   „ ñôBð6 òBó ðBó:ôHôTr  r—  c           	     ó"  — 	 dt         _        t        j                  | «      }|j                  dv r|j	                  d«      }|t        |j                  |j                  «      z  }|dk  r?|j                  t        |j                  |z  «      t        |j                  |z  «      f«      }|j                  |xs | d|d¬«       y# t        $ rÃ}t        j                  d| › d	|› �«       t        j                  | «      }|j                   dd
 \  }}|t        ||«      z  }|dk  r@t        j                  |t        ||z  «      t        ||z  «      ft        j"                  ¬«      }t        j$                  t'        |xs | «      |«       Y d}~yd}~ww xY w)a6  Compress a single image file to reduced size while preserving its aspect ratio and quality using either the
    Python Imaging Library (PIL) or OpenCV library. If the input image is smaller than the maximum dimension, it
    will not be resized.

    Args:
        f (str): The path to the input image file.
        f_new (str, optional): The path to the output image file. If not specified, the input file will be overwritten.
        max_dim (int, optional): The maximum dimension (width or height) of the output image.
        quality (int, optional): The image compression quality as a percentage.

    Examples:
        >>> from pathlib import Path
        >>> from ultralytics.data.utils import compress_one_image
        >>> for f in Path("path/to/dataset").rglob("*.jpg"):
        >>>    compress_one_image(f)
    N>   ÚLAÚRGBAÚRGBr½   r„   T)rš   ÚoptimizezHUB ops PIL failure r¼   r•   )Úinterpolation)r   ÚMAX_IMAGE_PIXELSrX   ÚmodeÚconvertrÈ   r  r  r  rÉ   r£   rZ   r   rN   r  Úimreadr«   Ú
INTER_AREAÚimwriterz   )	rg   Úf_newÚmax_dimrš   rª   ru  r¬   Ú	im_heightÚim_widths	            rE   r¦  r¦    sG  € ð")Ø!%ŒÔÜ�Z‰Z˜‹]ˆØ�7‰7�nÑ$Ø—‘˜EÓ"ˆBØ”c˜"Ÿ)™) R§X¡XÓ.Ñ.ˆØˆsŠ7Ø—‘œC §¡¨1¡Ó-¬s°2·9±9¸q±=Ó/AÐBÓCˆBØ
�‰�’
˜˜F¨G¸dˆÕCøÜò )Ü�‰Ð-¨a¨S°°1°#Ð6Ô7Ü�Z‰Z˜‹]ˆØ Ÿh™h r¨˜lÑˆ	�8Ø”c˜) XÓ.Ñ.ˆØˆsŠ7Ü—‘˜B¤ X°¡\Ó!2´C¸	ÀA¹Ó4FÐ GÔWZ×WeÑWeÔfˆBÜ�‰”C˜š
 “O R×(Ñ(ûð)ús   ‚B?C Ã	FÃB9F	Æ	Fc                óª   — ddl }|j                  «        t        j                  t	        | «      d¬«      j                  «       }|j                  «        |S )z1Load an Ultralytics *.cache dictionary from path.r   NT)Úallow_pickle)ÚgcÚdisabler[   rb  rz   ÚitemÚenable)r¾   ró  Úcaches      rE   Úload_dataset_cache_filerø  '  s9   € ãà‡J�J„LÜ�G‰G”C˜“I¨DÔ1×6Ñ6Ó8€EØ‡I�I„KØ€Lr  c                ó  — ||d<   t        |j                  «      rp|j                  «       r|j                  «        	 t	        t        |«      d«      5 }t        j                  ||«       ddd«       t        j                  | › d|› �«       yt        j                  | › d	|j                  › d
�«       y# 1 sw Y   ŒJxY w# t        $ rB}t        |«      j                  d¬«       t        j                  | › d|› d|› �«       Y d}~yd}~ww xY w)z9Save an Ultralytics dataset *.cache dictionary x to path.ÚversionÚwbNzNew cache created: T)Ú
missing_oku'   WARNING âš ï¸� Failed to save cache to r¼   zCache directory z" is not writable, cache not saved.)r   ra  rS  ÚunlinkrX   rz   r[   r£   r   r^   rZ   r   rN   )rc   r¾   rD   rú  r   r¬   s         rE   Úsave_dataset_cache_filerþ  1  sç   € à€A€i�LÜ˜Ÿ™Ô$Ø�;‰;Œ=Ø�K‰KŒMð	ZÜ”c˜$“i Ó&ð !¨$Ü—‘˜˜aÔ ÷!ä�K‰K˜6˜(Ð"5°d°VÐ<Õ=ô
 	�‰˜&˜Ð!1°$·+±+°Ð>`ÐaÕb÷!ð !ûô ò 	ZÜ�‹J×Ñ¨ÐÔ.Ü�N‰N˜f˜XÐ%LÈTÈFÐRTÐUVÐTWÐX×YÑYûð	Zús/   ¼B= ÁB1Á("B= Â1B:Â6B= Â=	DÃ8DÄD)rA   ú	list[str]rÛ  rÿ  )é
   é2   r¹   rL   )
r_   rÿ  r`   rò   ra   rò   rb   rÉ   rc   rz   )r~   rÿ  rÛ  rz   )rŠ   zImage.ImagerÛ  útuple[int, int])r¤   rô   rÛ  rô   )r¤   rô   rÛ  r.  )rú   rz   rû   rz   rü   zdict[int, str])r;   r;   )
r  r  r  úlist[np.ndarray]rì   rÉ   r  rÉ   rÛ  z
np.ndarray)r;   )r  r  rÖ   r  r  rÉ   rÛ  ztuple[np.ndarray, np.ndarray])r¾   r   rÛ  r   )r;  ú
str | PathrÛ  r  )T)rm  rz   rn  rÚ  rÛ  údict[str, Any])rL   )rm  r  rÂ   rz   rÛ  r  )Ni€  r  )rg   rz   rí  z
str | Nonerî  rÉ   rš   rÉ   )r¾   r   rÛ  rŠ  )rc   rz   r¾   r   rD   rŠ  rú  rz   )EÚ
__future__r   rÐ  r=   rO   rj  rS   r_  Úmultiprocessing.poolr   Úpathlibr   Útarfiler   Útypingr   r  Únumpyr[   ÚPILr   r	   Úultralytics.nn.autobackendr
   Úultralytics.utilsr   r   r   r   r   r   r   r   r   r   r   r   Úultralytics.utils.checksr   r   r   Úultralytics.utils.downloadsr   r   r   Úultralytics.utils.opsr   ÚHELP_URLrŸ   ÚVID_FORMATSr    rF   rt   r‚   rŽ   r­   rß   r  r  r  r+  r3  r=  rw  r•  r—  r¦  rø  rþ  rä   r  rE   ú<module>r     s°  ðõ #ã Û 	Û Û Û Û Ý +Ý Ý Ý ã 
Û ß å 8÷÷ ÷ ó ÷ FÑ Eß KÑ KÝ 0àW€ò€ò  d€Ø5°k°]À*È[ÈMÐZÐ óTð moðJ
ØðJ
Ø$)ðJ
Ø>CðJ
ØUXðJ
ØfióJ
óZ
óó'ó2ECóP(ðX abð&Øð&Ø&6ð&Ø?Bð&ØZ]ð&àó&ð2 ]^ðgØðgØ&6ðgØ?BðgØVYðgàógð& QRðØðØ&6ðØJMðà"óó:ó(	ô]ô@]k÷@cñ côL!)óHôcr  