
    ڧge                         d dl Z d dlm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	e          Zd
edededej        fdZd
ededej        fdZd
ededej        fdZdS )    N)Path)AnyCallableListOptionalTupleUnion)Image   )download_url)VisionDatasetc                   R    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
	 	 	 d,deeef         deded ee         d!ed"df fd#Zd$ed"eej        eeeej        f         f         fd%Zd"efd&Zd"efd'Zd"efd(Zd-d)Zd-d*Zd"efd+Z xZS ).	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 (str or ``pathlib.Path``): Root directory where images are.
        name (string): Name of the dataset to load.
        transform (callable, optional): A function/transform that takes in a 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.zip 69f8c90f78e171349abdf0307afefe4d)z9http://matthewalunbrown.com/patchdata/yosemite_harris.zipzyosemite_harris.zip a73253d1c6fbd3ba2613c45065c00d46)z8http://matthewalunbrown.com/patchdata/liberty_harris.zipzliberty_harris.zip c731fcfb3abb4091110d0ae8c7ba182c)z,http://icvl.ee.ic.ac.uk/vbalnt/notredame.zipznotredame.zip 509eda8535847b8c0a90bbb210c83484)z+http://icvl.ee.ic.ac.uk/vbalnt/yosemite.zipzyosemite.zip 533b2e8eb7ede31be40abc317b2fd4f0)z*http://icvl.ee.ic.ac.uk/vbalnt/liberty.zipzliberty.zip 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                    t                                          ||           || _        t          j                            | j        |          | _        t          j                            | j        | d          | _        t          j                            | j        | d          | _	        || _
        | j        |         | _        | j        |         | _        |r|                                  |                                 s|                                  t%          j        | j	        d          \  | _        | _        | _        d S )N)r    z.zipz.ptT)weights_only)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__s         Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/phototour.pyr&   zPhotoTour.__init__Z   s    	333	TY55diD??diD>>
Jt$	9T? 	MMOOO**,, 	JJLLL 05z$.W[/\/\/\,	4;    indexc                 D   | j         r+| j        |         }| j        |                     |          }|S | j        |         }| j        |d                  | j        |d                  }}| j        *|                     |          }|                     |          }|||d         fS )zs
        Args:
            index (int): Index

        Returns:
            tuple: (data1, data2, matches)
        Nr   r      )r   r5   r    r7   )r8   r<   r5   mdata1data2s         r:   __getitem__zPhotoTour.__getitem__u   s     : 	9U#D~)~~d++KLy1	!A$u>%NN5))ENN5))EeQqT!!r;   c                 F    t          | j        r| j        n| j                  S N)lenr   r5   r7   r8   s    r:   __len__zPhotoTour.__len__   s    
<499===r;   c                 J    t           j                            | j                  S rD   )r'   r(   existsr,   rF   s    r:   r1   z PhotoTour._check_datafile_exists   s    w~~dn---r;   c                 J    t           j                            | j                  S rD   )r'   r(   rI   r*   rF   s    r:   _check_downloadedzPhotoTour._check_downloaded   s    w~~dm,,,r;   c                    |                                  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          5 }|                    | j                   d d d            n# 1 swxY w Y   t          j        |           d S d S )Nz# Found cached data r   r   r>   z# Extracting data 
r)r1   printr,   rK   urlsr   r'   r(   r)   r   r   r+   zipfileZipFile
extractallr*   unlink)r8   urlfilenamemd5fpathrQ   zs          r:   r!   zPhotoTour.download   so   &&(( 	999:::F%%'' 	)DI&q)Cy+A.H)DI&q)CGLLH55Edi37779t~999:::NNN,, ,T]+++, , , , , , , , , , , , , , , Ie!	 	s   8DD#&D#c                 v   t          d| j                    t          | j        | j        | j        | j                           t          | j        | j                  t          | j        | j
                  f}t          | j        d          5 }t          j        ||           d d d            d S # 1 swxY w Y   d S )Nz# Caching data wb)rO   r,   read_image_filer*   	image_extlensr   read_info_file	info_fileread_matches_filesmatches_filesopenr3   save)r8   datasetfs      r:   r2   zPhotoTour.cache   s    000111 DM4>49TY;OPP4=$.99t}d.@AA
 $.$'' 	#1Jw"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   B..B25B2c                 &    | j         du rdnd}d| S )NTTrainTestzSplit: )r   )r8   splits     r:   
