
    ڧg"                        d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlmZ 	 d dlmZ n# e$ r	 d dlmZ Y nw xY wd dlmZ ddlmZmZ dd	lmZ d
ddej                            dd          ddddej                            ddd          ddddej                            dd          ddddej                            dd          ddddej                            dd          dd d!d"ej                            dd#          dd$d%d&ej                            dd#          dd'Z G d( d)e          Z G d* d+e          Z G d, d-e          ZdS ).    N)Path)AnyCallableDictListOptionalTupleUnion)Element)parse)Image   )download_and_extract_archiveverify_str_arg)VisionDatasetzJhttp://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tarzVOCtrainval_11-May-2012.tar 6cd6e144f989b92b3379bac3b3de84fd	VOCdevkitVOC2012)urlfilenamemd5base_dirzJhttp://host.robots.ox.ac.uk/pascal/VOC/voc2011/VOCtrainval_25-May-2011.tarzVOCtrainval_25-May-2011.tar 6c3384ef61512963050cb5d687e5bf1eTrainValVOC2011zJhttp://host.robots.ox.ac.uk/pascal/VOC/voc2010/VOCtrainval_03-May-2010.tarzVOCtrainval_03-May-2010.tar da459979d0c395079b5c75ee67908abbVOC2010zJhttp://host.robots.ox.ac.uk/pascal/VOC/voc2009/VOCtrainval_11-May-2009.tarzVOCtrainval_11-May-2009.tar a3e00b113cfcfebf17e343f59da3caa1VOC2009zJhttp://host.robots.ox.ac.uk/pascal/VOC/voc2008/VOCtrainval_14-Jul-2008.tar 2629fa636546599198acfcfbfcf1904aVOC2008zJhttp://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tarzVOCtrainval_06-Nov-2007.tar c52e279531787c972589f7e41ab4ae64VOC2007zFhttp://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tarzVOCtest_06-Nov-2007.tar b6e924de25625d8de591ea690078ad9f)201220112010200920082007	2007-testc                        e Zd ZU eed<   eed<   eed<   	 	 	 	 	 	 ddeeef         d	ed
ededee	         dee	         dee	         f fdZ
defdZ xZS )_VOCBase_SPLITS_DIR_TARGET_DIR_TARGET_FILE_EXTr%   trainFNrootyear	image_setdownload	transformtarget_transform
transformsc           	      Z    t                                          ||||           t          |dd t          dd          D                        _        g d}|dk    r|                    d           t          |d	|           _        |dk    r|dk    rd
n|}	t          |	         }
|
d          _        |
d          _	        |
d          _
        |
d         }t          j                             j        |          }|r't           j         j         j	         j
                   t          j                            |          st#          d          t          j                            |d j                  }t          j                            ||                    d          dz             }t)          t          j                            |                    5 }d |                                D             }d d d            n# 1 swxY w Y   t          j                            |d          fd|D              _        t          j                            | j                   fd|D              _        t3           j                  t3           j                  k    sJ d S )Nr3   c                 ,    g | ]}t          |          S  )str).0yrs     T/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/voc.py
<listcomp>z%_VOCBase.__init__.<locals>.<listcomp>P   s    >c>c>c2s2ww>c>c>c    i  i  )valid_values)r1   trainvalvalr*   testr4   r+   r   r   r   r   )r   r   zHDataset not found or corrupted. You can use download=True to download it	ImageSets
z.txtc                 6    g | ]}|                                 S r;   )strip)r=   xs     r?   r@   z%_VOCBase.__init__.<locals>.<listcomp>j   s     ;;;!''));;;rA   
JPEGImagesc                 V    g | ]%}t           j                            |d z             &S )z.jpg)ospathjoin)r=   rJ   	image_dirs     r?   r@   z%_VOCBase.__init__.<locals>.<listcomp>m   s-    OOOqrw||Iq6z::OOOrA   c                 `    g | ]*}t           j                            |j        z             +S r;   )rM   rN   rO   r0   )r=   rJ   self
target_dirs     r?   r@   z%_VOCBase.__init__.<locals>.<listcomp>p   s2    ```PQZT5J1JKK```rA   )super__init__r   ranger3   appendr4   DATASET_YEAR_DICTr   r   r   rM   rN   rO   r2   r   isdirRuntimeErrorr.   rstripopen	readlinesimagesr/   targetslen)rR   r2   r3   r4   r5   r6   r7   r8   valid_image_setskeydataset_year_dictr   voc_root
splits_dirsplit_ff
file_namesrP   rS   	__class__s   `                @@r?   rU   z_VOCBase.__init__D   s    	z96FGGG"4>c>cQVW[]aQbQb>c>c>cddd	7776>>##F+++'	;@PQQ!V^^	V0C0Ckk-c2$U+)*5$U+$Z07<<	844 	d(49t}Z^Zbccccw}}X&& 	kijjjW\\(K9IJJ
',,z9+;+;D+A+AF+JKK"',,w''(( 	<A;;Q[[]];;;J	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< GLL<88	OOOOJOOOW\\(D,<==
`````U_```4;3t|#4#4444444s   HHHreturnc                 *    t          | j                  S N)r`   r^   rR   s    r?   __len__z_VOCBase.__len__t   s    4;rA   )r%   r1   FNNN)__name__
__module____qualname__r<   __annotations__r
   r   boolr   r   rU   intrn   __classcell__)ri   s   @r?   r-   r-   ?   s         
  (,/3)-.5 .5CI.5 .5 	.5
 .5 H%.5 #8,.5 X&.5 .5 .5 .5 .5 .5`                 rA   r-   c                   f    e Zd ZdZdZdZdZedee	         fd            Z
dedeeef         fdZd	S )
VOCSegmentationa2  `Pascal VOC <http://host.robots.ox.ac.uk/pascal/VOC/>`_ Segmentation Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of the VOC Dataset.
        year (string, optional): The dataset year, supports years ``"2007"`` to ``"2012"``.
        image_set (string, optional): Select the image_set to use, ``"train"``, ``"trainval"`` or ``"val"``. If
            ``year=="2007"``, can also be ``"test"``.
        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.
        transform (callable, optional): A function/transform that  takes in a PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        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.
    SegmentationSegmentationClassz.pngrj   c                     | j         S rl   r_   rm   s    r?   maskszVOCSegmentation.masks   
    |rA   indexc                     t          j        | j        |                                       d          }t          j        | j        |                   }| j        |                     ||          \  }}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is the image segmentation.
        RGB)r   r\   r^   convertr|   r8   rR   r~   imgtargets       r?   __getitem__zVOCSegmentation.__getitem__   sg     jU+,,44U;;DJu-..?&//#v66KCF{rA   N)ro   rp   rq   __doc__r.   r/   r0   propertyr   r<   r|   rt   r	   r   r   r;   rA   r?   rw   rw   x   s         $ !K%KtCy    X sCx      rA   rw   c                       e Zd ZdZdZdZdZedee	         fd            Z
