a
    yfBy                     @   s  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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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Z.h dZ/e0e1dd2 dkZ3de. de/ Z4dd Z5dd Z6ejdddZ7dd Z8dd Z9d<dd Z:d=d!d"Z;d>d#d$Z<eed%d&d'Z=d?d(d)Z>d@d+d,Z?G d-d. d.Z@dAd1d2ZAed3 d4d5fd6d7ZBd8d9 ZCd:d; ZDdS )B    N)
ThreadPool)Path)
is_tarfile)ImageImageOps)check_class_names)DATASETS_DIRLOGGERNUM_THREADSROOTSETTINGS_FILETQDM	clean_urlcolorstremojisis_dir_writeable	yaml_load	yaml_save)
check_file
check_fontis_ascii)downloadsafe_download
unzip_file)segments2boxeszJSee https://docs.ultralytics.com/datasets for dataset formatting guidance.>
   ZtiffZtifjpgZbmpZmpoZwebpZpfmZdngZpngjpeg>   ZasfZmpegZmovtsZmp4ZmpgZaviZmkvZm4vZwmvZwebmZgif
PIN_MEMORYTtruezSupported formats are:
images: z	
videos: c                    s:   t j dt j t j dt j    fdd| D S )z0Define label paths as a function of image paths.imageslabelsc                    s.   g | ]&} | d dd d d qS )   .r   z.txt)joinrsplit.0xsasb R/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/data/utils.py
<listcomp>0       z#img2label_paths.<locals>.<listcomp>)ossep)Z	img_pathsr,   r)   r-   img2label_paths-   s    &r2   c                 C   s@   t dd | D }tt| }|d|   | S )z?Returns a single hash value of a list of paths (files or dirs).c                 s   s&   | ]}t j|rt j|V  qd S N)r0   pathexistsgetsize)r'   pr,   r,   r-   	<genexpr>5   r/   zget_hash.<locals>.<genexpr> )sumhashlibsha256strencodeupdater$   	hexdigest)pathssizehr,   r,   r-   get_hash3   s    rD   )imgc                 C   sn   | j }| jdkrjtt@ |  }|rL|dd}|dv rL|d |d f}W d   n1 s`0    Y  |S )z Returns exif-corrected PIL size.JPEGi  N>         r"   r   )rB   format
contextlibsuppress	ExceptionZgetexifget)rE   sZexifZrotationr,   r,   r-   	exif_size;   s    
.rO   c              
   C   sh  | \\}}}d\}}}z t |}|  t|}|d |d f}|d dk|d dk@ slJ d| d|j tv sJ d|j dt |j d	v rt|d
V}	|	dd |		 dkrt
t |j|dddd | d| d}W d   n1 s0    Y  d}W n> tyV }
 z$d}| d| d|
 }W Y d}
~
n
d}
~
0 0 ||f|||fS )zVerify one image.)r   r   r9   r"   r   	   image size  <10 pixelszInvalid image format .    r   r   rb      rF   d   Zsubsamplingquality   WARNING ⚠️ !: corrupt JPEG restored and savedN : ignoring corrupt image/label: )r   openverifyrO   rI   lowerIMG_FORMATSFORMATS_HELP_MSGseekreadr   exif_transposesaverL   )argsim_fileclsprefixnfncmsgimshapefer,   r,   r-   verify_imageH   s(    

