o
    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                       s|   e Zd ZdZdeeef ddf fddZdedee	ee
e f  ddfd	d
ZdeddfddZdeddfddZ  ZS )Flickr8kParserzBParser for extracting captions from the Flickr8k dataset web page.rootreturnNc                    s,   t    || _i | _d| _d | _d | _d S )NF)super__init__r   annotationsin_tablecurrent_tagcurrent_img)selfr   	__class__ a/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torchvision/datasets/flickr.pyr      s   

zFlickr8kParser.__init__tagattrsc                 C   s   || _ |dkrd| _d S d S )NtableTr   r   )r   r   r   r   r   r   handle_starttag      
zFlickr8kParser.handle_starttagc                 C   s   d | _ |dkrd| _d S d S )Nr   Fr   )r   r   r   r   r   handle_endtag"   r   zFlickr8kParser.handle_endtagdatac                 C   s   | j rJ|dkrd | _d S | jdkr3|dd }tj| j|d }t|d }|| _g | j	|< d S | jdkrL| jrN| j}| j	| 
|  d S d S d S d S )NzImage Not Founda/z_*.jpgr   Z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                       s   e Zd ZdZddefdeeef dedee	 dee	 de	ege
f ddf fd	d
Zdedee
e
f fddZdef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.
    Nr   ann_file	transformtarget_transformloaderr   c                    s   t  j|||d tj|| _t| j}t| j}|	|
  W d    n1 s,w   Y  |j| _tt| j | _|| _d S )Nr8   r9   )r   r   r&   r'   
expanduserr7   r   r   openfeedreadr   r3   sortedkeysidsr:   )r   r   r7   r8   r9   r:   parserfhr   r   r   r   F   s   

zFlickr8k.__init__indexc                 C   sN   | j | }| |}| jdur| |}| j| }| jdur#| |}||fS z
        Args:
            index (int): Index

        Returns:
            tuple: Tuple (image, target). target is a list of captions for the image.
        N)rB   r:   r8   r   r9   )r   rE   r,   imgtargetr   r   r   __getitem__Z   s   






zFlickr8k.__getitem__c                 C   
   t | jS NlenrB   r   r   r   r   __len__p      
zFlickr8k.__len__)r.   r/   r0   r1   r
   r   r2   r   r   r   r   r   intr4   rI   rO   r5   r   r   r   r   r6   7   s(    
r6   c                       s|   e Zd ZdZddefdededee dee deegef ddf fd	d
Z	de
deeef fddZde
f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.
    Nr   r7   r8   r9   r:   r   c           
         s   t  j|||d tj|| _tt| _t	| j"}|D ]}|
 d\}}	| j|d d  |	 qW d    n1 s?w   Y  tt| j | _|| _d S )Nr;   	r$   )r   r   r&   r'   r<   r7   r   r3   r   r=   r+   r%   r*   r@   rA   rB   r:   )
r   r   r7   r8   r9   r:   rD   liner,   captionr   r   r   r      s   

zFlickr30k.__init__rE   c                 C   s^   | j | }tj| j|}| |}| jdur| |}| j| }| jdur+| |}||fS rF   )	rB   r&   r'   r(   r   r:   r8   r   r9   )r   rE   r,   filenamerG   rH   r   r   r   rI      s   






zFlickr30k.__getitem__c                 C   rJ   rK   rL   rN   r   r   r   rO      rP   zFlickr30k.__len__)r.   r/   r0   r1   r
   r2   r   r   r   r   rQ   r4   rI   rO   r5   r   r   r   r   rR   t   s(    rR   )r)   r&   collectionsr   html.parserr   pathlibr   typingr   r   r   r   folderr
   Zvisionr   r   r6   rR   r   r   r   r   <module>   s    +=