dedeeef         fdZed	edee	ef         fd
            ZdS )VOCDetectionan  `Pascal VOC <http://host.robots.ox.ac.uk/pascal/VOC/>`_ Detection Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of the VOC Dataset.
        year (string, optional): The dataset year, supports years ``"2007"`` to ``"2012"``.
        image_set (string, optional): Select the image_set to use, ``"train"``, ``"trainval"`` or ``"val"``. If
            ``year=="2007"``, can also be ``"test"``.
        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.
            (default: alphabetic indexing of VOC's 20 classes).
        transform (callable, optional): A function/transform that takes in a PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, required): 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.
    MainAnnotationsz.xmlrj   c                     | j         S rl   r{   rm   s    r?   annotationszVOCDetection.annotations   r}   rA   r~   c                 ,   t          j        | j        |                                       d          }|                     t          | j        |                                                             }| j        |                     ||          \  }}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is a dictionary of the XML tree.
        r   )	r   r\   r^   r   parse_voc_xmlET_parser   getrootr8   r   s       r?   r   zVOCDetection.__getitem__   s     jU+,,44U;;##HT-=e-D$E$E$M$M$O$OPP?&//#v66KCF{rA   nodec                    i }t          |           }|rt          j        t                     }t          t          j        |          D ]7}|                                D ] \  }}||                             |           !8| j        dk    r|d         g|d<   | j        d |                                D             i}| j	        r%| j	        
                                }|s
||| j        <   |S )N
annotationobjectc                 P    i | ]#\  }}|t          |          d k    r|d         n|$S )r   r   )r`   )r=   indvs      r?   
<dictcomp>z.VOCDetection.parse_voc_xml.<locals>.<dictcomp>   s4    "a"a"avsA3A!!"a"a"arA   )listcollectionsdefaultdictmapr   r   itemsrW   tagtextrI   )r   voc_dictchildrendef_dicdcr   r   r   s           r?   r   zVOCDetection.parse_voc_xml   s    #%:: 	c&1&=d&C&CG,4h?? + + hhjj + +FCCL''****+x<''%,X%6$7!"a"aQXQ^Q^Q`Q`"a"a"abH9 	*9??$$D *%)"rA   N)ro   rp   rq   r   r.   r/   r0   r   r   r<   r   rt   r	   r   r   staticmethod
ET_Elementr   r   r;   rA   r?   r   r      s         & KKT#Y    X sCx      J 4S>    \  rA   r   ) r   rM   pathlibr   typingr   r   r   r   r   r	   r
   xml.etree.ElementTreer   r   defusedxml.ElementTreer   r   ImportErrorPILr   utilsr   r   visionr   rN   rO   rX   r-   rw   r   r;   rA   r?   <module>r      s       				       D D D D D D D D D D D D D D D D D D 7 7 7 7 7 788888888 8 8 8777777778       ? ? ? ? ? ? ? ? ! ! ! ! ! ! \11GLLi88	  \11GLL[)DD	  \11GLLi88	  \11GLLi88	  \11GLLi88	  \11GLLi88	  X-1GLLi88	 K+ + \6  6  6  6  6 } 6  6  6 r) ) ) ) )h ) ) )X< < < < <8 < < < < <s   / ==