
    Yhp                     z    d dl 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mZmZ ddlmZ  G d d	e          ZdS )
    N)Path)AnyCallableOptionalUnion   )default_loader)check_integritydownload_and_extract_archivedownload_url)VisionDatasetc                        e Zd ZdZdZdZdZdddefdee	e
f         dee         d	ee         d
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defdZddZ xZS )SBUa  `SBU Captioned Photo <http://www.cs.virginia.edu/~vicente/sbucaptions/>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where tarball
            ``SBUCaptionedPhotoDataset.tar.gz`` exists.
        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.
        download (bool, optional): If True, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.
        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.
    zHhttps://www.cs.rice.edu/~vo9/sbucaptions/SBUCaptionedPhotoDataset.tar.gzzSBUCaptionedPhotoDataset.tar.gz 9aec147b3488753cf758b4d493422285NTroot	transformtarget_transformdownloadloaderreturnc                 d   t                                          |||           || _        |r|                                  |                                 st          d          g | _        g | _        t          j	        
                    | j        dd          }t          j	        
                    | j        dd          }t          t          |          t          |                    D ]\  }}	|                                }
t          j	                            |
          }t          j	        
                    | j        d|          }t          j	                            |          rH|	                                }| j                            |           | j                            |           d S )N)r   r   zHDataset not found or corrupted. You can use download=True to download itdataset$SBU_captioned_photo_dataset_urls.txtz(SBU_captioned_photo_dataset_captions.txt)super__init__r   r   _check_integrityRuntimeErrorphotoscaptionsospathjoinr   zipopenrstripbasenameexistsappend)selfr   r   r   r   r   file1file2line1line2urlphotofilenamecaption	__class__s                 j/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/datasets/sbu.pyr   zSBU.__init__!   sg    	EUVVV 	MMOOO$$&& 	kijjj TY	3YZZTY	3]^^UT%[[99 	. 	.LE5,,..CG$$S))Ew||DIy%@@Hw~~h'' .,,..""5)))$$W---	. 	.    indexc                     t           j                            | j        d| j        |                   }|                     |          }| j        |                     |          }| j        |         }| j        |                     |          }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is a caption for the photo.
        r   )	r    r!   r"   r   r   r   r   r   r   )r)   r5   r0   imgtargets        r3   __getitem__zSBU.__getitem__B   s     7<<	9dk%6HIIkk(##>%..%%Cu% ,**622FF{r4   c                 *    t          | j                  S )z$The number of photos in the dataset.)lenr   )r)   s    r3   __len__zSBU.__len__U   s    4;r4   c                     | j         }t          j                            || j                  }t          || j                  sdS dS )z1Check the md5 checksum of the downloaded tarball.FT)r   r    r!   r"   r0   r
   md5_checksum)r)   r   fpaths      r3   r   zSBU._check_integrityY   s?    yT4=11ud&788 	5tr4   c           	         |                                  rdS t          | j        | j        | j        | j        | j                   t          t          j        	                    | j        dd                    5 }|D ]Z}|
                                }	 t          |t          j        	                    | j        d                     K# t          $ r Y Ww xY w	 ddd           dS # 1 swxY w Y   dS )zEDownload and extract the tarball, and download each individual photo.Nr   r   )r   r   r.   r   r0   r>   r$   r    r!   r"   r%   r   OSError)r)   fhliner.   s       r3   r   zSBU.downloada   s3      "" 	F$TXty$)T]TXTefff "',,ty)5[\\]] 	ac  kkmm bgll49i&H&HIIII    D		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s6   6C"3CC"
CC"CC""C&)C&)r   N)__name__
__module____qualname____doc__r.   r0   r>   r	   r   strr   r   r   boolr   r   inttupler9   r<   r   r   __classcell__)r2   s   @r3   r   r      s9        " UC0H5L
 )-/3'5. .CI. H%. #8,	.
 . #$. 
. . . . . .B sCx    &         $           r4   r   )r    pathlibr   typingr   r   r   r   folderr	   utilsr
   r   r   visionr   r    r4   r3   <module>rS      s    				       1 1 1 1 1 1 1 1 1 1 1 1 " " " " " " N N N N N N N N N N ! ! ! ! ! !g g g g g- g g g g gr4   