o
    ^i                     @   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__ V/var/www/html/RAG/RAG_venv/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   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r9   r:   )r   r   r'   r(   
expanduserr8   r   r   openfeedreadr   r4   sortedkeysidsr;   )r   r   r8   r9   r:   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)rC   r;   r9   r   r:   )r   rF   r-   imgtargetr   r   r   __getitem__Z   s   






zFlickr8k.__getitem__c                 C   
   t | jS NlenrC   r   r   r   r   __len__p      
zFlickr8k.__len__)r/   r0   r1   r2   r
   r   r3   r   r   r   r   r   intr5   rJ   rP   r6   r   r   r   r   r7   7   s(    
r7   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   r8   r9   r:   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=   r8   r   r4   r   r>   r,   r&   r+   rA   rB   rC   r;   )
r   r   r8   r9   r:   r;   rE   liner-   captionr   r   r   r      s   

zFlickr30k.__init__rF   c                 C   s^   | j | }tj| j|}| |}| jdur| |}| j| }| jdur+| |}||fS rG   )	rC   r'   r(   r)   r   r;   r9   r   r:   )r   rF   r-   filenamerH   rI   r   r   r   rJ      s   






zFlickr30k.__getitem__c                 C   rK   rL   rM   rO   r   r   r   rP      rQ   zFlickr30k.__len__)r/   r0   r1   r2   r
   r3   r   r   r   r   rR   r5   rJ   rP   r6   r   r   r   r   rS   t   s(    rS   )r*   r'   collectionsr   html.parserr   pathlibr   typingr   r   r   r   folderr
   visionr   r   r7   rS   r   r   r   r   <module>   s    +=