
    h                     t   S SK r S SKrS SKrS SKrS SKrS SKJrJr  S SKJr  S SK	J
r
  S SKJrJrJrJr  S SKrS SKJr  SSKJrJrJr  SS	KJr  \\R&                  \R&                  \\R6                     \R6                  4   r\\R&                  \R&                  \\R6                     4   rS
r\ R>                  " \SS9r  " S S\\5      r! " S S\!5      r" " S S\!5      r# " S S\!5      r$ " S S\!5      r% " S S\!5      r& " S S\!5      r' " S S\!5      r( " S S\!5      r) " S S\!5      r* " S  S!\!5      r+g)"    N)ABCabstractmethod)glob)Path)CallablecastOptionalUnion)Image   )	_read_pfmdownload_and_extract_archiveverify_str_arg)VisionDataset )slice_channelsc                   @  ^  \ rS rSrSrSrSS\\\4   S\	\
   SS4U 4S jjjrS	\\\4   S\R                  4S
 jr SS\S\	\   S\\\\	\   4      4S jjr\S	\S\\	\R&                     \	\R&                     4   4S j5       rS\S\\\4   4S jrS\4S jrSrU =r$ )StereoMatchingDataset   z+Base interface for Stereo matching datasetsFNroot
transformsreturnc                 H   > [         TU ]  US9  X l        / U l        / U l        g)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      ]/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/datasets/_stereo_matching.pyr   StereoMatchingDataset.__init__   s)    , 	d#$    	file_pathc                 t    [         R                  " U5      nUR                  S:w  a  UR                  S5      nU$ )NRGB)r   openmodeconvert)r   r#   imgs      r    	_read_imgStereoMatchingDataset._read_img:   s/    jj#88u++e$C
r"   paths_left_patternpaths_right_patternc                    [        [        [        U5      5      5      nU(       a  [        [        [        U5      5      5      nO[        S U 5       5      nU(       d  [        SU 35      eU(       d  [        SU 35      e[	        U5      [	        U5      :w  a*  [        S[	        U5       S[	        U5       SU SU S3	5      e[        S [        X45       5       5      nU$ )	Nc              3   &   #    U  H  nS v   M	     g 7fNr   .0_s     r    	<genexpr>4StereoMatchingDataset._scan_pairs.<locals>.<genexpr>L   s     8ZtZ   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   ,   #    U  H
  u  pX4v   M     g 7fr0   r   )r2   leftrights      r    r4   r5   [   s     S6R{td]6Rs   )listsortedr   FileNotFoundErrorlen
ValueErrorzip)r   r,   r-   
left_pathsright_pathspathss         r    _scan_pairs!StereoMatchingDataset._scan_pairs@   s     &&8!9:;
 vd+>&?@AK8Z88K#&VWiVj$kll#&VWjVk$lmmz?c+..Z))9#k:J9K L!!3 4 5""5!6b:  Sc*6RSSr"   c                     g r0   r   )r   r#   s     r    _read_disparity%StereoMatchingDataset._read_disparity^   s     	r"   indexc                    U R                  U R                  U   S   5      nU R                  U R                  U   S   5      nU R                  U R                  U   S   5      u  pEU R                  U R                  U   S   5      u  pgX#4nXF4n	XW4n
