
    ڧgؿ                        d dl Z d dlZd dlZd dlZd dlZd dlmZmZ 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Zd dlmZ ddlmZmZmZ dd	lmZ eej        ej        eej                 ej        f         Zeej        ej        eej                 f         Zd
Z e j         ed          Z! G d dee          Z" G d de"          Z# G d de"          Z$ G d de"          Z% G d de"          Z& G d de"          Z' G d de"          Z( G d de"          Z) G d de"          Z* G d de"          Z+ G d  d!e"          Z,dS )"    N)ABCabstractmethod)glob)Path)CallablecastListOptionalTupleUnion)Image   )	_read_pfmdownload_and_extract_archiveverify_str_arg)VisionDataset )slice_channelsc                   P    e Zd ZdZdZddeeef         dee	         ddf fdZ
deeef         dej        fd	Z	 dd
edee         deeeee         f                  fdZededeeej                 eej                 f         fd            Zdedeeef         fdZdefdZ xZS )StereoMatchingDatasetz+Base interface for Stereo matching datasetsFNroot
transformsreturnc                 v    t                                          |           || _        g | _        g | _        dS )a}  
        Args:
            root(str): Root directory of the dataset.
            transforms(callable, optional): A function/transform that takes in Tuples of
                (images, disparities, valid_masks) and returns a transformed version of each of them.
                images is a Tuple of (``PIL.Image``, ``PIL.Image``)
                disparities is a Tuple of (``np.ndarray``, ``np.ndarray``) with shape (1, H, W)
                valid_masks is a Tuple of (``np.ndarray``, ``np.ndarray``) with shape (H, W)
                In some cases, when a dataset does not provide disparities, the ``disparities`` and
                ``valid_masks`` can be Tuples containing None values.
                For training splits generally the datasets provide a minimal guarantee of
                images: (``PIL.Image``, ``PIL.Image``)
                disparities: (``np.ndarray``, ``None``) with shape (1, H, W)
                Optionally, based on the dataset, it can return a ``mask`` as well:
                valid_masks: (``np.ndarray | None``, ``None``) with shape (H, W)
                For some test splits, the datasets provides outputs that look like:
                imgaes: (``PIL.Image``, ``PIL.Image``)
                disparities: (``None``, ``None``)
                Optionally, based on the dataset, it can return a ``mask`` as well:
                valid_masks: (``None``, ``None``)
        )r   N)super__init__r   _images_disparities)selfr   r   	__class__s      a/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/_stereo_matching.pyr   zStereoMatchingDataset.__init__   s<    , 	d###$    	file_pathc                 n    t          j        |          }|j        dk    r|                    d          }|S )NRGB)r   openmodeconvert)r   r#   imgs      r!   	_read_imgzStereoMatchingDataset._read_img:   s4    j##8u++e$$C
r"   paths_left_patternpaths_right_patternc                 ,   t          t          t          |                              }|r*t          t          t          |                              }nt          d |D                       }|st          d|           |st          d|           t	          |          t	          |          k    r6t          dt	          |           dt	          |           d| d| d	          t          d t          ||          D                       }|S )	Nc              3      K   | ]}d V  d S Nr   .0_s     r!   	<genexpr>z4StereoMatchingDataset._scan_pairs.<locals>.<genexpr>L   s"      88t888888r"   z0Could not find any files matching the patterns: zFound z left files but z# right files using:
 left pattern: z
right pattern: 
c              3   $   K   | ]\  }}||fV  d S r/   r   )r1   leftrights      r!   r3   z4StereoMatchingDataset._scan_pairs.<locals>.<genexpr>[   s*      SS{tUdE]SSSSSSr"   )listsortedr   FileNotFoundErrorlen
ValueErrorzip)r   r+   r,   
left_pathsright_pathspathss         r!   _scan_pairsz!StereoMatchingDataset._scan_pairs@   sL    &&8!9!9::;;
  	9vd+>&?&?@@AAKK88Z88888K 	m#$kWi$k$klll 	n#$lWj$l$lmmmz??c+....:Z : :#k:J:J : :!3: :"5: : :   SSc*k6R6RSSSSSr"   c                     d S r/   r   )r   r#   s     r!   _read_disparityz%StereoMatchingDataset._read_disparity^   s	     	r"   indexc                 R   |                      | j        |         d                   }|                      | j        |         d                   }|                     | j        |         d                   \  }}|                     | j        |         d                   \  }}||f}||f}	||f}
| j        |                     ||	|
          \  }}	}
