
    /j                    8   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mZ d dl	m
Z
mZ 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mZmZmZmZ d dlmZ d d	lmZmZ d d
lm Z  d>dZ!d>dZ"	 	 	 	 	 	 d?d@dZ#dAd Z$dBd"Z%dCd&Z&dDd)Z'dEdFd/Z(d0 Z)dGdHd6Z*dId9Z+dJdKd=Z,dS )L    )annotationsN)defaultdict)ThreadPoolExecutoras_completed)Path)Image)
ASSETS_URLDATASETS_DIRLOGGERNUM_THREADSTQDMYAML)
check_file)downloadzip_directory)increment_pathreturn	list[int]c                 
    g dS )a  Convert 91-index COCO class IDs to 80-index COCO class IDs.

    Returns:
        (list[int | None]): A list of 91 elements where the index represents the 91-index class ID and the value is the
            corresponding 80-index class ID, or None if there is no mapping.
    )[r                           	   
   N                                       N      NN                      !   "   #   $   %   &   '   N(   )   *   +   ,   -   .   /   0   1   2   3   4   5   6   7   8   9   :   ;   N<   NN=   N>   ?   @   A   B   C   D   E   F   G   H   NI   J   K   L   M   N   O   N re       _/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/data/converter.pycoco91_to_coco80_classrh      s    \ \ \ \rf   c                 
    g dS )a  Convert 80-index (val2014) to 91-index (paper).

    Returns:
        (list[int]): A list of 80 class IDs where each value is the corresponding 91-index class ID.

    Examples:
        >>> import numpy as np
        >>> a = np.loadtxt("data/coco.names", dtype="str", delimiter="\n")
        >>> b = np.loadtxt("data/coco_paper.names", dtype="str", delimiter="\n")

        Convert the darknet to COCO format
        >>> x1 = [list(a[i] == b).index(True) + 1 for i in range(80)]

        Convert the COCO to darknet format
        >>> x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)]

    References:
        https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
    )Pr   r   r   r   r   r   r   r   r   r   r    r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r0   r1   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rX   r[   r]   r^   r_   r`   ra   rb   rc   rd   P   Q   R   T   U   V   W   X   Y   Z   re   re   rf   rg   coco80_to_coco91_classrt   ~   s    (Q Q Q Qrf   ../coco/annotations/coco_converted/FT
labels_dirstrsave_diruse_segmentsbooluse_keypoints	cls91to80lvisc                   t          |          }|dz  |dz  fD ]}|                    dd           t                      }t          t	          |                                                               d                    D ]}|rdn|j                            dd          }	t	          |          dz  |	z  }
|
                    dd           |r4|
dz                      dd           |
d	z                      dd           t          |d
          5 }t          j        |          }ddd           n# 1 swxY w Y   d |d         D             }t          t                    }|d         D ]#}||d                                      |           $g }t           |j                    d|           D ]R\  }}||d         }|d         |d         }}|r5t#          t	          |d                                       d                    n|d         }|r2|                    t#          t	          d          |z                       g }g }g }|D ]}|                    dd          rt)          j        |d         t(          j                  }|ddxx         |dd         dz  z  cc<   |ddgxx         |z  cc<   |dd gxx         |z  cc<   |d         dk    s|d          dk    r|r||d!         dz
           n
|d!         dz
  }|g|                                }||vr|r|                    d"          |                    |t)          j        |d"                                       d#d           t)          j        ||dg          z                      d#                                          z              |                    |           |rW|                    d$          }|t3          |          dk    r|                    g            t3          |          dk    r{t5          |          }t)          j        |d%          t)          j        ||g          z                      d#                                          }|                    |g|           Td& |D             }t)          j        |                              d#d          t)          j        ||g          z                      d#                                          }|                    |g|           t          |
|z                      d'          d(d
          5 }t;          t3          |                    D ]{}|rg ||         R }n.g |r!t3          ||                   dk    r||         n||         R }|                    d)t3          |          z                                  |z  d*z              |	 ddd           n# 1 swxY w Y   T|rt	          |          |j                             d+d                              d,d'          z  } t          | d(d
          5 }|!                    d- |D                        ddd           n# 1 swxY w Y   tE          j#        |rd.nd/ d0|                                            dS )1a  Convert COCO dataset annotations to a YOLO annotation format suitable for training YOLO models.

    Args:
        labels_dir (str, optional): Path to directory containing COCO dataset annotation files.
        save_dir (str, optional): Path to directory to save results to.
        use_segments (bool, optional): Whether to include segmentation masks in the output.
        use_keypoints (bool, optional): Whether to include keypoint annotations in the output.
        cls91to80 (bool, optional): Whether to map 91 COCO class IDs to the corresponding 80 COCO class IDs.
        lvis (bool, optional): Whether to convert data in lvis dataset way.

    Examples:
        >>> from ultralytics.data.converter import convert_coco

        Convert COCO annotations to YOLO format
        >>> convert_coco("coco/annotations/", use_segments=True, use_keypoints=False, cls91to80=False)

        Convert LVIS annotations to YOLO format
        >>> convert_coco("lvis/annotations/", use_segments=True, use_keypoints=False, cls91to80=False, lvis=True)
    labelsimagesTparentsexist_okz*.json 
instances_	train2017val2017utf-8encodingNc                $    i | ]}|d          d|S )iddre   ).0xs     rg   
<dictcomp>z convert_coco.<locals>.<dictcomp>  s"    <<<QtW..!<<<rf   r   image_idzAnnotations descr   heightwidthcoco_urlzhttp://images.cocodataset.org	file_namez./imagesiscrowdFbbox)dtyper   r   r   r   category_id	keypointssegmentationaxisc                    g | ]	}|D ]}|
S re   re   )r   ijs      rg   
<listcomp>z convert_coco.<locals>.<listcomp>G  s%     ; ; ;q ; ;A ; ; ; ;rf   .txta%g 
lvis_v1_z.jsonc              3      K   | ]	}| d V  
dS r   Nre   r   lines     rg   	<genexpr>zconvert_coco.<locals>.<genexpr>Y  s(      ??T[[[??????rf   LVISCOCOz/ data converted successfully.
Results saved to )$r   mkdirrh   sortedr   resolveglobstemreplaceopenjsonloadr   listappendr   itemsrx   relative_togetnparrayfloat64tolistreshapelenmerge_multi_segmentconcatenatewith_suffixrangewriterstripname
writelinesr   info)!rw   ry   rz   r|   r}   r~   pcoco80	json_filelnamefnfdatar   r   ann	image_txtimg_idannsimghwbboxessegmentsr   boxclssegsfiler   r   filenames!                                    rg   convert_cocor      s   8 h''H (X"55 - -	t,,,, $%%F D,,4466;;HEEFF O@ O@	H	 6 6|R H H(^^h&.
--- 	@ +$$TD$AAA)^""4$"???)g... 	 !9Q<<D	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  =<T(^<<<!$''& 	5 	5CJ(//4444	 !2!2!4!4;U);U;UVVV 5	K 5	KLFDF'Cx=#g,qA[_uDZ))556UVVWWWehiteuA <  T*%5%5%9!:!:;;;FHI  7  7779e,, hs6{"*===BQB3qrr7Q;&QFq QFq q6Q;;#a&A++8A]fS/!344s=GY\]G]*SZZ\\*f$$$ 77;//7$!((28C,<#=#=#E#Eb!#L#LrxYZ\]_`XaObOb#b"k"kln"o"o"v"v"x"xx   MM#&&&# 7!ggn55;#c((a--$OOB//// XX\\ 3C 8 8A!#!:!:!:RXq!f=M=M!M V VWY Z Z a a c cA$OOSI1I6666 ; ;C ; ; ;A!#!!4!4R!;!;bh1v>N>N!N W WXZ [ [ b b d dA$OOSI1I666 rAv**622C'JJJ Kds6{{++ K KA$ 1)A,11 -9ac(1+>N>NQR>R>RhqkkX^_`Xa    JJD		 199;;dBTIJJJJKK K K K K K K K K K K K K K K  	@H~~	(>(>z2(N(N(V(VW^`f(g(ggHhg666 @!??Y??????@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ KT-66vss_g_o_o_q_qssttttts7   D99D=	 D=	BX  XX  ZZ	Z		masks_dir
output_dirclassesintc           
     "   d t          |          D             }t          |                                           D ]R}|j        dv rEt	          j        t          |          t          j                  }|j        \  }}t          j
        d| d| d|            t          j        |          }g }	|D ]1}
