
    kh                     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 ddlmZ  G d d	e      Zy)
    N)Path)AnyCallableListOptionalTupleUnion)Image   )download_and_extract_archive)VisionDatasetc                        e Zd ZdZdZddgZdZdZ	 	 	 	 	 ddee	e
f   ded	ee   d
ee   dee   def fdZdedeeef   fdZdedefdZdefdZede	fd       ZdefdZddZ xZS )Kittiu  `KITTI <http://www.cvlibs.net/datasets/kitti/eval_object.php?obj_benchmark>`_ Dataset.

    It corresponds to the "left color images of object" dataset, for object detection.

    Args:
        root (str or ``pathlib.Path``): Root directory where images are downloaded to.
            Expects the following folder structure if download=False:

            .. code::

                <root>
                    └── Kitti
                        └─ raw
                            ├── training
                            |   ├── image_2
                            |   └── label_2
                            └── testing
                                └── image_2
        train (bool, optional): Use ``train`` split if true, else ``test`` split.
            Defaults to ``train``.
        transform (callable, optional): A function/transform that takes in a PIL image
            and returns a transformed version. E.g, ``transforms.PILToTensor``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        transforms (callable, optional): A function/transform that takes input sample
            and its target as entry and returns a transformed version.
        download (bool, optional): If true, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.

    z0https://s3.eu-central-1.amazonaws.com/avg-kitti/zdata_object_image_2.zipzdata_object_label_2.zipimage_2label_2roottrain	transformtarget_transform
transformsdownloadc           	      J   t         
|   ||||       g | _        g | _        || _        | j                  rdnd| _        |r| j                          | j                         st        d      t        j                  j                  | j                  | j
                  | j                        }| j                  r?t        j                  j                  | j                  | j
                  | j                        }t        j                  |      D ]  }	| j                  j!                  t        j                  j                  ||	             | j                  sI| j                  j!                  t        j                  j                  |	j#                  d      d    d              y )N)r   r   r   trainingtestingz<Dataset not found. You may use download=True to download it..r   z.txt)super__init__imagestargetsr   	_locationr   _check_existsRuntimeErrorospathjoin_raw_folderimage_dir_namelabels_dir_namelistdirappendsplit)selfr   r   r   r   r   r   	image_dir
labels_dirimg_file	__class__s             V/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/kitti.pyr   zKitti.__init__5   s;    	-!	 	 	
 
'+zzyMMO!!#]^^GGLL!1!14>>4CVCVW	::d&6&6H\H\]J

9- 	_HKKrww||Ix@Azz##BGGLLs@STU@V?WW[=\$]^	_    indexreturnc                     t        j                  | j                  |         }| j                  r| j	                  |      nd}| j
                  r| j                  ||      \  }}||fS )a  Get item at a given index.

        Args:
            index (int): Index
        Returns:
            tuple: (image, target), where
            target is a list of dictionaries with the following keys:

            - type: str
            - truncated: float
            - occluded: int
            - alpha: float
            - bbox: float[4]
            - dimensions: float[3]
            - locations: float[3]
            - rotation_y: float

        N)r
   openr   r   _parse_targetr   )r,   r3   imagetargets       r1   __getitem__zKitti.__getitem__V   sX    & 

4;;u-..2jj##E*d?? OOE6:ME6f}r2   c                    g }t        | j                  |         5 }t        j                  |d      }|D ]  }|j	                  |d   t        |d         t        |d         t        |d         |dd D cg c]  }t        |       c}|dd	 D cg c]  }t        |       c}|d	d
 D cg c]  }t        |       c}t        |d
         d        	 d d d        |S c c}w c c}w c c}w # 1 sw Y   |S xY w)N )	delimiterr   r                     )type	truncatedoccludedalphabbox
dimensionslocation
rotation_y)r6   r   csvreaderr*   floatint)r,   r3   r9   inpcontentlinexs          r1   r7   zKitti._parse_targeto   s    $,,u%& 	#jj4G  $Q%*47^$'QL!&tAw37!9 =aq =9=a&DAuQx&D7;Br{$C!U1X$C&+DHo		  !>&D$C	 s6   AC/5C 	C/C%"	C/+C*=C/ C//C9c                 ,    t        | j                        S N)lenr   r,   s    r1   __len__zKitti.__len__   s    4;;r2   c                     t         j                  j                  | j                  | j                  j
                  d      S )Nraw)r#   r$   r%   r   r0   __name__rW   s    r1   r&   zKitti._raw_folder   s'    ww||DIIt~~'>'>FFr2   c                       j                   g} j                  r|j                   j                         t	         fd|D              S )z#Check if the data directory exists.c              3      K   | ]V  }t         j                  j                  t         j                  j                  j                  j
                  |             X y wrU   )r#   r$   isdirr%   r&   r    ).0fnamer,   s     r1   	<genexpr>z&Kitti._check_exists.<locals>.<genexpr>   s;     m\a277==d.>.>PU!VWms   AA)r'   r   r*   r(   all)r,   folderss   ` r1   r!   zKitti._check_exists   s<    &&'::NN4//0melmmmr2   c                     | j                         ryt        j                  | j                  d       | j                  D ](  }t        | j                   | | j                  |       * y)z4Download the KITTI data if it doesn't exist already.NT)exist_ok)urldownload_rootfilename)r!   r#   makedirsr&   	resourcesr   data_url)r,   r`   s     r1   r   zKitti.download   s`     
D$$t4 ^^ 	E(}}oeW-"..	r2   )TNNNF)r4   N)r[   
__module____qualname____doc__rk   rj   r'   r(   r	   strr   boolr   r   r   rO   r   r   r:   r   r7   rX   propertyr&   r!   r   __classcell__)r0   s   @r1   r   r      s   @ BH!!I NO
 (,/3)-_CI_ _ H%	_
 #8,_ X&_ _B sCx 23 4 &    GS G Gnt nr2   r   )rL   r#   pathlibr   typingr   r   r   r   r   r	   PILr
   utilsr   visionr   r    r2   r1   <module>ry      s+    
 	  > >  / !RM Rr2   