
    QЦi                     v    S SK r S SKJr  S SKJrJrJrJrJr  S SK	J
r
  SSKJrJrJr  SSKJr   " S S	\5      rg)
    N)Path)AnyCallableOptionalTupleUnion)Image   )check_integritydownload_and_extract_archivedownload_url)VisionDatasetc                      ^  \ rS rSrSrSrSrSr   SS\\	\
4   S\\   S	\\   S
\SS4
U 4S jjjrS\S\\\4   4S jrS\4S jrS\4S jrSS jrSrU =r$ )SBU   a  `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
        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.
zHhttps://www.cs.rice.edu/~vo9/sbucaptions/SBUCaptionedPhotoDataset.tar.gzzSBUCaptionedPhotoDataset.tar.gz 9aec147b3488753cf758b4d493422285Nroot	transformtarget_transformdownloadreturnc                 2  > [         TU ]  XUS9  U(       a  U R                  5         U R                  5       (       d  [	        S5      e/ U l        / U l        [        R                  R                  U R                  SS5      n[        R                  R                  U R                  SS5      n[        [        U5      [        U5      5       H  u  pxUR                  5       n	[        R                  R                  U	5      n
[        R                  R                  U R                  SU
5      n[        R                  R                  U5      (       d  M  UR                  5       nU R
                  R!                  U
5        U R                  R!                  U5        M     g )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   _check_integrityRuntimeErrorphotoscaptionsospathjoinr   zipopenrstripbasenameexistsappend)selfr   r   r   r   file1file2line1line2urlphotofilenamecaption	__class__s                W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torchvision/datasets/sbu.pyr   SBU.__init__   s    	EUVMMO$$&&ijj TYY	3YZTYY	3]^UT%[9LE,,.CGG$$S)Eww||DIIy%@Hww~~h'',,.""5)$$W- :    indexc                 X   [         R                  R                  U R                  SU R                  U   5      n[
        R                  " U5      R                  S5      nU R                  b  U R                  U5      nU R                  U   nU R                  b  U R                  U5      nX44$ )zl
Args:
    index (int): Index

Returns:
    tuple: (image, target) where target is a caption for the photo.
r   RGB)r!   r"   r#   r   r   r	   r%   convertr   r    r   )r*   r7   r1   imgtargets        r4   __getitem__SBU.__getitem__=   s     77<<		9dkk%6HIjj"**51>>%..%Cu%  ,**62F{r6   c                 ,    [        U R                  5      $ )z$The number of photos in the dataset.)lenr   )r*   s    r4   __len__SBU.__len__P   s    4;;r6   c                     U R                   n[        R                  R                  XR                  5      n[        X R                  5      (       d  gg)z1Check the md5 checksum of the downloaded tarball.FT)r   r!   r"   r#   r1   r   md5_checksum)r*   r   fpaths      r4   r   SBU._check_integrityT   s8    yyT==1u&7&788r6   c           	         U R                  5       (       a  g[        U R                  U R                  U R                  U R                  U R
                  5        [        [        R                  R                  U R                  SS5      5       nU HH  nUR                  5       n [        U[        R                  R                  U R                  S5      5        MJ     SSS5        g! [         a     Mb  f = f! , (       d  f       g= f)zEDownload and extract the tarball, and download each individual photo.Nr   r   )r   r   r/   r   r1   rD   r%   r!   r"   r#   r&   r   OSError)r*   fhliner/   s       r4   r   SBU.download\   s       ""$TXXtyy$))T]]TXTeTef "'',,tyy)5[\]ackkm bggll499i&HI  ^]
    ^]s0   C5#4C$C5$
C2.C51C22C55
D)r    r   )NNT)r   N)__name__
__module____qualname____firstlineno____doc__r/   r1   rD   r   strr   r   r   boolr   intr   r   r=   rA   r   r   __static_attributes____classcell__)r3   s   @r4   r   r      s     UC0H5L
 )-/3.CI. H%. #8,	.
 . 
. .> sCx &   $  r6   r   )r!   pathlibr   typingr   r   r   r   r   PILr	   utilsr   r   r   visionr   r    r6   r4   <module>r\      s+    	  8 8  N N !b- br6   