|
dk    r
|                    |
d          }|dk    rt          j        d|
 d	| d
           Bt	          j        ||
k                        t          j                  t          j        t          j                  \  }}|D ]}t)          |          dk    r|                                }|g}|D ]Z}|                    t/          |d         |z  d                     |                    t/          |d         |z  d                     [|	                    |           3t          |          |j         dz  }t3          |dd          5 }|	D ]B}d                    t7          t          |                    }|                    |dz              C	 ddd           n# 1 swxY w Y   t          j
        d| d| d|            TdS )u  Convert a dataset of segmentation mask images to the YOLO segmentation format.

    This function takes the directory containing the binary format mask images and converts them into YOLO segmentation
    format. The converted masks are saved in the specified output directory.

    Args:
        masks_dir (str): The path to the directory where all mask images (png, jpg) are stored.
        output_dir (str): The path to the directory where the converted YOLO segmentation masks will be stored.
        classes (int): Total number of classes in the dataset, e.g., 80 for COCO.

    Examples:
        >>> from ultralytics.data.converter import convert_segment_masks_to_yolo_seg

        The classes here is the total classes in the dataset, for COCO dataset we have 80 classes
        >>> convert_segment_masks_to_yolo_seg("path/to/masks_directory", "path/to/output/directory", classes=80)

    Notes:
        The expected directory structure for the masks is:

            - masks
                ├─ mask_image_01.png or mask_image_01.jpg
                ├─ mask_image_02.png or mask_image_02.jpg
                ├─ mask_image_03.png or mask_image_03.jpg
                └─ mask_image_04.png or mask_image_04.jpg

        After execution, the labels will be organized in the following structure:

            - output_dir
                ├─ mask_yolo_01.txt
                ├─ mask_yolo_02.txt
                ├─ mask_yolo_03.txt
                └─ mask_yolo_04.txt
    c                    i | ]}|d z   |	S )r   re   r   r   s     rg   r   z5convert_segment_masks_to_yolo_seg.<locals>.<dictcomp>  s    ???1a!eQ???rf   >   .jpg.pngProcessing z	 imgsz = z x r   r   zUnknown class for pixel value z	 in file z, skipping.r   r   r   r   r   r   r    r   NzProcessed and stored at )r   r   iterdirsuffixcv2imreadrx   IMREAD_GRAYSCALEshaper   r   r   uniquer   warningfindContoursastypeuint8RETR_EXTERNALCHAIN_APPROX_SIMPLEr   squeezer   roundr   r   joinmapr   )r   r   r   pixel_to_class_mapping	mask_pathmask
