§
    >²¸jªN  ã                  óˆ  — d dl mZ d dl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mZm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mZmZ d d
lmZ d dlmZ d dlm Z m!Z! d dl"m#Z#m$Z$m%Z%m&Z&m'Z' erd dl(m)Z) dKd„Z*dLd„Z+dKd„Z,dMd„Z-dNd„Z.dOd"„Z/dPd$„Z0dQd'„Z1	 dRdSd,„Z2	 	 	 dTdUd3„Z3	 dVdWd;„Z4	 	 	 	 dXdYdD„Z5	 	 	 	 	 dZd[dH„Z6d\dJ„Z7dS )]é    )ÚannotationsN)ÚSequence)ÚDecimalÚInvalidOperation)ÚPath)ÚTYPE_CHECKINGÚAnyÚcast)Útqdm)ÚORIENTED_BOX_COORDINATES)Ú_image_file_resolution_whÚapproximate_mask_with_polygonsÚcheck_no_basename_collisions)Ú
Detections)Ú_DetectionDataType)Úpolygon_to_maskÚpolygon_to_xyxy)Úlist_files_with_extensionsÚread_txt_fileÚread_yaml_fileÚsave_text_fileÚsave_yaml_file)ÚDetectionDatasetÚvaluesú	list[str]Úreturnúnpt.NDArray[np.float32]c           	     óX  — | \  }}}}t          j        t          |¦  «        t          |¦  «        dz  z
  t          |¦  «        t          |¦  «        dz  z
  t          |¦  «        t          |¦  «        dz  z   t          |¦  «        t          |¦  «        dz  z   gt           j        ¬¦  «        S )Né   ©Údtype)ÚnpÚarrayÚfloatÚfloat32)r   Úx_centerÚy_centerÚwidthÚheights        úp/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/dataset/formats/yolo.pyÚ
_parse_boxr+   #   s™   € Ø(.Ñ%€Hˆh˜˜vÝŒ8å�(‰OŒO�e E™lœl¨QÑ.Ñ.Ý�(‰OŒO�e F™mœm¨aÑ/Ñ/Ý�(‰OŒO�e E™lœl¨QÑ.Ñ.Ý�(‰OŒO�e F™mœm¨aÑ/Ñ/ð		
õ Œjðñ ô ð ó    Úboxc                óº   — t          j        | d         | d         g| d         | d         g| d         | d         g| d         | d         ggt           j        ¬¦  «        S )Nr   é   r   é   r    ©r"   r#   r%   ©r-   s    r*   Ú_box_to_polygonr3   0   s`   € ÝŒ8Ø
ˆaŒ&�#�a”&Ð	˜C œF C¨¤FÐ+¨c°!¬f°c¸!´fÐ-=ÀÀAÄÈÈAÌÐ?OÐPÝŒjðñ ô ð r,   c                ój   — t          j        | t           j        ¬¦  «                             dd¦  «        S )Nr    éÿÿÿÿr   )r"   r#   r%   Úreshape©r   s    r*   Ú_parse_polygonr8   7   s)   € ÝŒ8�F¥"¤*Ð-Ñ-Ô-×5Ò5°b¸!Ñ<Ô<Ð<r,   Úpolygonsú Sequence[npt.NDArray[np.number]]Úresolution_whútuple[int, int]únpt.NDArray[np.bool_]c                óR   ‡— t          j        ˆfd„| D ¦   «         t          ¬¦  «        S )Nc                ó�   •— g | ]B}t          t          j        |¦  «                             t          j        ¦  «        ‰¬ ¦  «        ‘ŒCS ))Úpolygonr;   )r   r"   ÚroundÚastypeÚint32©Ú.0r@   r;   s     €r*   ú
<listcomp>z&_polygons_to_masks.<locals>.<listcomp>?   sZ   ø€ ð 	
ð 	
ð 	
ð
 õ	 Ýœ Ñ)Ô)×0Ò0µ´Ñ:Ô:Ø+ðñ ô ð	
ð 	
ð 	
r,   r    )r"   r#   Úbool©r9   r;   s    `r*   Ú_polygons_to_masksrI   ;   sK   ø€ õ Œ8ð	
ð 	
ð 	
ð 	
ð
 $ð	
