
    h                         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y)    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                 h    t         |           || _        i | _        d| _        d | _        d | _        y )NF)super__init__r   annotationsin_tablecurrent_tagcurrent_img)selfr   	__class__s     Y/var/www/html/eduruby.in/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   listtupler   r!   r#   r2   __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?   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**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.
        )rJ   rA   r?   r   r@   )r   rM   r1   imgtargets        r   __getitem__zFlickr8k.__getitem__Z   sq     % kk&!>>%..%C !!&)  ,**62FF{r   c                 ,    t        | j                        S NlenrJ   r   s    r   __len__zFlickr8k.__len__p       488}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                    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)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**84 't,$--  	>B >"&**,"4"4T":  -44W=>	>
 t//44678	> 	>s   !AC..C7rM   c                 0   | j                   |   }t        j                  j                  | j                  |      }| j                  |      }| 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     % 77<<		62kk(#>>%..%C !!&)  ,**6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      sF     	 # "  1 1 " !(>Z (>V:} :z< <r   