img_height	img_widthunique_valuesyolo_format_datavalueclass_indexcontours_contouryolo_formatpointoutput_pathr   itemr   s                        rg   !convert_segment_masks_to_yolo_segr  ^  s
   D @?g???)__,,.. %e %e	///:c)nnc.BCCD$(J!J	KTiTT*TTTTUUUIdOOM!& = =A::488CC"$$N#jE#j#jT]#j#j#jkkk ".U]**2844c6GI` !  ( = =G7||q((")//"3"3'2m%, P PE'..uU1X	5I1/M/MNNN'..uU1X
5JA/N/NOOOO(//<<<= z**	-D-D-DDKk3999 ,T, , ,D88CTNN33DJJtd{++++,, , , , , , , , , , , , , , , Kc;ccccXaccdddK%e %es   AI%%I)	,I)	dota_root_pathc           	     .   t          |           } i ddddddddd	d
dddddddddddddddddddddd d!d"d#d$id9fd-}d.D ]}| d/z  |z  }| d0z  | d1z  }| d0z  |z  }|                    d2d23           t          |                                          }t	          |d4| d56          D ]W}|j        d7k    r|j        }t          j        t          |                    }	|	j
        d8d         \  }
} ||||
||           Xd8S ):u
  Convert DOTA dataset annotations to YOLO OBB (Oriented Bounding Box) format.

    The function processes images in the 'train' and 'val' folders of the DOTA dataset. For each image, it reads the
    associated label from the original labels directory and writes new labels in YOLO OBB format to a new directory.

    Args:
        dota_root_path (str): The root directory path of the DOTA dataset.

    Examples:
        >>> from ultralytics.data.converter import convert_dota_to_yolo_obb
        >>> convert_dota_to_yolo_obb("path/to/DOTA")

    Notes:
        The directory structure assumed for the DOTA dataset:

            - DOTA
                ├─ images
                │   ├─ train
                │   └─ val
                └─ labels
                    ├─ train_original
                    └─ val_original

        After execution, the function will organize the labels into:

            - DOTA
                └─ labels
                    ├─ train
                    └─ val
    planer   shipr   zstorage-tankr   zbaseball-diamondr   ztennis-courtr   zbasketball-courtr   zground-track-fieldr   harborr   bridger   zlarge-vehicler   zsmall-vehicler   
helicopterr    
roundaboutr!   zsoccer-ball-fieldr"   zswimming-poolr#   zcontainer-craner$   airportr%   helipadr&   
image_namerx   image_widthr   image_heightorig_label_dirr   ry   c           
        ||  dz  }||  dz  }|                     d          5 }|                     d          5 }|                                }	|	D ]}
|
                                                                }t	          |          dk     r<|d         }|         }d |dd         D             fdt          d          D             }d	 |D             }|                    | d
d
                    |           d           	 ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zaConvert a single image's DOTA annotation to YOLO OBB format and save it to a specified directory.r   rr   r   r   c                ,    g | ]}t          |          S re   )float)r   r   s     rg   r   zCconvert_dota_to_yolo_obb.<locals>.convert_label.<locals>.<listcomp>  s    666q%((666rf   Nc                N    g | ]!}|d z  dk    r|         z  n
|         z  "S )r   r   re   )r   r   coordsr  r  s     rg   r   zCconvert_dota_to_yolo_obb.<locals>.convert_label.<locals>.<listcomp>  sI     % % %\]q1uzzF1I++vay<?W% % %rf   c                    g | ]}|d S )z.6gre   )r   coords     rg   r   zCconvert_dota_to_yolo_obb.<locals>.convert_label.<locals>.<listcomp>  s    #R#R#RuuNN#R#R#Rrf   r   r   )r   	readlinesstripsplitr   r   r   r   )r  r  r  r   ry   orig_label_path	save_pathr   glinesr   parts
class_name	class_idxnormalized_coordsformatted_coordsr&  class_mappings    ``             @rg   convert_labelz/convert_dota_to_yolo_obb.<locals>.convert_label  s+   (j+>+>+>>*2222	!!#&& 	G!Y^^C-@-@ 	GAKKMME G G

**,,u::>>"1X
)*5	66E"1"I666% % % % % %afghaiai% % %! $S#R@Q#R#R#R 9EEsxx0@'A'AEEEFFFFG	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	Gs6   D4 CDD4D 	 D4#D 	$D44D8;D8>   valtrainr   r   	_originalTr   r   z imagesr   r   N)
r  rx   r  r   r  r   r   r   ry   r   )r   r   r   r   r   r   r   r   r   rx   r   )r  r6  phase	image_dirr   ry   image_paths
image_pathimage_name_without_extr   r   r   r5  s               @rg   convert_dota_to_yolo_obbr?    s'   > .))N 	 	A	
 	 	A 	a 	! 	! 	 	 	b 	b 	R 	  	2!" 	2#$ 	2% M*G G G G G G( " R R"X-5	'(25H5H5HH!H,u4td3339,,..//{1Mu1M1M1MNNN 	R 	RJ F**%/_"*S__--C9RaR=DAqM0!QQQQQ	RR Rrf   arr1
