
    PhN                     L   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 d dlZd dlZd dl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j4                     eej4                     f   Zeej$                  ej$                  eej4                     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j4                  fdZ%de$deej4                  ej4                  f   fdZ&y)    N)ABCabstractmethod)globPath)CallableListOptionalTupleUnion)Image   )_read_png_16   )	_read_pfmverify_str_arg)VisionDataset)	KittiFlowSintelFlyingThings3DFlyingChairsHD1Kc                        e Zd ZdZddedee   ddf fdZdedej                  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returnc                 P    t         |   |       || _        g | _        g | _        y )N)r   )super__init__r   
_flow_list_image_list)selfr   r   	__class__s      mC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torchvision/datasets/_optical_flow.pyr    zFlowDataset.__init__$   s)    d#$%',.    	file_namec                 p    t        j                  |      }|j                  dk7  r|j                  d      }|S )NRGB)r   openmodeconvert)r#   r'   imgs      r%   	_read_imgzFlowDataset._read_img,   s/    jj#88u++e$C
r&   c                      y N r#   r'   s     r%   
_read_flowzFlowDataset._read_flow2   s     	r&   indexc                    | j                  | j                  |   d         }| j                  | j                  |   d         }| j                  r3| j                  | j                  |         }| j                  r|\  }}nd }nd x}}| j
                  | j                  ||||      \  }}}}| j                  s|||||fS |||fS )Nr   r   )r.   r"   r!   r3   _has_builtin_flow_maskr   )r#   r4   img1img2flowvalid_flow_masks         r%   __getitem__zFlowDataset.__getitem__7   s    ~~d..u5a89~~d..u5a89????4??5#9:D**(,%o"&%))D???&04dDRa0b-D$o&&/*Et_44t##r&   c                 ,    t        | j                        S r0   )lenr"   )r#   s    r%   __len__zFlowDataset.__len__N   s    4##$$r&   vc                 \    t         j                  j                  j                  | g|z        S r0   )torchutilsdataConcatDataset)r#   r?   s     r%   __rmul__zFlowDataset.__rmul__Q   s#    {{--tfqj99r&   r0   )__name__
__module____qualname__r6   strr
   r   r    r   r.   r   r3   intr   T1T2r;   r>   rA   rB   rC   rD   rE   __classcell__r$   s   @r%   r   r      s     #/S /hx.@ /D /3 5;;  C  $ $r2v $.% %:# :%++"2"2"@"@ :r&   r   c                        e Zd ZdZ	 	 	 ddede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  `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 (string): 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`.
    Nr   split	pass_namer   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(  si| xj                  t        t        t        ||	z  dz                    z  c_          y )Nr   r   rP   traintestvalid_valuesrQ   cleanfinalbothr\   rZ   r[   r   trainingr9   rU   *.pngr   *.flo)r   r    r   r   oslistdirsortedr   rI   ranger=   r"   r!   )r#   r   rP   rQ   r   passes	flow_root	split_dir
image_rootscene
image_listir$   s               r%   r    zSintel.__init__~   s*    	dz:ug4EFy+<VW'0F':'7#DzH$:%.	I&+w&6
EI	)I5JJ/#DZ%-?'-I)J$KL
s:23A$$*Q-AE9J)K(LL$ 4 G#OOvd3y57H77R3S.T'UUO 0  r&   r4   c                 "    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   r;   r#   r4   r$   s     r%   r;   zSintel.__getitem__        w"5))r&   r'   c                     t        |      S r0   	_read_flor2   s     r%   r3   zSintel._read_flow       ##r&   )rU   rZ   NrF   rG   rH   __doc__rI   r
   r   r    rJ   r   rK   rL   r;   npndarrayr3   rM   rN   s   @r%   r   r   U   s    &V  )-VV V 	V
 X&V 
V6* *r2v *$C $BJJ $r&   r   c            	            e Zd ZdZdZdde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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 (string): 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.
    TNr   rP   r   r   c                    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(  r)t	        t        t        |dz  dz                    | _
        y y )NrS   rP   rT   rW   r   ingimage_2z*_10.pngz*_11.pngzZCould not find the Kitti flow images. Please make sure the directory structure is correct.rU   flow_occ)r   r    r   r   rb   r   rI   FileNotFoundErrorzipr"   r!   )	r#   r   rP   r   images1images2r7   r8   r$   s	           r%   r    zKittiFlow.__init__   s    dz:ug4EFDzK'55=9c$"2Z"?@ABc$"2Z"?@ABg#l  gw/JD$$. 0 G$T#dZ.?*.L*M%NODO r&   r4   c                 "    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"``.
        rm   rn   s     r%   r;   zKittiFlow.__getitem__   ro   r&   r'   c                     t        |      S r0   )_read_16bits_png_with_flow_and_valid_maskr2   s     r%   r3   zKittiFlow._read_flow       8CCr&   rU   N)rF   rG   rH   ru   r6   rI   r
   r   r    rJ   r   rK   rL   r;   r   rv   rw   r3   rM   rN   s   @r%   r   r      s    & "PS P PHXDV Pbf P(* *r2v *DC DE"**bjj2H,I Dr&   r   c            	       ~     e Zd ZdZdde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 (string): 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`.
    Nr   rP   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                  j                  ||z        st        d
      t        j                  t        ||z        t        j                        }t        t        |            D ]]  }||   }	|dk(  r|	dk(  s|dk(  s|	dk(  s| xj                   ||   gz  c_        | xj"                  |d|z     |d|z  dz      ggz  c_        _ y )NrS   rP   )rU   valrW   r   rC   z*.ppmr_   zFlyingChairs_train_val.txtzmThe FlyingChairs_train_val.txt file was not found - please download it from the dataset page (see docstring).)dtyperU   r   r   r   )r   r    r   r   rb   r   rI   r`   pathexistsr}   rv   loadtxtint32rc   r=   r!   r"   )r#   r   rP   r   imagesflowssplit_file_name
split_listrj   split_idr$   s             r%   r    zFlyingChairs.__init__  s-   dz:ug4DEDzN*S!89:;tCv 789:6ww~~d_45#  ZZD?$: ;288L
s5z"A!!}H X]8WX=E!H:-  fQUmVAEAI5F%G$HH 	 #r&   r4   c                 "    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.
        rm   rn   s     r%   r;   zFlyingChairs.__getitem__  ro   r&   r'   c                     t        |      S r0   rq   r2   s     r%   r3   zFlyingChairs._read_flow)  rs   r&   r   rt   rN   s   @r%   r   r      sf    2IS I IHXDV Ibf I.* *r2v *$C $BJJ $r&   r   c                        e Zd ZdZ	 	 	 	 ddedede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  `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 (string): 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`.
    Nr   rP   rQ   camerar   r   c           
      $   t         |   ||       t        |dd       |j                         }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_        Cdk(  sI| xj                  ||dz      ||   ggz  c_        | xj                  ||dz      gz  c_           y )NrS   rP   rT   rW   rQ   rY   frames_cleanpassframes_finalpassr   )leftrightr\   r\   r   r   r   )into_future	into_pastz*/*c              3   :   K   | ]  }t        |      z    y wr0   r   ).0	image_dirr   s     r%   	<genexpr>z*FlyingThings3D.__init__.<locals>.<genexpr>f  s     U*YY& 8*s   optical_flowc              3   @   K   | ]  }t        |      z  z    y wr0   r   )r   flow_dirr   	directions     r%   r   z*FlyingThings3D.__init__.<locals>.<genexpr>i  s"     ]S\xtH~	9FBS\s   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productrb   r   rI   r}   r~   rc   r=   r"   r!   )r#   r   rP   rQ   r   r   rd   cameras