U R                  b  U R	                  XU
5      u  nn	n
U R
                  (       d  U
S   b*  US   US   U	S   [        [        R                  U
S   5      4$ US   US   U	S   4$ )a'  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   rG   r   r   _has_built_in_disparity_maskr   npndarray)r   rI   img_left	img_rightdsp_map_leftvalid_mask_leftdsp_map_rightvalid_mask_rightimgsdsp_mapsvalid_maskss              r    __getitem__!StereoMatchingDataset.__getitem__c   s    >>$,,u"5a"89NN4<<#6q#9:	(,(<(<T=N=Nu=UVW=X(Y%*.*>*>t?P?PQV?WXY?Z*['$ 0&9??&
 <	 ,,A0J7DGXa[$rzz;q>2RRR7DGXa[00r"   c                 ,    [        U R                  5      $ r0   )r>   r   )r   s    r    __len__StereoMatchingDataset.__len__   s    4<<  r"   )r   r   r   r0   )__name__
__module____qualname____firstlineno____doc__rK   r
   strr   r	   r   r   r   r*   r;   tuplerD   r   rL   rM   rG   intT1T2rW   rZ   __static_attributes____classcell__r   s   @r    r   r      s   5#( U39- 8H;M Y]  85d#3   .2 &c] 
eC#&'	(	<  x

7KXVXV`V`Ma7a1b  !1 !1r2v !1F! ! !r"   r   c                      ^  \ rS rSrSrSS\\\4   S\\	   SS4U 4S jjjr
S\S\\R                  S4   4S	 jrS
\S\4U 4S jjrSrU =r$ )CarlaStereo   a"  
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	U ]  X5        [        U5      S-  n[        US-  S-  S-  5      n[        US-  S-  S-  5      nU R	                  X45      nXPl        [        US-  S-  S-  5      n[        US-  S-  S-  5      nU R	                  Xg5      nXl        g )Nzcarla-highres	trainingF*im0.pngim1.pngdisp0GT.pfmzdisp1GT.pfmr   r   r   ra   rD   r   r   )
r   r   r   left_image_patternright_image_patternrT   left_disparity_patternright_disparity_patterndisparitiesr   s
            r    r   CarlaStereo.__init__   s    *DzO+ !3c!9I!EF!$"4s":Y"FG 2H!$TK%7#%=%M!N"%d[&83&>&N"O&&'=W'r"   r#   c                 N    [        U5      n[        R                  " U5      nS nX#4$ r0   _read_pfm_filerL   absr   r#   disparity_map
valid_masks       r    rG   CarlaStereo._read_disparity   (    &y1}-
((r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ 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   rd   r   rW   r   rI   r   s     r    rW   CarlaStereo.__getitem__        B+E233r"   r   r   r0   r\   r]   r^   r_   r`   r
   ra   r   r	   r   r   rb   rL   rM   rG   rc   rd   rW   rf   rg   rh   s   @r    rj   rj      so    6(U39- (8H;M (Y] ( () )rzz47G1H )4 4 4 4r"   rj   c            	          ^  \ rS rSrSrSrSS\\\4   S\S\	\
   SS4U 4S	 jjjrS
\S\\	\R                     S4   4S jrS\S\4U 4S jjrSrU =r$ )Kitti2012Stereo   a  
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.
TNr   splitr   r   c                 r  > [         TU ]  X5        [        USSS9  [        U5      S-  US-   -  n[	        US-  S-  5      n[	        US-  S-  5      nU R                  XE5      U l        US	:X  a)  [	        US
-  S-  5      nU R                  US 5      U l        g [        S U R                   5       5      U l        g )Nr   traintestvalid_values	Kitti2012ing	colored_0z*_10.png	colored_1r   disp_noc*.pngc              3   &   #    U  H  nS v   M	     g7fNNNr   r1   s     r    r4   +Kitti2012Stereo.__init__.<locals>.<genexpr>       $H<a\<r6   	r   r   r   r   ra   rD   r   r   r;   )r   r   r   r   left_img_patternright_img_patterndisparity_patternr   s          r    r   Kitti2012Stereo.__init__   s    *ug4EFDzK'55=9tk1J>?{ 2Z ?@''(8LG #D:$5$? @ $ 0 01BD ID $$H4<<$H HDr"   r#   c                     Uc  g[         R                  " [        R                  " U5      5      S-  nUS S S 2S S 24   nS nX#4$ Nr   g      p@rL   asarrayr   r&   r}   s       r    rG   Kitti2012Stereo._read_disparity  E    

5::i#89EA%dAqj1
((r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ 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.
r   r   s     r    rW   Kitti2012Stereo.__getitem__       B+E233r"   r   r   Nr\   r]   r^   r_   r`   rK   r
   ra   r   r	   r   r   rb   rL   rM   rG   rc   rd   rW   rf   rg   rh   s   @r    r   r      s    $L $( IU39- Ic IQYZbQc Ios I I"	) 	)x

7KT7Q1R 	)4 4 4 4r"   r   c            	          ^  \ rS rSrSrSrSS\\\4   S\S\	\
   SS4U 4S	 jjjrS
\S\\	\R                     S4   4S jrS\S\4U 4S jjrSrU =r$ )Kitti2015Stereoi  a  
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.
TNr   r   r   r   c                   > [         TU ]  X5        [        USSS9  [        U5      S-  US-   -  n[	        US-  S-  5      n[	        US-  S-  5      nU R                  XE5      U l        US	:X  a9  [	        US
-  S-  5      n[	        US-  S-  5      nU R                  Xg5      U l        g [        S U R                   5       5      U l        g )Nr   r   r   	Kitti2015r   image_2r   image_3r   
disp_occ_0
disp_occ_1c              3   &   #    U  H  nS v   M	     g7fr   r   r1   s     r    r4   +Kitti2015Stereo.__init__.<locals>.<genexpr>Z  r   r6   r   	r   r   r   r   r   r   ru   rv   r   s	           r    r   Kitti2015Stereo.__init__K  s    *ug4EFDzK'55=9ti/'9:y 07 :;''(8LG%()<w)F%G"&)$*=*G&H# $ 0 01G aD $$H4<<$H HDr"   r#   c                     Uc  g[         R                  " [        R                  " U5      5      S-  nUS S S 2S S 24   nS nX#4$ r   r   r}   s       r    rG   Kitti2015Stereo._read_disparity\  r   r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ r   r   r   s     r    rW   Kitti2015Stereo.__getitem__g  r   r"   r   r   r   rh   s   @r    r   r     s    'R $( IU39- Ic IQYZbQc Ios I I"	) 	)x

7KT7Q1R 	)4 4 4 4r"   r   c                   J  ^  \ rS rSrSr/ SQ/ SQ/ SQS.rSr     SS	\\\	4   S
\S\
\   S\S\
\   S\SS4U 4S jjjrS\\\	4   S\R                  4U 4S jjrS\S\\S   \\R$                  \R$                  4   4   4S jrS	\\\	4   SS4S jrS\S\4U 4S jjrSrU =r$ )Middlebury2014Stereoiw  a  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   TNr   r   calibrationuse_ambient_viewsr   downloadr   c                   >^ [         TU ]  X5        [        USSS9  X l        U(       a  [        USSS9  US:X  a  [	        S5      eOUS:w  a  [	        SU S	U S
35      eU(       a  U R                  U5        [        U5      S-  n[        R                  R                  X-  5      (       d  [        SU S35      eU R                  U   m[        U4S j[        R                  " X-  5       5       5      (       d  [        SU S35      eS/S/S/SS/S.U   nU H  nSU-   n	[        X-  U	-  S-  5      n
[        X-  U	-  S-  5      nU =R                  U R!                  X5      -  sl        US:X  a#  [#        S U R                   5       5      U l        M{  [        X-  U	-  S-  5      n[        X-  U	-  S-  5      nU =R$                  U R!                  X5      -  sl        M     X@l        g )Nr   )r   r   r   r   r   )perfect	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   Z   >#    U  H   nT  H  nUR                  U5      v   M     M"     g 7fr0   )
startswith)r2   scenessplit_sceness      r    r4   0Middlebury2014Stereo.__init__.<locals>.<genexpr>  s5      
 2! Q!  1   (+z:Provided root folder does not contain any scenes from the z split. z-perfectz
-imperfect)Nr   r   r   rn   ro   rp   c              3   &   #    U  H  nS v   M	     g7fr   r   r1   s     r    r4   r     s     (L|!|r6   z	disp0.pfmz	disp1.pfm)r   r   r   r   r?   _download_datasetr   ospathexistsr=   splitsanylistdirra   r   rD   r;   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   Middlebury2014Stereo.__init__  s    	*ug4ST
;Djk !pqq   eW %EEJG  LRS 
 ""4(Dz,,ww~~dl++#d5'1h$ijj{{5) 
 DL1
 
 
 $&`af`ggn$opp $"|&.	!

 ! #8"44M"4<-#?)#KL #DL=$@9$L MLLD,,-=QQL$((Lt||(L$L!*-dl].J[.X*Y'+.t|m/Kk/Y+Z(!!T%5%56M%hh! #8 "3r"   r#   c                 b  >^ [        U[        5      (       d  [        U5      nUR                  S:X  ao  U R                  (       a^  UR                  m[        U4S jS 5       5      n[        [        S U5      5      nUR                  U5        [        R                  " U5      n[        TU ]-  U5      $ )z
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.
rp   c              3   .   >#    U  H
  nTU-  v   M     g 7fr0   r   )r2   	view_name	base_paths     r    r4   1Middlebury2014Stereo._read_img.<locals>.<genexpr>0  s     %fMe	i)&;Mes   )zim1E.pngzim1L.pngc                 @    [         R                  R                  U 5      $ r0   )r   r   r   )ps    r    <lambda>0Middlebury2014Stereo._read_img.<locals>.<lambda>2  s    rww~~a7Hr"   )
isinstancer   namer   parentr;   filterappendrandomchoicer   r*   )r   r#   ambient_file_pathsr
  r   s      @r    r*   Middlebury2014Stereo._read_img"  s     )T**YI>>Y&4+A+A!((I!%%fMe%f!f!%f-HJ\&]!^%%i0&89Iw ++r"   r   c                     Uc  g[        U5      n[        R                  " U5      nSX"[        R                  :H  '   US:  R	                  S5      nX#4$ )Nr   r   )r{   rL   r|   infsqueezer}   s       r    rG   $Middlebury2014Stereo._read_disparity8  sR    &y1}-12rvv-.#a'003
((r"   c           	        ^ Sn[        T5      S-  mU R                  nUS:w  ae  U R                  U    HQ  nTU-  nS HC  nU SU 3nU SU S3nXW-  R                  5       (       a  M+  [	        UU S3[        U5      SS	9  ME     MS     g [        R                  " TS-  5        [        U4S
 jU R                  S    5       5      (       a  Sn	[	        U	[        T5      SS9  [        R                  " [        TS-  5      5       H`  u  pnU HT  nTS-  n[        U
5      U-  n[        R                  " USS9  [        R                  " [        U5      [        U5      5        MV     Mb     [        R                  " [        TS-  5      5        g g )Nz8https://vision.middlebury.edu/stereo/data/scenes2014/zipr   r   )r   r   -/z.zipT)urlfilenamedownload_rootremove_finishedc              3   Z   >#    U  H   o[         R                  " TS -  5      ;  v   M"     g7f)r   N)r   r   )r2   r   r   s     r    r4   9Middlebury2014Stereo._download_dataset.<locals>.<genexpr>Y  s#     S?R!BJJtf}55?Rr   zEhttps://vision.middlebury.edu/stereo/submit3/zip/MiddEval3-data-F.zip)r   r"  r#  zMiddEval3/testF)exist_ok	MiddEval3)r   r   r   r   r   ra   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_namesr3   r   scene_dst_dirscene_src_dirs    `              r    r   &Middlebury2014Stereo._download_datasetC  sp   MDz,,ZZ
#{{:6!J.
#;K$/=+!?J#+*Aj\ >I&3;;==4 )(2|4%8*-j/,0	 $<  7 KKv&St{{6?RSSSf -SQUYhlm13TDU=U9V1W-IA!,(,v(,Y%(?MDAC$6M8JK	 "- 2X c$"456 Tr"   rI   c                 >   > [        [        [        TU ]  U5      5      $ )aB  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   re   r   rW   r   s     r    rW    Middlebury2014Stereo.__getitem__i       B+E233r"   )r   r   r   )r   r   FNF)r\   r]   r^   r_   r`   r   rK   r
   ra   r   r	   boolr   r   r   r*   rb   rL   rM   rG   r   rc   re   rW   rf   rg   rh   s   @r    r   r   w  s)   6r


9-F^ $( 
 %."')-?3CI?3 ?3 c]	?3
  ?3 X&?3 ?3 
?3 ?3B,5d#3 , ,,	) 	)uZ7H%PRPZPZ\^\f\fPfJg7g1h 	)$7eCI&6 $74 $7L4 4 4 4r"   r   c                      ^  \ rS rSrSrSr SS\\\4   S\	\
   SS4U 4S jjjrS	\S\\R                  S4   4S
 jrS\S\4U 4S jjrSrU =r$ )	CREStereoiw  a  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                 r  > [         TU ]  X5        [        U5      S-  n/ SQnU H  n[        X-  S-  5      n[        X-  S-  5      nU R	                  XV5      nU =R
                  U-  sl        [        X-  S-  5      n[        X-  S-  5      n	U R	                  X5      n
U =R                  U
-  sl        M     g )Nr?  )shapenet
reflectivetreeholez
*_left.jpgz*_right.jpgz*_left.disp.pngz*_right.disp.pngrr   )r   r   r   dirsr   rs   rt   rT   ru   rv   rw   r   s              r    r   CREStereo.__init__  s    
 	*DzK'9A!$TX%<!="%dh&>"?##$6LDLLD L%(4E)E%F"&)$(5G*G&H#**+A[K, r"   r#   c                     [         R                  " [        R                  " U5      [         R                  S9nUS S S 2S S 24   S-  nS nX#4$ )Ndtypeg      @@rL   r   r   r&   float32r}   s       r    rG   CREStereo._read_disparity  sB    

5::i#8

K%dAqj1D8
((r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ )at  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.
r   r   s     r    rW   CREStereo.__getitem__  r   r"   r   r0   r   rh   s   @r    r?  r?  w  s    'R $( 
 *.-CI- X&- 
	- -,) )rzz47G1H )4 4 4 4r"   r?  c            	          ^  \ rS rSrSrSS\\\4   S\S\\	   SS4U 4S jjjr
S	\S\\R                  S4   4S
 jrS\S\4U 4S jjrSrU =r$ )FallingThingsStereoi  aK  `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.
Nr   variantr   r   c                   > [         TU ]  X5        [        U5      S-  n[        USSS9  S/S/SS/S.U   n[        S5      S-  [        S5      S.nU H  n[	        X-  XV   -  S	-  5      n[	        X-  XV   -  S
-  5      nU =R
                  U R                  Xx5      -  sl        [	        X-  XV   -  S-  5      n	[	        X-  XV   -  S-  5      n
U =R                  U R                  X5      -  sl        M     g )NFallingThingsrQ  )singlemixedr   r   rT  rU  rn   )rT  rU  z
*.left.jpgz*.right.jpgz*.left.depth.pngz*.right.depth.pngr   r   r   r   ra   r   rD   r   )r   r   rQ  r   variantssplit_prefixr   r   r   ru   rv   r   s              r    r   FallingThingsStereo.__init__  s   *DzO+w	8ST  jYw'
 	 3i#o#Y

 A"48lo#=#LM #DH|$>$N OLLD,,-=QQL%(LO)CFX)X%Y"&)$(\_*DGZ*Z&[#!1!12H!bb r"   r#   c                    [         R                  " [        R                  " U5      5      n[	        U5      R
                  S-  n[        U5       n[        R                  " U5      nUS   S   S   S   nSu  pxXv-  U-  UR                  [         R                  5      -  n	U	S S S 2S S 24   n	S n
X4sS S S 5        $ ! , (       d  f       g = f)Nz_camera_settings.jsoncamera_settingsr   intrinsic_settingsfx)   d   )
rL   r   r   r&   r   r  jsonloadastyperK  )r   r#   depthcamera_settings_pathf
intrinsicsfocalbaselinepixel_constantr~   r   s              r    rG   #FallingThingsStereo._read_disparity  s    

5::i01  $I558OO&'11J01!45IJ4PE'-$H%->%,,rzzBZZM)$1*5MJ , (''s   A B88
CrI   c                 >   > [        [        [        TU ]  U5      5      $ r   r   r   s     r    rW   FallingThingsStereo.__getitem__(  r   r"   r   )rT  Nr   rh   s   @r    rP  rP    s    *XcU39- c cT\]eTf crv c c6- -rzz47G1H -"4 4 4 4r"   rP  c                      ^  \ rS rSrSr   SS\\\4   S\S\S\\	   SS4
U 4S	 jjjr
S
\S\\R                  S4   4S jrS\S\4U 4S jjrSrU =r$ )SceneFlowStereoi7  aC  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.

Nr   rQ  	pass_namer   r   c                 N  > [         TU ]  X5        [        U5      S-  n[        USSS9  [        USSS9  S/S/SS/S.U   nX-  n[        S	5      [        S	5      S	-  S	-  [        S	5      S	-  S	-  S
.nU H  n[	        X-  Xb   -  S-  S-  5      n[	        X-  Xb   -  S-  S-  5      n	U =R
                  U R                  X5      -  sl        [	        US-  Xb   -  S-  S-  5      n
[	        US-  Xb   -  S-  S-  5      nU =R                  U R                  X5      -  sl        M     g )N	SceneFlowrQ  )FlyingThings3DDrivingMonkaar   ro  )cleanfinalr   frames_cleanpassframes_finalpassrn   )rt  rr  rs  r9   r   r:   	disparityz*.pfmrV  )r   r   rQ  ro  r   passesprefix_directoriesr  rs   rt   ru   rv   r   s               r    r   SceneFlowStereo.__init__m  sk    	*DzK'w	8_`y+<VW ))()');<
 	 ~ 3i"3i#o3Cy3,
 A!$TX0B0K%Kf%TW^%^!_"%dh1C1L&Lw&VY`&`"aLLD,,-?UUL%();>P>Y)Y\b)bel)l%m"&)$*<?Q?Z*Z]d*dgn*n&o#!1!12H!bb r"   r#   c                 N    [        U5      n[        R                  " U5      nS nX#4$ r0   rz   r}   s       r    rG   SceneFlowStereo._read_disparity  r   r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ r   r   r   s     r    rW   SceneFlowStereo.__getitem__  r   r"   r   )rr  ru  Nr   rh   s   @r    rn  rn  7  s    3p ( )-#cCI#c #c 	#c
 X&#c 
#c #cJ) )rzz47G1H )4 4 4 4r"   rn  c            	          ^  \ rS rSrSrSrSS\\\4   S\S\	\
   SS4U 4S	 jjjrS
\S\\\4   4S jrS
\S\\S   \\R                  \R                  4   4   4S jrS\S\4U 4S jjrSrU =r$ )SintelStereoi  a.  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.
TNr   ro  r   r   c                   > [         T	U ]  X5        [        USSS9  [        U5      S-  nS/S/SS/S.U   nU H  n[	        US-  U S3-  S	-  S
-  5      n[	        US-  U S3-  S	-  S
-  5      nU =R
                  U R                  Xg5      -  sl        [	        US-  S-  S	-  S
-  5      nU =R                  U R                  US 5      -  sl        M     g )Nro  )rv  ru  r   r   Sintelrv  ru  training_leftrn   r   _rightrw   )r   r   r   r   ra   r   rD   r   )
r   r   ro  r   
pass_namesr  r   r   r   r   s
            r    r   SintelStereo.__init__  s    *y+<VWDzH$YYg&
 	
 A"4*#4!E{#BS#H7#RS #D:$51#V$Ds$JW$T ULLD,,-=QQL #D:$5$E$Kg$U V!1!12CT!JJ r"   r#   c                    [        U5      nUR                  nUR                  nUR                  R                  n[        US-  UR                  -  U-  5      n[        US-  UR                  -  U-  5      n[        R
                  R                  U5      (       d  [        SU S35      e[        R
                  R                  U5      (       d  [        SU S35      eXg4$ )N
occlusions
outofframezOcclusion mask z does not existzOut of frame mask )r   r  r  ra   r   r   r   r=   )r   r#   fpathbasenamescenedir	sampledirocclusion_pathoutofframe_paths           r    _get_occlussion_mask_paths'SintelStereo._get_occlussion_mask_paths  s     Y::<<OO**	Y5EPQi,6FQRww~~n--#on5E_$UVVww~~o..#&88I$YZZ..r"   r   c                    Uc  g[         R                  " [        R                  " U5      [         R                  S9n[         R
                  " USSS9u  p4nUS-  US-  -   US-  -   n[         R                  " US	5      nU R                  U5      u  pg[         R                  " [        R                  " U5      5      S
:H  n[         R                  " [        R                  " U5      5      S
:H  n	[         R                  " X5      nX(4$ )Nr   rH     )axis   @   i @  )   r   r   r   )	rL   r   r   r&   rK  r   	transposer  logical_and)
r   r#   r~   rgbocclued_mask_pathout_of_frame_mask_pathr   off_masks
             r    rG   SintelStereo._read_disparity  s     

5::i#8

K((=!"5aAT
*Q%[8]I>484S4ST]4^1ZZ

+< =>!C
::ejj)?@AQF^^H9
((r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ )aM  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:  r   s     r    rW   SintelStereo.__getitem__  r<  r"   r   )rv  N)r\   r]   r^   r_   r`   rK   r
   ra   r   r	   r   r   rb   r  rL   rM   rG   rc   re   rW   rf   rg   rh   s   @r    r  r    s    *X $( KU39- K# KU]^fUg Ksw K K(/C /E#s(O /*) )uZ7H%PRPZPZ\^\f\fPfJg7g1h )(4 4 4 4r"   r  c            	          ^  \ rS rSrSrSS\\\4   S\S\\	   SS4U 4S jjjr
S	\S\\R                  S4   4S
 jrS\S\4U 4S jjrSrU =r$ )
InStereo2ki!  al  `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.
Nr   r   r   r   c                 <  > [         TU ]  X5        [        U5      S-  U-  n[        USSS9  [	        US-  S-  5      n[	        US-  S-  5      nU R                  XE5      U l        [	        US-  S-  5      n[	        US-  S	-  5      nU R                  Xg5      U l        g )
Nr  r   r   r   rn   zleft.pngz	right.pngzleft_disp.pngzright_disp.png)r   r   r   r   ra   rD   r   r   r   s	           r    r   InStereo2k.__init__A  s    *DzL(50ug4EFtczJ67s
[ 89''(8L!$TCZ/%A!B"%dSj3C&C"D ,,-C]r"   r#   c                     [         R                  " [        R                  " U5      [         R                  S9nUS S S 2S S 24   S-  nS nX#4$ )NrH  g      @rJ  r}   s       r    rG   InStereo2k._read_disparityP  sB    

5::i#8

K%dAqj1F:
((r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ r   r   r   s     r    rW   InStereo2k.__getitem__W  r   r"   r   r   r   rh   s   @r    r  r  !  s~    >^U39- ^c ^QYZbQc ^os ^ ^) )rzz47G1H )4 4 4 4r"   r  c            	          ^  \ rS rSrSrSrSS\\\4   S\S\	\
   SS4U 4S	 jjjrS
\S\\S   \\R                  \R                  4   4   4S jrS\S\4U 4S jjrSrU =r$ )ETH3DStereoif  a  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.
TNr   r   r   r   c                   > [         T	U ]  X5        [        USSS9  [        U5      S-  nUS:X  a  SOSnSn[	        X-  S	-  S
-  5      n[	        X-  S	-  S-  5      nU R                  Xg5      U l        US:X  a"  [        S U R                   5       5      U l        g [	        X-  S	-  S-  5      nU R                  US 5      U l        g )Nr   r   r   ETH3Dr   two_view_trainingtwo_view_testtwo_view_training_gtrn   ro   rp   r   c              3   &   #    U  H  nS v   M	     g7fr   r   r1   s     r    r4   'ETH3DStereo.__init__.<locals>.<genexpr>  r   r6   rq   )	r   r   r   r   ra   rD   r   r;   r   )
r   r   r   r   img_diranot_dirr   r   r   r   s
            r    r   ETH3DStereo.__init__  s    *ug4EFDzG#).')9%)t~3i?@ 4y @A''(8LF? $$H4<<$H HD #DOc$9M$I J $ 0 01BD IDr"   r#   r   c                     Uc  g[        U5      n[        R                  " U5      n[        U5      R                  S-  n[
        R                  " U5      n[        R                  " U5      R                  [        5      nX$4$ )Nr   zmask0nocc.png)
r{   rL   r|   r   r  r   r&   r   rb  r=  )r   r#   r~   	mask_pathr   s        r    rG   ETH3DStereo._read_disparity  sg    &y1}-O**_<	ZZ	*
ZZ
+2248
((r"   rI   c                 >   > [        [        [        TU ]  U5      5      $ r   r:  r   s     r    rW   ETH3DStereo.__getitem__  r   r"   r   r   )r\   r]   r^   r_   r`   rK   r
   ra   r   r	   r   r   rb   rL   rM   rG   rc   re   rW   rf   rg   rh   s   @r    r  r  f  s    /b $( JU39- Jc JQYZbQc Jos J J(
) 
)uZ7H%PRPZPZ\^\f\fPfJg7g1h 
)4 4 4 4r"   r  ),	functoolsr`  r   r  r*  abcr   r   r   pathlibr   typingr   r   r	   r
   numpyrL   PILr   utilsr   r   r   visionr   rb   rM   rd   re   __all__partialr{   r   rj   r   r   r   r?  rP  rn  r  r  r  r   r"   r    <module>r     sA     	   #   2 2   J J !
5;;Xbjj%92::EF
5;;Xbjj%99:
""9Q?n!C n!b=4' =4@R4+ R4jU4+ U4p}40 }4@U4% U4pe4/ e4Pm4+ m4`w4( w4tB4& B4Ja4' a4r"   