
    
3j                      `    S SK r S SKrS SKrS SKrS SKJs  Jr  SSK	J
r
JrJr   " S S\
5      rg)    N   )VaeImageProcessoris_valid_imageis_valid_image_imagelistc                   ^   \ rS rSrSrSS\S-  S\S-  S\R                  4S jjr SS\R                  S	\	S\
R                  \R                  -  \\R                  R                     -  4S
 jjr\S\S\S\S\\\4   4S j5       r\S\R                  S\S\S\R                  4S j5       rSrg)VideoProcessor   zSimple video processor.Nheightwidthreturnc                    [        U[        5      (       ae  [        US   [        R                  5      (       aC  US   R                  S:X  a0  [
        R                  " S[        5        [        R                  " USS9n[        U[        5      (       ae  [        US   [        R                  5      (       aC  US   R                  S:X  a0  [
        R                  " S[        5        [        R                  " USS9n[        U[        R                  [        R                  45      (       a  UR                  S:X  a  [        U5      nOr[        U[        5      (       a  [        US   5      (       d  [        U5      (       a  U/nO6[        U[        5      (       a  [        US   5      (       a  UnO[        S5      e[        R                  " U Vs/ s H  oPR                   " U4X#S.UD6PM     snSS9nUR#                  SS	S
SS5      nU$ s  snf )a9  
Preprocesses input video(s). Keyword arguments will be forwarded to `VaeImageProcessor.preprocess`.

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.

Returns:
    `torch.Tensor` of shape `(batch_size, num_channels, num_frames, height, width)`:
        A 5D tensor holding the batched channels-first video(s).
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   kwargsimgs         S/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/video_processor.pypreprocess_videoVideoProcessor.preprocess_video   s   : eT""z%(BJJ'G'GERSHMM]^L^MMn
 NN5q1EeT""z%(ELL'I'IeTUhmm_`N`MMp
 IIe!,E
 ebjj%,,788UZZ1_KEt$$a)A)AE]^cEdEdGEt$$)A%()K)KEw  chich\___SWWPVWchiopq aAq!, js   Hr%   output_typec                 `   UR                   S   n/ n[        U5       H=  nX   R                  SSSS5      nU R                  " Xr40 UD6nUR	                  U5        M?     US:X  a  [
        R                  " U5      nU$ US:X  a  [        R                  " U5      nU$ US:X  d  [        U S35      eU$ )	a  
Converts a video tensor to a list of frames for export. Keyword arguments will be forwarded to
`VaeImageProcessor.postprocess`.

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+   r&   
batch_sizeoutputs	batch_idx	batch_vidbatch_outputs	            r(   postprocess_video VideoProcessor.postprocess_video]   s     [[^
z*I(00Aq!<I++IMfMLNN<( +
 $hhw'G  D kk'*G  %},efgg    ratiosc                    ^ [        X-  5      m[        UR                  5       U4S jS9nX#   n[        US   5      [        US   5      4$ )aN  
Returns the binned height and width based on the aspect ratio.

Args:
    height (`int`): The height of the image.
    width (`int`): The width of the image.
    ratios (`dict`): A dictionary where keys are aspect ratios and values are tuples of (height, width).

Returns:
    `tuple[int, int]`: The closest binned height and width.
c                 2   > [        [        U 5      T-
  5      $ )N)absfloat)ratioars    r(   <lambda>:VideoProcessor.classify_height_width_bin.<locals>.<lambda>   s    SuPRAR=Sr:   )keyr   r   )r?   minkeysint)r
   r   r;   closest_ratio
default_hwrA   s        @r(   classify_height_width_bin(VideoProcessor.classify_height_width_binx   sI     6>"FKKM/ST*
:a=!3z!}#555r:   samples	new_width
new_heightc                    U R                   S   U R                   S   pCX2:w  d  XA:w  a  [        X#-  X-  5      n[        XE-  5      n[        X5-  5      nU R                   u  ppnU R                  SSSSS5      R	                  X-  XU5      n [
        R                  " XU4SSS9n Xa-
  S-  nX-   nXr-
  S-  nX-   nU S	S	2S	S	2UU2X24   n U R	                  XXU5      R                  SSSSS5      n U $ )
a  
Resizes and crops a tensor of videos to the specified dimensions.

Args:
    samples (`torch.Tensor`):
        A tensor of shape (N, C, T, H, W) where N is the batch size, C is the number of channels, T is the
        number of frames, H is the height, and W is the width.
    new_width (`int`): The desired width of the output videos.
    new_height (`int`): The desired height of the output videos.

Returns:
    `torch.Tensor`: A tensor containing the resized and cropped videos.
r   r   r   r   r   bilinearF)sizemodealign_cornersN)r/   maxrG   r#   reshapeFinterpolate)rL   rM   rN   orig_height
orig_widthr@   resized_widthresized_heightncthwstart_xend_xstart_yend_ys                    r(   resize_and_crop_tensor%VideoProcessor.resize_and_crop_tensor   s     #*--"2GMM!4DZ $
(?
0)2HIE
 23M !45N $MMMA!ooaAq!4<<QUA!LG mm}=J^cG
 %0Q6G'E%2q8G(EaGEM7=@AG ooaA9EMMaQRTUWXZ[\Gr:    )NN)r   )__name__
__module____qualname____firstlineno____doc__rG   r   r   r)   strr   r   r   PILImager8   staticmethoddicttuplerJ   re   __static_attributes__rg   r:   r(   r   r      s    "?cDj ?d
 ?hmhtht ?D 7;\\03	ell	"T#))//%:	:6 6# 6c 64 6ERUWZRZO 6 6" ) ) )RU )Z_ZfZf ) )r:   r   )r   numpyr   rn   r   torch.nn.functionalnn
functionalrV   image_processorr   r   r   r   rg   r:   r(   <module>ry      s.      
    X X[& [r:   