o
    ôj7  ã                   @   s|   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
 d dlmZ ddlmZmZmZmZ ddlmZ G dd	„ d	eƒZd
S )é    ©Újoin)ÚPath)ÚAnyÚCallableÚListÚOptionalÚTupleÚUnion)ÚImageé   )Úcheck_integrityÚdownload_and_extract_archiveÚlist_dirÚ
list_files)ÚVisionDatasetc                       s´   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df‡ fdd„Zdefdd„Zdedeeef fdd„Zde
fdd„Zddd„Zdefdd„Z‡  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.
    zomniglot-pyzDhttps://raw.githubusercontent.com/brendenlake/omniglot/master/pythonZ 68d2efa1b9178cc56df9314c21c6e718Z 6b91aef0f799c5bb55b94e3f2daec811)Úimages_backgroundÚimages_evaluationTNFÚrootÚ
backgroundÚ	transformÚtarget_transformÚdownloadÚreturnc                    s¢   t ƒ jt|ˆ jƒ||d� |ˆ _|rˆ  ¡  ˆ  ¡ stdƒ‚tˆ jˆ  	¡ ƒˆ _
tˆ j
ƒˆ _t‡ fdd„ˆ jD ƒg ƒˆ _‡ fd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   s.   � | ]‰ ‡ fd d„t tˆjˆ ƒƒD ƒV  qdS )c                    s   g | ]}t ˆ |ƒ‘qS © r   )Ú.0Úc©Úar   ún/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/omniglot.pyÚ
<listcomp>7   s    z/Omniglot.__init__.<locals>.<genexpr>.<listcomp>N)r   r   Útarget_folder)r   ©Úselfr   r    Ú	<genexpr>7   s   €, z$Omniglot.__init__.<locals>.<genexpr>c                    s0   g | ]\‰ }‡ fd d„t tˆj|ƒdƒD ƒ‘qS )c                    s   g | ]}|ˆ f‘qS r   r   )r   Úimage©Úidxr   r    r!   :   s    z0Omniglot.__init__.<locals>.<listcomp>.<listcomp>z.png)r   r   r"   )r   Ú	characterr#   r'   r    r!   9   s    ÿÿz%Omniglot.__init__.<locals>.<listcomp>)ÚsuperÚ__init__r   Úfolderr   r   Ú_check_integrityÚRuntimeErrorr   Ú_get_target_folderr"   r   Z
_alphabetsÚsumÚ_charactersÚ	enumerateZ_character_imagesÚ_flat_character_images)r$   r   r   r   r   r   ©Ú	__class__r#   r    r+   #   s   ÿ
þzOmniglot.__init__c                 C   s
   t | jƒS )N)Úlenr3   r#   r   r   r    Ú__len__?   s   
zOmniglot.__len__Úindexc                 C   s^   | j | \}}t| j| j| |ƒ}tj|dd� d¡}| jr#|  |¡}| jr+|  |¡}||fS )z 
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target character class.
        Úr)ÚmodeÚL)	r3   r   r"   r1   r   ÚopenÚconvertr   r   )r$   r8   Ú
image_nameZcharacter_classÚ
image_pathr&   r   r   r    Ú__getitem__B   s   

zOmniglot.__getitem__c                 C   s,   |   ¡ }tt| j|d ƒ| j| ƒsdS dS )Nú.zipFT)r/   r   r   r   Úzips_md5)r$   Úzip_filenamer   r   r    r-   V   s   zOmniglot._check_integrityc                 C   sN   |   ¡ r
tdƒ d S |  ¡ }|d }| jd | }t|| j|| j| d� d S )Nz%Files already downloaded and verifiedrA   ú/)ÚfilenameÚmd5)r-   Úprintr/   Údownload_url_prefixr   r   rB   )r$   rE   rC   Úurlr   r   r    r   \   s   zOmniglot.downloadc                 C   s   | j rdS dS )Nr   r   )r   r#   r   r   r    r/   f   s   zOmniglot._get_target_folder)TNNF)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   rH   rB   r
   Ústrr   Úboolr   r   r+   Úintr7   r	   r   r@   r-   r   r/   Ú__classcell__r   r   r4   r    r      s:    þú
þýüûúù

r   N)Úos.pathr   Úpathlibr   Útypingr   r   r   r   r	   r
   ÚPILr   Úutilsr   r   r   r   Úvisionr   r   r   r   r   r    Ú<module>   s     