U
    ­Ê¹j†¾  ã                   @   s   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d�Z!G dd„ deeƒZ"G dd„ de"ƒZ#G dd„ de"ƒZ$G dd„ de"ƒZ%G dd„ de"ƒZ&G dd„ de"ƒZ'G dd„ de"ƒZ(G dd„ de"ƒZ)G dd„ de"ƒZ*G dd„ de"ƒZ+G d 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© )Zslice_channelsc                       sÄ   e Zd ZdZdZdeee ddœ‡ fdd„Ze	ee
f ejdœdd	„Zdeee eeeee f  d
œdd„Zeeeeej eej f dœdd„ƒZee	eef dœdd„Zedœdd„Z‡  ZS )ÚStereoMatchingDatasetz+Base interface for Stereo matching datasetsFN©ÚrootÚ
transformsÚreturnc                    s$   t ƒ j|d� || _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__r   úd/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/datasets/_stereo_matching.pyr      s    zStereoMatchingDataset.__init__©Ú	file_pathr   c                 C   s"   t  |¡}|jdkr| d¡}|S )NÚRGB)r   ÚopenÚmodeÚconvert)r   r"   Zimgr   r   r    Ú	_read_img:   s    


zStereoMatchingDataset._read_img)Úpaths_left_patternÚpaths_right_patternr   c              
   C   s²   t tt|ƒƒƒ}|r&t tt|ƒƒƒ}nt dd„ |D ƒƒ}|sJtd|› �ƒ‚|s\td|› �ƒ‚t|ƒt|ƒkr–tdt|ƒ› dt|ƒ› d|› d|› d�	ƒ‚t d	d„ t||ƒD ƒƒ}|S )
Nc                 s   s   | ]
}d V  qd S ©Nr   ©Ú.0Ú_r   r   r    Ú	<genexpr>L   s     z4StereoMatchingDataset._scan_pairs.<locals>.<genexpr>z0Could not find any files matching the patterns: zFound z left files but z# right files using:
 left pattern: z
right pattern: Ú
c                 s   s   | ]\}}||fV  qd S r*   r   )r,   ÚleftÚrightr   r   r    r.   [   s     )ÚlistÚsortedr   ÚFileNotFoundErrorÚlenÚ
ValueErrorÚzip)r   r(   r)   Z
left_pathsZright_pathsÚpathsr   r   r    Ú_scan_pairs@   s    $ÿz!StereoMatchingDataset._scan_pairsc                 C   s   d S r*   r   )r   r"   r   r   r    Ú_read_disparity^   s    z%StereoMatchingDataset._read_disparity©Úindexr   c                 C   sÞ   |   | j| d ¡}|   | j| d ¡}|  | j| d ¡\}}|  | j| d ¡\}}||f}||f}	||f}
| jdk	rŽ|  ||	|
¡\}}	}
| js |
d dk	rÄ|d |d |	d ttj|
d ƒfS |d |d |	d fS d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   N)	r'   r   r:   r   r   Ú_has_built_in_disparity_maskr   ÚnpÚndarray)r   r<   Zimg_leftZ	img_rightZdsp_map_leftZvalid_mask_leftZdsp_map_rightZvalid_mask_rightÚimgsZdsp_mapsZvalid_masksr   r   r    Ú__getitem__c   s     
ü$z!StereoMatchingDataset.__getitem__)r   c                 C   s
   t | jƒS r*   )r5   r   )r   r   r   r    Ú__len__†   s    zStereoMatchingDataset.__len__)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r=   Ústrr	   r   r   r   r   r   r'   r   r
   r9   r   r>   r?   r:   ÚintÚT1ÚT2rA   rB   Ú__classcell__r   r   r   r    r      s   	 ýü&#r   c                       s^   e Zd ZdZdeee ddœ‡ fdd„Zeee	j