ñ 	
ô 	
õ ð	ñ 	ô 	ð 	r,   ÚlinesrG   c                ó4   — t          d„ | D ¦   «         ¦  «        S )Nc                óX   — g | ]'}t          |                     ¦   «         ¦  «        d k    ‘Œ(S )é   )ÚlenÚsplit)rE   Úlines     r*   rF   z"_with_seg_mask.<locals>.<listcomp>K   s-   € Ð8Ð8Ð8¨$•�D—J’J‘L”LÑ!Ô! AÒ%Ð8Ð8Ð8r,   )Úany©rJ   s    r*   Ú_with_seg_maskrS   J   s   € ÝÐ8Ð8°%Ð8Ñ8Ô8Ñ9Ô9Ð9r,   Ú	file_pathÚstrc           	     ón  ‡‡— t          | ¬¦  «        }t          |t          ¦  «        s(t          d| › dt	          |¦  «        j        › d�¦  «        ‚|                     d¦  «        Št          ‰t          ¦  «        rôt          ‰                     ¦   «         ¦  «        }dd
„Šˆfd„|D ¦   «         }t          |¦  «        rlt          |¦  «        s]d„ t          ||¦  «        D ¦   «         dd…         }d„ t          ||¦  «        D ¦   «         dd…         }t          d| › d|› d|› d�¦  «        ‚t          |¦  «        rt          |d„ ¬¦  «        }nt          |t          ¬¦  «        }ˆfd„|D ¦   «         S t          ‰t          ¦  «        rd„ ‰D ¦   «         S t          d| › dt	          ‰¦  «        j        › d�¦  «        ‚)a  Return class names from a YOLO data.yaml file ordered by class index.

    Supports list and dict forms of the ``names`` field. Dict keys that are
    all int-like (plain ints or digit strings) are sorted numerically so
    class index 10 follows index 9. All-non-numeric keys are sorted
    lexicographically. Mixed numeric/non-numeric keys raise ``ValueError``.
    Boolean YAML keys (``true``/``false``) are excluded from numeric sorting
    because ``bool`` is a subclass of ``int`` in Python.

    Args:
        file_path: Path to the data.yaml file.

    Returns:
        Class names in class-index order.

    Raises:
        ValueError: If the YAML root is not a mapping, if ``names`` is
            neither a list nor a dict, or if the dict has mixed key types.
    ©rT   z"Expected mapping in data.yaml at 'z', got ú.ÚnamesÚkeyr	   r   rG   c                óÜ   — t          | t          ¦  «        rdS t          | t          ¦  «        rdS t          | t          ¦  «        r(|                      ¦   «         }|                     ¦   «         S dS )NFT)Ú