| j        s|
d         6|d         |d         |	d         t          t          j        |
d                   fS |d         |d         |	d         fS )ao  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 3 or 4-tuple with ``(img_left, img_right, disparity, Optional[valid_mask])`` where ``valid_mask``
                can be a numpy boolean mask of shape (H, W) if the dataset provides a file
                indicating which disparity pixels are valid. The disparity is a numpy array of
                shape (1, H, W) and the images are PIL images. ``disparity`` is None for
                datasets on which for ``split="test"`` the authors did not provide annotations.
        r   r   )	r*   r   rC   r   r   _has_built_in_disparity_maskr   npndarray)r   rD   img_left	img_rightdsp_map_leftvalid_mask_leftdsp_map_rightvalid_mask_rightimgsdsp_mapsvalid_maskss              r!   __getitem__z!StereoMatchingDataset.__getitem__c   s0    >>$,u"5a"899NN4<#6q#9::	(,(<(<T=Nu=UVW=X(Y(Y%o*.*>*>t?PQV?WXY?Z*[*['')$ -0&(89?&
 h<<	 , 	1A0J7DGXa[$rz;q>2R2RRR7DGXa[00r"   c                 *    t          | j                  S r/   )r;   r   )r   s    r!   __len__zStereoMatchingDataset.__len__   s    4<   r"   r/   )__name__
__module____qualname____doc__rF   r   strr   r
   r   r   r   r*   r	   r   rA   r   rG   rH   rC   intT1T2rR   rT   __classcell__r    s   @r!   r   r      s~       55#(  U39- 8H;M Y]      85d#3      .2  &c] 
eC#&'	(	   <  x
7KXVXV`Ma7a1b    ^!1 !1r2v !1 !1 !1 !1F! ! ! ! ! ! ! ! !r"   r   c                        e Zd ZdZddeeef         dee         ddf fdZ	dede
ej        df         fdZd	edef fd
Z xZS )CarlaStereoaz  
    Carla simulator data linked in the `CREStereo github repo <https://github.com/megvii-research/CREStereo>`_.

    The dataset is expected to have the following structure: ::

        root
            carla-highres
                trainingF
                    scene1
                        img0.png
                        img1.png
                        disp0GT.pfm
                        disp1GT.pfm
                        calib.txt
                    scene2
                        img0.png
                        img1.png
                        disp0GT.pfm
                        disp1GT.pfm
                        calib.txt
                    ...

    Args:
        root (str or ``pathlib.Path``): Root directory where `carla-highres` is located.
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    Nr   r   r   c                    t                                          ||           t          |          dz  }t          |dz  dz  dz            }t          |dz  dz  dz            }|                     ||          }|| _        t          |dz  dz  dz            }t          |dz  dz  dz            }|                     ||          }|| _        d S )Nzcarla-highres	trainingF*im0.pngim1.pngdisp0GT.pfmzdisp1GT.pfmr   r   r   rY   rA   r   r   )
r   r   r   left_image_patternright_image_patternrO   left_disparity_patternright_disparity_patterndisparitiesr    s
            r!   r   zCarlaStereo.__init__   s    z***DzzO+ !3c!9I!EFF!$"4s":Y"FGG 24GHH!$TK%7#%=%M!N!N"%d[&83&>&N"O"O&&'=?VWW'r"   r#   c                 T    t          |          }t          j        |          }d }||fS r/   _read_pfm_filerG   absr   r#   disparity_map
valid_masks       r!   rC   zCarlaStereo._read_disparity   .    &y11}--
j((r"   rD   c                 l    t          t          t                                          |                    S a  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 3-tuple with ``(img_left, img_right, disparity)``.
            The disparity is a numpy array of shape (1, H, W) and the images are PIL images.
            If a ``valid_mask`` is generated within the ``transforms`` parameter,
            a 4-tuple with ``(img_left, img_right, disparity, valid_mask)`` is returned.
        r   r[   r   rR   r   rD   r    s     r!   rR   zCarlaStereo.__getitem__   '     B++E22333r"   r/   rU   rV   rW   rX   r   rY   r   r
   r   r   r   rG   rH   rC   rZ   r[   rR   r]   r^   s   @r!   r`   r`      s         6( (U39- (8H;M (Y] ( ( ( ( ( () )rz47G1H ) ) ) )4 4 4 4 4 4 4 4 4 4 4 4r"   r`   c            	            e Zd ZdZdZddeeef         dedee	         ddf fd	Z
d
edeeej                 df         fdZdedef fdZ xZS )Kitti2012Stereoa
  
    KITTI dataset from the `2012 stereo evaluation benchmark <http://www.cvlibs.net/datasets/kitti/eval_stereo_flow.php>`_.
    Uses the RGB images for consistency with KITTI 2015.

    The dataset is expected to have the following structure: ::

        root
            Kitti2012
                testing
                    colored_0
                        1_10.png
                        2_10.png
                        ...
                    colored_1
                        1_10.png
                        2_10.png
                        ...
                training
                    colored_0
                        1_10.png
                        2_10.png
                        ...
                    colored_1
                        1_10.png
                        2_10.png
                        ...
                    disp_noc
                        1.png
                        2.png
                        ...
                    calib

    Args:
        root (str or ``pathlib.Path``): Root directory where `Kitti2012` is located.
        split (string, optional): The dataset split of scenes, either "train" (default) or "test".
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    TtrainNr   splitr   r   c                    t                                          ||           t          |dd           t          |          dz  |dz   z  }t	          |dz  dz            }t	          |dz  dz            }|                     ||          | _        |d	k    r2t	          |d
z  dz            }|                     |d           | _        d S t          d | j        D                       | _        d S )Nr~   r}   testvalid_values	Kitti2012ing	colored_0z*_10.png	colored_1r}   disp_noc*.pngc              3      K   | ]}d V  dS NNNr   r0   s     r!   r3   z+Kitti2012Stereo.__init__.<locals>.<genexpr>  "      $H$Ha\$H$H$H$H$H$Hr"   	r   r   r   r   rY   rA   r   r   r8   )r   r   r~   r   left_img_patternright_img_patterndisparity_patternr    s          r!   r   zKitti2012Stereo.__init__   s    z***ug4EFFFFDzzK'55=9tk1J>??{ 2Z ?@@''(8:KLLG #D:$5$? @ @ $ 0 01BD I ID $$H$H4<$H$H$H H HDr"   r#   c                     |dS t          j        t          j        |                    dz  }|d d d d d f         }d }||fS Nr   g      p@rG   asarrayr   r&   rq   s       r!   rC   zKitti2012Stereo._read_disparity  S    :
5:i#8#899EA%dAAAqqqj1
j((r"   rD   c                 l    t          t          t                                          |                    S a  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 4-tuple with ``(img_left, img_right, disparity, valid_mask)``.
            The disparity is a numpy array of shape (1, H, W) and the images are PIL images.
            ``valid_mask`` is implicitly ``None`` if the ``transforms`` parameter does not
            generate a valid mask.
            Both ``disparity`` and ``valid_mask`` are ``None`` if the dataset split is test.
        rw   rx   s     r!   rR   zKitti2012Stereo.__getitem__  '     B++E22333r"   r}   NrU   rV   rW   rX   rF   r   rY   r   r
   r   r   r   rG   rH   rC   rZ   r[   rR   r]   r^   s   @r!   r|   r|      s        $ $L $( I IU39- Ic IQYZbQc Ios I I I I I I"	) 	)x
7KT7Q1R 	) 	) 	) 	)4 4 4 4 4 4 4 4 4 4 4 4r"   r|   c            	            e Zd ZdZdZddeeef         dedee	         ddf fd	Z
d
edeeej                 df         fdZdedef fdZ xZS )Kitti2015StereoaM  
    KITTI dataset from the `2015 stereo evaluation benchmark <http://www.cvlibs.net/datasets/kitti/eval_scene_flow.php>`_.

    The dataset is expected to have the following structure: ::

        root
            Kitti2015
                testing
                    image_2
                        img1.png
                        img2.png
                        ...
                    image_3
                        img1.png
                        img2.png
                        ...
                training
                    image_2
                        img1.png
                        img2.png
                        ...
                    image_3
                        img1.png
                        img2.png
                        ...
                    disp_occ_0
                        img1.png
                        img2.png
                        ...
                    disp_occ_1
                        img1.png
                        img2.png
                        ...
                    calib

    Args:
        root (str or ``pathlib.Path``): Root directory where `Kitti2015` is located.
        split (string, optional): The dataset split of scenes, either "train" (default) or "test".
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    Tr}   Nr   r~   r   r   c                 
   t                                          ||           t          |dd           t          |          dz  |dz   z  }t	          |dz  dz            }t	          |dz  dz            }|                     ||          | _        |d	k    rGt	          |d