np.ndarrayarr2c                    | dddddf         |dddddf         z
  dz                       d          }t          j        t          j        |d          |j                  S )a  Find a pair of indexes with the shortest distance between two arrays of 2D points.

    Args:
        arr1 (np.ndarray): A NumPy array of shape (N, 2) representing N 2D points.
        arr2 (np.ndarray): A NumPy array of shape (M, 2) representing M 2D points.

    Returns:
        (tuple[int, int]): A tuple (idx1, idx2) where idx1 is the index in arr1 and idx2 is the index in arr2 of the
            pair with the shortest distance.
    Nr   r   r   )sumr   unravel_indexargminr   )r@  rB  diss      rg   	min_indexrH    sk     D!!!tD!!!QQQJ//A5
:
:2
>
>CBIc555syAAArf   r   
list[list]c                   g }d | D             } d t          t          |                     D             }t          dt          |                     D ]]}t          | |dz
           | |                   \  }}||dz
                               |           ||                             |           ^t          d          D ]}|dk    r,t	          |          D ]\  }}t          |          dk    r5|d         |d         k    r#|ddd         }| |         dddddf         | |<   t          j        | |         |d          d          | |<   t          j        | |         | |         dd         g          | |<   |dt          |          dz
  hv r|                    | |                    d|d         |d         z
  g}|                    | |         |d         |d         dz                       6t          t          |          dz
  dd          D ]a}|dt          |          dz
  hvrI||         }t          |d         |d         z
            }|                    | |         |d                    b|S )	a  Merge multiple segments into one list by connecting the coordinates with the minimum distance between each
    segment.

    This function connects these coordinates with a thin line to merge all segments into one.

    Args:
        segments (list[list]): Original segmentations in COCO's JSON file. Each element is a list of coordinates, like
            [segmentation1, segmentation2,...].

    Returns:
        (list[np.ndarray]): A list of connected segments represented as NumPy arrays.
    c                ^    g | ]*}t          j        |                              d d          +S )r   r   )r   r   r   r   s     rg   r   z'merge_multi_segment.<locals>.<listcomp>"  s0    ===q##B**===rf   c                    g | ]}g S re   re   )r   r  s     rg   r   z'merge_multi_segment.<locals>.<listcomp>#  s    111q111rf   r   r   r   Nr   r   )	r   r   rH  r   	enumerater   rollr   abs)	r   r   idx_listr   idx1idx2kidxnidxs	            rg   r   r     s    	A==H===H11E#h--00111H 1c(mm$$ ! !xA<<
dQt$$$4     1XX 1 166#H-- ? ?3s88q==SVc!f__ddd)C"*1+dddAAAg"6HQK ghqkCF7CCC nhqk8A;rr?-KLLCMMA-...HHXa[))))c!fs1vo.CHHXa[Q#a&1*)<=>>>>?  3x==1,b"55 1 1QH 1222"1+Cs1vA//DHHXa[/000	1
 Hrf   sam_b.ptim_dir
str | Pathstr | Path | None	sam_modelc           
        ddl m} ddlm} ddlm}  || t          t          t          d                    d                    }t          |j
        d         d	                   dk    rt          j        d
           dS t          j        d            ||          }t          |j
        t          |j
                  d          D ]}|d         \  }	}
|d         }t          |          dk    r)|ddddgfxx         |
z  cc<   |ddddgfxx         |	z  cc<   t          j        |d                   } || ||          dd|          }|d         j        j        |d	<   |rt%          |          nt%          |           j        dz  }|                    dd           |j
        D ]}g }t%          |d                                       d          j        }||z  }|d         }t/          |d	                   D ]\  }}t          |          dk    rt1          ||                   g|                    d          R }|                    dt          |          z                                  |z             t9          |dd          5 }|                    d  |D                        ddd           n# 1 swxY w Y   t          j        d!|            dS )"u  Convert existing object detection dataset (bounding boxes) to segmentation dataset in YOLO format.

    Generates segmentation data using SAM auto-annotator as needed.

    Args:
        im_dir (str | Path): Path to image directory to convert.
        save_dir (str | Path, optional): Path to save the generated labels, labels will be saved into `labels-segment`
            in the same directory level of `im_dir` if save_dir is None.
        sam_model (str): Segmentation model to use for intermediate segmentation data.
        device (int | str, optional): The specific device to run SAM models.

    Notes:
        The input directory structure assumed for dataset:

            - im_dir
                ├─ 001.jpg
                ├─ ...
                └─ NNN.jpg
            - labels
                ├─ 001.txt
                ├─ ...
                └─ NNN.txt
    r   )SAM)YOLODataset)	xywh2xyxyi  r   )nameschannels)r   r   z;Segmentation labels detected, no need to generate new ones!NzBDetection labels detected, generating segment labels by SAM model!zGenerating segment labelstotalr   r   r   r   r   im_fileF)r   verbosesavedevicezlabels-segmentTr   r   r   r   r   r   r   r   c              3      K   | ]	}|d z   V  
