o
    Ni$A                     @   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m	Z	 ddl
m  mZ ddlmZ dZdZed	g d
ZG dd deZG dd dejjZG dd dejjZG dd deZG dd deZG dd deZejeej eej!eiZ"dS )zMS Coco Dataset.    )absolute_import)division)print_functionN)logginga0  @article{DBLP:journals/corr/LinMBHPRDZ14,
  author    = {Tsung{-}Yi Lin and
               Michael Maire and
               Serge J. Belongie and
               Lubomir D. Bourdev and
               Ross B. Girshick and
               James Hays and
               Pietro Perona and
               Deva Ramanan and
               Piotr Doll{'{a}}r and
               C. Lawrence Zitnick},
  title     = {Microsoft {COCO:} Common Objects in Context},
  journal   = {CoRR},
  volume    = {abs/1405.0312},
  year      = {2014},
  url       = {http://arxiv.org/abs/1405.0312},
  archivePrefix = {arXiv},
  eprint    = {1405.0312},
  timestamp = {Mon, 13 Aug 2018 16:48:13 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/LinMBHPRDZ14},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}
a  COCO is a large-scale object detection, segmentation, and
captioning dataset. This version contains images, bounding boxes "
and labels for the {year} version.
Note:
 * Some images from the train and validation sets don't have annotations.
 * Coco 2014 and 2017 uses the same images, but different train/val/test splits
 * The test split don't have any annotations (only images).
 * Coco defines 91 classes but the data only uses 80 classes.
 * Panotptic annotations defines defines 200 classes but only uses 133.
Splitnameimagesannotationsannotation_typec                   @   s   e Zd ZdZdZdZdZdS )AnnotationTypezWEnum of the annotation format types.

  Splits are annotated with different formats.
  bboxespanopticnoneN)__name__
__module____qualname____doc__BBOXESPANOPTICNONE r   r   ]/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/object_detection/coco.pyr   I   s
    r   c                       s&   e Zd ZdZ		d fdd	Z  ZS )
CocoConfigzBuilderConfig for CocoConfig.NFc                    s2   t t| jddtjdi| || _|| _d S )Nversionz1.1.0r   )superr   __init__tfdscoreVersionsplitshas_panoptic)selfr    r!   kwargs	__class__r   r   r   V   s   

zCocoConfig.__init__)NF)r   r   r   r   r   __classcell__r   r   r$   r   r   S   s
    r   c                   @   s  e Zd ZdZedejddeejj	dde
jdeejjdde
jdeejjd	d
e
jdeddde
jdgdedejddeejj	dde
jdeejjdde
jdeejjdde
jdgdedejdddeejj	dde
jdeejjdde
jdgdgZdd Zdd Zdd ZdS ) CocozBase MS Coco dataset.2014i  )year	train2014annotations_trainval2014r   val2014test2014image_info_test2014test2015image_info_test2015)r   descriptionr    2017i  	train2017annotations_trainval2017val2017test2017image_info_test20172017_panopticT!panoptic_annotations_trainval2017)r   r1   r!   r    c                 C   s   t jjddt j tjd}| jjr:|t jjddt j t j	tjtjt j
 t jjddtjdd n|d	t j	tjtjt j
 t jjd
dtjdi t jj| | jjt j|dtdS )Njpeg)encoding_format)imageimage/filenameimage/idpng   )num_classesidareabboxlabelis_crowd)panoptic_imagepanoptic_image/filenamepanoptic_objectsobjectsP   zhttp://cocodataset.org/#home)builderr1   featureshomepagecitation)r   rN   ImageTexttfint64builder_configr!   updateSequenceBBoxFeature
ClassLabelboolr   DatasetInfor1   FeaturesDict	_CITATION)r"   rN   r   r   r   _info   s@   

z
Coco._infoc           
         s  i }| j jD ]}d|j|d|j< d|j|d|j< qd | fdd| D }g }| j jD ]J}|d|j }|d|j }| j jrjt	j
|dd	|j}||}	t	j
|	d
|j}	nd}	|tjj|jt|||j|j|	dd q7|S )zReturns SplitGenerators.zzips/{}.zipz	{}_imageszannotations/{}.zipz{}_annotationszhttp://images.cocodataset.org/c                    s   i | ]	\}}| | qS r   r   ).0keyurlroot_urlr   r   
<dictcomp>   s    z*Coco._split_generators.<locals>.<dictcomp>r
   zpanoptic_{}.zipzpanoptic_{}N)	image_dirannotation_dir
split_namer   panoptic_dir)r   
gen_kwargs)rU   r    formatr	   r   r
   download_and_extractitemsr!   ospathjoinextractappendr   r   SplitGeneratordictr   )
r"   
dl_managerurlssplitextracted_pathsr    re   annotations_dirpanoptic_image_zip_pathrh   r   rb   r   _split_generators   sH   