z  dz            }t	          |dz  dz            }|                     ||          | _        d S t          d | j        D                       | _        d S )Nr~   r   r   	Kitti2015r   image_2r   image_3r}   
disp_occ_0
disp_occ_1c              3      K   | ]}d V  dS r   r   r0   s     r!   r3   z+Kitti2015Stereo.__init__.<locals>.<genexpr>Z  r   r"   r   	r   r   r~   r   r   r   rj   rk   r    s	           r!   r   zKitti2015Stereo.__init__K  s   z***ug4EFFFFDzzK'55=9ti/'9::y 07 :;;''(8:KLLG%()<w)F%G%G"&)$*=*G&H&H# $ 0 01GI` a aD $$H$H4<$H$H$H H HDr"   r#   c                     |dS t          j        t          j        |                    dz  }|d d d d d f         }d }||fS r   r   rq   s       r!   rC   zKitti2015Stereo._read_disparity\  r   r"   rD   c                 l    t          t          t                                          |                    S r   rw   rx   s     r!   rR   zKitti2015Stereo.__getitem__g  r   r"   r   r   r^   s   @r!   r   r     s        ' 'R $( I IU39- Ic IQYZbQc Ios I I I I I I"	) 	)x
7KT7Q1R 	) 	) 	) 	)4 4 4 4 4 4 4 4 4 4 4 4r"   r   c                   B    e Zd ZdZg dg dg ddZdZ	 	 	 	 	 ddeeef         dede	e         de
de	e         de
dd
f fdZdeeef         dej        f fdZdedeed         eej        ej        f         f         fdZdeeef         dd
fdZdedef fdZ xZS )Middlebury2014StereoaZ	  Publicly available scenes from the Middlebury dataset `2014 version <https://vision.middlebury.edu/stereo/data/scenes2014/>`.

    The dataset mostly follows the original format, without containing the ambient subdirectories.  : ::

        root
            Middlebury2014
                train
                    scene1-{perfect,imperfect}
                        calib.txt
                        im{0,1}.png
                        im1E.png
                        im1L.png
                        disp{0,1}.pfm
                        disp{0,1}-n.png
                        disp{0,1}-sd.pfm
                        disp{0,1}y.pfm
                    scene2-{perfect,imperfect}
                        calib.txt
                        im{0,1}.png
                        im1E.png
                        im1L.png
                        disp{0,1}.pfm
                        disp{0,1}-n.png
                        disp{0,1}-sd.pfm
                        disp{0,1}y.pfm
                    ...
                additional
                    scene1-{perfect,imperfect}
                        calib.txt
                        im{0,1}.png
                        im1E.png
                        im1L.png
                        disp{0,1}.pfm
                        disp{0,1}-n.png
                        disp{0,1}-sd.pfm
                        disp{0,1}y.pfm
                    ...
                test
                    scene1
                        calib.txt
                        im{0,1}.png
                    scene2
                        calib.txt
                        im{0,1}.png
                    ...

    Args:
        root (str or ``pathlib.Path``): Root directory of the Middleburry 2014 Dataset.
        split (string, optional): The dataset split of scenes, either "train" (default), "test", or "additional"
        use_ambient_views (boolean, optional): Whether to use different expose or lightning views when possible.
            The dataset samples with equal probability between ``[im1.png, im1E.png, im1L.png]``.
        calibration (string, optional): Whether or not to use the calibrated (default) or uncalibrated scenes.
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
        download (boolean, optional): Whether or not to download the dataset in the ``root`` directory.
    )