dS r   re   )r   texts     rg   r   z$yolo_bbox2segment.<locals>.<genexpr>  s&      77777777rf   z"Generated segment labels saved in )ultralyticsr\  ultralytics.datar]  ultralytics.utils.opsr^  dictr   r   r   r   r   r   r   r   r   masksxynr   parentr   r   r   rM  r   r   r   r   r   r   )rW  ry   rZ  rf  r\  r]  r^  datasetlabelr   r   boxesimsam_resultstextslb_nametxt_filer   r   r   r   r   s                         rg   yolo_bbox2segmentrx  G  sf   0  ,,,,,,////// k&t$uT{{2C2Ca'P'P'PQQQG
7>!Z())A--QRRR
KTUUUIIgnC,?,?Fabbb 	5 	5W~1hu::??aaa!QiAaaa!QiAZi())i99U+;+;UQV_efff'N04j!)UtH~~~tF||/BEU/UHNN4$N/// 8 8uY'((44V<<Ag%EleJ/00 	> 	>DAq1vv{{AKK0!))B--00DLL%#d))+3355<====(C'222 	8aLL77777777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8
K?X??@@@@@s    KK	K	c            	       	 ddt           dz  t          t           dgj                   t	          j        dz  dz  d	
           t          t                    5 	dD ]} dz  | z  }|                    d	d	           |  dz  }|	                                ryt          |d          5 }fd|D             }ddd           n# 1 swxY w Y   	fd|D             }t          t          |          t          |          d|            D ]}t          j        d| d|  d           	 ddd           n# 1 swxY w Y   t          j        d           dS )a  Create a synthetic COCO dataset with random images based on filenames from label lists.

    This function downloads COCO labels, reads image filenames from label list files, creates synthetic images for
    train2017 and val2017 subsets, and organizes them in the COCO dataset structure. It uses multithreading to generate
    images efficiently.

    Examples:
        >>> from ultralytics.data.converter import create_synthetic_coco_dataset
        >>> create_synthetic_coco_dataset()

    Notes:
        - Requires internet connection to download label files.
        - Generates random RGB images of varying sizes (480x480 to 640x640 pixels).
        - Existing test2017 directory is removed as it's not needed.
        - Reads image filenames from train2017.txt and val2017.txt files.
    
image_filer   c           
     R   |                                  st          j        dd          t          j        dd          f}t          j        d|t          j        dd          t          j        dd          t          j        dd          f                              |            dS dS )zcGenerate a synthetic image with random size and color for dataset augmentation or testing purposes.i  i  RGBr      )sizecolorN)existsrandomrandintr   newre  )rz  r~  s     rg   create_synthetic_imagez=create_synthetic_coco_dataset.<locals>.create_synthetic_image  s      "" 	N3,,fnS#.F.FGDI~a--v~a/E/Ev~VWY\G]G]^   d:	 	rf   cocoz/coco2017labels-segments.zip)dirr   test2017Tignore_errors)max_workers>   r   r   r   r   r   r   r   c                >    g | ]}|                                 z  S re   )r*  )r   r   r  s     rg   r   z1create_synthetic_coco_dataset.<locals>.<listcomp>  s&    "D"D"D$3#5"D"D"Drf   Nc                <    g | ]}                     |          S re   )submit)r   rz  r  executors     rg   r   z1create_synthetic_coco_dataset.<locals>.<listcomp>  s)    mmmS]8??+A:NNmmmrf   zGenerating images for ra  zLabels file z- does not exist. Skipping image creation for .z,Synthetic COCO dataset created successfully.)rz  r   )r
   r   r	   ro  shutilrmtreer   r   r   r  r   r   r   r   r   r   r   )
subset
subset_dirlabel_list_filer   image_filesfuturesr  r  r  r  s
          @@@rg   create_synthetic_coco_datasetr    sm   $    
C999:
KKKK M#.:-TBBBB		4	4	4 w. 	w 	wFx&0JTD999 "vOOO3O%%'' 	w/G<<< E"D"D"D"D!"D"D"DKE E E E E E E E E E E E E E E nmmmmalmmml7333w<<NogmNoNoppp  A uouulruuuvvvv	ww w w w w w w w w w w w w w w$ K>?????s8   "AE3CECECA!EEEr   path
n_channelsr   zipc                    ddl m} ddlm} t	                                                       r fd|ddhz
  D             }|D ]Z}	 t          ||           |r|                                 *# t          $ r$}t          j
        d| d|            Y d	}~Sd	}~ww xY w|rt                      d	S d	S                      d
          }	t          j        t          j        t!                               t          j                  }