df dœdd„Zeed	œ‡ fd
d„Z‡  ZS )ÚCarlaStereoai  
    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 (string): Root directory where `carla-highres` is located.
        transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
    Nr   c           	         s’   t ƒ  ||¡ t|ƒd }t|d d d ƒ}t|d d d ƒ}|  ||¡}|| _t|d d d ƒ}t|d d d ƒ}|  ||¡}|| _d S )Nzcarla-highresZ	trainingFÚ*úim0.pngúim1.pngúdisp0GT.pfmzdisp1GT.pfm©r   r   r   rG   r9   r   r   )	r   r   r   Úleft_image_patternÚright_image_patternr@   Úleft_disparity_patternÚright_disparity_patternÚdisparitiesr   r   r    r   ¦   s    zCarlaStereo.__init__r!   c                 C   s   t |ƒ}t |¡}d }||fS r*   ©Ú_read_pfm_filer>   Úabs©r   r"   Údisparity_mapÚ
valid_maskr   r   r    r:   µ   s    
zCarlaStereo._read_disparityr;   c                    s   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   rI   r   rA   ©r   r<   r   r   r    rA   »   s    zCarlaStereo.__getitem__)N©rC   rD   rE   rF   rG   r	   r   r   r
   r>   r?   r:   rH   rI   rA   rK   r   r   r   r    rL   Š   s   rL   c                       sh   e Zd ZdZdZdeeee ddœ‡ fdd„Zee	ee
j df dœd	d
„Zeedœ‡ fdd„Z‡  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 (string): 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ÚtrainN©r   Úsplitr   r   c                    s    t ƒ  ||¡ t|ddd� t|ƒd |d  }t|d d ƒ}t|d d ƒ}|  ||¡| _|d	kr†t|d
 d ƒ}|  |d ¡| _ntdd„ | jD ƒƒ| _d S )Nrd   ©rb   Útest©Zvalid_valuesZ	Kitti2012ÚingZ	colored_0z*_10.pngZ	colored_1rb   Zdisp_nocú*.pngc                 s   s   | ]
}d V  qdS ©©NNNr   r+   r   r   r    r.     s     z+Kitti2012Stereo.__init__.<locals>.<genexpr>©	r   r   r   r   rG   r9   r   r   r2   )r   r   rd   r   Úleft_img_patternÚright_img_patternÚdisparity_patternr   r   r    r   ó   s    zKitti2012Stereo.__init__r!   c                 C   sB   |d krdS t  t |¡¡d }|d d d …d d …f }d }||fS ©Nrk   g      p@©r>   Úasarrayr   r$   rZ   r   r   r    r:     s    zKitti2012Stereo._read_disparityr;   c                    s   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.
        r^   r_   r   r   r    rA     s    zKitti2012Stereo.__getitem__)rb   N©rC   rD   rE   rF   r=   rG   r	   r   r   r
   r>   r?   r:   rH   rI   rA   rK   r   r   r   r    ra   Ê   s
   &ra   c                       sh   e Zd ZdZdZdeeee ddœ‡ fdd„Zee	ee
j df dœd	d
„Zeedœ‡ fdd„Z‡  ZS )ÚKitti2015Stereoa<  
    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 (string): 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.
    Trb   Nrc   c                    s°   t ƒ  ||¡ t|ddd� t|ƒd |d  }t|d d ƒ}t|d d ƒ}|  ||¡| _|d	kr–t|d
 d ƒ}t|d d ƒ}|  ||¡| _ntdd„ | jD ƒƒ| _d S )Nrd   re   rg   Z	Kitti2015rh   Zimage_2ri   Zimage_3rb   Z
disp_occ_0Z
disp_occ_1c                 s   s   | ]
}d V  qdS rj   r   r+   r   r   r    r.   Z  s     z+Kitti2015Stereo.__init__.<locals>.<genexpr>rl   ©r   r   rd   r   rm   rn   rT   rU   r   r   r    r   K  s    zKitti2015Stereo.__init__r!   c                 C   sB   |d krdS t  t |¡¡d }|d d d …d d …f }d }||fS rp   rq   rZ   r   r   r    r:   \  s    zKitti2015Stereo._read_disparityr;   c                    s   t ttƒ  |¡ƒS rs   r^   r_   r   r   r    rA   g  s    zKitti2015Stereo.__getitem__)rb   Nrt   r   r   r   r    ru     s
   )ru   c                       s  e Zd ZdZdddddddd	d
