§
    >²¸jG'  ã                  óò  — d dl mZ ddg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mZmZ d dlZd dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl!m#Z$ d dl!m%Z& d dl'm(Z(m)Z)  e*h d£¦  «        Z+dQd„Z, ee$dd¬¦  «        	 dRdSd!„¦   «         Z# ee&dd¬¦  «        dTd%„¦   «         Z%erd d&l-m.Z.  ed'¦  «        Z/	 	 	 dUdVd/„Z0dWd3„Z1dXd7„Z2dYd;„Z3dZdA„Z4	 dRd[dF„Z5	 	 	 d\d]dP„Z6dS )^é    )ÚannotationsÚcheck_no_basename_collisionsÚtrain_test_splitN)ÚCallable)ÚPath)ÚTYPE_CHECKINGÚTypeVarÚcast)Ú
deprecatedÚvoid)ÚImage)Útqdm)Ú_cv2)Ú_EXIF_ORIENTATION_TAG)Ú
Detections)Úmask_to_polygons)Úmask_to_rle)Úrle_to_mask)Úapproximate_polygonÚfilter_polygons_by_area>   é   é   é   é   Ú
image_pathÚstrÚreturnútuple[int, int]c                óè   — t          j        | ¦  «        5 }|j        \  }}|                     ¦   «                              t
          ¦  «        }ddd¦  «         n# 1 swxY w Y   |t          v r||fS ||fS )aF  Return the `(width, height)` at which `cv2.imread` loads an image file.

    Only the file header is read, which is much faster than decoding the image (#1554).
    Loading applies the EXIF orientation tag, and orientations 5 to 8 turn the image a
    quarter turn, so for those the header's width and height are swapped.
    N)r   ÚopenÚsizeÚgetexifÚgetr   Ú_QUARTER_TURN_EXIF_ORIENTATIONS)r   ÚimageÚwidthÚheightÚorientations        úi/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/dataset/utils.pyÚ_image_file_resolution_whr*   %   s¿   € õ 
Œ�JÑ	Ô	ð A 5Øœ
‰ˆˆvØ—m’m‘o”o×)Ò)Õ*?Ñ@Ô@ˆðAð Að Añ Aô Að Að Að Að Að Að Aøøøð Að Að Að Að Õ5Ð5Ð5Ø�uˆ}ÐØ�&ˆ=Ðs   •7AÁAÁAz0.28.0z0.31.0)ÚtargetÚdeprecated_inÚ	remove_inFÚmaskúnpt.NDArray[np.bool_]Ú
compressedÚboolúlist[int] | strc                ón   — t          t          t                   t          z  t	          | |¦  «        ¦  «        S )z\Deprecated since 0.28.0.

    Use `supervision.detection.utils.converters.mask_to_rle`.
    )r
   ÚlistÚintr   r   )r.   r0   s     r)   r   r   4   s&   € õ ••S”	�C‘¥ d¨JÑ!7Ô!7Ñ8Ô8Ð8ó    Úrleú1npt.NDArray[np.integer] | list[int] | str | bytesÚresolution_whc                ór   — t          t          j        t          j                 t          | |¦  «        ¦  «        S )z\Deprecated since 0.28.0.

    Use `supervision.detection.utils.converters.rle_to_mask`.
    )r
   ÚnptÚNDArrayÚnpÚbool_r   )r7   r9   s     r)   r   r   ?   s'   € õ •”�BœHÔ%¥t¨C°Ñ'?Ô'?Ñ@Ô@Ð@r6   )ÚDetectionDatasetÚTç        ç      ð?Úmin_image_area_percentageÚfloatÚmax_image_area_percentageÚapproximation_percentageúlist[npt.NDArray[np.number]]c                óH  ‡— | j         \  }}||z  }||z  }||z  }t          t          t          j        t
          j                          t          | ¬¦  «        ¦  «        }	t          |	¦  «        dk    rt          |	d|¬¦  «        }	nt          |	||¬¦  «        }	ˆfd„|	D ¦   «         S )zÄFilter mask polygons by area and optionally simplify them.

    The default `approximation_percentage=0.0` preserves the original contour
    unless callers explicitly ask for simplification.
    )r.   é   N)ÚpolygonsÚmin_areaÚmax_areac                ó2   •— g | ]}t          |‰¬ ¦  «        ‘ŒS ))ÚpolygonÚ