isinstancerG   ÚintrU   ÚstripÚisdigit)rZ   Ústrippeds     r*   Ú_is_int_likez*_extract_class_names.<locals>._is_int_likel   sf   € å˜#�tÑ$Ô$ð Ø�uÝ˜#�sÑ#Ô#ð Ø�tÝ˜#�sÑ#Ô#ð *ØŸ9š9™;œ;�Ø×'Ò'Ñ)Ô)Ð)Ø�5r,   c                ó&   •— g | ]} ‰|¦  «        ‘ŒS © rc   )rE   Úkra   s     €r*   rF   z(_extract_class_names.<locals>.<listcomp>w   s!   ø€ Ð2Ð2Ð2¨�L�L ‘O”OÐ2Ð2Ð2r,   c                ó   — g | ]	\  }}|¯|‘Œ
S rc   rc   ©rE   rd   Úils      r*   rF   z(_extract_class_names.<locals>.<listcomp>y   s!   € ÐFÐFÐF¡5 1 bÀ2ÐF˜QÐFÐFÐFr,   Nr0   c                ó   — g | ]	\  }}|°|‘Œ
S rc   rc   rf   s      r*   rF   z(_extract_class_names.<locals>.<listcomp>z   s!   € ÐHÐHÐH¡  BÀRÐH˜1ÐHÐHÐHr,   z'Expected 'names' dict in data.yaml at 'zI' to have either all numeric or all non-numeric keys, got a mix: numeric z and non-numeric z keys.c                ó    — t          | ¦  «        S ©N)r]   )rd   s    r*   ú<lambda>z&_extract_class_names.<locals>.<lambda>�   s   € µS¸±V´V€ r,   )rZ   c                ó:   •— g | ]}t          ‰|         ¦  «        ‘ŒS rc   ©rU   )rE   rZ   rY   s     €r*   rF   z(_extract_class_names.<locals>.<listcomp>„   s#   ø€ Ð7Ð7Ð7 C•�E˜#”J‘”Ð7Ð7Ð7r,   c                ó,   — g | ]}t          |¦  «        ‘ŒS rc   rm   )rE   Únames     r*   rF   z(_extract_class_names.<locals>.<listcomp>†   s   € Ð,Ð,Ð,˜d•�D‘	”	Ð,Ð,Ð,r,   z7Expected 'names' to be a list or dict in data.yaml at ')rZ   r	   r   rG   )r   r\   ÚdictÚ
ValueErrorÚtypeÚ__name__ÚgetÚlistÚkeysrQ   ÚallÚzipÚsortedrU   )	rT   Údatarv   Úint_likeÚmixed_numericÚmixed_otherÚsorted_keysra   rY   s	          @@r*   Ú_extract_class_namesr   N   s9  øø€ õ( *°IÐ>Ñ>Ô>€DÝ�d�DÑ!Ô!ð 
Ýð+°ð +ð +Ý˜‘J”JÔ'ð+ð +ð +ñ
ô 
ð 	
ð �HŠH�WÑÔ€EÝ�%�ÑÔð 8Ý�E—J’J‘L”LÑ!Ô!ˆð		ð 		ð 		ð 		ð 3Ð2Ð2Ð2¨TÐ2Ñ2Ô2ˆÝˆx‰=Œ=ð 	¥ X¡¤ð 	ØFÐF­C°°hÑ,?Ô,?ÐFÑFÔFÀrÈÀrÔJˆMØHÐH­#¨d°HÑ*=Ô*=ÐHÑHÔHÈÈ!ÈÔLˆKÝðO¸)ð Oð Oà(ðOð Oà;FðOð Oð Oñô ð õ
 ˆx‰=Œ=ð 	0Ý  Ð+;Ð+;Ð<Ñ<Ô<ˆKˆKå  ­3Ð/Ñ/Ô/ˆKØ7Ð7Ð7Ð7¨;Ð7Ñ7Ô7Ð7Ý�%�ÑÔð -Ø,Ð, eÐ,Ñ,Ô,Ð,Ý
ð	6Øð	6ð 	6Ý" 5™kœkÔ2ð	6ð 	6ð 	6ñô ð r,   Ú
image_namec                óP   — t           j                             | ¦  «        \  }}|dz   S )Nú.txt)ÚosÚpathÚsplitext)r€   Ú	base_nameÚ_s      r*   Ú_image_name_to_annotation_namerˆ   �   s&   € Ý”7×#Ò# JÑ/Ô/�L€IˆqØ�vÑÐr,   Úvaluer]   c                óD  — 	 t          | ¦  «        S # t          $ r Y nw xY w	 t          | ¦  «        }n"# t          $ r t          d| ›d�¦  «        d‚w xY w|                     ¦   «         r||                     ¦   «         k    rt          d| ›d�¦  «        ‚t          |¦  «        S )aM  Parse a YOLO class id written as an integer or as a whole decimal number.

    Label files saved with ``np.savetxt`` write every column as a float, such as
    ``1.000000000000000000e+00``, and Ultralytics reads the class column as a float, so
    such datasets train there. Values that are not finite whole numbers still raise.
    zInvalid class id z- in YOLO annotation; expected a whole number.N)r]   rq   r   r   Ú	is_finiteÚto_integral_value)r‰   Úclass_ids     r*   Ú_parse_class_idrŽ   ’   sÝ   € ðÝ�5‰zŒzÐøÝð ð ð ØˆðøøøðÝ˜5‘>”>ˆˆøÝð ð ð ÝØV ÐVÐVÐVñ
ô 
àð	ðøøøð ×ÒÑÔð 
 8¨x×/IÒ/IÑ/KÔ/KÒ#KÐ#KÝØV ÐVÐVÐVñ
