
    Yh                         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mZmZmZ ddlmZ  G d d	e          Zd
S )    join)Path)AnyCallableOptionalUnion)Image   )check_integritydownload_and_extract_archivelist_dir
list_files)VisionDatasetc                        e Zd ZdZdZdZdddZ	 	 	 	 	 dd
eee	f         de
dee         dee         de
deeeee	f         gef                  ddf fdZdefdZdedeeef         fdZde
fdZddZdefdZ xZS )Omniglota  `Omniglot <https://github.com/brendenlake/omniglot>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``omniglot-py`` exists.
        background (bool, optional): If True, creates dataset from the "background" set, otherwise
            creates from the "evaluation" set. This terminology is defined by the authors.
        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 zip files from the internet and
            puts it in root directory. If the zip files are already downloaded, they are 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.
    zomniglot-pyzDhttps://raw.githubusercontent.com/brendenlake/omniglot/master/python 68d2efa1b9178cc56df9314c21c6e718 6b91aef0f799c5bb55b94e3f2daec811)images_backgroundimages_evaluationTNFroot
background	transformtarget_transformdownloadloaderreturnc                 Z    t                                          t          | j                  ||           | _        |r                                                                   st          d          t           j         	                                           _
        t           j
                   _        t           fd j        D             g            _         fdt           j                  D              _        t           j        g            _        | _        d S )N)r   r   zHDataset not found or corrupted. You can use download=True to download itc              3   v   K   | ]2fd t          t          j                            D             V  3dS )c                 0    g | ]}t          |          S  r   ).0cas     o/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/datasets/omniglot.py
<listcomp>z/Omniglot.__init__.<locals>.<genexpr>.<listcomp>;   s!    HHHQd1ajjHHH    N)r   r   target_folder)r"   r$   selfs    @r%   	<genexpr>z$Omniglot.__init__.<locals>.<genexpr>;   sN      bbQHHHH(40BA+F+F"G"GHHHbbbbbbr'   c           	      v    g | ]4\  }fd t          t          j        |          d          D             5S )c                     g | ]}|fS r!   r!   )r"   imageidxs     r%   r&   z0Omniglot.__init__.<locals>.<listcomp>.<listcomp>>   s    ___eeS\___r'   z.png)r   r   r(   )r"   	characterr.   r)   s     @r%   r&   z%Omniglot.__init__.<locals>.<listcomp>=   s[     "
 "
 "
Y `___z$t7I92U2UW]'^'^___"
 "
 "
r'   )super__init__r   folderr   r   _check_integrityRuntimeErrorr   _get_target_folderr(   r   
_alphabetssum_characters	enumerate_character_images_flat_character_imagesr   )r)   r   r   r   r   r   r   	__class__s   `      r%   r1   zOmniglot.__init__&   s.    	dDK00IXhiii$ 	MMOOO$$&& 	kijjj!$)T-D-D-F-FGG"4#566&)bbbbRVRabbbdf'
 '
"
 "
 "
 "
"+D,<"="="
 "
 "
 >AAWY[=\=\#r'   c                 *    t          | j                  S )N)lenr;   r)   s    r%   __len__zOmniglot.__len__D   s    4.///r'   indexc                 f   | j         |         \  }}t          | j        | j        |         |          }| j        )t          j        |d                              d          n|                     |          }| j        r|                     |          }| j	        r| 	                    |          }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target character class.
        Nr)modeL)
r;   r   r(   r8   r   r
   openconvertr   r   )r)   rA   
image_namecharacter_class
image_pathr-   s         r%   __getitem__zOmniglot.__getitem__G   s     '+&A%&H#
O$,d.>.OQ[\\
AEAT
:C00088===Z^ZeZefpZqZq> 	*NN5))E  	E"33ODDOo%%r'   c                     |                                  }t          t          | j        |dz             | j        |                   sdS dS )N.zipFT)r5   r   r   r   zips_md5)r)   zip_filenames     r%   r3   zOmniglot._check_integrity[   sH    ..00tDI|f/DEEt}UaGbcc 	5tr'   c                     |                                  rd S |                                 }|dz   }| j        dz   |z   }t          || j        || j        |                    d S )NrM   /)filenamemd5)r3   r5   download_url_prefixr   r   rN   )r)   rR   rO   urls       r%   r   zOmniglot.downloada   sq      "" 	F**,,&(&,|;$S$)lPTP]^fPghhhhhhr'   c                     | j         rdndS )Nr   r   )r   r?   s    r%   r5   zOmniglot._get_target_folderj   s    &*oN"";NNr'   )TNNFN)r   N)__name__
__module____qualname____doc__r2   rT   rN   r	   strr   boolr   r   r   r1   intr@   tuplerK   r3   r   r5   __classcell__)r<   s   @r%   r   r      s        & F`?? H  (,/3>B CI  H%	
 #8,  5d#3"4c"9:; 
     <0 0 0 0 0& &sCx & & & &($    i i i iOC O O O O O O O Or'   r   N)os.pathr   pathlibr   typingr   r   r   r	   PILr
   utilsr   r   r   r   visionr   r   r!   r'   r%   <module>rf      s                1 1 1 1 1 1 1 1 1 1 1 1       V V V V V V V V V V V V ! ! ! ! ! !`O `O `O `O `O} `O `O `O `O `Or'   