a
    PSic                     @   s   d dl Z d dlmZmZmZmZmZmZ d dlZ	d dl
Z
d dlmZ ddlmZ ddlmZ G dd deZeeee
jd	d
dZeee
jdddZeee
jdddZdS )    N)AnyCallableListOptionalTupleUnion)Image   )download_url)VisionDatasetc                       s  e Zd ZdZg dg dg dg dg dg ddZd	d
dd	d
ddZdddddddZdddddddZdZdZ	dZ
d0eeeee edd fddZeeejeeeejf f d d!d"Zed#d$d%Zed#d&d'Zed#d(d)Zdd#d*d+Zdd#d,d-Zed#d.d/Z  ZS )1	PhotoToura  `Multi-view Stereo Correspondence <http://matthewalunbrown.com/patchdata/patchdata.html>`_ Dataset.

    .. note::

        We only provide the newer version of the dataset, since the authors state that it

            is more suitable for training descriptors based on difference of Gaussian, or Harris corners, as the
            patches are centred on real interest point detections, rather than being projections of 3D points as is the
            case in the old dataset.

        The original dataset is available under http://phototour.cs.washington.edu/patches/default.htm.


    Args:
        root (string): Root directory where images are.
        name (string): Name of the dataset to load.
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version.
        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.

    )z:http://matthewalunbrown.com/patchdata/notredame_harris.zipznotredame_harris.zipZ 69f8c90f78e171349abdf0307afefe4d)z9http://matthewalunbrown.com/patchdata/yosemite_harris.zipzyosemite_harris.zipZ a73253d1c6fbd3ba2613c45065c00d46)z8http://matthewalunbrown.com/patchdata/liberty_harris.zipzliberty_harris.zipZ c731fcfb3abb4091110d0ae8c7ba182c)z,http://icvl.ee.ic.ac.uk/vbalnt/notredame.zipznotredame.zipZ 509eda8535847b8c0a90bbb210c83484)z+http://icvl.ee.ic.ac.uk/vbalnt/yosemite.zipzyosemite.zipZ 533b2e8eb7ede31be40abc317b2fd4f0)z*http://icvl.ee.ic.ac.uk/vbalnt/liberty.zipzliberty.zipZ fdd9152f138ea5ef2091746689176414)notredame_harrisyosemite_harrisliberty_harris	notredameyosemitelibertyg^)?gqh ?gOe?)r   r   r   r   r   r   g$?g48E?g?i$ i	 i, i i` i )r   r   r   r   r   r   bmpzinfo.txtzm50_100000_100000_0.txtTNF)rootnametrain	transformdownloadreturnc                    s   t  j||d || _tj| j|| _tj| j| d| _tj| j| d| _	|| _
| j| | _| j| | _|r|   |  s|   t| j	\| _| _| _d S )N)r   z.zipz.pt)super__init__r   ospathjoinr   data_dir	data_down	data_filer   meansmeanstdsstdr   _check_datafile_existscachetorchloaddatalabelsmatches)selfr   r   r   r   r   	__class__ Z/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/datasets/phototour.pyr   Y   s    zPhotoTour.__init__)indexr   c                 C   s|   | j r(| j| }| jdur$| |}|S | j| }| j|d  | j|d   }}| jdurn| |}| |}|||d fS )zs
        Args:
            index (int): Index

        Returns:
            tuple: (data1, data2, matches)
        Nr   r	      )r   r*   r   r,   )r-   r2   r*   mZdata1Zdata2r0   r0   r1   __getitem__o   s    






zPhotoTour.__getitem__)r   c                 C   s   t | jr| jn| jS N)lenr   r*   r,   r-   r0   r0   r1   __len__   s    zPhotoTour.__len__c                 C   s   t j| jS r6   )r   r   existsr!   r8   r0   r0   r1   r&      s    z PhotoTour._check_datafile_existsc                 C   s   t j| jS r6   )r   r   r:   r   r8   r0   r0   r1   _check_downloaded   s    zPhotoTour._check_downloadedc                 C   s   |   rtd| j  d S |  s| j| j d }| j| j d }| j| j d }tj| j	|}t
|| j	|| td| j d dd l}||d}|| j W d    n1 s0    Y  t| d S )Nz# Found cached data r   r	   r3   z# Extracting data 
r)r&   printr!   r;   urlsr   r   r   r   r   r
   r    zipfileZipFile
extractallr   unlink)r-   urlfilenamemd5fpathr@   zr0   r0   r1   r      s    *zPhotoTour.downloadc                 C   s~   t d| j  t| j| j| j| j t| j| jt	| j| j
f}t| jd}t|| W d    n1 sp0    Y  d S )Nz# Caching data wb)r>   r!   read_image_filer   	image_extlensr   read_info_file	info_fileread_matches_filesmatches_filesopenr(   save)r-   datasetfr0   r0   r1   r'      s    zPhotoTour.cachec                 C   s   | j du rdnd}d| S )NTTrainTestzSplit: )r   )r-   splitr0   r0   r1   
extra_repr   s    zPhotoTour.extra_repr)TNF)__name__
__module____qualname____doc__r?   r"   r$   rL   rK   rN   rP   strboolr   r   r   intr   r(   Tensorr   r   r5   r9   r&   r;   r   r'   rX   __classcell__r0   r0   r.   r1   r      sX   		 &r   )r   rK   nr   c              
   C   s   t j tjddd}tttt ddd}g }|| |}|D ]^}t |}td|jdD ]@}	td|jdD ],}
|	|
|	|
d |	d f}|
|| qjqXq<tt|d	| S )
z&Return a Tensor containing the patches)_imgr   c                 S   s   t j|  t jdddS )z(Convert PIL image type to numpy 2D array)dtype@   )nparraygetdatauint8reshape)rc   r0   r0   r1   	PIL2array   s    z"read_image_file.<locals>.PIL2array)	_data_dir
_image_extr   c                 S   s:   g }t | D ]"}||r|t j| | qt|S )zFReturn a list with the file names of the images containing the patches)r   listdirendswithappendr   r   sorted)rl   rm   filesZfile_dirr0   r0   r1   
find_files   s
    
z#read_image_file.<locals>.find_filesr   re   N)r   rf   ndarrayr]   r   rQ   rangeheightwidthcroprp   r(   
ByteTensorrg   )r   rK   rb   rk   rs   patches
list_filesrG   imgyxpatchr0   r0   r1   rJ      s    	

rJ   )r   rN   r   c                 C   sJ   t tj| |}dd |D }W d   n1 s60    Y  t|S )zjReturn a Tensor containing the list of labels
    Read the file and keep only the ID of the 3D point.
    c                 S   s   g | ]}t | d  qS )r   )r_   rW   ).0liner0   r0   r1   
<listcomp>       z"read_info_file.<locals>.<listcomp>N)rQ   r   r   r   r(   
LongTensor)r   rN   rT   r+   r0   r0   r1   rM      s    ,rM   )r   matches_filer   c              
   C   s   g }t tj| |R}|D ]<}| }|t|d t|d t|d |d kg qW d   n1 sn0    Y  t|S )zReturn a Tensor containing the ground truth matches
    Read the file and keep only 3D point ID.
    Matches are represented with a 1, non matches with a 0.
    r      r	      N)	rQ   r   r   r   rW   rp   r_   r(   r   )r   r   r,   rT   r   
line_splitr0   r0   r1   rO      s    PrO   )r   typingr   r   r   r   r   r   numpyrf   r(   PILr   utilsr
   visionr   r   r]   r_   r`   rJ   rM   rO   r0   r0   r0   r1   <module>   s     *	