
    %`iR                     >   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 d dlZd dlZd dlmZ ddlmZmZ d	d
lmZ d	dlmZmZ d	dlmZ eej        ej        eej                 eej                 f         Zeej        ej        eej                 f         Z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&de'dej        fdZ(de'deej        ej        f         fdZ)dS )    N)ABCabstractmethod)globPath)AnyCallableOptionalUnion)Image   )
decode_png	read_file   )default_loader)	_read_pfmverify_str_arg)VisionDataset)	KittiFlowSintelFlyingThings3DFlyingChairsHD1Kc            	           e Zd ZdZdefdeeef         dee	         de	ege
f         ddf fdZdedeej        ej        f         fd	Zedefd
            Zdedeeef         fdZdefdZdedej        j        j        fdZ xZS )FlowDatasetFNroot
transformsloaderreturnc                     t                                          |           || _        g | _        g | _        || _        d S )N)r   )super__init__r   
_flow_list_image_list_loader)selfr   r   r   	__class__s       v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchvision/datasets/_optical_flow.pyr"   zFlowDataset.__init__$   sA     	d###$%',.    	file_namec                 ,    |                      |          S N)r%   r&   r*   s     r(   	_read_imgzFlowDataset._read_img2   s    ||I&&&r)   c                     d S r,    r-   s     r(   
_read_flowzFlowDataset._read_flow5   s	     	r)   indexc                    |                      | j        |         d                   }|                      | j        |         d                   }| j        r0|                     | j        |                   }| j        r|\  }}nd }nd x}}| j        |                     ||||          \  }}}}| j        s|||||fS |||fS )Nr   r   )r.   r$   r#   r1   _has_builtin_flow_maskr   )r&   r2   img1img2flowvalid_flow_masks         r(   __getitem__zFlowDataset.__getitem__:   s    ~~d.u5a899~~d.u5a899? 	*??4?5#9::D* '(,%oo"&%))D??&04dDRa0b0b-D$o& 	$/*Et_44t##r)   c                 *    t          | j                  S r,   )lenr$   )r&   s    r(   __len__zFlowDataset.__len__Q   s    4#$$$r)   vc                 R    t           j        j                            | g|z            S r,   )torchutilsdataConcatDataset)r&   r=   s     r(   __rmul__zFlowDataset.__rmul__T   s!    {--tfqj999r)   )__name__
__module____qualname__r4   r   r   strr   r
   r	   r   r"   r   r?   Tensorr.   r   r1   intT1T2r9   r<   r@   rA   rB   rC   __classcell__r'   s   @r(   r   r      sW        #
 *.'5	 CI X& #$	
 
     '3 '5el1J+K ' ' ' ' C    ^$ $r2v $ $ $ $.% % % % %:# :%+"2"@ : : : : : : : :r)   r   c                        e Zd ZdZdddefdeeef         dededee	         d	e	ege
f         d
df fdZded
eeef         f fdZded
ej        fdZ xZS )r   a  `Sintel <http://sintel.is.tue.mpg.de/>`_ Dataset for optical flow.

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

        root
            Sintel
                testing
                    clean
                        scene_1
                        scene_2
                        ...
                    final
                        scene_1
                        scene_2
                        ...
                training
                    clean
                        scene_1
                        scene_2
                        ...
                    final
                        scene_1
                        scene_2
                        ...
                    flow
                        scene_1
                        scene_2
                        ...

    Args:
        root (str or ``pathlib.Path``): Root directory of the Sintel Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        pass_name (string, optional): The pass to use, either "clean" (default), "final", or "both". See link above for
            details on the different passes.
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
            ``valid_flow_mask`` is expected for consistency with other datasets which
            return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    traincleanNr   split	pass_namer   r   r   c                    t                                          |||           t          |dd           t          |dd           |dk    rdd	gn|g}t          |          d
z  }|dz  dz  }|D ]}|dk    rdn|}||z  |z  }	t	          j        |	          D ]}
t          t          t          |	|
z  dz                                }t          t          |          dz
            D ]$}| xj        ||         ||dz            ggz  c_        %|dk    r=| xj        t          t          t          ||