ô 
ð 	
õ ˆx‰=Œ=Ðs   ‚ ‘
�¢2 ²AFÚ
with_masksÚis_obbr   c                ót  ‡— t          | ¦  «        dk    rt          j        ¦   «         S g }g }g }g }‰\  }}	| D �]G}
|
                     ¦   «         }|                     t          |d         ¦  «        ¦  «         t          |¦  «        dk    rSt          |dd …         ¬¦  «        }|                     |¦  «         |r#|                     t          |¬¦  «        ¦  «         Œ¥t          |¦  «        dk    r�t          |dd …         ¬¦  «        }|                     t          |¬¦  «        ¦  «         |r;|                     t          j        |dd …         t          j        ¬¦  «        ¦  «         |r|                     |¦  «         �ŒIt          j        |t          ¬¦  «        }t          j        |t          j        ¬¦  «        }|t          j        ||	||	gt          j        ¬¦  «        z  }i }|rŽt          j        |t          j        ¬¦  «        }|                     dd	d
¦  «        }|t          j        ||	gt          j        ¬¦  «        z  }t          t           j        t          j                 |¦  «        |t&          <   |st          |||¬¦  «        S ˆfd„|D ¦   «         }t)          |‰¬¦  «        }t          ||||¬¦  «        S )Nr   rM   r/   r7   r2   ©r@   r    r5   é   r   )r�   Úxyxyrz   c                óV   •— g | ]%}|t          j        ‰t           j        ¬ ¦  «        z  ‘Œ&S )r    r1   rD   s     €r*   rF   z2yolo_annotations_to_detections.<locals>.<listcomp>Ö   s?   ø€ ð ð ð àð 	•"”(˜=µ´
Ð;Ñ;Ô;Ñ;ðð ð r,   rH   )r�   r”   rz   Úmask)rN   r   ÚemptyrO   ÚappendrŽ   r+   r3   r8   r   r"   r#   r%   r]   r6   r
   ÚnptÚNDArrayÚgenericr   rI   )rJ   r;   r�   r�   Úclass_id_listÚrelative_xyxy_listÚrelative_polygon_listÚrelative_xyxyxyxy_listÚwÚhrP   r   r-   r@   r�   Úrelative_xyxyr”   rz   Úrelative_xyxyxyxyÚxyxyxyxyr9   r–   s    `                    r*   Úyolo_annotations_to_detectionsr¥   ª   s·  ø€ õ ˆ5�z„z�Q‚€ÝÔÑ!Ô!Ð!à!€MØ79ÐØ;=ÐØ<>ÐØ�D€A€qØð 6ñ 6ˆØ—’‘”ˆØ×Ò�_¨V°A¬YÑ7Ô7Ñ8Ô8Ð8Ýˆv‰;Œ;˜!ÒÐÝ F¨1¨2¨2¤JÐ/Ñ/Ô/ˆCØ×%Ò% cÑ*Ô*Ð*Øð GØ%×,Ò,­_ÀÐ-EÑ-EÔ-EÑFÔFÐFøÝ�‰[Œ[˜1Š_ˆ_Ý$¨F°1°2°2¬JÐ7Ñ7Ô7ˆGØ×%Ò%¥o¸gÐ&FÑ&FÔ&FÑGÔGÐGØð VØ&×-Ò-­b¬h°v¸a¸b¸b´zÍÌÐ.TÑ.TÔ.TÑUÔUÐUØð 6Ø%×,Ò,¨WÑ5Ô5Ð5ùåŒx˜­SÐ1Ñ1Ô1€HÝ”HÐ/µr´zÐBÑBÔB€MØ�2œ8 Q¨¨1¨a L½¼
ÐCÑCÔCÑC€DØ!€Dàð QÝœHÐ%;Å2Ä:ÐNÑNÔNÐØ$×,Ò,¨R°°AÑ6Ô6ˆØ•B”H˜a ˜V­2¬:Ð6Ñ6Ô6Ñ6ˆÝ)-­c¬k½"¼*Ô.EÀxÑ)PÔ)PˆÕ%Ñ&àð CÝ 8°$¸TÐBÑBÔBÐBðð ð ð à,ðñ ô €Hõ  x¸}ÐMÑMÔM€DÝ˜x¨d¸ÀDÐIÑIÔIÐIr,   Úimages_directory_pathÚannotations_directory_pathÚdata_yaml_pathÚforce_masksÚshow_progressú2tuple[list[str], list[str], dict[str, Detections]]c                ót  — |r|rt          j        dt          d¬¦  «         d„ t          | g d¢¬¦  «        D ¦   «         }t	          |¬¦  «        }i }t          |t          |¦  «        d| ¬	¦  «        D ]Ã}	t          |	¦  «        j        }
t          j
                             ||
› d
�¦  «        }t          j
                             |¦  «        st          j        ¦   «         ||	<   Œot          |	¦  «        \  }}t!          |d¬¦  «        }||f}| o|pt#          |¬¦  «        }t%          ||||¬¦  «        }|||	<   ŒÄ|||fS )a”  
    Loads YOLO annotations and returns class names, images,
        and their corresponding detections.

    Args:
        images_directory_path: The path to the directory containing the images.
        annotations_directory_path: The path to the directory
            containing the YOLO annotation files.
        data_yaml_path: The path to the data
            YAML file containing class information.
        force_masks: If True, forces masks to be loaded
            for all annotations, regardless of whether they are present.
            This parameter has no effect when `is_obb=True`; mask generation
            is always disabled for OBB annotations.
        is_obb: If True, loads the annotations in OBB format.
            OBB annotations are defined as `[class_id, x, y, x, y, x, y, x, y]`,
            where pairs of [x, y] are box corners.
        show_progress: If True, display a progress bar during loading.

    Returns:
        A tuple containing a list of class names, a dictionary with
            image names as keys and images as values, and a dictionary
            with image names as keys and corresponding Detections instances as values.
    zl`force_masks=True` has no effect when `is_obb=True`; mask generation is always disabled for OBB annotations.r   ©Ú
stacklevelc                ó,   — g | ]}t          |¦  «        ‘ŒS rc   rm   )rE   r„   s     r*   rF   z)load_yolo_annotations.<locals>.<listcomp>  s.   € ð ð ð àõ 	ˆD‰	Œ	ðð ð r,   )	ÚbmpÚdngÚjpgÚjpegÚmpoÚpngÚtifÚtiffÚwebp)Ú	directoryÚ
extensionsrW   zLoading YOLO annotations©ÚtotalÚdescÚdisabler‚   T)rT   Ú
skip_emptyrR   )rJ   r;   r�   r�   )ÚwarningsÚwarnÚUserWarningr   r   r   rN   r   Ústemrƒ   r„   ÚjoinÚexistsr   r—   r   r   rS   r¥   )r¦   r§   r¨   r©   r�   rª   Úimage_pathsÚclassesr   Ú
image_pathÚ
image_stemÚannotation_pathr    r¡   rJ   r;   r�   Ú
annotations                     r*   Úload_yolo_annotationsrÌ   Þ   s¥  € ð@ ð 
�+ð 
ÝŒðFåØð		
ñ 	
ô 	
ð 	
ðð å.Ø+ð
ð 
ð 
ð
ñ 
ô 
ðñ ô €Kõ$ #¨^Ð<Ñ<Ô<€GØ€KåØÝ�+ÑÔØ'Ø!Ð!ð	ñ ô ð -ð -ˆ
õ ˜*Ñ%Ô%Ô*ˆ
Ýœ'Ÿ,š,Ð'AÀjÐCVÐCVÐCVÑWÔWˆÝŒw�~Š~˜oÑ.Ô.ð 	Ý&0Ô&6Ñ&8Ô&8ˆK˜
Ñ#Øå(¨Ñ4Ô4‰ˆˆ1Ý¨ÀDÐIÑIÔIˆØ˜A˜ˆà�ZÐP [Ð%OµNÈÐ4OÑ4OÔ4Oˆ
Ý3ØØ'Ø!Øð	
ñ 
ô 
ˆ
ð #-ˆ�JÑÐØ�K Ð,Ð,r,   r”   únpt.NDArray[np.number]r�   Úimage_shapeútuple[int, int, int]r@   únpt.NDArray[np.number] | Nonec           
     ó¾  — |\  }}}|€i| t          j        ||||gt           j        ¬¦  «        z  }|\  }}	}
}||
z   dz  }|	|z   dz  }|
|z
  }||	z
  }t          |¦  «        › d|d›d|d›d|d›d|d›�	S |t          j        ||gt           j        ¬¦  «        z  }|                     d¦  «        }d                     d„ |D ¦   «         ¦  «        }t          |¦  «        › d|› �S )Nr    r   ú ú.5fr5   c                ó   — g | ]}|d ›‘ŒS )rÓ   rc   )rE   r‰   s     r*   rF   z"object_to_yolo.<locals>.<listcomp>G  s   € Ð"PÐ"PÐ"P°e e > >Ð"PÐ"PÐ"Pr,   )r"   r#   r%   r]   r6   rÄ   )r”   r�   rÎ   r@   r¡   r    r‡   Úxyxy_relativeÚx_minÚy_minÚx_maxÚy_maxr&   r'   r(   r)   Úpolygon_relativeÚpolygon_parseds                     r*   Úobject_to_yolorÜ   5  s   € ð �G€A€qˆ!Ø€Ø�rœx¨¨A¨q°!¨½B¼JÐGÑGÔGÑGˆØ%2Ñ"ˆˆu�e˜UØ˜E‘M QÑ&ˆØ˜E‘M QÑ&ˆØ˜‘ˆØ˜‘ˆÝ�h‘-”-ÐXÐX (ÐXÐXÐX°ÐXÐXÐXÀÐXÐXÐXÈFÐXÐXÐXÐXà"¥R¤X¨q°!¨f½B¼JÐ%GÑ%GÔ%GÑGÐØ+×3Ò3°BÑ7Ô7ÐØŸšÐ"PÐ"PÐ?OÐ"PÑ"PÔ"PÑQÔQˆÝ�h‘-”-Ð2Ð2 .Ð2Ð2Ð2r,   ç        ç      ð?ç      è?Ú
detectionsÚmin_image_area_percentager$   Úmax_image_area_percentageÚapproximation_percentagec                óÂ  — |rAt          | ¦  «        dk    r.t          | j        vr t          dt          › dt          › d�¦  «        ‚|r,| j        �%t          j        dt          › d�t          d¬	¦  «         g }| D �]g\  }}}	}
}	}|
€t          d
¦  «        ‚t          |
t          t          j        f¦  «        s t          dt          |
¦  «        ›d�¦  «        ‚t          |
¦  «        }|rt          j        |t                   t          j        ¬¦  «        }|j        dk    r t          d|j        › dt          › d�¦  «        ‚t!          ||||¬¦  «        }|                     |¦  «         Œì|�Rt%          ||||¬¦  «        }|D ]:}t'          |¬¦  «        }t!          ||||¬¦  «        }|                     |¦  «         Œ;�Œ@t!          |||¬¦  «        }|                     |¦  «         �Œi|S )aB  Convert detections to YOLO annotation lines.

    Args:
        detections: The detections to serialize. Each detection must have a
            valid integer ``class_id``. When ``is_obb=True``, each non-empty
            detection must also carry ``detections.data['xyxyxyxy']`` with
            shape ``(N, 4, 2)``.
        image_shape: The ``(height, width, channels)`` shape of the source
            image, used to normalize coordinates to ``[0, 1]``.
        min_image_area_percentage: Minimum detection area as a fraction of the
            image area; smaller detections are omitted. Ignored when
            ``is_obb=True``.
        max_image_area_percentage: Maximum detection area as a fraction of the
            image area; larger detections are omitted. Ignored when
            ``is_obb=True``.
        approximation_percentage: Fraction of polygon points removed during
            contour approximation when saving mask annotations. Ignored when
            ``is_obb=True``.
        is_obb: If ``True``, serializes oriented bounding-box corners from
            ``detections.data['xyxyxyxy']`` as a 9-token YOLO OBB line
            ``class_id x1 y1 x2 y2 x3 y3 x4 y4``. Mask data is ignored.

    Returns:
        A list of YOLO annotation strings, one per detection (or one per
        polygon for instance-segmentation annotations).

    Raises:
        ValueError: If any detection has ``class_id=None`` or a non-integer
            ``class_id``.
        ValueError: If ``is_obb=True`` and any non-empty detection is missing
            ``'xyxyxyxy'`` in ``detections.data``.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.core import Detections
        >>> from supervision.dataset.formats.yolo import detections_to_yolo_annotations
        >>> detections = Detections(
        ...     xyxy=np.array([[10, 10, 90, 90]], dtype=np.float32),
        ...     class_id=np.array([0]),
        ... )
        >>> detections_to_yolo_annotations(detections, image_shape=(100, 100, 3))
        ['0 0.50000 0.50000 0.80000 0.80000']

        ```
    r   z`is_obb=True` requires `'zŒ'` in `detections.data` with shape (N, 4, 2). Load OBB datasets via `DetectionDataset.from_yolo(..., is_obb=True)` or set `detections.data['z'']` (shape (N, 4, 2)) before exporting.Nzo`detections.mask` is ignored when `is_obb=True`; OBB annotations use corner coordinates from `detections.data['z']`.r   r­   z*Class ID is required for YOLO annotations.z<Detections class_id must be an integer for YOLO export, got rX   r    )r“   r   z8OBB data for each detection must have shape (4, 2), got z. Ensure `detections.data['z)']` has shape (N, 4, 2) before exporting.)r”   r�   rÎ   r@   )r–   rá   râ   rã   r’   )r”   r�   rÎ   )rN   r   rz   rq   r–   rÀ   rÁ   rÂ   r\   r]   r"   Úintegerrr   Úasarrayr%   ÚshaperÜ   r˜   r   r   )rà   rÎ   rá   râ   rã   r�   rË   r”   r–   r‡   r�   rz   Úclass_id_intÚcornersÚnext_objectr9   r@   s                    r*   Údetections_to_yolo_annotationsrë   K  s¢  € ðn 	ð
