
    Yh                         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
e          Z G d de          Z G d de          ZdS )    N)defaultdict)
HTMLParser)Path)AnyCallableOptionalUnion   )default_loader)VisionDatasetc                        e Zd ZdZdeeef         ddf fdZdedee	ee
e         f                  ddfdZdeddfd	Zd
eddfdZ xZS )Flickr8kParserzBParser for extracting captions from the Flickr8k dataset web page.rootreturnNc                     t                                                       || _        i | _        d| _        d | _        d | _        d S )NF)super__init__r   annotationsin_tablecurrent_tagcurrent_img)selfr   	__class__s     m/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/datasets/flickr.pyr   zFlickr8kParser.__init__   sI    	 24 *.*.    tagattrsc                 2    || _         |dk    r	d| _        d S d S )NtableTr   r   )r   r   r   s      r   handle_starttagzFlickr8kParser.handle_starttag   s%    '>> DMMM >r   c                 2    d | _         |dk    r	d| _        d S d S )Nr   Fr    )r   r   s     r   handle_endtagzFlickr8kParser.handle_endtag"   s%    '>>!DMMM >r   datac                    | j         r|dk    r	d | _        d S | j        dk    rp|                    d          d         }t          j                            | j        |dz             }t          j        |          d         }|| _        g | j	        |<   d S | j        dk    rD| j        r?| j        }| j	        |         
                    |                                           d S d S d S d S )NzImage Not Founda/z_*.jpgr   li)r   r   r   splitospathjoinr   globr   appendstrip)r   r$   img_ids      r   handle_datazFlickr8kParser.handle_data(   s    = 	>(((#'   !S((C,di(1BCC6**1-#) +- (((!T))d.>)) (//

=====	> 	> *)))r   )__name__
__module____qualname____doc__r	   strr   r   listtupler   r!   r#   r2   __classcell__r   s   @r   r   r      s        LL/U39- /$ / / / / / /!3 !tE#x}:L4M/N !SW ! ! ! !" " " " " "> > > > > > > > > >r   r   c                        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	Zd
edee
e
f         fdZdefdZ x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                    t                                          |||           t          j                            |          | _        t          | j                  }t          | j                  5 }|	                    |
                                           d d d            n# 1 swxY w Y   |j        | _        t          t          | j                                                            | _        || _        d S )Nr?   r@   )r   r   r+   r,   
expanduserr>   r   r   openfeedreadr   r8   sortedkeysidsrA   )	r   r   r>   r?   r@   rA   parserfhr   s	           r   r   zFlickr8k.__init__F   s    	EUVVV**844  	**$-   	#BKK		"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#!-t/44667788s   2(B&&B*-B*indexc                     | j         |         }|                     |          }| j        |                     |          }| j        |         }| j        |                     |          }||fS z
        Args:
            index (int): Index

        Returns:
            tuple: Tuple (image, target). target is a list of captions for the image.
        )rJ   rA   r?   r   r@   )r   rM   r1   imgtargets        r   __getitem__zFlickr8k.__getitem__Z   sp     % kk&!!>%..%%C !&) ,**622FF{r   c                 *    t          | j                  S NlenrJ   r   s    r   __len__zFlickr8k.__len__p       48}}r   )r3   r4   r5   r6   r   r	   r7   r   r   r   r   r   intr9   rR   rX   r:   r;   s   @r   r=   r=   7   s         $ )-/3'5 CI  H%	
 #8, #$ 
     ( sCx    ,        r   r=   c                        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	Z	d
e
deeef         fdZde
fdZ x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   r>   r?   r@   rA   r   c                 P   t                                          |||           t          j                            |          | _        t          t                    | _        t          | j                  5 }|D ]T}|
                                                    d          \  }}	| j        |d d                                      |	           U	 d d d            n# 1 swxY w Y   t          t          | j                                                            | _        || _        d S )NrC   	r(   )r   r   r+   r,   rD   r>   r   r8   r   rE   r0   r*   r/   rH   rI   rJ   rA   )r   r   r>   r?   r@   rA   rL   liner1   captionr   s             r   r   zFlickr30k.__init__   s=    	EUVVV**844 't,,$-   	>B > >"&**,,"4"4T":": -44W====>	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>
 t/44667788s   7ACC #C rM   c                 "   | j         |         }t          j                            | j        |          }|                     |          }| j        |                     |          }| j        |         }| j        |                     |          }||fS rO   )	rJ   r+   r,   r-   r   rA   r?   r   r@   )r   rM   r1   filenamerP   rQ   s         r   rR   zFlickr30k.__getitem__   s     % 7<<	622kk(##>%..%%C !&) ,**622FF{r   c                 *    t          | j                  S rT   rU   rW   s    r   rX   zFlickr30k.__len__   rY   r   )r3   r4   r5   r6   r   r7   r   r   r   r   rZ   r9   rR   rX   r:   r;   s   @r   r\   r\   t   s         $ )-/3'5   H%	
 #8, #$ 
     * sCx    .        r   r\   )r.   r+   collectionsr   html.parserr   pathlibr   typingr   r   r   r	   folderr   visionr   r   r=   r\    r   r   <module>rk      s    				 # # # # # # " " " " " "       1 1 1 1 1 1 1 1 1 1 1 1 " " " " " " ! ! ! ! ! !(> (> (> (> (>Z (> (> (>V: : : : :} : : :z< < < < < < < < < <r   