o
    Ni)                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZ	ddl
m  mZ ddlmZ dZdZdZd	Zd
ZdZdZg dZdZdZedg dZG dd dejjZdd Z dd Z!dd Z"dS )Kitti dataset.    )absolute_import)division)print_functionNa  @inproceedings{Geiger2012CVPR,
  author = {Andreas Geiger and Philip Lenz and Raquel Urtasun},
  title = {Are we ready for Autonomous Driving? The KITTI Vision Benchmark Suite},
  booktitle = {Conference on Computer Vision and Pattern Recognition (CVPR)},
  year = {2012}
}
a  Kitti contains a suite of vision tasks built using an autonomous driving
platform. The full benchmark contains many tasks such as stereo, optical flow,
visual odometry, etc. This dataset contains the object detection dataset,
including the monocular images and bounding boxes. The dataset contains 7481
training images annotated with 3D bounding boxes. A full description of the
annotations can be found in the readme of the object development kit readme on
the Kitti homepage.
z%http://www.cvlibs.net/datasets/kitti/z/https://s3.eu-central-1.amazonaws.com/avg-kittizdata_object_image_2.zipzdata_object_label_2.zipzdevkit_object.zip)CarVanTruck
PedestrianPerson_sittingCyclistTramMisc
   RawBoundingBoxtopbottomleftrightc                   @   sD   e Zd ZdZejddZejdgZdd Z	dd Z
d	d
 ZdS )Kittir   z3.2.0zDevkit updated.z3.1.0c                 C   s   t jjtdt jjdtjdt jjddt jjdtjdt j t jjdtjdt jjdtjdt jjdtjdd}t jj	| t
t jt j t j t j|dttd	S )
N)names )shapedtype   )num_classes)   )type	truncatedoccludedalphabbox
dimensionslocation
rotation_yimagezimage/file_nameobjects)builderdescriptionfeatureshomepagecitation)tfdsr*   
ClassLabel_OBJECT_LABELSTensortffloat32BBoxFeaturecoreDatasetInfo_DESCRIPTIONFeaturesDictImageTextSequence_HOMEPAGE_URL	_CITATION)selfannotationsr   r   ^/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/object_detection/kitti.py_infoQ   s(   

zKitti._infoc              	   C   s   t d t t d t t d t d}||}t||d \}}}tjj	tj
j||d ||d d|ddtjj	tj
j||d ||d d|ddtjj	tj
j||d ||d d|ddgS )	N/)imagesr>   devkitrC   rB   r>   training)rB   r>   subdir	image_ids)name
gen_kwargs)	_DATA_URL_IMAGES_FNAME_LABELS_FNAME_DEVKIT_FNAMEdownload_build_splitsiter_archiver-   r4   SplitGeneratorSplitTRAIN
VALIDATIONTEST)r=   
dl_manager	filenamesfilestrain_imagesvalidation_imagestest_imagesr   r   r?   _split_generatorsi   sB   




zKitti._split_generatorsc                 c   sR   t jjj}t }|D ])\}}tj|\}	}
|
dkrq|	tjj	d |kr(qt
||t|	dd < q|D ]o\}}tj|\}	}
|
dkrHq7|	tjj	d |krTq7t|	dd }||vraq7|| }|tj| tjd|j}|||j}|j\}}}|D ]}t|d |||d< |d= qtj|\}}|||d	}||fV  q7dS )
a  Yields images and annotations.

    Args:
      images: object that iterates over the archive of images.
      annotations: object that iterates over the archive of annotations.
      subdir: subdirectory from which to extract images and annotations, e.g.
        training or testing.
      image_ids: file ids for images in this split.

    Yields:
      A tuple containing the example's key, and the example.
    z.txtr   iNz.png)r   bbox_rawr!   r%   )r-   r4   lazy_importscv2dictospathsplitextsplitsep_parse_kitti_annotationsintimdecodenp
