
    QЦiL                     T   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Jr  S SKrS SKrS SKJr  SSKJrJr  S	S
KJrJr  S	SKJr  \\R$                  \R$                  \\R6                     \\R6                     4   r\\R$                  \R$                  \\R6                     4   rS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\R6                  4S jr&S\%S\\R6                  \R6                  4   4S jr'g)    N)ABCabstractmethod)globPath)CallableListOptionalTupleUnion)Image   )
decode_png	read_file   )	_read_pfmverify_str_arg)VisionDataset)	KittiFlowSintelFlyingThings3DFlyingChairsHD1Kc                     ^  \ rS rSrSrSS\\\4   S\\	   SS4U 4S jjjr
S\S\R                  4S	 jr\S\4S
 j5       rS\S\\\4   4S jrS\4S jrS\S\R*                  R,                  R.                  4S jrSrU =r$ )FlowDataset   FNroot
transformsreturnc                 H   > [         TU ]  US9  X l        / U l        / U l        g )N)r   )super__init__r   
_flow_list_image_list)selfr   r   	__class__s      a/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torchvision/datasets/_optical_flow.pyr"   FlowDataset.__init__#   s'    d#$%',.    	file_namec                 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FlowDataset._read_img+   s/    jj#88u++e$C
r)   c                     g N r%   r*   s     r'   
_read_flowFlowDataset._read_flow1   s     	r)   indexc                    U R                  U R                  U   S   5      nU R                  U R                  U   S   5      nU R                  (       a7  U R                  U R                  U   5      nU R                  (       a  Uu  pEOS nOS =pEU R
                  b  U R                  X#XE5      u  p#pEU R                  (       d  Ub  X#XE4$ X#U4$ )Nr   r   )r1   r$   r#   r7   _has_builtin_flow_maskr   )r%   r9   img1img2flowvalid_flow_masks         r'   __getitem__FlowDataset.__getitem__6   s    ~~d..u5a89~~d..u5a89????4??5#9:D**(,%o"&%))D??&04D0b-D&&/*Et44t##r)   c                 ,    [        U R                  5      $ r4   )lenr$   )r%   s    r'   __len__FlowDataset.__len__M   s    4##$$r)   vc                 \    [         R                  R                  R                  U /U-  5      $ r4   )torchutilsdataConcatDataset)r%   rF   s     r'   __rmul__FlowDataset.__rmul__P   s#    {{--tfqj99r)   )r#   r$   r   r4   )__name__
__module____qualname____firstlineno__r;   r   strr   r
   r   r"   r   r1   r   r7   intT1T2r@   rD   rH   rI   rJ   rK   rL   __static_attributes____classcell__r&   s   @r'   r   r      s     #/U39- /8H;M /Y] / /3 5;;  C  $ $r2v $.% %:# :%++"2"2"@"@ : :r)   r   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\\\4   4U 4S jjrS\S\R                   4S jrSrU =r$ )r   T   aD  `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`.
Nr   split	pass_namer   r   c                 8  > [         TU ]  XS9  [        USSS9  [        USSS9  US:X  a  SS	/OU/n[        U5      S
-  nUS-  S-  nU H  nUS:X  a  SOUnX-  U-  n[        R
                  " U5       H  n	[        [        [        X-  S-  5      5      5      n
[        [        U
5      S-
  5       H"  nU =R                  X   XS-      //-  sl        M$     US:X  d  Mj  U =R                  [        [        [        Xi-  S-  5      5      5      -  sl        M     M     g )Nr   r   r[   traintestvalid_valuesr\   cleanfinalbothrg   re   rf   r   trainingr>   r`   *.pngr   *.flo)r!   r"   r   r   oslistdirsortedr   rR   rangerC   r$   r#   )r%   r   r[   r\   r   passes	flow_root	split_dir
image_rootscene
image_listir&   s               r'   r"   Sintel.__init__}   s    	d:ug4EFy+<VW'0F':'7#DzH$:%.	I&+w&6
EI)I5JJ/#DZ-?'-I)J$KL
s:23A$$*-E9J)K(LL$ 4 G#OOvd3y7H77R3S.T'UUO 0  r)   r9   c                 "   > [         TU ]  U5      $ 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%   r9   r&   s     r'   r@   Sintel.__getitem__        w"5))r)   r*   c                     [        U5      $ r4   	_read_flor6   s     r'   r7   Sintel._read_flow       ##r)   r5   )r`   re   NrN   rO   rP   rQ   __doc__r   rR   r   r
   r   r"   rS   rT   rU   r@   npndarrayr7   rV   rW   rX   s   @r'   r   r   T   s    &V  )-VCIV V 	V
 X&V 
V V6* *r2v *$C $BJJ $ $r)   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\\\4   4U 4S jjrS\S\\R$                  \R$                  4   4S jrSrU =r$ )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.
TNr   r[   r   r   c                   > [         TU ]  XS9  [        USSS9  [        U5      S-  US-   -  n[	        [        [        US-  S-  5      5      5      n[	        [        [        US-  S	-  5      5      5      nU(       a  U(       d  [        S
5      e[        XE5       H  u  pgU =R                  Xg//-  sl	        M     US:X  a)  [	        [        [        US-  S-  5      5      5      U l
        g g )Nr^   r[   r_   rb   r   ingimage_2z*_10.pngz*_11.pngzZCould not find the Kitti flow images. Please make sure the directory structure is correct.r`   flow_occ)r!   r"   r   r   rm   r   rR   FileNotFoundErrorzipr$   r#   )	r%   r   r[   r   images1images2r<   r=   r&   s	           r'   r"   KittiFlow.__init__   s    d:ug4EFDzK'55=9c$"2Z"?@ABc$"2Z"?@ABg#l  g/JD$. 0 G$T#dZ.?*.L*M%NODO r)   r9   c                 "   > [         TU ]  U5      $ )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"``.
ry   rz   s     r'   r@   KittiFlow.__getitem__   r|   r)   r*   c                     [        U5      $ r4   )_read_16bits_png_with_flow_and_valid_maskr6   s     r'   r7   KittiFlow._read_flow       8CCr)   )r#   r`   N)rN   rO   rP   rQ   r   r;   r   rR   r   r
   r   r"   rS   rT   rU   r@   r   r   r   r7   rV   rW   rX   s   @r'   r   r      s    & "PU39- Pc PQYZbQc Pos P P(* *r2v *DC DE"**bjj2H,I D Dr)   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\\\4   4U 4S
 jjrS\S\R                   4S jrSrU =r$ )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`.
Nr   r[   r   r   c                   > [         T
U ]  XS9  [        USSS9  [        U5      S-  n[	        [        [        US-  S-  5      5      5      n[	        [        [        US-  S-  5      5      5      nS	n[        R                  R                  X-  5      (       d  [        S
5      e[        R                  " [        X-  5      [        R                  S9n[        [        U5      5       Hb  nXx   n	US:X  a  U	S:X  d  US:X  d  M  U	S:X  d  M#  U =R                   XX   /-  sl        U =R"                  USU-     USU-  S-      //-  sl        Md     g )Nr^   r[   )r`   valrb   r   rJ   z*.ppmrj   zFlyingChairs_train_val.txtzmThe FlyingChairs_train_val.txt file was not found - please download it from the dataset page (see docstring).)dtyper`   r   r   r   )r!   r"   r   r   rm   r   rR   rk   pathexistsr   r   loadtxtint32rn   rC   r#   r$   )r%   r   r[   r   imagesflowssplit_file_name
split_listru   split_idr&   s             r'   r"   FlyingChairs.__init__  s&   d:ug4DEDzN*S!89:;tCv 789:6ww~~d455#  ZZD$: ;288L
s5z"A!}H X]8WX=EH:-  fQUmVAEAI5F%G$HH 	 #r)   r9   c                 "   > [         TU ]  U5      $ )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.
ry   rz   s     r'   r@   FlyingChairs.__getitem__  r|   r)   r*   c                     [        U5      $ r4   r~   r6   s     r'   r7   FlyingChairs._read_flow(  r   r)   r5   r   r   rX   s   @r'   r   r      s|    2IU39- Ic IQYZbQc Ios I I.* *r2v *$C $BJJ $ $r)   r   c                      ^  \ rS rSrSr    SS\\\4   S\S\S\S\\	   S	S4U 4S
 jjjr
S\S	\\\4   4U 4S jjrS\S	\R                   4S jrSrU =r$ )r   i,  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`.
Nr   r[   r\   camerar   r   c           
      &  >^^ [         TU ]  XS9  [        USSS9  UR                  5       n[        USSS9  S/S/SS/S.U   n[        TS	S
S9  TS:X  a  SS/OT/n[	        U5      S-  nSn[
        R                  " XgU5       GH  u  nmm[        [        [        X-  U-  S-  5      5      5      n	[        U4S jU	 5       5      n	[        [        [        US-  U-  S-  5      5      5      n
[        UU4S jU
 5       5      n
U	(       a  U
(       d  [        S5      e[        X5       H  u  p[        [        [        US-  5      5      5      n[        [        [        US-  5      5      5      n[        [        U5      S-
  5       H  nTS:X  a9  U =R                  X   XS-      //-  sl        U =R                  X   /-  sl        MB  TS:X  d  MJ  U =R                  XS-      X   //-  sl        U =R                  XS-      /-  sl        M     M     GM     g )Nr^   r[   r_   rb   r\   rd   frames_cleanpassframes_finalpassr   )leftrightrg   rg   r   r   r   )into_future	into_pastz*/*c              3   @   >#    U  H  n[        U5      T-  v   M     g 7fr4   r   ).0	image_dirr   s     r'   	<genexpr>*FlyingThings3D.__init__.<locals>.<genexpr>e  s     U*YY& 8*s   optical_flowc              3   F   >#    U  H  n[        U5      T-  T-  v   M     g 7fr4   r   )r   flow_dirr   	directions     r'   r   r   h  s!     ]S\xtH~	9FBS\s   !zcCould not find the FlyingThings3D flow images. Please make sure the directory structure is correct.ri   z*.pfmr   r   r   )r!   r"   r   upperr   	itertoolsproductrm   r   rR   r   r   rn   rC   r$   r#   )r%   r   r[   r\   r   r   ro   cameras
directions
image_dirs	flow_dirsr   r   r   r   ru   r   r&   s       `           @r'   r"   FlyingThings3D.__init__I  s    	d:ug4EFy+<VW()()');<
 	 	vx6OP'-'767#fXDz,,1
,5,=,=fz,Z(IvyS)9E)AE)I%J KLJU*UUJtC~(=(E(M$NOPI]S\]]IY'K 
 (+:'A#	SW)<%= >?tC7(:$;<=s5zA~.A M1((fiA-G,HH(EH:5"k1((fUmVY-G,HH(Ea%L>9 / (B -[r)   r9   c                 "   > [         TU ]  U5      $ rx   ry   rz   s     r'   r@   FlyingThings3D.__getitem__{  r|   r)   r*   c                     [        U5      $ r4   )r   r6   s     r'   r7   FlyingThings3D._read_flow  r   r)   r5   )r`   re   r   Nr   rX   s   @r'   r   r   ,  s    >  )-0:CI0: 0: 	0:
 0: X&0: 
0: 0:d* *r2v *$C $BJJ $ $r)   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                  \R                  4   4S jrS\S\\\4   4U 4S jjrSrU =r$ )r   i  au  `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.
TNr   r[   r   r   c           
        > [         TU ]  XS9  [        USSS9  [        U5      S-  nUS:X  a  [	        S5       H  n[        [        [        US-  S	-  US
 S3-  5      5      5      n[        [        [        US-  S-  US
 S3-  5      5      5      n[	        [        U5      S-
  5       H:  nU =R                  XW   /-  sl	        U =R                  Xg   XgS-      //-  sl
        M<     M     Ow[        [        [        US-  S-  S-  5      5      5      n[        [        [        US-  S-  S-  5      5      5      n	[        X5       H  u  pU =R                  X//-  sl
        M     U R                  (       d  [        S5      eg )Nr^   r[   r_   rb   hd1kr`   $   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   rn   rm   r   rR   rC   r#   r$   r   r   )r%   r   r[   r   seq_idxr   r   ru   r   r   image1image2r&   s               r'   r"   HD1K.__init__  s}   d:ug4EFDzF"G 9tC~(=
(JPWX[}\bMc(c$defS)<y)HgVY]Z`Ka)a%b cds5zA~.AOOz1O$$&)VE])C(DD$ / % T#d-=&=	&II&U"VWXGT#d-=&=	&II&U"VWXG"%g"7  f%5$66  #8 #f   r)   r*   c                     [        U5      $ r4   r   r6   s     r'   r7   HD1K._read_flow  r   r)   r9   c                 "   > [         TU ]  U5      $ )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"``.
ry   rz   s     r'   r@   HD1K.__getitem__  r|   r)   r5   r   )rN   rO   rP   rQ   r   r;   r   rR   r   r
   r   r"   r   r   r   r7   rS   rT   rU   r@   rV   rW   rX   s   @r'   r   r     s    ( "U39- c QYZbQc os  2DC DE"**bjj2H,I D* *r2v * *r)   r   r*   r   c           	         [        U S5       n[        R                  " USSS9R                  5       nUS:w  a  [	        S5      e[        [        R                  " USSS95      n[        [        R                  " USSS95      n[        R                  " US	S
U-  U-  S9nUR                  XCS
5      R                  S
SS5      sSSS5        $ ! , (       d  f       g= f)z#Read .flo file in Middlebury formatrbc   )counts   PIEHz)Magic number incorrect. Invalid .flo filez<i4r   z<f4r   r   N)r-   r   fromfiletobytes
ValueErrorrS   reshape	transpose)r*   fmagicwhrJ   s         r'   r   r     s     
i	!As!,446GHIIAuA./AuA./{{1e1q5195||A!$..q!Q7 
		s   B2C		
Cc                    [        [        U 5      5      R                  [        R                  5      nUS S2S S 2S S 24   USS S 2S S 24   p2US-
  S-  nUR                  5       nUR                  5       UR                  5       4$ )Nr   i   @   )r   r   torH   float32boolnumpy)r*   flow_and_validr>   r?   s       r'   r   r     sy    	) 4588GN*2A2q!84nQ1W6M/5LBD%**,O ::<..000r)   )(r   rk   abcr   r   r   pathlibr   typingr   r	   r
   r   r   r   r   rH   PILr   io.imager   r   rI   r   r   visionr   r   rT   rU   __all__r   r   r   r   r   r   rR   r   r   r5   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)   