
    kh                     |    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y)    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 )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          U/var/www/teggl/fontify/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.   r3   r   )r    r4   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 r0   )lenr   )r    s    r"   __len__zCocoDetection.__len__>   s    488}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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   r3   )r    r$   annr!   s      r"   r3   zCocoCaptions._load_targetn   s%    */'*>r*BC3ICCCs   ")	r@   rA   rB   rC   r7   r   rD   r3   rE   rF   s   @r"   rH   rH   B   s)    )VDs DtCy D Dr#   rH   )os.pathr*   pathlibr   typingr   r   r   r   r   r	   PILr
   visionr   r   rH    r#   r"   <module>rR      s2      > >  !5M 5p-D= -Dr#   