fromstringreaduint8IMREAD_COLORcvtColorCOLOR_BGR2RGBr   _build_bounding_box)r=   rB   r>   rE   rF   r^   all_annotationsfpathfobjprefixextimage_idimgheightwidth_objfnamerecordr   r   r?   _generate_examples   s@   
zKitti._generate_examplesN)__name__
__module____qualname____doc__r-   r4   VersionVERSIONSUPPORTED_VERSIONSr@   r[   r}   r   r   r   r?   r   I   s    
%r   c                 C   s4   t jj|| j | || j | | j| | j| dS )zBuilds and returns TFDS bounding box.

  Args:
    bbox: RawBoundingBox, bounding box in Kitti coordinates (origin top left).
    height: Image height in pixels.
    width: Image width in pixels.

  Returns:
    A TFDS BBox (origin bottom left).
  )yminymaxxminxmax)r-   r*   BBoxr   r   r   r   )r!   rw   rx   r   r   r?   ro      s   ro   c                 C   s   g }| D ]X}t tj| gddd \}}}}}}}	}
}}}}}}}|dkr(q||t|t|t|tt|t|
t|t|	ddd |||fD dd |||fD t|d	 q|S )
zLoads and parses the Kitti object annotations.

  Args:
    annotations_csv: csv file containing annotations for a single image.

  Returns:
    A list of labelled bounding boxes. Each bounding box is stored as a
    dictionary of features.
   )	delimiterr   DontCarer   c                 S      g | ]}t |qS r   float.0vr   r   r?   
<listcomp>       z,_parse_kitti_annotations.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r?   r      r   )r   r   r   r    r\   r"   r#   r$   )listcsvreaderdecodeappendr   rf   r   )annotations_csvr>   lineobj_typer   r   r    r   r   r   r   rw   rx   lengthxyzr$   r   r   r?   re      s0   


re   c                 C   s  d}d}| D ]3\}}|t jddkr#dd | ddD }q|t jddkr9|  }d	d |D }q|s>J |sBJ tt	}t
|D ]\}}|| }|d
d }	||	 | qKtj }
tjjdd tdtt| d }tdtt| d }ttjjtt	| |dd}ttjjtt	t| t| |dd}g }g }g }| D ]\}}||v r|| q||v r|| q|| qtj|
 |||fS )av  Splits the train data into train/val/test by video.

  Ensures that images from the same video do not traverse the splits.

  Args:
    devkit: object that iterates over the devkit archive.

  Returns:
    train_images: File ids for the training set images.
    validation_images: File ids for the validation set images.
    test_images: File ids for the test set images.
  Nmappingztrain_rand.txtc                 S   s   g | ]
}t | d  qS )   )rf   stripr   r   r   r   r?   r     s    z!_build_splits.<locals>.<listcomp>utf-8,ztrain_mapping.txtc                 S   s   g | ]}| d qS )r   )r   r   r   r   r?   r     s    r   r   {   )seedd   F)replace)r`   ra   joinrj   r   rc   
splitlinescollectionsdefaultdictr   	enumerater   rh   random	get_stater   max_TEST_SPLIT_PERCENT_VIDEOSlen _VALIDATION_SPLIT_PERCENT_VIDEOSsetchoicesortedkeysitemsextend	set_state)rC   mapping_line_idsmapping_linesrq   rr   video_to_imageru   mapping_lineidr   video_idnumpy_original_statenum_test_videosnum_validation_videostest_videosvalidation_videosrZ   rY   rX   kr   r   r   r?   rN      sd   


rN   )#r   
__future__r   r   r   r   r   r`   numpyrh   tensorflow.compat.v2compatv2r1   tensorflow_datasets.public_api
public_apir-   r<   r6   r;   rI   rJ   rK   rL   r/   r   r   
namedtupler   r4   GeneratorBasedBuilderr   ro   re   rN   r   r   r   r?   <module>   s6   	u#