directions
image_dirs	flow_dirsr   r   r   r   rj   r   r$   s       `           @r%   r    zFlyingThings3D.__init__J  s"    	dz:ug4EFy+<VW()()');<
 	 	vx6OP'-'767#fXDz,,1
,5,=,=fgz,Z(IvyS	)9E)AE)I%J KLJU*UUJtC~(=(E(M$NOPI]S\]]IY'K 
 (+:y'A#	8SW)<%= >?tC7(:$;<=s5zA~.A M1((fQiA-G,HH(E!H:5"k1((fQUmVAY-G,HH(E!a%L>9 / (B -[r&   r4   c                 "    t         |   |      S rl   rm   rn   s     r%   r;   zFlyingThings3D.__getitem__|  ro   r&   r'   c                     t        |      S r0   )r   r2   s     r%   r3   zFlyingThings3D._read_flow  rs   r&   )rU   rZ   r   Nrt   rN   s   @r%   r   r   -  s    >  )-0:0: 0: 	0:
 0: X&0: 
0:d* *r2v *$C $BJJ $r&   r   c            	            e Zd ZdZdZddededee   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 (string): 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.
    TNr   rP   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_
        >  nxt        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      y )NrS   rP   rT   rW   hd1krU   $   hd1k_flow_gtr|   06dz_*.png
hd1k_inputr{   r   hd1k_challengez*10.pngz*11.pngzTCould not find the HD1K images. Please make sure the directory structure is correct.)r   r    r   r   rc   rb   r   rI   r=   r!   r"   r~   r}   )r#   r   rP   r   seq_idxr   r   rj   r   r   image1image2r$   s               r%   r    zHD1K.__init__  s   dz:ug4EFDzF"G 9tC~(=
(JPWX[}\bMc(c$defS)<y)HgVY]Z`Ka)a%b cds5zA~.AOOaz1O$$&)VAE])C(DD$ / % T#d-=&=	&II&U"VWXGT#d-=&=	&II&U"VWXG"%gw"7  ff%5$66  #8 #f   r&   r'   c                     t        |      S r0   r   r2   s     r%   r3   zHD1K._read_flow  r   r&   r4   c                 "    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"``.
        rm   rn   s     r%   r;   zHD1K.__getitem__  ro   r&   r   )rF   rG   rH   ru   r6   rI   r
   r   r    r   rv   rw   r3   rJ   r   rK   rL   r;   rM   rN   s   @r%   r   r     s    ( "S  HXDV bf 2DC DE"**bjj2H,I D* *r2v * *r&   r   r'   r   c                    t        | d      5 }t        j                  |dd      j                         }|dk7  rt	        d      t        t        j                  |dd            }t        t        j                  |dd            }t        j                  |d	d
|z  |z        }|j                  ||d
      j                  d