Adirondack	Jadeplant
MotorcyclePianoPipesPlayroom	PlaytableRecycleShelvesVintage)BackpackBicycle1Cable
Classroom1CouchFlowersMaskShopvacSticksStorageSword1Sword2Umbrella)PlantsClassroom2E
Classroom2	AustraliaDjembeLCrusadePCrusadeHoopsBicycle2	StaircaseNewkuba
AustraliaPDjembe
LivingroomComputer)r}   
additionalr   Tr}   perfectFNr   r~   calibrationuse_ambient_viewsr   downloadr   c                 R   t                                          ||           t          |dd           || _        |r(t          |dd           |dk    rt	          d          n|dk    rt	          d| d	| d
          |r|                     |           t          |          dz  }t          j        	                    ||z            st          d| d          | j        |         t          fdt          j        ||z            D                       st          d| d          dgdgdgddgd|         }|D ]}d|z   }	t          ||z  |	z  dz            }
t          ||z  |	z  dz            }| xj        |                     |
|          z  c_        |dk    r$t#          d | j        D                       | _        t          ||z  |	z  dz            }t          ||z  |	z  dz            }| xj        |                     ||          z  c_        || _        d S )Nr~   )r}   r   r   r   r   )r   	imperfectbothNr   zMSplit 'test' has only no calibration settings, please set `calibration=None`.zSplit 'zr' has calibration settings, however None was provided as an argument.
Setting calibration to 'perfect' for split 'zF'. Available calibration settings are: 'perfect', 'imperfect', 'both'.Middlebury2014zThe z7 directory was not found in the provided root directoryc              3   L   K   | ]}D ]}|                     |          V  d S r/   )
startswith)r1   scenessplit_sceness      r!   r3   z0Middlebury2014Stereo.__init__.<locals>.<genexpr>  s^       
 
 !	
 
  Q
 
 
 
 
 
 
r"   z:Provided root folder does not contain any scenes from the z split. z-perfectz
-imperfect)Nr   r   r   rc   rd   re   c              3      K   | ]}d V  dS r   r   r0   s     r!   r3   z0Middlebury2014Stereo.__init__.<locals>.<genexpr>  s"      (L(L!(L(L(L(L(L(Lr"   z	disp0.pfmz	disp1.pfm)r   r   r   r~   r<   _download_datasetr   ospathexistsr:   splitsanylistdirrY   r   rA   r8   r   r   )r   r   r~   r   r   r   r   calibrartion_suffixescalibration_suffixscene_patternr   r   left_dispartity_patternright_dispartity_patternr   r    s                 @r!   r   zMiddlebury2014Stereo.__init__  s    	z***ug4STTTT
 		;Djkkkk !pqqq   Se S SEJS S S  
  	)""4(((Dzz,,w~~dUl++ 	k#$i5$i$i$ijjj{5) 
 
 
 
 D5L11
 
 
 
 
 	q $$oaf$o$o$oppp $"|&.	!
 !

 ! #8 	i 	i"44M"4%<-#?)#KLL #D5L=$@9$L M MLLD,,-=?PQQQLL$((L(Lt|(L(L(L$L$L!!*-dUl].J[.X*Y*Y'+.te|m/Kk/Y+Z+Z(!!T%5%56MOg%h%hh!!!!2r"   r#   c                    t          |t                    st          |          }|j        dk    rp| j        ri|j        t          fddD                       }t          t          d |                    }|                    |           t          j	        |          }t                                          |          S )a  
        Function that reads either the original right image or an augmented view when ``use_ambient_views`` is True.
        When ``use_ambient_views`` is True, the dataset will return at random one of ``[im1.png, im1E.png, im1L.png]``
        as the right image.
        re   c              3   "   K   | ]	}|z  V  
d S r/   r   )r1   	view_name	base_paths     r!   r3   z1Middlebury2014Stereo._read_img.<locals>.<genexpr>0  s(      %f%f	i)&;%f%f%f%f%f%fr"   )zim1E.pngzim1L.pngc                 @    t           j                            |           S r/   )r   r   r   )ps    r!   <lambda>z0Middlebury2014Stereo._read_img.<locals>.<lambda>2  s    rw~~a7H7H r"   )
isinstancer   namer   parentr8   filterappendrandomchoicer   r*   )r   r#   ambient_file_pathsr   r    s      @r!   r*   zMiddlebury2014Stereo._read_img"  s     )T** 	(YI>Y&&4+A&!(I!%%f%f%f%fMe%f%f%f!f!f!%f-H-HJ\&]&]!^!^%%i000&899Iww  +++r"   r   c                     |dS t          |          }t          j        |          }d||t          j        k    <   |dk                        d          }||fS )Nr   r   )ro   rG   rp   infsqueezerq   s       r!   rC   z$Middlebury2014Stereo._read_disparity8  s]    :&y11}--12mrv-.#a'0033
j((r"   c           	      X   d}t                    dz  | j        }|dk    rw| j        |         D ]g}|z  }dD ]]}| d| }| d| d}t          d|            ||z                                  s#t          || dt          |          d	
           ^hd S t          j        dz             t          fd| j        d         D                       rd}	t          |	t                    d	           t          j
        t          dz                      D ]g\  }
}}|D ]^}dz  }t          |
          |z  }t          j        |d	           t          j        t          |          t          |                     _ht          j        t          dz                       d S d S )Nz8https://vision.middlebury.edu/stereo/data/scenes2014/zipr   r   )r   r   -/z.zipzDownloading T)urlfilenamedownload_rootremove_finishedc              3   J   K   | ]}|t          j        d z            vV  dS )r   N)r   r   )r1   r   r   s     r!   r3   z9Middlebury2014Stereo._download_dataset.<locals>.<genexpr>Z  s6      SS!1BJtf}555SSSSSSr"   zEhttps://vision.middlebury.edu/stereo/submit3/zip/MiddEval3-data-F.zip)r  r  r  zMiddEval3/testF)exist_ok	MiddEval3)r   r~   r   printr   r   rY   r   makedirsr   walkshutilmovermtree)r   r   base_url
