o
    Vh                     @   st   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	 d	eZdS )
    N)Path)AnyCallableListOptionalTupleUnion)Image   )download_and_extract_archive)VisionDatasetc                       s   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dZdedeeef fddZdedefddZdefddZede	fddZdefddZd!ddZ  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_2TNFroottrain	transformtarget_transform
transformsdownloadc           
   	      s   t  j||||d g | _g | _|| _| jrdnd| _|r!|   |  s)tdt	j
| j| j| j}| jrBt	j
| j| j| j}t	|D ]#}	| jt	j
||	 | jrj| jt	j
||	dd  d qGd S )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__ N/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/datasets/kitti.pyr   5   s.   	&zKitti.__init__indexreturnc                 C   sB   t | j| }| jr| |nd}| jr| ||\}}||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)   r1   imagetargetr/   r/   r0   __getitem__V   s
   zKitti.__getitem__c                 C   s   g }t | j| P}tj|dd}|D ]=}||d t|d t|d t|d dd |d	d
 D dd |d
d D dd |dd D t|d d qW d    |S 1 s\w   Y  |S )N )	delimiterr   r
         c                 S      g | ]}t |qS r/   float.0xr/   r/   r0   
<listcomp>z       z'Kitti._parse_target.<locals>.<listcomp>      c                 S   r<   r/   r=   r?   r/   r/   r0   rB   {   rC      c                 S   r<   r/   r=   r?   r/   r/   r0   rB   |   rC      )type	truncatedoccludedalphabbox
dimensionslocation
rotation_y)r3   r   csvreaderr'   r>   int)r)   r1   r6   inpcontentliner/   r/   r0   r4   o   s(   




zKitti._parse_targetc                 C   s
   t | jS N)lenr   r)   r/   r/   r0   __len__   s   
zKitti.__len__c                 C   s   t j| j| jjdS )Nraw)r    r!   r"   r   r.   __name__rX   r/   r/   r0   r#      s   zKitti._raw_folderc                    s0    j g} jr| j t fdd|D S )z#Check if the data directory exists.c                 3   s,    | ]}t jt j j j|V  qd S rV   )r    r!   isdirr"   r#   r   )r@   fnamerX   r/   r0   	<genexpr>   s   * z&Kitti._check_exists.<locals>.<genexpr>)r$   r   r'   r%   all)r)   foldersr/   rX   r0   r      s   zKitti._check_existsc                 C   sF   |   rdS tj| jdd | jD ]}t| j | | j|d qdS )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]   r/   r/   r0   r      s   
zKitti.download)TNNNF)r2   N)r[   
__module____qualname____doc__rg   rf   r$   r%   r   strr   boolr   r   r   rR   r   r   r7   r   r4   rY   propertyr#   r   r   __classcell__r/   r/   r-   r0   r      sB     
!r   )rP   r    pathlibr   typingr   r   r   r   r   r   PILr	   utilsr   visionr   r   r/   r/   r/   r0   <module>   s     