t%          j        g d          }t%          j        dd|          } ||j        |
ddd          } ||          }t          j        t!          |	          t%          j        |dd                              t$          j                                      ddd                     t          j
        d|	            d	S )aK  Convert RGB images to multispectral images by interpolating across wavelength bands.

    This function takes RGB images and interpolates them to create multispectral images with a specified number of
    channels. It can process either a single image or a directory of images.

    Args:
        path (str | Path): Path to an image file or directory containing images to convert.
        n_channels (int): Number of spectral channels to generate in the output image.
        replace (bool): Whether to replace the original image file with the converted one.
        zip (bool): Whether to zip the converted images into a zip file.

    Examples:
        Convert a single image
        >>> convert_to_multispectral("path/to/image.jpg", n_channels=10)

        Convert a dataset
        >>> convert_to_multispectral("coco8", n_channels=10)
    r   )interp1d)IMG_FORMATSc                J    g | ]}                     d |           D ]}| S )z*.)rglob)r   extr   r  s      rg   r   z,convert_to_multispectral.<locals>.<listcomp>  s<    aaa#$**U_Z]U_U_J`J`aaQAaaaarf   tiftiffzError converting z: Nz.tiff)i  i  i  i  i  linearFextrapolate)kindbounds_error
fill_valuer}  r   r   z
Converted )scipy.interpolater  ultralytics.data.utilsr  r   is_dirconvert_to_multispectralunlink	Exceptionr   r   r   r   r   cvtColorr   rx   COLOR_BGR2RGBr   r   linspaceTimwritemulticlipr   r   	transpose)r  r  r   r  r  r  im_filesim_pather  r   rgb_wavelengthstarget_wavelengthsr   multispectrals   `              rg   r  r    s    & +*****222222::D{{}} 0aaaa+"?aaa 	@ 	@G@(*=== %NN$$$ @ @ @>>>1>>????????@  	 $	  	  &&w//l3:c$ii00#2CDD (???33[c:>>H_&([hiii,--[))27=!S+I+I+P+PQSQY+Z+Z+d+defhikl+m+mnnn.../////s   	&A00
B:BBimage_recordsr   c                j   g }g }| D ]}|                     di                                dg           D ]a}t          |          dz
  }|dk    r2|                    |           |                    |dd                    t          |          dk    r nbt          |          dk    r n|r t          t          |                    dk    rt	          d          |d         }|d	z  dk    r t          d
 |D                       r|d	z  d	gS |dz  dk    r|d	z  dk    r|dz  dgS t	          d          )a  Infer kpt_shape [num_keypoints, dims] from NDJSON pose annotations.

    Scans up to 50 pose annotations across image records. Annotation format is [classId, cx, cy, w, h, kp1_x, kp1_y,
    kp1_vis, ...] so keypoint values start at index 5.

    Tries dims=3 first (x, y, visibility) with visibility validation ({0, 1, 2}), then falls back to dims=2 (x, y only)
    when values are unambiguously not divisible by 3.
    r   poser   r   NrG   r   zZPose dataset missing required 'kpt_shape'. See https://docs.ultralytics.com/datasets/pose/r   c              3  :   K   | ]}|d dd         D ]}|dv V  	dS )r   Nr   )r   r   r   re   )r   r   vs      rg   r   z*_infer_ndjson_kpt_shape.<locals>.<genexpr>  s=      KKQ1QTT7KKa!y.KKKKKKKrf   r   )r   r   r   set
ValueErrorall)r  kpt_lengthssamplesrecordr   kpt_lenns          rg   _infer_ndjson_kpt_shaper    sw    KG 	 	::mR0044VR@@ 	 	C#hhlG{{""7+++s122w''';2%% &{r!!E "  w#c+..//144uvvvAA 	1uzzcKK'KKKKKzQ{ 	1uzza!eqjjQ{
q
r
rrrf   ndjson_pathr  r   c                `   !"#$K   ddl m}  |d           ddlt          t	          |                     } t          |pt
                    }t          |           5 }d |D             }ddd           n# 1 swxY w Y   |d         |dd         }}t          j                    }|D ]Y}|	                    t          j        d |                                D             d	                                                     Z|                                dd
         }	|| j        z  dz  }
|
                                r?	 t#          j        |
                              d          |	k    r|
S n# t(          $ r Y nw xY wd |D             }|                    d          dk    d |                    di                                           D             t+                     |                    dd          }sd|vrt-          dt/          |                     d|vrd|vrd |D             }t+          |          dk     r t-          dt+          |           d          t1          j        d                              |           t7          dt+          |          dz            }|d|         D ]}d|d<   |                    d           t;          j        dt+          |           dt+          |          |z
   d| d            |d!k    rd"|vrt?          |          |d"<                                    r1|