percentage)r   )Ú.0rN   rF   s     €r)   ú
<listcomp>z2approximate_mask_with_polygons.<locals>.<listcomp>l   s7   ø€ ð ð ð àõ 	 GÐ8PÐQÑQÔQðð ð r6   )
Úshaper
   r4   r;   r<   r=   Únumberr   Úlenr   )
r.   rC   rE   rF   r'   r&   Ú
image_areaÚminimum_detection_areaÚmaximum_detection_arearJ   s
      `      r)   Úapproximate_mask_with_polygonsrX   Q   sÑ   ø€ ð ”J�M€FˆEØ˜%‘€JØ6¸ÑCÐØ6¸ÑCÐå•D�œ¥R¤YÔ/Ô0Õ2BÈÐ2MÑ2MÔ2MÑNÔN€HÝ
ˆ8�}„}˜ÒÐÝ*Ø¨Ð7Mð
ñ 
ô 
ˆˆõ +ØØ+Ø+ð
ñ 
ô 
ˆð
ð ð ð àðñ ô ð r6   Úclass_listsúlist[list[str]]ú	list[str]c                ó”   — t          ¦   «         }| D ]}|D ]}|                     |¦  «         ŒŒt          t          |¦  «        ¦  «        S ©N)ÚsetÚaddÚsortedr4   )rY   Úunique_classesÚ
class_listÚ
class_names       r)   Úmerge_class_listsrd   r   s]   € Ý‘U”U€Nà!ð +ð +ˆ
Ø$ð 	+ð 	+ˆJØ×Ò˜zÑ*Ô*Ð*Ð*ð	+õ •$�~Ñ&Ô&Ñ'Ô'Ð'r6   Úsource_classesÚtarget_classesúdict[int, int]c                ó–   — i }t          | ¦  «        D ]6\  }}||vrt          d|› d�¦  «        ‚|                     |¦  «        }|||<   Œ7|S )z:Returns the index map of source classes -> target classes.zClass zP not found in target classes. source_classes must be a subset of target_classes.)Ú	enumerateÚ
ValueErrorÚindex)re   rf   Úindex_mappingÚirc   Úcorresponding_indexs         r)   Úbuild_class_index_mappingro   |   s„   € ð €Må" >Ñ2Ô2ð /ð /‰ˆˆ:Ø˜^Ð+Ð+ÝðE˜ð Eð Eð Eñô ð ð -×2Ò2°:Ñ>Ô>ÐØ.ˆ�aÑÐàÐr6   Úsource_to_target_mappingÚ
detectionsr   c                ó†  — |j         €t          d¦  «        ‚t          t          j        |j         ¦  «        ¦  «        t          |                      ¦   «         ¦  «        z
  rt          d¦  «        ‚t          j        |¦  «        }t          |¦  «        dk    r, t          j	        | j
        ¦  «        |j         ¦  «        |_         |S )Nz(Detections must have class_id attribute.zFDetections class_id must be a subset of source_to_target_mapping keys.r   )Úclass_idrj   r^   r=   ÚuniqueÚkeysÚcopyÚdeepcopyrT   Ú	vectorizer#   )rp   rq   Údetections_copys      r)   Úmap_detections_class_idrz   Ž   sº   € ð ÔÐ"ÝÐCÑDÔDÐDÝ
�2Œ9�ZÔ(Ñ)Ô)Ñ*Ô*­SÐ1I×1NÒ1NÑ1PÔ1PÑ-QÔ-QÑQð 
ÝØTñ
ô 
ð 	
õ ”m JÑ/Ô/€Oå
ˆ:�„˜ÒÐØ#M¥2¤<Ð0HÔ0LÑ#MÔ#MØÔ$ñ$
ô $
ˆÔ ð Ðr6   Úimage_pathsÚkeyúCallable[[str], str]Úoutput_kindÚNonec                ó¶   — i }| D ]S} ||¦  «        }|                      ¦   «         }||v r'||         \  }}t          d|›d|›d|› d|›d�	¦  «        ‚||f||<   ŒTdS )a0  Raise if two image paths would be written to the same output file.

    Dataset image paths may share a basename when they originate from different
    directories (a legal, common state after :meth:`DetectionDataset.merge`).
    Exporting them into a single flat output directory keyed on the basename or
    stem would silently overwrite one file with another and mispair images with
    their annotations. This guard detects such collisions before any file is
    written and names the colliding source paths.

    Args:
        image_paths: The dataset image paths about to be written.
        key: Maps an image path to the output file name it would be written to.
        output_kind: Human-readable description of the output (e.g. ``"image"``
            or ``"YOLO annotation"``) used in the error message.

    Raises:
        ValueError: If two image paths map to the same output file name.

    Examples:
        ```pycon
        >>> from pathlib import Path
        >>> from supervision.dataset.utils import check_no_basename_collisions
        >>> check_no_basename_collisions(
        ...     ["a/img.jpg", "b/img.jpg"], lambda p: Path(p).name, "image"
        ... )
        Traceback (most recent call last):
        ...
        ValueError: Cannot export dataset: image paths 'a/img.jpg' and ...

        ```
    z#Cannot export dataset: image paths z and z both map to z file z6. Ensure all output paths are unique before exporting.N)Úcasefoldrj   )	r{   r|   r~   Úseenr   Úoutput_nameÚcase_keyÚ
first_nameÚ
first_paths	            r)   r   r   ¢   sÅ   € ðH (*€DØ!ð 
3ð 
3ˆ
Ø�c˜*‘o”oˆØ×'Ò'Ñ)Ô)ˆØ�tÐÐØ%)¨(¤^Ñ"ˆJ˜
ÝðG°jð Gð GØðGð GØ.9ðGð GØAKðGð Gð Gñô ð ð
 & zÐ2ˆˆX‰ˆð
3ð 
3r6   Údatasetr?   Úimages_directory_pathÚshow_progressc                óš  — t          | j        d„ d¬¦  «         t          |¦  «                             dd¬¦  «         t	          | j        d| ¬¦  «        D ]u}t
          j                             |t          |¦  «        j        ¦  «        }|| j	        v r#| j	        |         }t          j        ||¦  «         Œ`t          j        ||¦  «         ŒvdS )	a  Save all images from a dataset to a directory.

    Images already in memory are written with ``cv2.imwrite``; images stored
    only as file paths are copied with ``shutil.copyfile``.

    Args:
        dataset: The dataset whose images are saved.
        images_directory_path: Destination directory path; created
            automatically if it does not exist.
        show_progress: If ``True``, display a tqdm progress bar while
            saving images.

    Examples:
        ```pycon
        >>> from supervision.dataset.core import DetectionDataset
        >>> from supervision.dataset.utils import save_dataset_images
        >>> dataset = DetectionDataset(classes=["cat"], images={}, annotations={})
        >>> save_dataset_images(dataset, "/tmp/images")

        ```
    c                ó*   — t          | ¦  «        j        S r]   )r   Úname)r   s    r)   ú<lambda>z%save_dataset_images.<locals>.<lambda>ð   s   € �t JÑ/Ô/Ô4€ r6   r%   )r{   r|   r~   T)ÚparentsÚexist_okzSaving images)ÚdescÚdisableN)r   r{   r   Úmkdirr   ÚosÚpathÚjoinrŒ   Ú_images_in_memoryÚcv2ÚimwriteÚshutilÚcopyfile)r‡   rˆ   r‰   r   Ú