($0*rs   c                    s  | \}}}}}}}dddddg df\}}	}
}} }zFt |}|  t|}|d |d f}|d dk|d dk@ sJ d| d|j tv sJ d|j d	t |j d
v r0t|dX}|dd |	 dkrt
t |j|dddd | d| d}W d   n1 s&0    Y  tj|rd}	t|}dd |	   D }tdd |D r|stjdd |D tjd}dd |D  t|ddt fd}tj|tjd}W d   n1 s0    Y  t|}|r|rf|jd d||  ks8J dd||   d|ddddf d|ddddf }n:|jd dksJ d|jd  d |ddddf }| dksJ d!||dk  | dksJ d"||dk   |dddf  }||ks(J d#t| d$| d%|d  tj|dd&d'\}}t||k r|| } rj fd(d|D  | d| d)|t|  d*}n*d}
tjd|rd||  ndftjd}n*d}tjd|rd||  ndftjd}|rN|ddddf d||}|dkrNt |d+ dk |d, dk B d-d.!tj}tj||d/ gdd0}|ddddf }||| |||	|
||f
W S  t"y } z<d}| d| d1| }ddddd||	|
||g
W  Y d}~S d}~0 0 dS )2zVerify one image-label pair.r   r9   Nr"   rP   rQ   rR   zinvalid image format rS   rT   rU   rV   rW   rX   rF   rY   rZ   r\   r]   c                 S   s   g | ]}t |r| qS r,   )lensplitr&   r,   r,   r-   r.   y   r/   z&verify_image_label.<locals>.<listcomp>c                 s   s   | ]}t |d kV  qdS )rH   N)rt   r&   r,   r,   r-   r8   z   r/   z%verify_image_label.<locals>.<genexpr>c                 S   s   g | ]}|d  qS )r   r,   r&   r,   r,   r-   r.   {   r/   Zdtypec                 S   s,   g | ]$}t j|d d t jdddqS )r"   Nrv   rW   )nparrayfloat32reshaper&   r,   r,   r-   r.   |   r/   rw      zlabels require z columns eachzlabels require 5 columns, z columns detectedz,non-normalized or out of bounds coordinates znegative label values zLabel class z exceeds dataset class count z. Possible class labels are 0-T)axisZreturn_indexc                    s   g | ]} | qS r,   r,   r&   segmentsr,   r-   r.      r/   : z duplicate labels removed).r   ).r"                 ?).N)r}   r^   )#r   r_   r`   rO   rI   ra   rb   rc   rd   re   r   rf   rg   r0   r4   isfilestrip
splitlinesanyrx   ry   rz   concatenater{   r   rt   rp   maxminintuniquezeroswhereastyperL   )rh   ri   Zlb_filerk   ZkeypointZnum_clsZnkptndimnmrl   nerm   rn   	keypointsro   rp   rq   ZlbclassesnlpointsZmax_cls_iZkpt_maskrr   r,   r~   r-   verify_image_labela   sx     
($0
00.&$$ (&
*r   r"   c                 C   sp   t j| t jd}t j|t jd}||jd ddf}tj|||d | d | | d |  }}t	|||fS )a  
    Convert a list of polygons to a binary mask of the specified image size.

    Args:
        imgsz (tuple): The size of the image as (height, width).
        polygons (list[np.ndarray]): A list of polygons. Each polygon is an array with shape [N, M], where
                                     N is the number of polygons, and M is the number of points such that M % 2 = 0.
        color (int, optional): The color value to fill in the polygons on the mask. Defaults to 1.
        downsample_ratio (int, optional): Factor by which to downsample the mask. Defaults to 1.

    Returns:
        (np.ndarray): A binary mask of the specified image size with the polygons filled in.
    rv   r   rw   rW   )colorr"   )
rx   r   uint8asarrayint32r{   rp   cv2ZfillPolyresize)imgszpolygonsr   downsample_ratiomaskZnhnwr,   r,   r-   polygon2mask   s    r   c                    s   t  fdd|D S )a  
    Convert a list of polygons to a set of binary masks of the specified image size.

    Args:
        imgsz (tuple): The size of the image as (height, width).
        polygons (list[np.ndarray]): A list of polygons. Each polygon is an array with shape [N, M], where
                                     N is the number of polygons, and M is the number of points such that M % 2 = 0.
        color (int): The color value to fill in the polygons on the masks.
        downsample_ratio (int, optional): Factor by which to downsample each mask. Defaults to 1.

    Returns:
        (np.ndarray): A set of binary masks of the specified image size with the polygons filled in.
    c                    s"   g | ]}t |d g qS )rw   )r   r{   r&   r   r   r   r,   r-   r.      r/   z"polygons2masks.<locals>.<listcomp>)rx   ry   )r   r   r   r   r,   r   r-   polygons2masks   s    r   c           
      C   s   t j| d | | d | ft|dkr,t jnt jd}g }g }tt|D ]8}t| || dg|dd}|| ||	  qJt 