z  dz                                z  c_        d S )Nr   r   r   rQ   rO   testvalid_valuesrR   rP   finalbothr[   rP   rZ   r   trainingr7   rO   *.pngr   *.flo)r!   r"   r   r   oslistdirsortedr   rG   ranger;   r$   r#   )r&   r   rQ   rR   r   r   passes	flow_root	split_dir
image_rootscene
image_listir'   s                r(   r"   zSintel.__init__   s    	dz&IIIug4EFFFFy+<VWWWW'0F':':'7##DzzH$:%.	 		V 		VI&+w&6&6

EI	)I5JJ// V V#DZ%-?'-I)J)J$K$KLL
s:233 M MA$$*Q-AE9J)K(LL$$$G##OOvd3y57H77R3S3S.T.T'U'UUOOV		V 		Vr)   r2   c                 F    t                                          |          S a  Return example at given index.

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

        Returns:
            tuple: A 3-tuple with ``(img1, img2, flow)``.
            The flow is a numpy array of shape (2, H, W) and the images are PIL images.
            ``flow`` is None if ``split="test"``.
            If a valid flow mask is generated within the ``transforms`` parameter,
            a 4-tuple with ``(img1, img2, flow, valid_flow_mask)`` is returned.
        r!   r9   r&   r2   r'   s     r(   r9   zSintel.__getitem__        ww""5)))r)   r*   c                      t          |          S r,   	_read_flor-   s     r(   r1   zSintel._read_flow       ###r)   rD   rE   rF   __doc__r   r   rG   r   r
   r	   r   r"   rI   rJ   rK   r9   npndarrayr1   rL   rM   s   @r(   r   r   X   s       ) )\  )-'5V VCIV V 	V
 X&V #$V 
V V V V V V8* *r2v * * * * * *$C $BJ $ $ $ $ $ $ $ $r)   r   c                        e Zd ZdZdZddefdeeef         dede	e
         de
egef         d	df
 fd
Zded	eeef         f fdZded	eej        ej        f         fdZ xZS )r   a  `KITTI <http://www.cvlibs.net/datasets/kitti/eval_scene_flow.php?benchmark=flow>`__ dataset for optical flow (2015).

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

        root
            KittiFlow
                testing
                    image_2
                training
                    image_2
                    flow_occ

    Args:
        root (str or ``pathlib.Path``): Root directory of the KittiFlow Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    TrO   Nr   rQ   r   r   r   c                 P   t                                          |||           t          |dd           t          |          dz  |dz   z  }t	          t          t          |dz  dz                                }t	          t          t          |dz  d	z                                }|r|st          d
          t          ||          D ]\  }}| xj	        ||ggz  c_	        |dk    r6t	          t          t          |dz  dz                                | _
        d S d S )NrT   rQ   rU   rW   r   ingimage_2z*_10.pngz*_11.pngzZCould not find the Kitti flow images. Please make sure the directory structure is correct.rO   flow_occ)r!   r"   r   r   ra   r   rG   FileNotFoundErrorzipr$   r#   )
r&   r   rQ   r   r   images1images2r5   r6   r'   s
            r(   r"   zKittiFlow.__init__   sA    	dz&IIIug4EFFFFDzzK'55=9c$"2Z"?@@AABBc$"2Z"?@@AABB 	g 	#l   gw// 	/ 	/JD$$.G$T#dZ.?*.L*M*M%N%NOODOOO r)   r2   c                 F    t                                          |          S )a  Return example at given index.

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

        Returns:
            tuple: A 4-tuple with ``(img1, img2, flow, valid_flow_mask)``
            where ``valid_flow_mask`` is a numpy boolean mask of shape (H, W)
            indicating which flow values are valid. The flow is a numpy array of
            shape (2, H, W) and the images are PIL images. ``flow`` and ``valid_flow_mask`` are None if
            ``split="test"``.
        rl   rm   s     r(   r9   zKittiFlow.__getitem__   rn   r)   r*   c                      t          |          S r,   )_read_16bits_png_with_flow_and_valid_maskr-   s     r(   r1   zKittiFlow._read_flow       8CCCr)   )rD   rE   rF   rt   r4   r   r   rG   r   r
   r	   r   r"   rI   rJ   rK   r9   tupleru   rv   r1   rL   rM   s   @r(   r   r      s"        , "
 )-'5P PCIP P X&	P
 #$P 