dg
dddddddddddddgdddddddd d!d"d#d$d%d&d'gd(œZd)Zd=eeee eee	 ed-d.œ‡ fd/d0„Z
eeef ejd1œ‡ fd2d3„Zeeed4 eejejf f d1œd5d6„Zed-d7œd8d9„Zeed:œ‡ fd;d<„Z‡  ZS )>ÚMiddlebury2014StereoaI	  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 (string): 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.
    Z
AdirondackZ	JadeplantZ
MotorcycleZPianoZPipesZPlayroomZ	PlaytableZRecycleZShelvesZVintageZBackpackZBicycle1ZCableZ
Classroom1ZCouchZFlowersÚMaskZShopvacZSticksZStorageZSword1ZSword2ZUmbrellaZPlantsZClassroom2EZ
Classroom2Z	AustraliaZDjembeLZCrusadePZCrusadeZHoopsZBicycle2Z	StaircaseZNewkubaZ
AustraliaPZDjembeZ
LivingroomZComputer)rb   Ú
additionalrf   Trb   ÚperfectFN)r   rd   ÚcalibrationÚuse_ambient_viewsr   Údownloadr   c                    s®  t ƒ  ||¡ t|ddd� || _|rFt|ddd� |dkrdtdƒ‚n|dkrdtd|› d	|› d
�ƒ‚|rr|  |¡ t|ƒd }tj 	|| ¡sžt
d|› d�ƒ‚| j| ‰ t‡ fdd„t || ¡D ƒƒsØt
d|› d�ƒ‚dgdgdgddgdœ| }|D ]ª}d| }	t|| |	 d ƒ}
t|| |	 d ƒ}|  j|  |
|¡7  _|dk�rdtdd„ | jD ƒƒ| _qøt|| |	 d ƒ}t|| |	 d ƒ}|  j|  ||¡7  _qø|| _d S )Nrd   )rb   rf   ry   rg   r{   )rz   Ú	imperfectÚbothNrf   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   s"   | ]}ˆ D ]}|  |¡V  q
qd S r*   )Ú
startswith)r,   ÚsceneÚs©Zsplit_scenesr   r    r.     s    þz0Middlebury2014Stereo.__init__.<locals>.<genexpr>z:Provided root folder does not contain any scenes from the z split.Ú z-perfectz
-imperfect)Nrz   r~   r   rM   rN   rO   c                 s   s   | ]
}d V  qdS rj   r   r+   r   r   r    r.     s     z	disp0.pfmz	disp1.pfm)r   r   r   rd   r6   Ú_download_datasetr   ÚosÚpathÚexistsr4   ÚsplitsÚanyÚlistdirrG   r   r9   r2   r   r|   )r   r   rd   r{   r|   r   r}   Zcalibrartion_suffixesZcalibration_suffixZscene_patternrm   rn   Zleft_dispartity_patternZright_dispartity_patternr   r„   r    r   á  sN    	
ÿ

ýüû
zMiddlebury2014Stereo.__init__r!   c                    sp   t |tƒst|ƒ}|jdkrd| jrd|j‰ t‡ fdd„dD ƒƒ}ttdd„ |ƒƒ}| |¡ t 	|¡}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.
        rO   c                 3   s   | ]}ˆ | V  qd S r*   r   )r,   Z	view_name©Ú	base_pathr   r    r.   0  s     z1Middlebury2014Stereo._read_img.<locals>.<genexpr>)zim1E.pngzim1L.pngc                 S   s   t j | ¡S r*   )r‡   rˆ   r‰   )Úpr   r   r    Ú<lambda>2  ó    z0Middlebury2014Stereo._read_img.<locals>.<lambda>)Ú
isinstancer   Únamer|   Úparentr2   ÚfilterÚappendÚrandomÚchoicer   r'   )r   r"   Zambient_file_pathsr   r�   r    r'   "  s    


