a
    PSic                     @   sd   d dl Z d dlZd dl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)AnyCallableListOptionalTuple)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
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 (string): 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_2Zlabel_2TNF)roottrain	transformtarget_transform
transformsdownloadc           
   	      s   t  j||||d g | _g | _|| _|| _| jr6dnd| _|rH|   |  sXt	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   trainingtestingz<Dataset not found. You may use download=True to download it..r   z.txt)super__init__imagestargetsr   r   	_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_dirZ
labels_dirimg_file	__class__ V/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/datasets/kitti.pyr   4   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+   r8   r:   r+   r+   r,   r=   {   r>      c                 S   s   g | ]}t |qS r+   r8   r:   r+   r+   r,   r=   |   r>      )typeZ	truncatedZoccludedalphabbox
dimensionslocationZ
rotation_y)r/   r   csvreaderr$   r9   int)r&   r-   r2   inpcontentliner+   r+   r,   r0   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__rP   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 rN   )r   r   isdirr   r    r   )r;   fnamerP   r+   r,   	<genexpr>   r>   z&Kitti._check_exists.<locals>.<genexpr>)r!   r   r$   r"   all)r&   Zfoldersr+   rP   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)urldownload_rootfilename)r   r   makedirsr    	resourcesr	   data_url)r&   rU   r+   r+   r,   r      s    
zKitti.download)TNNNF)rS   
__module____qualname____doc__r^   r]   r!   r"   strboolr   r   r   rJ   r   r   r3   r   r0   rQ   propertyr    r   r   __classcell__r+   r+   r)   r,   r      s6         "r   )rH   r   typingr   r   r   r   r   PILr   utilsr	   visionr
   r   r+   r+   r+   r,   <module>   s   