
    ڧgT                         d dl Zd dlmZ d dlmZmZ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ListOptionalTuple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 )CocoDetectiona  `MS Coco Detection <https://cocodataset.org/#detection-2016>`_ Dataset.

    It requires the `COCO API to be installed <https://github.com/pdollar/coco/tree/master/PythonAPI>`_.

    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          U/var/www/html/ai-engine/env/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.   r3   r   )r    r4   r$   imagetargets        r"   __getitem__zCocoDetection.__getitem__/   s    %%% 	[Y4;;YYYZZZXe_  $$""2&&?& OOE6::ME6f}r#   c                 *    t          | j                  S r0   )lenr   )r    s    r"   __len__zCocoDetection.__len__=   s    48}}r#   )NNN)__name__
__module____qualname____doc__r	   strr   r   r   r   r7   r
   r.   r   r   r3   r   r<   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 the `COCO API to be installed <https://github.com/pdollar/coco/tree/master/PythonAPI>`_.

    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>m   s    CCC3ICCCr#   )r   r3   )r    r$   r!   s     r"   r3   zCocoCaptions._load_targetl   s)    CC%''*>*>r*B*BCCCCr#   )	r@   rA   rB   rC   r7   r   rD   r3   rE   rF   s   @r"   rH   rH   A   sf        ( (TDs DtCy D D D D D D D D D Dr#   rH   )os.pathr*   pathlibr   typingr   r   r   r   r   r	   PILr
   visionr   r   rH   rL   r#   r"   <module>rU      s           > > > > > > > > > > > > > > > >       ! ! ! ! ! !4 4 4 4 4M 4 4 4n,D ,D ,D ,D ,D= ,D ,D ,D ,D ,Dr#   