zMiddlebury2014Stereo._read_imgrk   c                 C   sB   |d krdS t |ƒ}t |¡}d||tjk< |dk d¡}||fS )Nrk   r   )rX   r>   rY   ÚinfZsqueezerZ   r   r   r    r:   8  s    
z$Middlebury2014Stereo._read_disparity)r   r   c                    sJ  d}t ˆ ƒd ‰ | j}|dkr”| j| D ]h}ˆ | }dD ]V}|› d|› �}|› d|› d�}td|› �ƒ ||  ¡ s8t||› d�t|ƒd	d
� q8q(n²t ˆ d ¡ t	‡ fdd„| jd D ƒƒ�rFd}	t|	tˆ ƒd	d� t 
tˆ d ƒ¡D ]J\}
}}|D ]:}ˆ d }t |
ƒ| }tj|d	d� t t|ƒt|ƒ¡ qöqèt tˆ d ƒ¡ d S )Nz8https://vision.middlebury.edu/stereo/data/scenes2014/zipr€   rf   )rz   r~   ú-ú/z.zipzDownloading T)ÚurlÚfilenameÚdownload_rootÚremove_finishedc                 3   s    | ]}|t  ˆ d  ¡kV  qdS )rf   N)r‡   rŒ   )r,   rƒ   r   r   r    r.   Z  s     z9Middlebury2014Stereo._download_dataset.<locals>.<genexpr>zEhttps://vision.middlebury.edu/stereo/submit3/zip/MiddEval3-data-F.zip)rœ   rž   rŸ   zMiddEval3/testF)Úexist_okZ	MiddEval3)r   rd   rŠ   Úprintr‰   r   rG   r‡   Úmakedirsr‹   ÚwalkÚshutilÚmoveÚrmtree)r   r   Úbase_urlZ
split_nameZsplit_sceneZ
split_rootr{   Z
scene_nameZ	scene_urlZtest_set_urlZ	scene_dirZscene_namesr-   r‚   Zscene_dst_dirZscene_src_dirr   r   r    r†   C  s8    üz&Middlebury2014Stereo._download_datasetr;   c                    s   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   rJ   r   rA   r_   r   r   r    rA   j  s    z Middlebury2014Stereo.__getitem__)rb   rz   FNF)rC   rD   rE   rF   rŠ   r=   rG   r	   Úboolr   r   r   r   r   r'   r
   r>   r?   r:   r†   rH   rJ   rA   rK   r   r   r   r    rw   w  s|   :öóñä/     ùøA('rw   c                       sb   e Zd ZdZdZdeee ddœ‡ fdd„Zee	e
jdf dœdd	„Zeed
œ‡ fdd„Z‡  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   c                    s¨   t ƒ  ||¡ t|ƒd }ddddg}|D ]x}t|| d ƒ}t|| d ƒ}|  ||¡}|  j|7  _t|| d ƒ}t|| d	 ƒ}	|  ||	¡}
|  j|
7  _q*d S )
Nrª   ZshapenetZ
reflectiveÚtreeÚholez
*_left.jpgz*_right.jpgz*_left.disp.pngz*_right.disp.pngrQ   )r   r   r   Údirsrƒ   rR   rS   r@   rT   rU   rV   r   r   r    r   ¤  s    zCREStereo.__init__r!   c                 C   s<   t jt |¡t jd�}|d d d …d d …f d }d }||fS )N©Zdtypeg      @@©r>   rr   r   r$   Úfloat32rZ   r   r   r    r:   º  s    zCREStereo._read_disparityr;   c                    s   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.
        r^   r_   r   r   r    rA   Á  s    zCREStereo.__getitem__)Nrt   r   r   r   r    rª   x  s   ) ýürª   c                       s`   e Zd ZdZdeeee ddœ‡ fdd„Zeee	j
df dœdd	„Zeed
œ‡ fdd„Z‡  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 (string): 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.
    ÚsingleN)r   Úvariantr   r   c                    sð   t ƒ  ||¡ t|ƒd }t|ddd� dgdgddgdœ| }tdƒd tdƒdœ}|D ]�}t|| ||  d	 ƒ}t|| ||  d
 ƒ}|  j|  ||¡7  _t|| ||  d ƒ}	t|| ||  d ƒ}
|  j|  |	|
¡7  _qZd S )NZFallingThingsr³   )r²   Úmixedr   rg   r²   r´   rM   )r²   r´   z
*.left.jpgz*.right.jpgz*.left.depth.pngz*.right.depth.png©r   r   r   r   rG   r   r9   r   )r   r   r³   r   ÚvariantsZsplit_prefixrƒ   rm   rn   rT   rU   r   r   r    r   ý  s&    ýü
þzFallingThingsStereo.__init__r!   c              
   C   s    t  t |¡¡}t|ƒjd }t|dƒ�n}t |¡}|d d d d }d\}}|| | | t j	¡ }	|	d d d …d d …f }	d }
|	|
fW  5 Q R £ S Q R X d S )Nz_camera_settings.jsonÚrZcamera_settingsr   Zintrinsic_settingsZfx)é   éd   )
r>   rr   r   r$   r   r”   ÚjsonÚloadÚastyper°   )r   r"   ÚdepthZcamera_settings_pathÚfZ
intrinsicsZfocalZbaselineZpixel_constantr[   r\   r   r   r    r:     s    
z#FallingThingsStereo._read_disparityr;   c                    s   t ttƒ  |¡ƒS r]   r^   r_   r   r   r    rA   )  s    zFallingThingsStereo.__getitem__)r²   Nr`   r   r   r   r    r±   Ð  s   ,r±   c                       sb   e Zd ZdZdeeeee ddœ‡ fdd„Zeee	j
df dœd	d
„Zeedœ‡ fdd„Z‡  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 (string): 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ÚcleanN)r   r³   Ú	pass_namer   r   c                    s(  t ƒ  ||¡ t|ƒd }t|ddd� t|ddd� dgdgddgdœ| }|| }td	ƒtd	ƒd	 d	 td	ƒd	 d	 d
œ}|D ] }t|| ||  d d ƒ}t|| ||  d d ƒ}	|  j|  ||	¡7  _t|d ||  d d ƒ}
t|d ||  d d ƒ}|  j|  |
|¡7  _q‚d S )NZ	SceneFlowr³   )rÀ   ÚDrivingÚMonkaarg   rÂ   )rÁ   Úfinalr   Zframes_cleanpassZframes_finalpassrM   )rÄ   rÀ   rÃ   r0   ri   r1   Z	disparityz*.pfmrµ   )r   r   r³   rÂ   r   ZpassesZprefix_directoriesr�   rR   rS   rT   rU   r   r   r    r   n  s,    ýüýzSceneFlowStereo.__init__r!   c                 C   s   t |ƒ}t |¡}d }||fS r*   rW   rZ   r   r   r    r:   “  s    
zSceneFlowStereo._read_disparityr;   c                    s   t ttƒ  |¡ƒS r]   r^   r_   r   r   r    rA   ™  s    zSceneFlowStereo.__getitem__)rÀ   rÁ   Nr`   r   r   r   r    r¿   8  s   8   ûú%r¿   c                       sŠ   e Zd ZdZdZdeeee ddœ‡ fdd„Zee	eef dœd	d
„Z
eee	d e	ejejf f dœdd„Zeedœ‡ fdd„Z‡  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 (string): 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.
    TrÅ   N)r   rÂ   r   r   c           	         sÎ   t ƒ  ||¡ t|ddd� t|ƒd }dgdgddgdœ| }|D ]„}t|d |› d� d	 d
 ƒ}t|d |› d� d	 d
 ƒ}|  j|  ||¡7  _t|d d d	 d
 ƒ}|  j|  |d ¡7  _qDd S )NrÂ   )rÅ   rÁ   r   rg   ZSintelrÅ   rÁ   ZtrainingÚ_leftrM   ri   Ú_rightrV   )r   r   r   r   rG   r   r9   r   )	r   r   rÂ   r   Z
pass_namesr�   rm   rn   ro   r   r   r    r   ×  s    ýüzSintelStereo.__init__r!   c                 C   sˆ   t |ƒ}|j}|j}|jj}t|d |j | ƒ}t|d |j | ƒ}tj |¡sdtd|› d�ƒ‚tj |¡s€td|› d�ƒ‚||fS )NZ
occlusionsZ
outofframezOcclusion mask z does not existzOut of frame mask )r   r“   r”   rG   r‡   rˆ   r‰   r4   )r   r"   ZfpathÚbasenameZscenedirZ	sampledirZocclusion_pathZoutofframe_pathr   r   r    Ú_get_occlussion_mask_pathsë  s    z'SintelStereo._get_occlussion_mask_pathsrk   c           
      C   s¦   |d krdS t jt |¡t jd�}t j|ddd�\}}}|d |d  |d  }t  |d	¡}|  |¡\}}t  t |¡¡d
k}t  t |¡¡d
k}	t  |	|¡}||fS )Nrk   r®   é   éÿÿÿÿ)Zaxisé   é@   i @  )é   r   r   r   )	r>   rr   r   r$   r°   rd   Z	transposerÊ   Úlogical_and)
r   r"   r[   r·   ÚgÚbZocclued_mask_pathZout_of_frame_mask_pathr\   Zoff_maskr   r   r    r:      s    zSintelStereo._read_disparityr;   c                    s   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¨   r_   r   r   r    rA     s    zSintelStereo.__getitem__)rÅ   N)rC   rD   rE   rF   r=   rG   r	   r   r   r
   rÊ   r   r>   r?   r:   rH   rJ   rA   rK   r   r   r   r    rÆ   ¨  s   ,(rÆ   c                       s`   e Zd ZdZdeeee ddœ‡ fdd„Zeee	j