P P P P P P4* *r2v * * * * * *DC DE"*bj2H,I D D D D D D D Dr)   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ef         f fd
Zdedej        fdZ xZS )r   a  `FlyingChairs <https://lmb.informatik.uni-freiburg.de/resources/datasets/FlyingChairs.en.html#flyingchairs>`_ Dataset for optical flow.

    You will also need to download the FlyingChairs_train_val.txt file from the dataset page.

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

        root
            FlyingChairs
                data
                    00001_flow.flo
                    00001_img1.ppm
                    00001_img2.ppm
                    ...
                FlyingChairs_train_val.txt


    Args:
        root (str or ``pathlib.Path``): Root directory of the FlyingChairs Dataset.
        split (string, optional): The dataset split, either "train" (default) or "val"
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
            ``valid_flow_mask`` is expected for consistency with other datasets which
            return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
    rO   Nr   rQ   r   r   c                    t                                          ||           t          |dd           t          |          dz  }t	          t          t          |dz  dz                                }t	          t          t          |dz  dz                                }d	}t          j        	                    ||z            st          d
          t          j        t          ||z            t          j                  }t          t          |                    D ]a}||         }	|dk    r|	dk    s|dk    rE|	dk    r?| xj        ||         gz  c_        | xj        |d|z           |d|z  dz            ggz  c_        bd S )N)r   r   rQ   )rO   valrW   r   rA   z*.ppmr^   zFlyingChairs_train_val.txtzmThe FlyingChairs_train_val.txt file was not found - please download it from the dataset page (see docstring).)dtyperO   r   r   r   )r!   r"   r   r   ra   r   rG   r_   pathexistsr|   ru   loadtxtint32rb   r;   r#   r$   )r&   r   rQ   r   imagesflowssplit_file_name
split_listri   split_idr'   s             r(   r"   zFlyingChairs.__init__  s   dz:::ug4DEEEEDzzN*S!899::;;tCv 78899::6w~~d_455 	#   ZD?$: ; ;28LLL
s5zz"" 	I 	IA!!}H  X]]8WX==E!H:-  fQUmVAEAI5F%G$HH  		I 	Ir)   r2   c                 F    t                                          |          S )a  Return example at given index.

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

        Returns:
            tuple: A 3-tuple with ``(img1, img2, flow)``.
            The flow is a numpy array of shape (2, H, W) and the images are PIL images.
            ``flow`` is None if ``split="val"``.
            If a valid flow mask is generated within the ``transforms`` parameter,
            a 4-tuple with ``(img1, img2, flow, valid_flow_mask)`` is returned.
        rl   rm   s     r(   r9   zFlyingChairs.__getitem__*  rn   r)   r*   c                      t          |          S r,   rp   r-   s     r(   r1   zFlyingChairs._read_flow9  rr   r)   )rO   N)rD   rE   rF   rt   r   rG   r   r
   r	   r"   rI   rJ   rK   r9   ru   rv   r1   rL   rM   s   @r(   r   r      s         2I IU39- Ic IQYZbQc Ios I I I I I I.* *r2v * * * * * *$C $BJ $ $ $ $ $ $ $ $r)   r   c                        e Zd ZdZddddefdeeef         deded	ed
ee	         de	ege
f         ddf fdZdedeeef         f fdZdedej        fdZ xZS )r   a  `FlyingThings3D <https://lmb.informatik.uni-freiburg.de/resources/datasets/SceneFlowDatasets.en.html>`_ dataset for optical flow.

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

        root
            FlyingThings3D
                frames_cleanpass
                    TEST
                    TRAIN
                frames_finalpass
                    TEST
                    TRAIN
                optical_flow
                    TEST
                    TRAIN

    Args:
        root (str or ``pathlib.Path``): Root directory of the intel FlyingThings3D Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        pass_name (string, optional): The pass to use, either "clean" (default) or "final" or "both". See link above for
            details on the different passes.
        camera (string, optional): Which camera to return images from. Can be either "left" (default) or "right" or "both".
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
            ``valid_flow_mask`` is expected for consistency with other datasets which
            return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    rO   rP   leftNr   rQ   rR   camerar   r   r   c           
         t                                          |||           t          |dd           |                                }t          |dd           dgdgddgd|         }t          d	d
           dk    rddgng}t	          |          dz  }d}	t          j        |||	          D ]\  }t          t          t          ||z  |z  dz                                }