|}t | }t || }tt|D ]0}	||	 |	d  }|| }t j|d|	d d}q||fS )z!Return a (640, 640) overlap mask.r   r"      rv   rw   )r   r   )Za_minZa_max)rx   r   rt   r   r   ranger   r{   appendr:   r   Zargsortry   Zclip)
r   r   r   masksZareasmssir   indexr   r,   r,   r-   polygons2masks_overlap   s$    

r   )r4   returnc                    s   t  dpt  d}|s4J d   dt|dkrR fdd|D }t|dksJ d   dt| d	| |d
 S )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. An AssertionError
    is raised if no YAML file is found or if multiple YAML files are found.

    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"   c                    s   g | ]}|j  j kr|qS r,   )stemr'   rq   r4   r,   r-   r.      r/   z%find_dataset_yaml.<locals>.<listcomp>zExpected 1 YAML file in 'z', but found z.
r   )listglobrglobresolvert   )r4   filesr,   r   r-   find_dataset_yaml   s    .r   c                    s  t | }d}t|st|rFt|tddd}tt| }|jd }}t|dd dD ]L}| vrV|dksrd vrt	t
|  d	| d
td  d d< qVd vrd vrt	t
|  dd v rd v rt d  d krt	t
|  dt d  d d  dd vr@dd t d D  d< nt d  d< t d  d< t|p dpt ddj st   d< dD ]} |rt | tr |   }| s | dr | dd   }t| |< nfdd | D  |< q fdddD \}}|rdd t|trj|n|gD }tdd |D st| }	d |	 d!d"d |D d#  d$}
|r|rt|
 n|
d%t d&t d$7 }
t|
t }d}|d'r| d(rt|tdd) n8|d*rDtd+| d, t!"|}nt#|d- i d.t$t | d/ d0}|d1v rd2| d3t%d4t n
d5| d6}td7| d8 t&t' d rd9nd:  S );az  
    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. Defaults to True.

    Returns:
        (dict): Parsed dataset information and paths.
    r9   TFdirZunzipdelete)Zappend_filename)trainvalr   
validation 'uE   :' key missing ❌.
'train' and 'val' are required in all data YAMLs.uQ   WARNING ⚠️ renaming data YAML 'validation' key to 'val' to match YOLO format.namesrm   uI    key missing ❌.
 either 'names' or 'nc' are required in all data YAMLs.z 'names' length z
 and 'nc: z' must match.c                 S   s   g | ]}d | qS )class_r,   )r'   r   r,   r,   r-   r.   &  r/   z%check_det_dataset.<locals>.<listcomp>r4   Z	yaml_file)r   r   testZminivalz../   Nc                    s   g | ]}t  |  qS r,   )r=   r   r&   r   r,   r-   r.   ;  r/   c                 3   s   | ]}  |V  qd S r3   )rM   r&   )datar,   r-   r8   >  r/   z$check_det_dataset.<locals>.<genexpr>)r   r   c                 S   s   g | ]}t | qS r,   )r   r   r&   r,   r,   r-   r.   @  r/   c                 s   s   | ]}|  V  qd S r3   r5   r&   r,   r,   r-   r8   A  r/   z

Dataset 'u)   ' images not found ⚠️, missing path 'c                 S   s   g | ]}|  s|qS r,   r   r&   r,   r,   r-   r.   C  r/   r   r   z%
Note dataset download directory is 'z'. You can update this in 'http.zip)urlr   r   bash zRunning z ...yaml(r"   zs)>   r   Nu   success ✅ z, saved to boldzfailure u    ❌zDataset download 
z	Arial.ttfzArial.Unicode.ttf)(r   zipfile
is_zipfiler   r   r   r   parentr   SyntaxErrorr   r	   infopoprt   r   r   r   rM   is_absoluter   
isinstancer=   r5   
startswithr   allr   warningr   FileNotFoundErrortimeendswithr0   systemexecroundr   r   r   )datasetautodownloadfileextract_dirZnew_dirkr(   r   rN   namemtrdtr,   )r   r4   r-   check_det_dataset   st    
**
(
  ,r   r9   c                 C   s  t | dr t| tddd} n&t| jdv rFt| }t|tddd} t| } |  rZ| nt|   }| st	