split_namesplit_scene
split_rootr   
scene_name	scene_urltest_set_url	scene_dirscene_namesr2   r   scene_dst_dirscene_src_dirs    `              r!   r   z&Middlebury2014Stereo._download_datasetC  s$   MDzz,,Z
#{:6  !J.
#;  K$/!?!?+!?!?J#+ > >j > > >I444555&3;;== 4 )(2%8%8%8*-j//,0	     Kv&&&SSSSt{6?RSSSSS 7f -SQUYYhlmmmm13TDU=U9V9V1W1W L L-I{A!, L L(,v(,Y%(?MDAAAAC$6$6M8J8JKKKK	L c$"455666667 7r"   rD   c                 l    t          t          t                                          |                    S )az  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 4-tuple with ``(img_left, img_right, disparity, valid_mask)``.
            The disparity is a numpy array of shape (1, H, W) and the images are PIL images.
            ``valid_mask`` is implicitly ``None`` for `split=test`.
        r   r\   r   rR   rx   s     r!   rR   z Middlebury2014Stereo.__getitem__j  '     B++E22333r"   )r}   r   FNF)rU   rV   rW   rX   r   rF   r   rY   r   r
   boolr   r   r   r*   r   rG   rH   rC   r   rZ   r\   rR   r]   r^   s   @r!   r   r   w  s       6 6r
 
 

 
 

 
 
9- -F^ $( 
 %."')-?3 ?3CI?3 ?3 c]	?3
  ?3 X&?3 ?3 
?3 ?3 ?3 ?3 ?3 ?3B,5d#3 , , , , , , ,,	) 	)uZ7H%PRPZ\^\fPfJg7g1h 	) 	) 	) 	)%7eCI&6 %74 %7 %7 %7 %7N4 4 4 4 4 4 4 4 4 4 4 4r"   r   c                        e Zd ZdZdZ	 ddeeef         dee	         ddf fdZ
dedeej        df         fd	Zd
edef fdZ xZS )	CREStereoa  Synthetic dataset used in training the `CREStereo <https://arxiv.org/pdf/2203.11483.pdf>`_ architecture.
    Dataset details on the official paper `repo <https://github.com/megvii-research/CREStereo>`_.

    The dataset is expected to have the following structure: ::

        root
            CREStereo
                tree
                    img1_left.jpg
                    img1_right.jpg
                    img1_left.disp.jpg
                    img1_right.disp.jpg
                    img2_left.jpg
                    img2_right.jpg
                    img2_left.disp.jpg
                    img2_right.disp.jpg
                    ...
                shapenet
                    img1_left.jpg
                    img1_right.jpg
                    img1_left.disp.jpg
                    img1_right.disp.jpg
                    ...
                reflective
                    img1_left.jpg
                    img1_right.jpg
                    img1_left.disp.jpg
                    img1_right.disp.jpg
                    ...
                hole
                    img1_left.jpg
                    img1_right.jpg
                    img1_left.disp.jpg
                    img1_right.disp.jpg
                    ...

    Args:
        root (str): Root directory of the dataset.
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    TNr   r   r   c                    t                                          ||           t          |          dz  }g d}|D ]}t          ||z  dz            }t          ||z  dz            }|                     ||          }| xj        |z  c_        t          ||z  dz            }t          ||z  dz            }	|                     ||	          }
| xj        |
z  c_        d S )Nr  )shapenet
reflectivetreeholez
*_left.jpgz*_right.jpgz*_left.disp.pngz*_right.disp.pngrg   )r   r   r   dirsr   rh   ri   rO   rj   rk   rl   r    s              r!   r   zCREStereo.__init__  s   
 	z***DzzK'999 		- 		-A!$TAX%<!=!="%dQh&>"?"?##$68KLLDLLD LL%(4E)E%F%F"&)$(5G*G&H&H#**+ACZ[[K,		- 		-r"   r#   c                     t          j        t          j        |          t           j                  }|d d d d d f         dz  }d }||fS )Ndtypeg      @@rG   r   r   r&   float32rq   s       r!   rC   zCREStereo._read_disparity  sO    
5:i#8#8
KKK%dAAAqqqj1D8
j((r"   rD   c                 l    t          t          t                                          |                    S )a  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 4-tuple with ``(img_left, img_right, disparity, valid_mask)``.
            The disparity is a numpy array of shape (1, H, W) and the images are PIL images.
            ``valid_mask`` is implicitly ``None`` if the ``transforms`` parameter does not
            generate a valid mask.
        rw   rx   s     r!   rR   zCREStereo.__getitem__  ry   r"   r/   r   r^   s   @r!   r  r  x  s        ' 'R $( 
 *.- -CI- X&- 
	- - - - - -,) )rz47G1H ) ) ) )4 4 4 4 4 4 4 4 4 4 4 4r"   r  c            	            e Zd ZdZddeeef         dedee         ddf fdZ	d	ede