zCoco._split_generatorsc                 #   s   |t jkr	d}n|t jkrd}n|t jkrd}tj|d||}t| |}|j	}	|j
}
| jjr6d}nd}dd |	D | jj| d	 _d
d |	D d}t|
dd dD ]l|t jkrg|jd d}n|t jkry|jd d}|d }ng }|s|d7 }fdd dtj||d dd dd | fdd|D i}| jjr|d }tj||}||d< ||d< d |fV  qWtd|t|
 dS )a  Generate examples as dicts.

    Args:
      image_dir: `str`, directory containing the images
      annotation_dir: `str`, directory containing annotations
      split_name: `str`, <split_name><year> (ex: train2014, val2017)
      annotation_type: `AnnotationType`, the annotation format (NONE, BBOXES,
        PANOPTIC)
      panoptic_dir: If annotation_type is PANOPTIC, contains the panoptic
        image directory

    Yields:
      example key and data
    zinstances_{}.jsonzpanoptic_{}.jsonzimage_info_{}.jsonr
   rJ   rK   c                 S   s   g | ]}|d  qS )r   r   r_   cr   r   r   
<listcomp>S  s    z+Coco._generate_examples.<locals>.<listcomp>rF   c                 S   s   i | ]	}|d  |d qS )rC   r   r   r{   r   r   r   rd   W  s    z+Coco._generate_examples.<locals>.<dictcomp>r   c                 S      | d S NrC   r   )xr   r   r   <lambda>[      z)Coco._generate_examples.<locals>.<lambda>r`   rC   )img_idsegments_info   c                    s<   t jj| d  |  d  ||  d  | |  d  dS )Nheightwidth)yminxminymaxxmax)r   rN   BBox)r   yr   r   )
image_infor   r   
build_bbox  s   

z+Coco._generate_examples.<locals>.build_bboxr<   	file_namer=   r>   c              	      s<   g | ]}|d  |d  |d  |d  t |d dqS )rC   rD   rE   category_idiscrowdrB   )rZ   )r_   instance)r   categories_id2namer   r   r}     s    



rH   rI   z,%d/%d images do not contains any annotationsN)r   r   r   r   rm   rn   ro   rj   ANNOTATION_CLS
categoriesr	   rU   r!   inforN   namessortedget_annotationsr   len)r"   re   rf   rg   r   rh   instance_filenameinstance_pathcoco_annotationr   r	   objects_keyannotation_skipped	instancespanoptic_annotationexamplepanoptic_filenamepanoptic_image_pathr   )r   r   r   r   _generate_examples  sj   





zCoco._generate_examplesN)r   r   r   r   r   DESCRIPTIONrj   r   r   TRAINr   r   
VALIDATIONTESTr   r   BUILDER_CONFIGSr^   rz   r   r   r   r   r   r'   b   s    


M5,r'   c                   @   s8   e Zd ZdZdd Zedd Zedd Zdd	 Zd
S )CocoAnnotationCoco annotation helper class.c                 C   sB   t jj|}t|}W d    n1 sw   Y  || _d S )N)rS   iogfileGFilejsonload_data)r"   annotation_pathfdatar   r   r   r     s   
zCocoAnnotation.__init__c                 C   
   | j d S )z1Return the category dicts, as sorted in the file.r   r   r"   r   r   r   r        
zCocoAnnotation.categoriesc                 C   r   )z.Return the image dicts, as sorted in the file.r	   r   r   r   r   r   r	     r   zCocoAnnotation.imagesc                 C   s   t z;Return all annotations associated with the image id string.)NotImplementedErrorr"   r   r   r   r   r     s   zCocoAnnotation.get_annotationsN)	r   r   r   r   r   propertyr   r	   r   r   r   r   r   r     s    

r   c                       (   e Zd ZdZ fddZdd Z  ZS )CocoAnnotationBBoxesr   c                    sT   t t| | tt}| jd D ]}||d  | qdd | D | _	d S )Nr
   image_idc                 S   s&   i | ]\}}|t t|d d dqS )c                 S   r~   r   r   )ar   r   r   r     r   z:CocoAnnotationBBoxes.__init__.<locals>.<dictcomp>.<lambda>r   )listr   )r_   kvr   r   r   rd     s    z1CocoAnnotationBBoxes.__init__.<locals>.<dictcomp>)
r   r   r   collectionsdefaultdictr   r   rq   rl   _img_id2annotations)r"   r   img_id2annotationsr   r$   r   r   r     s   
zCocoAnnotationBBoxes.__init__c                 C   s   | j |g S r   )r   getr   r   r   r   r     s   z$CocoAnnotationBBoxes.get_annotationsr   r   r   r   r   r   r&   r   r   r$   r   r     s    r   c                       r   )CocoAnnotationPanopticr   c                    s*   t t| | dd | jd D | _d S )Nc                 S   s   i | ]}|d  |qS )r   r   )r_   r   r   r   r   rd     s    
z3CocoAnnotationPanoptic.__init__.<locals>.<dictcomp>r
   )r   r   r   r   r   )r"   r   r$   r   r   r     s   zCocoAnnotationPanoptic.__init__c                 C   s
   | j | S r   )r   r   r   r   r   r     s   
z&CocoAnnotationPanoptic.get_annotationsr   r   r   r$   r   r     s    r   )#r   
__future__r   r   r   r   r   rm   abslr   tensorflow.compat.v2compatv2rS   tensorflow_datasets.public_api
public_apir   r]   r   
namedtupler   objectr   r   BuilderConfigr   GeneratorBasedBuilderr'   r   r   r   r   r   r   r   r   r   r   r   <module>   s6   
  G