d| d t }t | dkrtjd	td
  ddd nd|  d}t||jd dt | ddtd| d}t	| |d }|d  r|d n|d  r*|d nd}|d  rD|d nd}	|dkrd|sdt	
d n|dkr~|	s~t	
d tdd |d dD }
dd |d  D }ttt|}|||	d D ]\}}t| d  d!| d"}|du r
t	| nd#d |d$D }t|}td%d& |D }|d'kr|dkrftt|  d(| d)nt	
| d*| d+| d, nN||
krt	
| d*| d+| d-|
 d.| 	 nt	| d*| d+| d/ q|||	|
|d0S )1a  
    Checks 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 ''. Defaults to ''.

    Returns:
        (dict): 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): A dictionary of class names in the dataset.
    )zhttp:/zhttps:/TFr   >   r   z.gzz.taru(   
Dataset not found ⚠️, missing path z, attempting download...Zimagenetr   zdata/scripts/get_imagenet.sh)shellcheckz?https://github.com/ultralytics/assets/releases/download/v0.0.0/r   )r   u   Dataset download success ✅ (z.1fzs), saved to r   r   r   r   r   Nr   uI   WARNING ⚠️ Dataset 'split=val' not found, using 'split=test' instead.uI   WARNING ⚠️ Dataset 'split=test' not found, using 'split=val' instead.c                 S   s   g | ]}|  r|qS r,   )is_dirr&   r,   r,   r-   r.     r/   z%check_cls_dataset.<locals>.<listcomp>*c                 S   s   g | ]}|  r|jqS r,   )r   r   r&   r,   r,   r-   r.     r/   r   r   r   : ...c                 S   s&   g | ]}|j d d  tv r|qS r"   Nsuffixra   rb   )r'   r4   r,   r,   r-   r.     r/   *.*c                 S   s   h | ]
}|j qS r,   )r   )r'   r   r,   r,   r-   	<setcomp>  r/   z$check_cls_dataset.<locals>.<setcomp>r   r   u    :' no training images found ❌ z found z images in u(    classes: WARNING ⚠️ no images foundu     classes: ERROR ❌️ requires z classes, not u    classes ✅ )r   r   r   rm   r   )r=   r   r   r   r   r   r   r   r   r	   r   r   
subprocessrunr   r   r   r   r   r5   rt   r   iterdirdict	enumeratesorteditemsr   r   r   )r   ru   r   data_dirr   r   rN   Z	train_setZval_setZtest_setrm   r   r   vrk   r   rl   ndr,   r,   r-   check_cls_datasetZ  s\    "







( r  c                   @   s@   e Zd ZdZdddZedd Zd	d
 ZdddZdd Z	dS )HUBDatasetStatsa  
    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). Default is 'coco8.yaml'.
        task (str): Dataset task. Options are 'detect', 'segment', 'pose', 'classify'. Default is 'detect'.
        autodownload (bool): Attempt to download dataset if not found locally. Default is False.

    Example:
        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.
        ```python
        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()
        ```
    
coco8.yamldetectFc           
   
   C   s  t | }td| d || _| jdkrHt|}t|}||d< nt| t |\}}}z0t|}d|d< t	|| t
||}||d< W n. ty }	 ztd|	W Y d}	~	n
d}	~	0 0 t |d  d| _| jd	 | _t|d
 t|d
  d| _|| _dS )zInitialize class.z Starting HUB dataset checks for z....classifyr4   r9   zerror/HUB/dataset_stats/initNz-hubr    r   )rm   r   )r   r   r	   r   taskr   r  _unzipr   r   r   rL   hub_dirim_dirrt   r   valuesstatsr   )
selfr4   r  r   	unzip_dirr   r   r   Z	yaml_pathrr   r,   r,   r-   __init__  s(    



  zHUBDatasetStats.__init__c                 C   sV   t | dsdd| fS t| | jd}| sDJ d|  d| ddt |t|fS )	zUnzip data.zip.r   FNr   zError unzipping z, z6 not found. path/to/abc.zip MUST unzip to path/to/abc/T)r=   r   r   r   r   r   )r4   r  r,   r,   r-   r    s    