å�
‰OŒO˜aÒÐÝ$¨J¬OÐ;Ð;åð2Õ(@ð 2ð 2õ ":ð2ð 2ð 2ñ
ô 
ð 	
ð ð 
�*”/Ð-ÝŒð@å!9ð@ð @ð @õ Øð	
ñ 	
ô 	
ð 	
ð €JØ,6ð 0+ñ 0+Ñ(ˆˆd�A�x  DØÐÝÐIÑJÔJÐJÝ˜(¥S­"¬*Ð$5Ñ6Ô6ð 	Ýð+Ý˜H‘~”~ð+ð +ð +ñô ð õ ˜8‘}”}ˆàð 	Ý”j Õ&>Ô!?ÅrÄzÐRÑRÔRˆGØŒ} Ò&Ð&Ý ð8Ø"œ=ð8ð 8å)Að8ð 8ð 8ñô ð õ )ØØ%Ø'Øð	ñ ô ˆKð ×Ò˜kÑ*Ô*Ð*ØàÐÝ5ØØ*CØ*CØ)Að	ñ ô ˆHð $ð /ð /�Ý&¨wÐ7Ñ7Ô7�Ý,ØØ)Ø +Ø#ð	ñ ô �ð ×!Ò! +Ñ.Ô.Ð.Ð.ñ/õ )Ø L¸kðñ ô ˆKð ×Ò˜kÑ*Ô*Ð*Ñ*ØÐr,   Údatasetr   ÚNonec           	     ó¬  — t          | j        d„ d¬¦  «         t          |¦  «                             dd¬¦  «         t	          | t          | ¦  «        d| ¬¦  «        D ]u\  }}}	t          |¦  «        j        }
t          |
¬¦  «        }t          j	         
                    ||¦  «        }t          |	|j        ||||¬	¦  «        }t          ||¬