!                    d#           stE          j#        d$z  d%           $                    dd&           d}stK          |          }|d'<   |&                    dd           |&                    d(d           t/          |          D ]D}d)z  |z  $                    dd&           d$z  |z  $                    dd&           d*| ||<   Efd+!tO          j(        tS          d,t+          |                              "*                                4 d{V #tW          t+          |          d-| j,         d. d/t+          |           d01            !"#fd2$tO          j-        $fd3|D               d{V } .                                 ddd          d{V  n# 1 d{V swxY w Y   t_          d4 |D                       }|dk    rta          d5|  d6          |t+          |          k     r*t;          j        d7| d8t+          |           d9|             rtc                      }|D ]}|d         |d:         }}rx|                    d;i           }|                    d<g           }|r|d         nd}|                    |z                      |te          |                    z  |z             |                    d)z  |z  |z             rnd)z  }|3                    d=          D ].}|                                r||vr|!                                 /rS |	|d<   t#          j4        |
|           |
S )>a  Convert NDJSON dataset format to Ultralytics YOLO dataset structure.

    This function converts datasets stored in NDJSON (Newline Delimited JSON) format to the standard YOLO format. For
    detection/segmentation/pose/obb tasks, it creates separate directories for images and labels. For classification
    tasks, it creates the ImageNet-style {split}/{class_name}/ folder structure. It supports parallel processing for
    efficient conversion of large datasets and can download images from URLs.

    The NDJSON format consists of:
    - First line: Dataset metadata with class names, task type, and configuration
    - Subsequent lines: Individual image records with annotations and optional URLs

    Args:
        ndjson_path (str | Path): Path to the input NDJSON file containing dataset information.
        output_path (str | Path | None, optional): Directory where the converted YOLO dataset will be saved. If None,
            uses the DATASETS_DIR directory. Defaults to None.

    Returns:
        (Path): Path to the generated data.yaml file (detection) or dataset directory (classification).

    Examples:
        Convert a local NDJSON file:
        >>> yaml_path = await convert_ndjson_to_yolo("dataset.ndjson")
        >>> print(f"Dataset converted to: {yaml_path}")

        Convert with custom output directory:
        >>> yaml_path = await convert_ndjson_to_yolo("dataset.ndjson", output_path="./converted_datasets")

        Use with YOLO training
        >>> from ultralytics import YOLO
        >>> model = YOLO("yolo26n.pt")
        >>> model.train(data="https://github.com/ultralytics/assets/releases/download/v0.0.0/coco8-ndjson.ndjson")
    r   )check_requirementsaiohttpNc                    g | ]<}|                                 t          j        |                                           =S re   )r*  r   loadsr   s     rg   r   z*convert_ndjson_to_yolo.<locals>.<listcomp>@  s7    HHHd4::<<HDJJLL))HHHrf   r   c                &    i | ]\  }}|d k    ||S )urlre   r   rS  r  s      rg   r   z*convert_ndjson_to_yolo.<locals>.<dictcomp>F  s#    GGGtq!AJJaJJJrf   T)	sort_keysr%   z	data.yamlhashc                    h | ]
}|d          S )r+  re   )r   r  s     rg   	<setcomp>z)convert_ndjson_to_yolo.<locals>.<setcomp>R  s    :::&fWo:::rf   taskclassifyc                4    i | ]\  }}t          |          |S re   )r   r  s      rg   r   z*convert_ndjson_to_yolo.<locals>.<dictcomp>V  s$    WWWA3q661WWWrf   class_namesdetectr8  z6Dataset missing required 'train' split. Found splits: r7  testc                D    g | ]}|                     d           dk    |S )r+  r8  )r   r   r"  s     rg   r   z*convert_ndjson_to_yolo.<locals>.<listcomp>_  s,    SSS1w79R9RQ9R9R9Rrf   r   zDataset has only zR image(s) and no 'val' split. Need at least 2 images to auto-split into train/val.r   r+  u?   WARNING ⚠️ No 'val' split found in dataset. Auto-splitting z images into z train, zS val. For best results, manually assign validation images in Platform dataset page.r  	kpt_shape)
missing_okr   r  r   r_  typer   zimages/c           	     <  K   |4 d{V  |d         |d         }}|                     di           }rL |j         dg           }|r|d         nd}                     |t          |                    }|z  |z  |z  }	ndz  |z  |z  }	dz  |z  t          |          j         d	z  }
g } |j                    D ]}d
 ||         D             } |
                    |rd                    |          dz   nd           |	                                srkdD ]h}||k    r	r|z  |z  |z  n
