o
    NiA!                     @   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m	  m
Z ddlmZ dZdZdZd	Zd
ZdZdd ZG dd dejjZG dd dejjZdS )zPASCAL VOC datasets.    )absolute_import)division)print_functionNaJ  @misc{{pascal-voc-{year},
	author = "Everingham, M. and Van~Gool, L. and Williams, C. K. I. and Winn, J. and Zisserman, A.",
	title = "The {{PASCAL}} {{V}}isual {{O}}bject {{C}}lasses {{C}}hallenge {year} {{(VOC{year})}} {{R}}esults",
	howpublished = "http://www.pascal-network.org/challenges/VOC/voc{year}/workshop/index.html"}}
a  This dataset contains the data from the PASCAL Visual Object Classes Challenge
{year}, a.k.a. VOC{year}, corresponding to the Classification and Detection
competitions.
A total of {num_images} images are included in this dataset, where each image
contains a set of objects, out of 20 different classes, making a total of
{num_objects} annotated objects.
In the Classification competition, the goal is to predict the set of labels
contained in the image, while in the Detection competition the goal is to
predict the bounding box and label of each individual object.
WARNING: As per the official dataset, the test set of VOC2012 does not contain
annotations.
z1http://host.robots.ox.ac.uk/pascal/VOC/voc{year}/z http://pjreddie.com/media/files/)	aeroplanebicyclebirdboatbottlebuscarcatchaircowdiningtabledoghorse	motorbikepersonpottedplantsheepsofatrain	tvmonitor)frontalrearleftrightunspecifiedc                 c   s>   t jj| d}tjj| }|	d}t
|	dj}t
|	dj}|dD ]_}|	dj }|	dj }|	djd	k}	|	d
jd	k}
|	d}t
|	dj}t
|	dj}t
|	dj}t
|	dj}||tj|| || || || |	|
dV  q-W d   dS 1 sw   Y  dS )z=Function to get all the objects from the annotation XML file.rsizewidthheightobjectnamepose	truncated1	difficultbndboxxmaxxminymaxymin)labelr$   bboxis_truncatedis_difficultN)tfiogfileGFilexmletreeElementTreeparsegetrootfindfloattextfindalllowertfdsfeaturesBBox)annon_filepathfrootr   r    r!   objr-   r$   r/   r0   r(   r)   r*   r+   r,    rF   \/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/object_detection/voc.py_get_example_objectsU   s4   


"rH   c                       s$   e Zd ZdZ	d fdd	Z  ZS )	VocConfigzBuilderConfig for Voc.NTc                    s:   || _ || _|| _tt| jd|tjdd| d S )Nz4.0.0)r#   versionrF   )	year	filenameshas_test_annotationssuperrI   __init__r?   coreVersion)selfrK   rL   rM   kwargs	__class__rF   rG   rO   {   s   

zVocConfig.__init__)NNT)__name__
__module____qualname____doc__rO   __classcell__rF   rF   rT   rG   rI   x   s    rI   c                   @   st   e Zd ZdZedejddddddd	d
dedejddddddd	ddgZdd Zdd Z	dd Z
dd ZdS )VoczPascal VOC 2007 or 2012.2007i  i&  i@`  )rK   
num_imagesnum_objectszVOCtrainval_06-Nov-2007.tarzVOCtest_06-Nov-2007.tar)trainvaltestT)rK   descriptionrL   rM   2012i  i-  i:k  zVOCtrainval_11-May-2012.tarzVOC2012test.tarFc                 C   s   t jj| | jjt jt j t j t j	t jj
tdt j t jj
tdtjtjdt j	t jj
tdt j	t jj
tddtj| jjdtj| jjddS )N)names)r-   r.   r$   r/   r0   imagezimage/filenameobjectslabelslabels_no_difficult)rK   )builderra   r@   homepagecitation)r?   rP   DatasetInfobuilder_configra   r@   FeaturesDictImageTextSequence
ClassLabel_VOC_LABELSBBoxFeature
_VOC_POSESr1   bool_VOC_URLformatrK   _VOC_CITATION)rR   rF   rF   rG   _info   s.   z	Voc._infoc                 C   sz   | dd | jj D }tjjtjjt	|d dddtjjtjj
t	|d dddtjjtjjt	|d dddgS )	Nc                 S   s    i | ]\}}|t jt|qS rF   )ospathjoin_VOC_DATA_URL).0kvrF   rF   rG   
<dictcomp>   s    z)Voc._split_generators.<locals>.<dictcomp>r`   )	data_pathset_name)r#   
gen_kwargsr_   r   val)download_and_extractrm   rL   itemsr?   rP   SplitGeneratorSplitTESTdictTRAIN
VALIDATION)rR   
dl_managerpathsrF   rF   rG   _split_generators   s    

zVoc._split_generatorsc           	   	   c   s    t j|t jd| jj|}| jjp|dk}tj	j
|d}|D ]}| }| |||}||fV  q%W d   dS 1 sCw   Y  dS )zYields examples.z%VOCdevkit/VOC{}/ImageSets/Main/{}.txtr`   r   N)r{   r|   r}   normpathrx   rm   rK   rM   r1   r2   r3   r4   strip_generate_example)	rR   r   r   set_filepathload_annotationsrC   lineimage_idexamplerF   rF   rG   _generate_examples   s    
"zVoc._generate_examplesc           	   	   C   s   t j|t jd| jj|}t j|t jd| jj|}|rAtt|}t	t
dd |D }t	t
dd |D }ng }g }g }||d |||dS )Nz!VOCdevkit/VOC{}/JPEGImages/{}.jpgz"VOCdevkit/VOC{}/Annotations/{}.xmlc                 s   s    | ]}|d  V  qdS )r-   NrF   r   rE   rF   rF   rG   	<genexpr>   s    z(Voc._generate_example.<locals>.<genexpr>c                 s   s$    | ]}|d  dkr|d V  qdS )r0   r   r-   NrF   r   rF   rF   rG   r      s    z.jpgrd   )r{   r|   r}   r   rx   rm   rK   listrH   sortedset)	rR   r   r   r   image_filepathrB   rf   rg   rh   rF   rF   rG   r      s6   


zVoc._generate_exampleN)rV   rW   rX   rY   rI   _VOC_DESCRIPTIONrx   BUILDER_CONFIGSrz   r   r   r   rF   rF   rF   rG   r[      s6    
r[   )rY   
__future__r   r   r   r{   xml.etree.ElementTreer5   tensorflow.compat.v2compatv2r1   tensorflow_datasets.public_api
public_apir?   ry   r   rw   r~   rs   ru   rH   rP   BuilderConfigrI   GeneratorBasedBuilderr[   rF   rF   rF   rG   <module>   s"   	#