ej        df         fd
Zdedef fdZ xZS )FallingThingsStereoa  `FallingThings <https://research.nvidia.com/publication/2018-06_falling-things-synthetic-dataset-3d-object-detection-and-pose-estimation>`_ dataset.

    The dataset is expected to have the following structure: ::

        root
            FallingThings
                single
                    dir1
                        scene1
                            _object_settings.json
                            _camera_settings.json
                            image1.left.depth.png
                            image1.right.depth.png
                            image1.left.jpg
                            image1.right.jpg
                            image2.left.depth.png
                            image2.right.depth.png
                            image2.left.jpg
                            image2.right
                            ...
                        scene2
                    ...
                mixed
                    scene1
                        _object_settings.json
                        _camera_settings.json
                        image1.left.depth.png
                        image1.right.depth.png
                        image1.left.jpg
                        image1.right.jpg
                        image2.left.depth.png
                        image2.right.depth.png
                        image2.left.jpg
                        image2.right
                        ...
                    scene2
                    ...

    Args:
        root (str or ``pathlib.Path``): Root directory where FallingThings is located.
        variant (string): Which variant to use. Either "single", "mixed", or "both".
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    singleNr   variantr   r   c                    t                                          ||           t          |          dz  }t          |dd           dgdgddgd|         }t          d          dz  t          d          d}|D ]}t	          ||z  ||         z  d	z            }t	          ||z  ||         z  d
z            }| xj        |                     ||          z  c_        t	          ||z  ||         z  dz            }	t	          ||z  ||         z  dz            }
| xj        |                     |	|
          z  c_        d S )NFallingThingsr.  )r-  mixedr   r   r-  r1  rc   )r-  r1  z
*.left.jpgz*.right.jpgz*.left.depth.pngz*.right.depth.pngr   r   r   r   rY   r   rA   r   )r   r   r.  r   variantssplit_prefixr   r   r   rj   rk   r    s              r!   r   zFallingThingsStereo.__init__  s}   z***DzzO+w	8STTTT  jYw'
 
 	 3ii#o#YY
 

  	c 	cA"4!8l1o#=#LMM #D1H|A$>$N O OLLD,,-=?PQQQLL%(LO)CFX)X%Y%Y"&)$(\!_*DGZ*Z&[&[#!1!12HJa!b!bb	c 	cr"   r#   c                    t          j        t          j        |                    }t	          |          j        dz  }t          |d          5 }t          j        |          }|d         d         d         d         }d\  }}||z  |z  |                    t           j	                  z  }	|	d d d d d f         }	d }
|	|
fcd d d            S # 1 swxY w Y   d S )Nz_camera_settings.jsonrcamera_settingsr   intrinsic_settingsfx)   d   )
rG   r   r   r&   r   r   jsonloadastyper)  )r   r#   depthcamera_settings_pathf
intrinsicsfocalbaselinepixel_constantrr   rs   s              r!   rC   z#FallingThingsStereo._read_disparity  s   
5:i0011  $I58OO&,, 		-1J01!45IJ4PE'-$Hn%->%,,rzBZBZZM)$111*5MJ *,		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		- 		-s   A0CCCrD   c                 l    t          t          t                                          |                    S rv   rw   rx   s     r!   rR   zFallingThingsStereo.__getitem__)  ry   r"   )r-  Nrz   r^   s   @r!   r,  r,    s        * *Xc cU39- c cT\]eTf crv c c c c c c6- -rz47G1H - - - -"4 4 4 4 4 4 4 4 4 4 4 4r"   r,  c                        e Zd ZdZ	 	 	 ddeeef         dededee         d	df
 fd
Z	ded	e
ej        df         fdZded	ef fdZ xZS )SceneFlowStereoa  Dataset interface for `Scene Flow <https://lmb.informatik.uni-freiburg.de/resources/datasets/SceneFlowDatasets.en.html>`_ datasets.
    This interface provides access to the `FlyingThings3D, `Monkaa` and `Driving` datasets.

    The dataset is expected to have the following structure: ::

        root
            SceneFlow
                Monkaa
                    frames_cleanpass
                        scene1
                            left
                                img1.png
                                img2.png
                            right
                                img1.png
                                img2.png
                        scene2
                            left
                                img1.png
                                img2.png
                            right
                                img1.png
                                img2.png
                    frames_finalpass
                        scene1
                            left
                                img1.png
                                img2.png
                            right
                                img1.png
                                img2.png
                        ...
                        ...
                    disparity
                        scene1
                            left
                                img1.pfm
                                img2.pfm
                            right
                                img1.pfm
                                img2.pfm
                FlyingThings3D
                    ...
                    ...

    Args:
        root (str or ``pathlib.Path``): Root directory where SceneFlow is located.
        variant (string): Which dataset variant to user, "FlyingThings3D" (default), "Monkaa" or "Driving".
        pass_name (string): Which pass to use, "clean" (default), "final" or "both".
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.

    FlyingThings3DcleanNr   r.  	pass_namer   r   c                    t                                          ||           t          |          dz  }t          |dd           t          |dd           dgdgddgd|         }||z  }t          d	          t          d	          d	z  d	z  t          d	          d	z  d	z  d
}|D ]}t	          ||z  ||         z  dz  dz            }t	          ||z  ||         z  dz  dz            }	| xj        |                     ||	          z  c_        t	          |dz  ||         z  dz  dz            }
t	          |dz  ||         z  dz  dz            }| xj        |                     |
|          z  c_        d S )N	SceneFlowr.  )rI  DrivingMonkaar   rK  )rJ  finalr   frames_cleanpassframes_finalpassrc   )rO  rI  rN  r6   r   r7   	disparityz*.pfmr2  )r   r   r.  rK  r   passesprefix_directoriesr   rh   ri   rj   rk   r    s               r!   r   zSceneFlowStereo.__init__n  s    	z***DzzK'w	8_````y+<VWWWW ))()');<
 
 	 g~ 3ii"3ii#o3Cyy3,
 
  	c 	cA!$TAX0B70K%Kf%TW^%^!_!_"%dQh1CG1L&Lw&VY`&`"a"aLLD,,-?ATUUULL%();>PQX>Y)Y\b)bel)l%m%m"&)$*<?QRY?Z*Z]d*dgn*n&o&o#!1!12HJa!b!bb	c 	cr"   r#   c                 T    t          |          }t          j        |          }d }||fS r/   rn   rq   s       r!   rC   zSceneFlowStereo._read_disparity  rt   r"   rD   c                 l    t          t          t                                          |                    S rv   rw   rx   s     r!   rR   zSceneFlowStereo.__getitem__  ry   r"   )rI  rJ  Nrz   r^   s   @r!   rH  rH  8  s        3 3p ( )-#c #cCI#c #c 	#c
 X&#c 
#c #c #c #c #c #cJ) )rz47G1H ) ) ) )4 4 4 4 4 4 4 4 4 4 4 4r"   rH  c            	            e Zd ZdZdZddeeef         dedee	         ddf fd	Z
d
edeeef         fdZd
edeed         eej        ej        f         f         fdZdedef fdZ xZS )SintelStereoa  Sintel `Stereo Dataset <http://sintel.is.tue.mpg.de/stereo>`_.

    The dataset is expected to have the following structure: ::

        root
            Sintel
                training
                    final_left
                        scene1
                            img1.png
                            img2.png
                            ...
                        ...
                    final_right
                        scene2
                            img1.png
                            img2.png
                            ...
                        ...
                    disparities
                        scene1
                            img1.png
                            img2.png
                            ...
                        ...
                    occlusions
                        scene1
                            img1.png
                            img2.png
                            ...
                        ...
                    outofframe
                        scene1
                            img1.png
                            img2.png
                            ...
                        ...

    Args:
        root (str or ``pathlib.Path``): Root directory where Sintel Stereo is located.
        pass_name (string): The name of the pass to use, either "final", "clean" or "both".
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    TrP  Nr   rK  r   r   c                    t                                          ||           t          |dd           t          |          dz  }dgdgddgd|         }|D ]}t	          |dz  | dz  d	z  d
z            }t	          |dz  | dz  d	z  d
z            }| xj        |                     ||          z  c_        t	          |dz  dz  d	z  d
z            }| xj        |                     |d           z  c_        d S )NrK  )rP  rJ  r   r   SintelrP  rJ  training_leftrc   r   _rightrl   )r   r   r   r   rY   r   rA   r   )
r   r   rK  r   
pass_namesr   r   r   r   r    s
            r!   r   zSintelStereo.__init__  sF   z***y+<VWWWWDzzH$YYg&
 
 	
  	K 	KA"4*#4!{{{#BS#H7#RSS #D:$51$Ds$JW$T U ULLD,,-=?PQQQLL #D:$5$E$Kg$U V V!1!12CT!J!JJ	K 	Kr"   r#   c                    t          |          }|j        }|j        }|j        j        }t          |dz  |j        z  |z            }t          |dz  |j        z  |z            }t          j                            |          st          d| d          t          j                            |          st          d| d          ||fS )N
