a
    PSic                     @   sl   d dl mZ d dl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)AnyCallableListOptionalTuple)Image   )download_and_extract_archivecheck_integritylist_dir
list_files)VisionDatasetc                       s   e Zd ZdZdZdZdddZdeee	e
 e	e
 edd
 fddZedddZeeeef dddZedddZddddZedddZ  ZS )Omniglotaq  `Omniglot <https://github.com/brendenlake/omniglot>`_ Dataset.

    Args:
        root (string): 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 an 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   Y/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/datasets/omniglot.py
<listcomp>6       z/Omniglot.__init__.<locals>.<genexpr>.<listcomp>N)r   r   target_folder)r   selfr   r   	<genexpr>6   r    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   9   r    z0Omniglot.__init__.<locals>.<listcomp>.<listcomp>z.png)r   r   r!   )r   	characterr"   r&   r   r   8   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__)r   c                 C   s
   t | jS )N)lenr2   r"   r   r   r   __len__>   s    zOmniglot.__len__)indexr   c                 C   s^   | j | \}}t| j| j| |}tj|ddd}| jrF| |}| jrV| |}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target character class.
        r)modeL)	r2   r   r!   r0   r	   openconvertr   r   )r#   r7   
image_nameZcharacter_classZ
image_pathr%   r   r   r   __getitem__A   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,   U   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 verifiedr?   /)filenamemd5)r,   printr.   download_url_prefixr   r   r@   )r#   rC   rA   urlr   r   r   r   [   s    zOmniglot.downloadc                 C   s   | j r
dS dS )Nr   r   )r   r"   r   r   r   r.   e   s    zOmniglot._get_target_folder)TNNF)__name__
__module____qualname____doc__r+   rF   r@   strboolr   r   r*   intr6   r   r   r>   r,   r   r.   __classcell__r   r   r3   r   r   
   s.       
r   N)os.pathr   typingr   r   r   r   r   PILr	   utilsr   r   r   r   visionr   r   r   r   r   r   <module>   s
   