zHUBDatasetStats._unzipc                 C   s   t || jt|j  dS )z*Saves a compressed image for HUB previews.N)compress_one_imager
  r   r   )r  rq   r,   r,   r-   _hub_ops  s    zHUBDatasetStats._hub_opsc              	      s>  fdd dD ]}dj |< j|}|du r6qdd t|dD }|sTqjdkrd	d
lm} |j| }t	t
|jt}|jD ]}	||	d   d7  < qt
|| dt
|d	| ddd |jD dj |< qd	dlm}
 |
j| jjd}tfddt|jt
|ddD }t| |d	 dt
|tt|d	kd |d	kd	 d fddt|j|jD dj |< q|rjjddd jd }td|  d t|d}t j | W d   n1 s0    Y  |r8ttj!j ddd j S )z(Return dataset JSON for Ultralytics HUB.c                    s    j dkr| d }np j dv r2dd | d D }nR j dkrr| d j\}}}t| d | d ||| fd	}ntd
 j  dt| d |}dd |D S )z:Update labels to integer class and 4 decimal place floats.r  Zbboxes>   segmentZobbc                 S   s   g | ]}|  qS r,   )flattenr&   r,   r,   r-   r.     r/   z<HUBDatasetStats.get_json.<locals>._round.<locals>.<listcomp>r   Zposer   r"   zUndefined dataset task=r#   rj   c                 S   s,   g | ]$\}}t |d  gdd |D qS )r   c                 s   s   | ]}t t|d V  qdS )   N)r   floatr&   r,   r,   r-   r8     r/   zFHUBDatasetStats.get_json.<locals>._round.<locals>.<listcomp>.<genexpr>)r   )r'   cr   r,   r,   r-   r.     r/   )r  rp   rx   r   r{   
ValueErrorzip)r!   ZcoordinatesnZnkr  Zzippedr  r,   r-   _round  s    



&z(HUBDatasetStats.get_json.<locals>._roundr   Nc                 S   s&   g | ]}|j d d  tv r|qS r   r   r   r,   r,   r-   r.     r/   z,HUBDatasetStats.get_json.<locals>.<listcomp>r   r  r   )ImageFolderr"   )total	per_class)r  Z
unlabelledr  c                 S   s   g | ]\}}t |j|iqS r,   r   r   r'   r   r   r,   r,   r-   r.     r/   )Zinstance_statsZimage_statsr!   YOLODataset)img_pathr   r  c                    s.   g | ]&}t j|d  t  jd dqS )rj   rm   )Z	minlength)rx   Zbincountr   r   r  r   )r'   labelr  r,   r-   r.     s   Z
Statisticsr  descc                    s"   g | ]\}}t |j |iqS r,   r  r   )r  r,   r-   r.   (  r/   Tparentsexist_okz
stats.jsonzSaving r   wrW   F)indent	sort_keys)"r  r   rM   r   r   r  Ztorchvision.datasetsr  rx   r   rt   r   r   r   Zimgstolistultralytics.datar"  ry   r   r!   r:   r   r  im_filesr	  mkdirr	   r   r   r_   jsondumpdumps)r  rg   verboseru   r4   r   r  r   r(   ro   r"  Z
stats_pathrq   r,   )r  r  r-   get_json  sT    





.zHUBDatasetStats.get_jsonc              	   C   s   ddl m} | jjddd dD ]|}| j|du r6q || j| | jd}tt:}t|	| j
|jt|| dd	D ]}qxW d   q 1 s0    Y  q td
| j  | jS )z$Compress images for Ultralytics HUB.r   r!  Tr'  r   N)r#  r   z imagesr%  zDone. All images saved to )r.  r"  r
  r0  r   rM   r   r
   r   imapr  r/  rt   r	   r   )r  r"  ru   r   poolr   r,   r,   r-   process_images6  s    