occlusions
outofframezOcclusion mask z does not existzOut of frame mask )r   r   r   rY   r   r   r   r:   )r   r#   fpathbasenamescenedir	sampledirocclusion_pathoutofframe_paths           r!   _get_occlussion_mask_pathsz'SintelStereo._get_occlussion_mask_paths  s     Y:<O*	Y5EPQQi,6FQRRw~~n-- 	W#$Un$U$U$UVVVw~~o.. 	[#$Y$Y$Y$YZZZ..r"   r   c                    |dS t          j        t          j        |          t           j                  }t          j        |dd          \  }}}|dz  |dz  z   |dz  z   }t          j        |d	          }|                     |          \  }}t          j        t          j        |                    d
k    }t          j        t          j        |                    d
k    }	t          j        |	|          }||fS )Nr   r&     )axis   @   i @  )   r   r   r   )	rG   r   r   r&   r)  r~   	transposeri  logical_and)
r   r#   rr   r6  gbocclued_mask_pathout_of_frame_mask_pathrs   off_masks
             r!   rC   zSintelStereo._read_disparity   s    : 
5:i#8#8
KKK(=!"5551aAT
*Q%[8]I>>484S4ST]4^4^11Z
+< = =>>!C
:ej)?@@AAQF^Hj99
j((r"   rD   c                 l    t          t          t                                          |                    S )a  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 4-tuple with ``(img_left, img_right, disparity, valid_mask)`` is returned.
            The disparity is a numpy array of shape (1, H, W) and the images are PIL images whilst
            the valid_mask is a numpy array of shape (H, W).
        r  rx   s     r!   rR   zSintelStereo.__getitem__  r  r"   )rP  N)rU   rV   rW   rX   rF   r   rY   r   r
   r   r   r   ri  rG   rH   rC   rZ   r\   rR   r]   r^   s   @r!   rY  rY    s&       * *X $( K KU39- K# KU]^fUg Ksw K K K K K K(/C /E#s(O / / / /*) )uZ7H%PRPZ\^\fPfJg7g1h ) ) ) )(4 4 4 4 4 4 4 4 4 4 4 4r"   rY  c            	            e Zd ZdZddeeef         dedee         ddf fdZ	d	ede
ej        df         fd
Zdedef fdZ xZS )
InStereo2ka  `InStereo2k <https://github.com/YuhuaXu/StereoDataset>`_ dataset.

    The dataset is expected to have the following structure: ::

        root
            InStereo2k
                train
                    scene1
                        left.png
                        right.png
                        left_disp.png
                        right_disp.png
                        ...
                    scene2
                    ...
                test
                    scene1
                        left.png
                        right.png
                        left_disp.png
                        right_disp.png
                        ...
                    scene2
                    ...

    Args:
        root (str or ``pathlib.Path``): Root directory where InStereo2k is located.
        split (string): Either "train" or "test".
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    r}   Nr   r~   r   r   c                    t                                          ||           t          |          dz  |z  }t          |dd           t	          |dz  dz            }t	          |dz  dz            }|                     ||          | _        t	          |dz  dz            }t	          |dz  d	z            }|                     ||          | _        d S )
Nrz  r~   r   r   rc   zleft.pngz	right.pngzleft_disp.pngzright_disp.png)r   r   r   r   rY   rA   r   r   r   s	           r!   r   zInStereo2k.__init__B  s    z***DzzL(50ug4EFFFFtczJ677s
[ 899''(8:KLL!$TCZ/%A!B!B"%dSj3C&C"D"D ,,-CE\]]r"   r#   c                     t          j        t          j        |          t           j                  }|d d d d d f         dz  }d }||fS )Nr&  g      @r(  rq   s       r!   rC   zInStereo2k._read_disparityQ  sO    
5:i#8#8
KKK%dAAAqqqj1F:
j((r"   rD   c                 l    t          t          t                                          |                    S rv   rw   rx   s     r!   rR   zInStereo2k.__getitem__X  ry   r"   r   rz   r^   s   @r!   rz  rz  "  s         >^ ^U39- ^c ^QYZbQc ^os ^ ^ ^ ^ ^ ^) )rz47G1H ) ) ) )4 4 4 4 4 4 4 4 4 4 4 4r"   rz  c            	            e Zd ZdZdZddeeef         dedee	         ddf fd	Z
d
edeed         eej        ej        f         f         fdZdedef fdZ xZS )ETH3DStereoaf  ETH3D `Low-Res Two-View <https://www.eth3d.net/datasets>`_ dataset.

    The dataset is expected to have the following structure: ::

        root
            ETH3D
                two_view_training
                    scene1
                        im1.png
                        im0.png
                        images.txt
                        cameras.txt
                        calib.txt
                    scene2
                        im1.png
                        im0.png
                        images.txt
                        cameras.txt
                        calib.txt
                    ...
                two_view_training_gt
                    scene1
                        disp0GT.pfm
                        mask0nocc.png
                    scene2
                        disp0GT.pfm
                        mask0nocc.png
                    ...
                two_view_testing
                    scene1
                        im1.png
                        im0.png
                        images.txt
                        cameras.txt
                        calib.txt
                    scene2
                        im1.png
                        im0.png
                        images.txt
                        cameras.txt
                        calib.txt
                    ...

    Args:
        root (str or ``pathlib.Path``): Root directory of the ETH3D Dataset.
        split (string, optional): The dataset split of scenes, either "train" (default) or "test".
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    Tr}   Nr   r~   r   r   c                    t                                          ||           t          |dd           t          |          dz  }|dk    rdnd}d}t	          ||z  d	z  d
z            }t	          ||z  d	z  dz            }|                     ||          | _        |dk    r%t          d | j        D                       | _        d S t	          ||z  d	z  dz            }|                     |d           | _        d S )Nr~   r   r   ETH3Dr}   two_view_trainingtwo_view_testtwo_view_training_gtrc   rd   re   r   c              3      K   | ]}d V  dS r   r   r0   s     r!   r3   z'ETH3DStereo.__init__.<locals>.<genexpr>  r   r"   rf   )	r   r   r   r   rY   rA   r   r8   r   )
r   r   r~   r   img_diranot_dirr   r   r   r    s
            r!   r   zETH3DStereo.__init__  s   z***ug4EFFFFDzzG#).')9)9%%)tg~3i?@@w 4y @AA''(8:KLLF?? $$H$H4<$H$H$H H HD #D8Oc$9M$I J J $ 0 01BD I IDr"   r#   r   c                    |dS t          |          }t          j        |          }t          |          j        dz  }t          j        |          }t          j        |                              t                    }||fS )Nr   zmask0nocc.png)