dz  |z  |z  }|                                r3|	j        	                    dd           |
                    |	            ni|	                                s_|                     d          x}rG|	j        	                    dd           t          d          D ]}	 |                      |                    d                    4 d{V }|                                 |	                    |                                 d{V            ddd          d{V  n# 1 d{V swxY w Y    ddd          d{V  dS # t           $ ra}|dk     rt#          j        d|z             d{V  n2t'          j        d| d|            Y d}~ ddd          d{V  dS Y d}~d}~ww xY w	 ddd          d{V  dS # 1 d{V swxY w Y   dS )z/Process single image record with async session.Nr+  r   r   classificationr   r   r   r   c                ^    g | ]*}d                      t          t          |                    +S )r   )r   r  rx   )r   r  s     rg   r   zBconvert_ndjson_to_yolo.<locals>.process_record.<locals>.<listcomp>  s,    %\%\%\4chhs3~~&>&>%\%\%\rf   r   r   )r8  r7  r  Tr   r  r   r3   )rb  )timeoutr   zFailed to download z after 3 attempts: F)r   rx   r   r   keys
write_textr   r  ro  r   renamer   ClientTimeoutraise_for_statuswrite_bytesreadr  asynciosleepr   r   )session	semaphorer  r+  original_namer   	class_idsclass_idr1  r=  
label_pathlines_to_writekeyr   	candidatehttp_urlattemptresponser  _reuser  r  dataset_diris_classifications                      rg   process_recordz.convert_ndjson_to_yolo.<locals>.process_record  s|      2	 2	 2	 2	 2	 2	 2	 2	#)'?F6N=E **]B77K  b+KO,<bAA	+4;9Q<<!(__Xs8}}EE
(50:=M

 )83e;mK
(83e;mATATAY>_>_>__
!#+;+--  C%\%\;WZK[%\%\%\N%%.&`dii&?&?$&F&F^`aaa $$&& - "5 " "::$  1N[1_z9MII"-"81"<}"L "
 %++-- "&-33D43PPP%,,Z888!E" "((** -FJJu<M<M0M -%++D4+HHH#(88 - -
-'.{{8WEZEZacEZEdEd{'e'e N N N N N N Niq ( 9 9 ; ; ; * 6 6X]]__7L7L7L7L7L7L M M MN N N N N N N N N N N N N N N N N N N N N N N N N N N $(W2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	X  ) - - -&{{&-mAwJ&?&? ? ? ? ? ? ? ? ? &/eX/e/ebc/e/e f f f',uuuuc2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	\ !@ ? ? ? ?- e2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	sg   GL1J	AI$J	$
I..J	1I.2J	5L	
K4>K/L)L/K44L
LL   zConverting u    → z (z images)ra  c                \   K    |            d {V }                     d           |S )Nr   )update)r  resultpbarr  r  r  s     rg   tracked_processz/convert_ndjson_to_yolo.<locals>.tracked_process  s@      )>'9fEEEEEEEEFKKNNNMrf   c                &    g | ]} |          S re   re   )r   r  r  s     rg   r   z*convert_ndjson_to_yolo.<locals>.<listcomp>  s#    (](](]V)@)@(](](]rf   c              3     K   | ]}|d V  	dS )r   Nre   r  s     rg   r   z)convert_ndjson_to_yolo.<locals>.<genexpr>  s'      00aa0000000rf   z#Failed to download any images from z$. Check network connection and URLs.zDownloaded /z images from r   r   r  *)5ultralytics.utils.checksr  r  r   r   r
   r   hashlibsha256r   r   dumpsr   encode	hexdigestr   is_filer   r   r   r  r   r  r   r  Randomshufflemaxaddr   r   r  r  r  r  r  r   rl  popr  	SemaphoreminClientSessionr   r   gathercloserD  RuntimeErrorr  rx   r  re  )%r  r  r  r   r/  dataset_recordr  _hr"  _hash	yaml_pathsplitsr  train_records	val_count	data_yamlr+  resultssuccess_countexpected_pathsr   r   r   cidscidimg_rootr   r  r  r  r  r  r  r  r  r  r  s%                              @@@@@@@@@@rg   convert_ndjson_to_yolor(    s     B <;;;;;y!!!NNNz+..//K{2l33K	k		 IaHHaHHHI I I I I I I I I I I I I I I$)!HeABBiMN 
		B c c
		$*GGqwwyyGGGSWXXX__aabbbbLLNN3B3E  00Kk)I 	y##''//588   9 	 	 	D	::M:::F '**622j@WW););M2)N)N)T)T)V)VWWWK fh//D &  fV\]cVdVdffggg6#7#7SSSSSM=!!A%% LM(:(: L L L   M!$$]333As=11R788I":I:. # #"'

JJuNa"%m"4"4a aCF}CUCUXaCaa akta a a  
 v~~+^;;&=m&L&L{# !!F FD)))  	FM+0EEEEdT222I 	1((	(	'mT***fd###F^^ 	1 	1E8#e+224$2OOO8#e+224$2OOO000Ie4 4 4 4 4 4 4 4 4n !#c3}+=+=">">??I$$&&       'm$$a{/aakaaSEWEWaaa
 
 

	 	 	 	 	 	 	 	
  (](](](]}(](](]^^^^^^^

                           00700000Mrrrrssss=))))c]ccS5G5GccVaccddd   	F 	FAj!F)tA  FeeM2..ww/44!%,d1gg1"";?[__S#c((5S5S#SVZ#Z[[[["";#9A#=#DEEEE"3Q;;+:P$$ 	 	Ayy{{ q66


  "	&	)Y'''s7   #A<<B B ,E6 6
FF<A7T
TT)r   r   )ru   rv   FFTF)rw   rx   ry   rx   rz   r{   r|   r{   r}   r{   r~   r{   )r   rx   r   rx   r   r   )r  rx   )r@  rA  rB  rA  )r   rI  )NrV  N)rW  rX  ry   rY  rZ  rx   )r   FF)r  rX  r  r   r   r{   r  r{   )r  r   r   r   )N)r  rX  r  rY  r   r   )-
__future__r   r  r	  r   r  r  collectionsr   concurrent.futuresr   r   pathlibr   r   numpyr   PILr   ultralytics.utilsr	   r
   r   r   r   r   r  r   ultralytics.utils.downloadsr   r   ultralytics.utils.filesr   rh   rt   r   r  r?  rH  r   rx  r  r  r  r(  re   rf   rg   <module>r2     su   # " " " " "      # # # # # # ? ? ? ? ? ? ? ?       



           W W W W W W W W W W W W W W W W / / / / / / ? ? ? ? ? ? ? ? 2 2 2 2 2 2c c c cLe e e eR -%uu uu uu uu uupHe He He HeVYR YR YR YRxB B B B0 0 0 0f=A =A =A =A =A@4@ 4@ 4@n00 00 00 00 00f#s #s #s #sLT T T T T T Trf   