¦  «         ŒvdS )aÕ  Save dataset annotations in YOLO format.

    Args:
        dataset: The dataset whose annotations are saved.
        annotations_directory_path: Path to the directory where annotation
            ``.txt`` files are written; created automatically if absent.
        min_image_area_percentage: Minimum detection area as a fraction of the
            image area; smaller detections are omitted. Ignored when
            ``is_obb=True``.
        max_image_area_percentage: Maximum detection area as a fraction of the
            image area; larger detections are omitted. Ignored when
            ``is_obb=True``.
        approximation_percentage: Fraction of polygon points removed during
            contour approximation when saving mask annotations. Ignored when
            ``is_obb=True``.
        is_obb: If ``True``, writes oriented bounding-box annotations using
            the 9-token format ``class_id x1 y1 x2 y2 x3 y3 x4 y4``. Each
            non-empty detection must carry ``detections.data['xyxyxyxy']``
            with shape ``(N, 4, 2)``.
        show_progress: If ``True``, display a tqdm progress bar while
            saving annotations.

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

        ```
    c                óD   — t          t          | ¦  «        j        ¦  «        S rj   )rˆ   r   ro   )rÈ   s    r*   rk   z'save_yolo_annotations.<locals>.<lambda>ö  s   € Õ=½dÀ:Ñ>NÔ>NÔ>SÑTÔT€ r,   zYOLO annotation)rÆ   rZ   Úoutput_kindT©ÚparentsÚexist_okzSaving YOLO annotationsr»   )r€   )rà   rÎ   rá   râ   rã   r�   )rJ   rT   N)r   rÆ   r   Úmkdirr   rN   ro   rˆ   rƒ   r„   rÄ   rë   rç   r   )rì   r§   rá   râ   rã   r�   rª   rÈ   ÚimagerË   r€   Úyolo_annotations_nameÚyolo_annotations_pathrJ   s                 r*   Úsave_yolo_annotationsrø   Ì  s  € õP !ØÔ'ØTÐTØ%ðñ ô ð õ
 	Ð	#Ñ$Ô$×*Ò*°4À$Ð*ÑGÔGÐGÝ)-ØÝ�'‰lŒlØ&Ø!Ð!ð	*ñ *ô *ð Eð EÑ%ˆ
�E˜:õ ˜*Ñ%Ô%Ô*ˆ
Ý >È*Ð UÑ UÔ UÐÝ "¤§¢Ø&Ð(=ñ!
ô !
Ðõ /Ø!ØœØ&?Ø&?Ø%=Øð
ñ 
ô 
ˆõ 	˜UÐ.CÐDÑDÔDÐDÐDð'Eð Er,   rÇ   c                óž   — t          |¦  «        |dœ}t          | ¦  «        j                             dd¬¦  «         t	          || ¬¦  «         d S )N)ÚncrY   Trñ   )rz   rT   )rN   r   Úparentrô   r   )r¨   rÇ   rz   s      r*   Úsave_data_yamlrü     sR   € Ý�g‘,”,¨Ð1Ð1€DÝˆÑÔÔ×%Ò%¨d¸TÐ%ÑBÔBÐBÝ˜¨Ð7Ñ7Ô7Ð7Ð7Ð7r,   )r   r   r   r   )r-   r   r   r   )r9   r:   r;   r<   r   r=   )rJ   r   r   rG   )rT   rU   r   r   )r€   rU   r   rU   )r‰   rU   r   r]   )F)
rJ   r   r;   r<   r�   rG   r�   rG   r   r   )FFF)r¦   rU   r§   rU   r¨   rU   r©   rG   r�   rG   rª   rG   r   r«   rj   )
r”   rÍ   r�   r]   rÎ   rÏ   r@   rÐ   r   rU   )rÝ   rÞ   rß   F)rà   r   rÎ   rÏ   rá   r$   râ   r$   rã   r$   r�   rG   r   r   )rÝ   rÞ   rß   FF)rì   r   r§   rU   rá   r$   râ   r$   rã   r$   r�   rG   rª   rG   r   rí   )r¨   rU   rÇ   r   r   rí   )8Ú
__future__r   rƒ   rÀ   Úcollections.abcr   Údecimalr   r   Úpathlibr   Útypingr   r	   r
   Únumpyr"   Únumpy.typingr™   Ú	tqdm.autor   Úsupervision.configr   Úsupervision.dataset.utilsr   r   r   Úsupervision.detection.corer   Ú#supervision.detection.utils._typingr   Ú&supervision.detection.utils.convertersr   r   Úsupervision.utils.filer   r   r   r   r   Úsupervision.dataset.corer   r+   r3   r8   rI   rS   r   rˆ   rŽ   r¥   rÌ   rÜ   rë   rø   rü   rc   r,   r*   ú<module>r     s$  ðØ "Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ø Ð Ð Ð Ð Ð Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à 7Ð 7Ð 7Ð 7Ð 7Ð 7ðð ð ð ð ð ð ð ð ð ð
 2Ð 1Ð 1Ð 1Ð 1Ð 1Ø BÐ BÐ BÐ BÐ BÐ BØ SÐ SÐ SÐ SÐ SÐ SÐ SÐ Sðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð :Ø9Ð9Ð9Ð9Ð9Ð9ð
ð 
ð 
ð 
ðð ð ð ð=ð =ð =ð =ðð ð ð ð:ð :ð :ð :ð<ð <ð <ð <ð~ð ð ð ð
ð ð ð ð8 ð	1Jð 1Jð 1Jð 1Jð 1Jðp ØØðT-ð T-ð T-ð T-ð T-ðv .2ð	3ð 3ð 3ð 3ð 3ð2 (+Ø'*Ø&*Øð~ð ~ð ~ð ~ð ~ðH (+Ø'*Ø&*ØØðAEð AEð AEð AEð AEðH8ð 8ð 8ð 8ð 8ð 8r,   