t          fd|
D                       }
t          t          t          |dz  |z  dz                                }t          fd|D                       }|
r|st          d          t          |
|          D ]\  }}t          t          t          |dz                                }t          t          t          |dz                                }t          t          |          dz
            D ]}dk    r:| xj        ||         ||dz            ggz  c_        | xj        ||         gz  c_        Bdk    r<| xj        ||dz            ||         ggz  c_        | xj        ||dz            gz  c_        d S )NrT   rQ   rU   rW   rR   rY   frames_cleanpassframes_finalpassr   )r   rightr[   r[   r   r   r   )into_future	into_pastz*/*c              3   <   K   | ]}t          |          z  V  d S r,   r   ).0	image_dirr   s     r(   	<genexpr>z*FlyingThings3D.__init__.<locals>.<genexpr>z  s.      UUYY& 8UUUUUUr)   optical_flowc              3   B   K   | ]}t          |          z  z  V  d S r,   r   )r   flow_dirr   	directions     r(   r   z*FlyingThings3D.__init__.<locals>.<genexpr>}  s3      ]]xtH~~	9FB]]]]]]r)   zcCould not find the FlyingThings3D flow images. Please make sure the directory structure is correct.r]   z*.pfmr   r   r   )r!   r"   r   upperr   	itertoolsproductra   r   rG   r|   r}   rb   r;   r$   r#   )r&   r   rQ   rR   r   r   r   rc   cameras
directions
image_dirs	flow_dirsr   r   r   r   ri   r   r'   s       `            @r(   r"   zFlyingThings3D.__init__]  s    	dz&IIIug4EFFFFy+<VWWWW()()');<
 
 	 	vx6OPPPP'-'7'767##fXDzz,,1
,5,=fgz,Z,Z 	: 	:(IvyS	)9E)AE)I%J%J K KLLJUUUU*UUUUUJtC~(=(E(M$N$NOOPPI]]]]]S\]]]]]I Y 'K  
 (+:y'A'A 	: 	:#	8SW)<%=%= > >??tC7(:$;$;<<==s5zzA~.. : :A M11((fQiA-G,HH((E!H:5"k11((fQUmVAY-G,HH((E!a%L>9:	:	: 	:r)   r2   c                 F    t                                          |          S rk   rl   rm   s     r(   r9   zFlyingThings3D.__getitem__  rn   r)   r*   c                      t          |          S r,   )r   r-   s     r(   r1   zFlyingThings3D._read_flow  rr   r)   rs   rM   s   @r(   r   r   =  s        D  )-'51: 1:CI1: 1: 	1:
 1: X&1: #$1: 
1: 1: 1: 1: 1: 1:f* *r2v * * * * * *$C $BJ $ $ $ $ $ $ $ $r)   r   c                        e Zd ZdZdZddefdeeef         dede	e
         de
egef         d	df
 fd
Zded	eej        ej        f         fdZded	eeef         f fdZ xZS )r   a  `HD1K <http://hci-benchmark.iwr.uni-heidelberg.de/>`__ dataset for optical flow.

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

        root
            hd1k
                hd1k_challenge
                    image_2
                hd1k_flow_gt
                    flow_occ
                hd1k_input
                    image_2

    Args:
        root (str or ``pathlib.Path``): Root directory of the HD1K Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    TrO   Nr   rQ   r   r   r   c           
         t                                          |||           t          |dd           t          |          dz  }|dk    rt	          d          D ]}t          t          t          |dz  d	z  |d
dz                                }t          t          t          |dz  dz  |d
dz                                }t	          t          |          dz
            D ];}| xj	        ||         gz  c_	        | xj
        ||         ||dz            ggz  c_
        <nt          t          t          |dz  dz  dz                                }	t          t          t          |dz  dz  dz                                }
t          |	|
          D ]\  }}| xj
        ||ggz  c_
        | j
        st          d          d S )NrT   rQ   rU   rW   hd1krO   $   hd1k_flow_gtr{   06dz_*.png
hd1k_inputrz   r   hd1k_challengez*10.pngz*11.pngzTCould not find the HD1K images. Please make sure the directory structure is correct.)r!   r"   r   r   rb   ra   r   rG   r;   r#   r$   r}   r|   )r&   r   rQ   r   r   seq_idxr   r   ri   r~   r   image1image2r'   s                r(   r"   zHD1K.__init__  s    	dz&IIIug4EFFFFDzzF"G 99 E EtC~(=
(JPWMcMcMcMc(c$d$deeffS)<y)HgKaKaKaKa)a%b%b c cdds5zzA~.. E EAOOaz1OO$$&)VAE])C(DD$$$EE T#d-=&=	&II&U"V"VWWXXGT#d-=&=	&II&U"V"VWWXXG"%gw"7"7 7 7  ff%5$66    	#f  	 	r)   r*   c                      t          |          S r,   r   r-   s     r(   r1   zHD1K._read_flow  r   r)   r2   c                 F    t                                          |          S )a  Return example at given index.

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

        Returns:
            tuple: A 4-tuple with ``(img1, img2, flow, valid_flow_mask)`` where ``valid_flow_mask``
            is a numpy boolean mask of shape (H, W)
            indicating which flow values are valid. The flow is a numpy array of
            shape (2, H, W) and the images are PIL images. ``flow`` and ``valid_flow_mask`` are None if
            ``split="test"``.
        rl   rm   s     r(   r9   zHD1K.__getitem__  rn   r)   )rD   rE   rF   rt   r4   r   r   rG   r   r
   r	   r   r"   r   ru   rv   r1   rI   rJ   rK   r9   rL   rM   s   @r(   r   r     s        . "
 )-'5 CI  X&	
 #$ 
     >DC DE"*bj2H,I D D D D* *r2v * * * * * * * * * *r)   r   r*   r   c                    t          | d          5 }t          j        |dd                                          }|dk    rt	          d          t          t          j        |dd                    }t          t          j        |dd                    }t          j        |d	d
|z  |z            }|                    ||d
                              d
dd          cddd           S # 1 swxY w Y   dS )z#Read .flo file in Middlebury formatrbc   )counts   PIEHz)Magic number incorrect. Invalid .flo filez<i4r   z<f4r   r   N)openru   fromfiletobytes
ValueErrorrI   reshape	transpose)r*   fmagicwhrA   s         r(   rq   rq     s0    
i		 8!As!,,,4466GHIIIAuA...//AuA...//{1e1q519555||Aq!$$..q!Q778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   CC--C14C1c                 <   t          t          |                                         t          j                  }|d dd d d d f         |dd d d d f         }}|dz
  dz  }|                                }|                                |                                fS )Nr   i   @   )r   r   tor?   float32boolnumpy)r*   flow_and_validr7   r8   s       r(   r   r      s    	) 4 45588GGN*2A2qqq!!!84nQ111W6M/D5LBD%**,,O ::<<..0000r)   )*r   r_   abcr   r   r   pathlibr   typingr   r	   r
   r   r   ru   r?   PILr   io.imager   r   folderr   r@   r   r   visionr   r   rv   rJ   rK   __all__r   r   r   r   r   r   rG   rq   r   r0   r)   r(   <module>r      s       				 # # # # # # # #             1 1 1 1 1 1 1 1 1 1 1 1            , , , , , , , , " " " " " " , , , , , , , , ! ! ! ! ! !
5;Xbj%98BJ;OOP
5;Xbj%99:7: 7: 7: 7: 7:#} 7: 7: 7:tX$ X$ X$ X$ X$[ X$ X$ X$vCD CD CD CD CD CD CD CDLA$ A$ A$ A$ A$; A$ A$ A$Hc$ c$ c$ c$ c$[ c$ c$ c$LI* I* I* I* I*; I* I* I*X8 8 8 8 8 8"1 1rzSUS]G]A^ 1 1 1 1 1 1r)   