a
    h                     @   s   d dl Z d dlZd dlmZ d dlm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
 d
eZG dd deZG dd deZdS )    N)defaultdict)
HTMLParser)Path)AnyCallableOptionalUnion   )default_loader)VisionDatasetc                       sr   e Zd ZdZeeef dd fddZeee	ee
e f  ddddZedd	d
dZeddddZ  ZS )Flickr8kParserzBParser for extracting captions from the Flickr8k dataset web page.N)rootreturnc                    s,   t    || _i | _d| _d | _d | _d S )NF)super__init__r   annotationsin_tablecurrent_tagcurrent_img)selfr   	__class__ I/var/www/auris/lib/python3.9/site-packages/torchvision/datasets/flickr.pyr      s    
zFlickr8kParser.__init__)tagattrsr   c                 C   s   || _ |dkrd| _d S )NtableTr   r   )r   r   r   r   r   r   handle_starttag   s    zFlickr8kParser.handle_starttag)r   r   c                 C   s   d | _ |dkrd| _d S )Nr   Fr   )r   r   r   r   r   handle_endtag"   s    zFlickr8kParser.handle_endtag)datar   c                 C   s   | j r|dkrd | _nv| jdkrb|dd }tj| j|d }t|d }|| _g | j	|< n*| jdkr| jr| j}| j	| 
|  d S )NzImage Not Founda/z_*.jpgr   li)r   r   r   splitospathjoinr   globr   appendstrip)r   r    img_idr   r   r   handle_data(   s    
zFlickr8kParser.handle_data)__name__
__module____qualname____doc__r   strr   r   listtupler   r   r   r-   __classcell__r   r   r   r   r      s
   "r   c                       sx   e Zd ZdZddefeeef eee	 ee	 e	ege
f dd fddZeee
e
f dddZed	d
dZ  ZS )Flickr8ka?  `Flickr8k Entities <http://hockenmaier.cs.illinois.edu/8k-pictures.html>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory where images are downloaded to.
        ann_file (string): Path to annotation file.
        transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader,
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    Nr   ann_file	transformtarget_transformloaderr   c                    s   t  j|||d tj|| _t| j}t| j}|	|
  W d    n1 sX0    Y  |j| _tt| j | _|| _d S )Nr9   r:   )r   r   r&   r'   
expanduserr8   r   r   openfeedreadr   r3   sortedkeysidsr;   )r   r   r8   r9   r:   r;   parserfhr   r   r   r   F   s    
,zFlickr8k.__init__indexr   c                 C   sN   | j | }| |}| jdur(| |}| j| }| jdurF| |}||fS z
        Args:
            index (int): Index

        Returns:
            tuple: Tuple (image, target). target is a list of captions for the image.
        N)rC   r;   r9   r   r:   )r   rG   r,   imgtargetr   r   r   __getitem__Z   s    






zFlickr8k.__getitem__r   c                 C   s
   t | jS NlenrC   r   r   r   r   __len__p   s    zFlickr8k.__len__)r.   r/   r0   r1   r
   r   r2   r   r   r   r   r   intr4   rK   rQ   r5   r   r   r   r   r6   7   s   
r6   c                       sp   e Zd ZdZddefeeee ee eegef dd fddZ	e
eeef dddZe
d	d
dZ  ZS )	Flickr30ka8  `Flickr30k Entities <https://bryanplummer.com/Flickr30kEntities/>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory where images are downloaded to.
        ann_file (string): Path to annotation file.
        transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader,
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    Nr7   c           
         s   t  j|||d tj|| _tt| _t	| jD}|D ].}|
 d\}}	| j|d d  |	 q:W d    n1 s~0    Y  tt| j | _|| _d S )Nr<   	r#   )r   r   r&   r'   r=   r8   r   r3   r   r>   r+   r%   r*   rA   rB   rC   r;   )
r   r   r8   r9   r:   r;   rE   liner,   captionr   r   r   r      s    
8zFlickr30k.__init__rF   c                 C   s^   | j | }tj| j|}| |}| jdur8| |}| j| }| jdurV| |}||fS rH   )	rC   r&   r'   r(   r   r;   r9   r   r:   )r   rG   r,   filenamerI   rJ   r   r   r   rK      s    






zFlickr30k.__getitem__rL   c                 C   s
   t | jS rM   rN   rP   r   r   r   rQ      s    zFlickr30k.__len__)r.   r/   r0   r1   r
   r2   r   r   r   r   rR   r4   rK   rQ   r5   r   r   r   r   rS   t   s   rS   )r)   r&   collectionsr   html.parserr   pathlibr   typingr   r   r   r   folderr
   Zvisionr   r   r6   rS   r   r   r   r   <module>   s   +=