df dœdd	„Zeed
œ‡ fdd„Z‡  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 (string): 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.
    rb   Nrc   c                    sŒ   t ƒ  ||¡ t|ƒd | }t|ddd� t|d d ƒ}t|d d ƒ}|  ||¡| _t|d d ƒ}t|d d	 ƒ}|  ||¡| _d S )
NrÓ   rd   re   rg   rM   zleft.pngz	right.pngzleft_disp.pngzright_disp.png)r   r   r   r   rG   r9   r   r   rv   r   r   r    r   B  s    zInStereo2k.__init__r!   c                 C   s<   t jt |¡t jd�}|d d d …d d …f d }d }||fS )Nr®   g      �@r¯   rZ   r   r   r    r:   Q  s    zInStereo2k._read_disparityr;   c                    s   t ttƒ  |¡ƒS r]   r^   r_   r   r   r    rA   X  s    zInStereo2k.__getitem__)rb   Nr`   r   r   r   r    rÓ   "  s   rÓ   c                       sr   e Zd ZdZdZdeeee ddœ‡ fdd„Zee	e
d e
ejejf f d	œd
d„Zeedœ‡ fdd„Z‡  ZS )ÚETH3DStereoaU  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 (string): 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.
    Trb   Nrc   c           	         s¸   t ƒ  ||¡ t|ddd� t|ƒd }|dkr4dnd}d}t|| d	 d
 ƒ}t|| d	 d ƒ}|  ||¡| _|dkr’tdd„ | jD ƒƒ| _n"t|| d	 d ƒ}|  |d ¡| _d S )Nrd   re   rg   ZETH3Drb   Ztwo_view_trainingZtwo_view_testZtwo_view_training_gtrM   rN   rO   rf   c                 s   s   | ]
}d V  qdS rj   r   r+   r   r   r    r.   ª  s     z'ETH3DStereo.__init__.<locals>.<genexpr>rP   )	r   r   r   r   rG   r9   r   r2   r   )	r   r   rd   r   Zimg_dirZanot_dirrm   rn   ro   r   r   r    r   ›  s    zETH3DStereo.__init__rk   r!   c                 C   sN   |d krdS t |ƒ}t |¡}t|ƒjd }t |¡}t |¡ t	¡}||fS )Nrk   zmask0nocc.png)
rX   r>   rY   r   r”   r   r$   rr   r¼   r©   )r   r"   r[   Z	mask_pathr\   r   r   r    r:   ¯  s    

zETH3DStereo._read_disparityr;   c                    s   t ttƒ  |¡ƒS rs   r¨   r_   r   r   r    rA   »  s    zETH3DStereo.__getitem__)rb   N)rC   rD   rE   rF   r=   rG   r	   r   r   r   r
   r>   r?   r:   rH   rJ   rA   rK   r   r   r   r    rÔ   g  s
   1(rÔ   )-Ú	functoolsrº   r‡   r—   r¤   Úabcr   r   r   Úpathlibr   Útypingr   r   r   r	   r
   r   Únumpyr>   ZPILr   Úutilsr   r   r   Zvisionr   r?   rI   rJ   Ú__all__ÚpartialrX   r   rL   ra   ru   rw   rª   r±   r¿   rÆ   rÓ   rÔ   r   r   r   r    Ú<module>   s:    q@UX  XhpzE