("zHUBDatasetStats.process_imagesN)r  r  F)FF)
__name__
__module____qualname____doc__r  staticmethodr  r  r5  r8  r,   r,   r,   r-   r    s   



Jr    2   c           	   
   C   s  z`t | }|t|j|j }|dk rH|t|j| t|j| f}|j|pR| d|dd W n ty } zt	
d|  d|  t| }|jdd \}}|t|| }|dk rtj|t|| t|| ftjd	}tt|p| | W Y d}~n
d}~0 0 dS )
at  
    Compresses 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. Default is 1920 pixels.
        quality (int, optional): The image compression quality as a percentage. Default is 50%.

    Example:
        ```python
        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)
        ```
    r   rF   T)r[   optimizeu#   WARNING ⚠️ HUB ops PIL failure r   NrW   )interpolation)r   r_   r   heightwidthr   r   rg   rL   r	   r   r   Zimreadrp   Z
INTER_AREAZimwriter=   )	rq   Zf_newZmax_dimr[   ro   r   rr   Z	im_heightZim_widthr,   r,   r-   r  F  s    
"
&r  zcoco8/images)g?g?r   Fc              	   C   s   t | } tdd | dD }t|}td tjg d||d}g d}|D ] }| j|  rR| j| 	  qRt
d|  d	|   tt|||d
D ]|\}}	|rt tt|	gd  rt| j||  d0}
|
d|	| j  d  W d   q1 s0    Y  qdS )aL  
    Automatically split a dataset into train/val/test splits and save the resulting splits into autosplit_*.txt files.

    Args:
        path (Path, optional): Path to images directory. Defaults to DATASETS_DIR / 'coco8/images'.
        weights (list | tuple, optional): Train, validation, and test split fractions. Defaults to (0.9, 0.1, 0.0).
        annotated_only (bool, optional): If True, only images with an associated txt file are used. Defaults to False.

    Example:
        ```python
        from ultralytics.data.utils import autosplit

        autosplit()
        ```
    c                 s   s(   | ] }|j d d  tv r|V  qdS r   r   r&   r,   r,   r-   r8   |  r/   zautosplit.<locals>.<genexpr>r   r   )r   r"   rW   )weightsr   )zautosplit_train.txtzautosplit_val.txtzautosplit_test.txtzAutosplitting images from z!, using *.txt labeled images only)r  az./r   N)r   r   r   rt   randomseedchoicesr   r5   unlinkr	   r   r   r  r2   r=   r_   writerelative_toas_posix)r4   rD  Zannotated_onlyr   r  indicestxtr(   r   rE   rq   r,   r,   r-   	autosplitk  s    
rO  c                 C   s2   ddl }|  tjt| dd }|  |S )z1Load an Ultralytics *.cache dictionary from path.r   NT)Zallow_pickle)gcdisablerx   loadr=   itemenable)r4   rP  cacher,   r,   r-   load_dataset_cache_file  s
    rV  c                 C   st   ||d< t |jrX| r"|  tt|| |d| t	
|  d|  nt	|  d|j d dS )z9Save an Ultralytics dataset *.cache dictionary x to path.versionz
.cache.npyzNew cache created: u   WARNING ⚠️ Cache directory z# is not writeable, cache not saved.N)r   r   r5   rI  rx   rg   r=   with_suffixrenamer	   r   r   )rk   r4   r(   rW  r,   r,   r-   save_dataset_cache_file  s    
rZ  )r"   r"   )r"   )r"   )T)r9   )Nr>  r?  )ErJ   r;   r1  r0   rF  r   r   r   Zmultiprocessing.poolr   pathlibr   tarfiler   r   numpyrx   ZPILr   r   Zultralytics.nn.autobackendr   Zultralytics.utilsr   r	   r
   r   r   r   r   r   r   r   r   r   Zultralytics.utils.checksr   r   r   Zultralytics.utils.downloadsr   r   r   Zultralytics.utils.opsr   ZHELP_URLrb   ZVID_FORMATSr=   getenvra   r   rc   r2   rD   rO   rs   r   r   r   r   r   r   r  r  r  rO  rV  rZ  r,   r,   r,   r-   <module>   sP   8H



]
N 
%"
