a
    h                     @   sl   d dl Z d dlZd dlmZ d dl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Optional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ee	e
f eee ee ee ed
 fddZeeeef dddZeedddZedddZee	dddZedddZd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.zipZimage_2Zlabel_2TNF)roottrain	transformtarget_transform
transformsdownloadc           
   	      s   t  j||||d g | _g | _|| _| jr0dnd| _|rB|   |  sRtdt	j
| j| j| j}| jrt	j
| j| j| j}t	|D ]F}	| jt	j
||	 | jr| jt	j
||	dd  d qd S )N)r   r   r   ZtrainingZ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   Z	image_dirZ
labels_dirZimg_file	__class__ H/var/www/auris/lib/python3.9/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#   r(   imagetargetr&   r&   r'   __getitem__V   s
    zKitti.__getitem__c                 C   s   g }t | j| }tj|dd}|D ]z}||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    n1 s0    Y  |S )N )	delimiterr   r         c                 S   s   g | ]}t |qS r&   float.0xr&   r&   r'   
<listcomp>z       z'Kitti._parse_target.<locals>.<listcomp>      c                 S   s   g | ]}t |qS r&   r3   r5   r&   r&   r'   r8   {   r9      c                 S   s   g | ]}t |qS r&   r3   r5   r&   r&   r'   r8   |   r9      )typeZ	truncatedZoccludedalphaZbboxZ
dimensionslocationZ
rotation_y)r*   r   csvreaderr!   r4   int)r#   r(   r-   inpcontentliner&   r&   r'   r+   o   s     



$zKitti._parse_target)r)   c                 C   s
   t | jS N)lenr   r#   r&   r&   r'   __len__   s    zKitti.__len__c                 C   s   t j| j| jjdS )Nraw)r   r   r   r   r%   __name__rI   r&   r&   r'   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 rG   )r   r   isdirr   r   r   )r6   fnamerI   r&   r'   	<genexpr>   r9   z&Kitti._check_exists.<locals>.<genexpr>)r   r   r!   r   all)r#   foldersr&   rI   r'   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)urlZdownload_rootfilename)r   r   makedirsr   	resourcesr	   data_url)r#   rN   r&   r&   r'   r      s    
zKitti.download)TNNNF)rL   
__module____qualname____doc__rW   rV   r   r   r   strr   boolr   r   r   rC   tupler   r.   listr+   rJ   propertyr   r   r   __classcell__r&   r&   r$   r'   r      s6         
!r   )rA   r   pathlibr   typingr   r   r   r   ZPILr   utilsr	   Zvisionr
   r   r&   r&   r&   r'   <module>   s   