
    h                     t    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y)    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                  j                                     | _	        y )Nr   )COCO)
super__init__pycocotools.cocor   cocolistsortedimgskeysids)selfr   r   r   r   r   r   	__class__s          W/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/datasets/coco.pyr   zCocoDetection.__init__   sI     	z96FG)M	tyy~~22456    idc                     | j                   j                  |      d   d   }t        j                  t        j
                  j                  | 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)   sM    yy!!"%a(5zz"'',,tyy$78@@GGr!   c                 j    | j                   j                  | j                   j                  |            S N)r   loadAnns	getAnnIds)r   r"   s     r    _load_targetzCocoDetection._load_target-   s&    yy!!$))"5"5b"9::r!   indexc                     t        |t              st        dt        |       d      | j                  |   }| j                  |      }| j                  |      }| 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   sz    %%B4;-yYZZXXe_  $""2&??& OOE6:ME6f}r!   c                 ,    t        | j                        S r.   )lenr   )r   s    r    __len__zCocoDetection.__len__>   s    488}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CI7 7 H%	7
 #8,7 X&7 
7Hc Hekk H;s ;tCy ; sCx  r!   r   c                   2     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                 N    t         |   |      D cg c]  }|d   	 c}S c c}w )Ncaption)r   r1   )r   r"   annr   s      r    r1   zCocoCaptions._load_targetn   s%    */'*>r*BC3ICCCs   ")	r>   r?   r@   rA   r5   r   rB   r1   rD   rE   s   @r    rG   rG   B   s)    )VDs DtCy D Dr!   rG   )os.pathr(   pathlibr   typingr   r   r   r   PILr   visionr
   r   rG    r!   r    <module>rQ      s2      1 1  !5M 5p-D= -Dr!   