extra_reprzPhotoTour.extra_repr   s%    :--6    r;   )TNF)r"   N)__name__
__module____qualname____doc__rP   r-   r/   r^   r]   r`   rb   r	   strr   boolr   r   r&   intr3   Tensorr   r   rB   rG   r1   rK   r!   r2   rk   __classcell__)r9   s   @r:   r   r      sJ        2
 
 


 
 


 
 


 
 

 xwwttt- D2 "!  E "!  D  !" D II-M (,] ]CI] ] 	]
 H%] ] 
] ] ] ] ] ]6" "u|U3U\CY=Z/Z)[ " " " "(> > > > >. . . . .-4 - - - -   .# # # #!C ! ! ! ! ! ! ! !r;   r   r*   r]   nr"   c           
      
   dt           j         dt          j        fd}dt          dt          dt          t                   fd}g } || |          }|D ]}t          j        |          }t          d|j        d          D ]X}	t          d|j        d          D ]?}
|	                    |
|	|
dz   |	dz   f          }|
                     ||                     @Yt          j        t          j        |d	|                             S )
z&Return a Tensor containing the patches_imgr"   c                     t          j        |                                 t           j                                      dd          S )z(Convert PIL image type to numpy 2D array)dtype@   )nparraygetdatauint8reshape)rw   s    r:   	PIL2arrayz"read_image_file.<locals>.PIL2array   s1    xbh777??BGGGr;   	_data_dir
_image_extc                     g }t          j        |           D ]J}|                    |          r3|                    t           j                            | |                     Kt          |          S )zFReturn a list with the file names of the images containing the patches)r'   listdirendswithappendr(   r)   sorted)r   r   filesfile_dirs       r:   
find_filesz#read_image_file.<locals>.find_files   sh    
9-- 	@ 	@H  ,, @RW\\)X>>???e}}r;   r   rz   N)r
   r{   ndarrayrp   r   rc   rangeheightwidthcropr   r3   
ByteTensorr|   )r*   r]   ru   r   r   patches
list_filesrX   imgyxpatchs               r:   r\   r\      s;   H H
 H H H Hc s tCy     GHi00J 1 1jq#*b)) 	1 	1A1ci,, 1 1!QBB!788yy//00001	1 BHWRaR[11222r;   r`   c                     t          t          j                            | |                    5 }d |D             }ddd           n# 1 swxY w Y   t	          j        |          S )zjReturn a Tensor containing the list of labels
    Read the file and keep only the ID of the 3D point.
    c                 \    g | ])}t          |                                d                    *S )r   )rr   rj   ).0lines     r:   
<listcomp>z"read_info_file.<locals>.<listcomp>   s,    5554#djjll1o&&555r;   N)rc   r'   r(   r)   r3   
LongTensor)r*   r`   rf   r6   s       r:   r_   r_      s     
bgll8Y//	0	0 6A5515556 6 6 6 6 6 6 6 6 6 6 6 6 6 6F###s   AAAmatches_filec                    g }t          t          j                            | |                    5 }|D ]q}|                                }|                    t          |d                   t          |d                   t          |d         |d         k              g           r	 ddd           n# 1 swxY w Y   t          j        |          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)	rc   r'   r(   r)   rj   r   rr   r3   r   )r*   r   r7   rf   r   
line_splits         r:   ra   ra      s   
 G	bgll8\22	3	3 jq 	j 	jDJNNC
1..JqM0B0BC
STYcdeYfHfDgDghiiii	jj j j j j j j j j j j j j j j G$$$s   A5B22B69B6)r'   pathlibr   typingr   r   r   r   r   r	   numpyr{   r3   PILr
   utilsr   visionr   r   rp   rr   rs   r\   r_   ra    r;   r:   <module>r      s`   				       > > > > > > > > > > > > > > > >                  ! ! ! ! ! !k! k! k! k! k! k! k! k!\3c 3c 3c 3el 3 3 3 38$S $S $U\ $ $ $ $
% 
%C 
%EL 
% 
% 
% 
% 
% 
%r;   