final_pathr%   s         r)   Úsave_dataset_imagesrœ   Ô   sí   € õ4 !ØÔ'Ø4Ð4Øðñ ô ð õ
 	Ð	ÑÔ×%Ò%¨d¸TÐ%ÑBÔBÐBÝØÔØØ!Ð!ðñ ô ð 
4ð 
4ˆ
õ
 ”W—\’\Ð"7½¸jÑ9IÔ9IÔ9NÑOÔOˆ
Ø˜Ô2Ð2Ð2ØÔ-¨jÔ9ˆEÝŒK˜
 EÑ*Ô*Ð*Ð*åŒO˜J¨
Ñ3Ô3Ð3Ð3ð
4ð 
4r6   çš™™™™™é?TÚdataúlist[T]Útrain_ratioÚrandom_stateú
int | NoneÚshuffleútuple[list[T], list[T]]c                óü   — t          j        |¦  «        }|r%t          | ¦  «        } |                     | ¦  «         nt          | ¦  «        } t	          t          | ¦  «        |z  ¦  «        }| d|…         | |d…         fS )ah  
    Splits the data into two parts using the provided train_ratio.

    Args:
        data: The data to split.
        train_ratio: The ratio of the training set to the entire dataset.
        random_state: The seed for the random number generator.
        shuffle: Whether to shuffle the data before splitting.

    Returns:
        The split data. The input list is copied and never mutated.

    Examples:
        ```pycon
        >>> train, test = train_test_split(
        ...     [1, 2, 3, 4, 5], train_ratio=0.6, random_state=0
        ... )
        >>> len(train), len(test)
        (3, 2)

        ```
    N)ÚrandomÚRandomr4   r£   r5   rT   )rž   r    r¡   r£   ÚrngÚsplit_indexs         r)   r   r     s{   € õ8 Œ-˜Ñ
%Ô
%€CØð Ý�D‰zŒzˆØ�Š�DÑÔÐÐå�D‰zŒzˆå•c˜$‘i”i +Ñ-Ñ.Ô.€KØ���Ô˜t K L LÔ1Ð1Ð1r6   )r   r   r   r   )F)r.   r/   r0   r1   r   r2   )r7   r8   r9   r   r   r/   )rA   rB   rA   )
r.   r/   rC   rD   rE   rD   rF   rD   r   rG   )rY   rZ   r   r[   )re   r[   rf   r[   r   rg   )rp   rg   rq   r   r   r   )r{   r[   r|   r}   r~   r   r   r   )r‡   r?   rˆ   r   r‰   r1   r   r   )r�   NT)
rž   rŸ   r    rD   r¡   r¢   r£   r1   r   r¤   )7Ú
__future__r   Ú__all__rv   r“   r¦   r™   Úcollections.abcr   Úpathlibr   Útypingr   r	   r
   Únumpyr=   Únumpy.typingr;   Ú	deprecater   r   ÚPILr   Ú	tqdm.autor   Úsupervisionr   r—   Úsupervision._cv2._imager   Úsupervision.detection.corer   Ú&supervision.detection.utils.convertersr   r   Ú_mask_to_rler   Ú_rle_to_maskÚ$supervision.detection.utils.polygonsr   r   Ú	frozensetr$   r*   Úsupervision.dataset.corer?   r@   rX   rd   ro   rz   r   rœ   r   © r6   r)   ú<module>r¾      s  ðØ "Ð "Ð "Ð "Ð "Ð "à)Ð+=Ð
>€à €€€Ø 	€	€	€	Ø €€€Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à #Ð #Ð #Ð #Ð #Ð #Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø CÐ CÐ CÐ CÐ CÐ Cðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ð ð ð
 #, )¨L¨L¨LÑ"9Ô"9Ð ðð ð ð ð €�<¨xÀ8ÐLÑLÔLà49ð9ð 9ð 9ð 9ñ MÔLð9ð €�<¨xÀ8ÐLÑLÔLðAð Að Añ MÔLðAð ð :Ø9Ð9Ð9Ð9Ð9Ð9à€GˆC�L„L€ð
 (+Ø'*Ø&)ð	ð ð ð ð ðB(ð (ð (ð (ðð ð ð ð$ð ð ð ð(/3ð /3ð /3ð /3ðj  ð*4ð *4ð *4ð *4ð *4ð^ Ø#Øð	$2ð $2ð $2ð $2ð $2ð $2ð $2r6   