
    kh-                         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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y)    N)defaultdict)
HTMLParser)Path)AnyCallableDictListOptionalTuple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                 h    t         |           || _        i | _        d| _        d | _        d | _        y )NF)super__init__r   annotationsin_tablecurrent_tagcurrent_img)selfr   	__class__s     W/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/flickr.pyr   zFlickr8kParser.__init__   s8    	 24 *.*.    tagattrsc                 ,    || _         |dk(  rd| _        y y )NtableTr   r   )r   r   r    s      r   handle_starttagzFlickr8kParser.handle_starttag   s    '> DM r   c                 ,    d | _         |dk(  rd| _        y y )Nr"   Fr#   )r   r   s     r   handle_endtagzFlickr8kParser.handle_endtag"   s    '>!DM r   datac                    | j                   r|dk(  rd | _        y | j                  dk(  rp|j                  d      d   }t        j
                  j                  | j                  |dz         }t        j                  |      d   }|| _        g | j                  |<   y | j                  dk(  rF| j                  r9| j                  }| j                  |   j                  |j                                y y y y )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,dii(1BC6*1-#) +-  (!!T)d.>.>))  (//

= /?) r   )__name__
__module____qualname____doc__r   strr   r   r	   r   r
   r$   r&   r5   __classcell__r   s   @r   r   r      sv    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                  j	                  |      | _        t        | j                        }t        | j
                        5 }|j                  |j                                d d d        |j                  | _        t        t        | j                  j                                     | _        || _        y # 1 sw Y   SxY w)Nr@   rA   )r   r   r.   r/   
expanduserr?   r   r   openfeedreadr   listsortedkeysidsrB   )	r   r   r?   r@   rA   rB   parserfhr   s	           r   r   zFlickr8k.__init__F   s     	EUV**84  		*$--  	#BKK	"	#!--t//44678	# 	#s   " CCindexc                     | j                   |   }| j                  |      }| j                  | 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.
        )rL   rB   r@   r   rA   )r   rO   r4   imgtargets        r   __getitem__zFlickr8k.__getitem__Z   sq     % kk&!>>%..%C !!&)  ,**62FF{r   c                 ,    t        | j                        S NlenrL   r   s    r   __len__zFlickr8k.__len__p       488}r   )r6   r7   r8   r9   r   r   r:   r   r
   r   r   r   intr   rT   rZ   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@   rA   rB   r   c                    t         
|   |||       t        j                  j	                  |      | _        t        t              | _        t        | j
                        5 }|D ]E  }|j                         j                  d      \  }}	| j                  |d d    j                  |	       G 	 d d d        t        t        | j                  j                                     | _        || _        y # 1 sw Y   BxY w)NrD   	r+   )r   r   r.   r/   rE   r?   r   rI   r   rF   r3   r-   r2   rJ   rK   rL   rB   )r   r   r?   r@   rA   rB   rN   liner4   captionr   s             r   r   zFlickr30k.__init__   s     	EUV**84 't,$--  	>B >"&**,"4"4T":  -44W=>	>
 t//44678	> 	>s   !AC..C7rO   c                 0   | j                   |   }t        j                  j                  | j                  |      }| j                  |      }| j                  | j                  |      }| j                  |   }| j                  | j                  |      }||fS rQ   )	rL   r.   r/   r0   r   rB   r@   r   rA   )r   rO   r4   filenamerR   rS   s         r   rT   zFlickr30k.__getitem__   s     % 77<<		62kk(#>>%..%C !!&)  ,**62FF{r   c                 ,    t        | j                        S rV   rW   rY   s    r   rZ   zFlickr30k.__len__   r[   r   )r6   r7   r8   r9   r   r:   r
   r   r   r   r\   r   rT   rZ   r;   r<   s   @r   r^   r^   t   s    $ )-/3'5  H%	
 #8, #$ 
* sCx . r   r^   )r1   r.   collectionsr   html.parserr   pathlibr   typingr   r   r   r	   r
   r   r   folderr   visionr   r   r>   r^    r   r   <module>rm      sI     	 # "  D D D " !(>Z (>V:} :z< <r   