dd      cddd       S # 1 sw Y   yxY w)z#Read .flo file in Middlebury formatrbc   )counts   PIEHz)Magic number incorrect. Invalid .flo filez<i4r   z<f4r   r   N)r*   rv   fromfiletobytes
ValueErrorrJ   reshape	transpose)r'   fmagicwhrC   s         r%   rr   rr     s     
i	!As!,446GHIIAuA./AuA./{{1e1q5195||Aq!$..q!Q7 
		s   B:CCc                     t        |       j                  t        j                        }|d dd d d d f   |dd d d d f   }}|dz
  dz  }|j	                         }|j                         |j                         fS )Nr   i   @   )r   torA   float32boolnumpy)r'   flow_and_validr9   r:   s       r%   r   r     sv    !),//>N*2A2q!84nQ1W6M/D5LBD%**,O ::<..000r&   )'r   r`   abcr   r   r   pathlibr   typingr   r	   r
   r   r   r   rv   rA   PILr   io.imager   rB   r   r   visionr   rw   rK   rL   __all__r   r   r   r   r   r   rI   rr   r   r1   r&   r%   <module>r      s    	 #   9 9    # , ! 5;;Xbjj%98BJJ;OOP
5;;Xbjj%99:4:#} 4:nT$[ T$n:D :DzA$; A$H_$[ _$D@*; @*F8 8 8"1 1rzzSUS]S]G]A^ 1r&   