o
    pi                     @   s\   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 de
ZdS )    N)ListOptionalUnion   )VaeImageProcessoris_valid_imageis_valid_image_imagelistc                   @   sd   e Zd ZdZddee dee dejfddZ	dd	ejd
e	de
ejejeejj f fddZdS )VideoProcessorzSimple video processor.Nheightwidthreturnc                    s*  t |tr!t |d tjr!|d jdkr!tdt tj|dd}t |trBt |d t	j
rB|d jdkrBtdt t	j|dd}t |tjt	j
frU|jdkrUt|}n%t |tr`t|d sdt|rh|g}nt |trvt|d rv|}ntdt	j fdd|D dd	}|dd
ddd}|S )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.Imagec                    s   g | ]
}j | d qS ))r
   r   )
preprocess).0imgr
   selfr    W/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/video_processor.py
<listcomp>R   s    z3VideoProcessor.preprocess_video.<locals>.<listcomp>)dim   r         )
isinstancelistnpndarrayndimwarningswarnFutureWarningconcatenatetorchTensorcatr   r   
ValueErrorstackpermute)r   videor
   r   r   r   r   preprocess_video   s0   ((
 zVideoProcessor.preprocess_videor   r*   output_typec                 C   s   |j d }g }t|D ]}|| dddd}| ||}|| q|dkr.t|}|S |dkr9t|}|S |dksDt| 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_outputr   r   r   postprocess_videoY   s   



z VideoProcessor.postprocess_video)NN)r   )__name__
__module____qualname____doc__r   intr$   r%   r+   strr   r   r   r   PILImager8   r   r   r   r   r	      s    ">r	   )r    typingr   r   r   numpyr   r?   r$   image_processorr   r   r   r	   r   r   r   r   <module>   s   