
    bi                     b    d dl Z d dlmZmZmZ d dlZd dlZd dlZddl	m
Z
mZmZ  G d de
      Zy)    N)ListOptionalUnion   )VaeImageProcessoris_valid_imageis_valid_image_imagelistc                       e Zd ZdZd
dee   dee   dej                  fdZ	 ddej                  de	de
ej                  ej                  eej                  j                     f   fd	Zy)VideoProcessorzSimple video processor.Nheightwidthreturnc           
         t        |t              r`t        |d   t        j                        rC|d   j                  dk(  r1t        j                  dt               t        j                  |d      }t        |t              r`t        |d   t        j                        rC|d   j                  dk(  r1t        j                  dt               t        j                  |d      }t        |t        j                  t        j                  f      r|j                  dk(  rt        |      }nYt        |t              rt        |d         st        |      r|g}n,t        |t              rt        |d         r|}nt        d      t        j                  |D cg c]  }| j!                  |||       c}d      }|j#                  dd	d
dd      }|S c c}w )a  
        Preprocesses input video(s).

        Args:
            video (`List[PIL.Image]`, `List[List[PIL.Image]]`, `torch.Tensor`, `np.array`, `List[torch.Tensor]`, `List[np.array]`):
                The input video. It can be one of the following:
                * List of the PIL images.
                * List of list of PIL images.
                * 4D Torch tensors (expected shape for each tensor `(num_frames, num_channels, height, width)`).
                * 4D NumPy arrays (expected shape for each array `(num_frames, height, width, num_channels)`).
                * List of 4D Torch tensors (expected shape for each tensor `(num_frames, num_channels, height,
                  width)`).
                * List of 4D NumPy arrays (expected shape for each array `(num_frames, height, width, num_channels)`).
                * 5D NumPy arrays: expected shape for each array `(batch_size, num_frames, height, width,
                  num_channels)`.
                * 5D Torch tensors: expected shape for each array `(batch_size, num_frames, num_channels, height,
                  width)`.
            height (`int`, *optional*, defaults to `None`):
                The height in preprocessed frames of the video. If `None`, will use the `get_default_height_width()` to
                get default height.
            width (`int`, *optional*`, defaults to `None`):
                The width in preprocessed frames of the video. If `None`, will use get_default_height_width()` to get
                the default width.
        r      zPassing `video` as a list of 5d np.ndarray is deprecated.Please concatenate the list along the batch dimension and pass it as a single 5d np.ndarray)axiszPassing `video` as a list of 5d torch.Tensor is deprecated.Please concatenate the list along the batch dimension and pass it as a single 5d torch.TensorzeInput is in incorrect format. Currently, we only support numpy.ndarray, torch.Tensor, PIL.Image.Image)r   r   )dim   r         )
isinstancelistnpndarrayndimwarningswarnFutureWarningconcatenatetorchTensorcatr   r	   
ValueErrorstack
preprocesspermute)selfvideor   r   imgs        T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/video_processor.pypreprocess_videozVideoProcessor.preprocess_video   sx   2 eT"z%(BJJ'GERSHMM]^L^MMn
 NN5q1EeT"z%(ELL'IeTUhmm_`N`MMp
 IIe!,E
 ebjj%,,78UZZ1_KEt$a)AE]^cEdGEt$)A%()KEw  Y^_RUT__Su_M_efg aAq!, `s   Gr'   output_typec                 R   |j                   d   }g }t        |      D ]<  }||   j                  dddd      }| j                  ||      }|j	                  |       > |dk(  rt        j                  |      }|S |dk(  rt        j                  |      }|S |dk(  st        | d      |S )	z
        Converts a video tensor to a list of frames for export.

        Args:
            video (`torch.Tensor`): The video as a tensor.
            output_type (`str`, defaults to `"np"`): Output type of the postprocessed `video` tensor.
        r   r   r   r   r   ptpilz9 does not exist. Please choose one of ['np', 'pt', 'pil'])	shaperanger%   postprocessappendr   r#   r   r"   )r&   r'   r+   
batch_sizeoutputs	batch_idx	batch_vidbatch_outputs           r)   postprocess_videoz VideoProcessor.postprocess_videoY   s     [[^
z* 	)Ii(00Aq!<I++I{CLNN<(	)
 $hhw'G  D kk'*G  %},efgg    )NN)r   )__name__
__module____qualname____doc__r   intr   r    r*   strr   r   r   r   PILImager8    r9   r)   r   r      sw    ";hsm ;8TW= ;didpdp ;| 7;\\03	rzz5<<ciioo)>>	?r9   r   )r   typingr   r   r   numpyr   r@   r   image_processorr   r   r	   r   rB   r9   r)   <module>rF      s.     ( (  
  X XX& Xr9   