
    Yh                         d dl Zd dlmZ d dlmZmZmZmZ d dl	m
Z
 ddlmZ  G d de          Z G d	 d
e          ZdS )    N)Path)AnyCallableOptionalUnion)Image   )VisionDatasetc                        e Zd ZdZ	 	 	 ddeeef         dedee         dee         dee         ddf fd	Z	d
e
dej        fdZd
e
dee         fdZde
deeef         fdZde
fdZ xZS )CocoDetectionah  `MS Coco Detection <https://cocodataset.org/#detection-2016>`_ Dataset.

    It requires `pycocotools <https://github.com/ppwwyyxx/cocoapi>`_ to be installed,
    which could be installed via ``pip install pycocotools`` or ``conda install conda-forge::pycocotools``.

    Args:
        root (str or ``pathlib.Path``): Root directory where images are downloaded to.
        annFile (string): Path to json annotation file.
        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.
    NrootannFile	transformtarget_transform
transformsreturnc                     t                                          ||||           ddlm}  ||          | _        t          t          | j        j                                                            | _	        d S )Nr   )COCO)
super__init__pycocotools.cocor   cocolistsortedimgskeysids)selfr   r   r   r   r   r   	__class__s          k/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/datasets/coco.pyr   zCocoDetection.__init__   sp     	z96FGGG))))))DMM	ty~22445566    idc                     | j                             |          d         d         }t          j        t          j                            | j        |                                        d          S )Nr   	file_nameRGB)	r   loadImgsr   openospathjoinr   convert)r   r"   r)   s      r    _load_imagezCocoDetection._load_image)   sQ    y!!"%%a(5z"',,ty$7788@@GGGr!   c                 f    | j                             | j                             |                    S N)r   loadAnns	getAnnIds)r   r"   s     r    _load_targetzCocoDetection._load_target-   s(    y!!$)"5"5b"9"9:::r!   indexc                 "   t          |t                    s t          dt          |           d          | j        |         }|                     |          }|                     |          }| j        |                     ||          \  }}||fS )Nz#Index must be of type integer, got z	 instead.)
isinstanceint
ValueErrortyper   r,   r1   r   )r   r2   r"   imagetargets        r    __getitem__zCocoDetection.__getitem__0   s    %%% 	[Y4;;YYYZZZXe_  $$""2&&?& OOE6::ME6f}r!   c                 *    t          | j                  S r.   )lenr   )r   s    r    __len__zCocoDetection.__len__>   s    48}}r!   )NNN)__name__
__module____qualname____doc__r   strr   r   r   r   r5   r   r,   r   r   r1   tupler:   r=   __classcell__r   s   @r    r   r   
   s>        ( )-/3)-7 7CI7 7 H%	7
 #8,7 X&7 
7 7 7 7 7 7Hc Hek H H H H;s ;tCy ; ; ; ; sCx            r!   r   c                   8     e Zd ZdZdedee         f fdZ xZS )CocoCaptionsa[  `MS Coco Captions <https://cocodataset.org/#captions-2015>`_ Dataset.

    It requires `pycocotools <https://github.com/ppwwyyxx/cocoapi>`_ to be installed,
    which could be installed via ``pip install pycocotools`` or ``conda install conda-forge::pycocotools``.

    Args:
        root (str or ``pathlib.Path``): Root directory where images are downloaded to.
        annFile (string): Path to json annotation file.
        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.

    Example:

        .. code:: python

            import torchvision.datasets as dset
            import torchvision.transforms as transforms
            cap = dset.CocoCaptions(root = 'dir where images are',
                                    annFile = 'json annotation file',
                                    transform=transforms.PILToTensor())

            print('Number of samples: ', len(cap))
            img, target = cap[3] # load 4th sample

            print("Image Size: ", img.size())
            print(target)

        Output: ::

            Number of samples: 82783
            Image Size: (3L, 427L, 640L)
            [u'A plane emitting smoke stream flying over a mountain.',
            u'A plane darts across a bright blue sky behind a mountain covered in snow',
            u'A plane leaves a contrail above the snowy mountain top.',
            u'A mountain that has a plane flying overheard in the distance.',
            u'A mountain view with a plume of smoke in the background']

    r"   r   c                 Z    d t                                          |          D             S )Nc                     g | ]
}|d          S )caption ).0anns     r    
<listcomp>z-CocoCaptions._load_target.<locals>.<listcomp>o   s    CCC3ICCCr!   )r   r1   )r   r"   r   s     r    r1   zCocoCaptions._load_targetn   s)    CC%''*>*>r*B*BCCCCr!   )	r>   r?   r@   rA   r5   r   rB   r1   rD   rE   s   @r    rG   rG   B   sf        ) )VDs DtCy D D D D D D D D D Dr!   rG   )os.pathr(   pathlibr   typingr   r   r   r   PILr   visionr
   r   rG   rK   r!   r    <module>rT      s           1 1 1 1 1 1 1 1 1 1 1 1       ! ! ! ! ! !5 5 5 5 5M 5 5 5p-D -D -D -D -D= -D -D -D -D -Dr!   