ro   rG   rp   r   r   r   r&   r   r>  r  )r   r#   rr   	mask_pathrs   s        r!   rC   zETH3DStereo._read_disparity  ss    :&y11}--OO*_<	Z	**
Z
++22488
j((r"   rD   c                 l    t          t          t                                          |                    S r   r  rx   s     r!   rR   zETH3DStereo.__getitem__  r   r"   r   )rU   rV   rW   rX   rF   r   rY   r   r
   r   r   r   rG   rH   rC   rZ   r\   rR   r]   r^   s   @r!   r  r  g  s        / /b $( J JU39- Jc JQYZbQc Jos J J J J J J(
) 
)uZ7H%PRPZ\^\fPfJg7g1h 
) 
) 
) 
)4 4 4 4 4 4 4 4 4 4 4 4r"   r  )-	functoolsr<  r   r   r  abcr   r   r   pathlibr   typingr   r   r	   r
   r   r   numpyrG   PILr   utilsr   r   r   visionr   rH   r[   r\   __all__partialro   r   r`   r|   r   r   r  r,  rH  rY  rz  r  r   r"   r!   <module>r     s        				   # # # # # # # #             ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?           J J J J J J J J J J ! ! ! ! ! !
5;Xbj%92:EF
5;Xbj%99:
""9Q???n! n! n! n! n!C n! n! n!b=4 =4 =4 =4 =4' =4 =4 =4@R4 R4 R4 R4 R4+ R4 R4 R4jU4 U4 U4 U4 U4+ U4 U4 U4p~4 ~4 ~4 ~4 ~40 ~4 ~4 ~4BU4 U4 U4 U4 U4% U4 U4 U4pe4 e4 e4 e4 e4/ e4 e4 e4Pm4 m4 m4 m4 m4+ m4 m4 m4`w4 w4 w4 w4 w4( w4 w4 w4tB4 B4 B4 B4 B4& B4 B4 B4Ja4 a4 a4 a4 a4' a4 a4 a4 a4 a4r"   