
from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


_A	  =					logging.get_logger(__name__)

class 		lowercase_       (       __SCREAMING_SNAKE_CASE				):
 A__						:				List[Any]         =    ["""input_values""", """attention_mask"""]

 def __init__(      self     ,	__UpperCamelCase=1     ,	__UpperCamelCase=1_6_0_0_0     ,	__UpperCamelCase=1_2_8     ,	__UpperCamelCase=1_0_2_4     ,	__UpperCamelCase=0.0     ,	__UpperCamelCase=True     ,	__UpperCamelCase=-4.2_677_393     ,	__UpperCamelCase=4.5_689_974     ,	__UpperCamelCase=False     ,	**__UpperCamelCase     ,	):




        """simple docstring"""


        super().__init__(feature_size=__UpperCamelCase     ,	sampling_rate=__UpperCamelCase     ,	padding_value=__UpperCamelCase     ,	**__UpperCamelCase     )
        UpperCamelCase_							       =       num_mel_bins
        UpperCamelCase_							       =       max_length
        UpperCamelCase_							       =       do_normalize
        UpperCamelCase_							       =       mean
        UpperCamelCase_							       =       std
        UpperCamelCase_							       =       return_attention_mask

 def lowerCamelCase_				(      self     ,	__UpperCamelCase     ,	__UpperCamelCase     ,	):




        """simple docstring"""


        UpperCamelCase_							       =       torch.from_numpy(__UpperCamelCase     ).unsqueeze(0     )
        UpperCamelCase_							       =       ta_kaldi.fbank(
            __UpperCamelCase     ,	htk_compat=__UpperCamelCase     ,	sample_frequency=self.sampling_rate     ,	use_energy=__UpperCamelCase     ,	window_type="""hanning"""     ,	num_mel_bins=self.num_mel_bins     ,	dither=0.0     ,	frame_shift=1_0     ,	)

        UpperCamelCase_							       =       fbank.shape[0]
        UpperCamelCase_							       =       max_length - n_frames

        # pad or truncate, depending on difference
        if difference > 0:
               UpperCamelCase_							       =       torch.nn.ZeroPadad((0, 0, 0, difference)     )
               UpperCamelCase_							       =       pad_module(__UpperCamelCase     )
        elif difference < 0:
               UpperCamelCase_							       =       fbank[0:max_length, :]

        UpperCamelCase_							       =       fbank.numpy()

        return fbank

 def lowerCamelCase_				(      self     ,	__UpperCamelCase     ):




        """simple docstring"""


        return (input_values - (self.mean)) / (self.std * 2)

 def __call__(      self     ,	__UpperCamelCase     ,	__UpperCamelCase = None     ,	__UpperCamelCase = None     ,	**__UpperCamelCase     ,	):




        """simple docstring"""


        if sampling_rate is not None:
               if sampling_rate != self.sampling_rate:
                      raise ValueError(
                          f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of'''
                          f''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'''
                          f''' {self.sampling_rate} and not {sampling_rate}.'''     )
        else:
               logger.warning(
                   """It is strongly recommended to pass the `sampling_rate` argument to this function. """
                   """Failing to do so can result in silent errors that might be hard to debug."""     )

        UpperCamelCase_							       =       isinstance(__UpperCamelCase     ,	np.ndarray     ) and len(raw_speech.shape     ) > 1
        if is_batched_numpy and len(raw_speech.shape     ) > 2:
               raise ValueError(f'''Only mono-channel audio is supported for input to {self}'''     )
        UpperCamelCase_							       =       is_batched_numpy or (
            isinstance(__UpperCamelCase     ,	(list, tuple)     ) and (isinstance(raw_speech[0]     ,	(np.ndarray, tuple, list)     ))
        )

        if is_batched:
               UpperCamelCase_							       =       [np.asarray(__UpperCamelCase     ,	dtype=np.floataa     ) for speech in raw_speech]
        elif not is_batched and not isinstance(__UpperCamelCase     ,	np.ndarray     ):
               UpperCamelCase_							       =       np.asarray(__UpperCamelCase     ,	dtype=np.floataa     )
        elif isinstance(__UpperCamelCase     ,	np.ndarray     ) and raw_speech.dtype is np.dtype(np.floataa     ):
               UpperCamelCase_							       =       raw_speech.astype(np.floataa     )

        # always return batch
        if not is_batched:
               UpperCamelCase_							       =       [raw_speech]

        # extract fbank features and pad/truncate to max_length
        UpperCamelCase_							       =       [self._extract_fbank_features(__UpperCamelCase     ,	max_length=self.max_length     ) for waveform in raw_speech]

        # convert into BatchFeature
        UpperCamelCase_							       =       BatchFeature({"""input_values""": features}     )

        # make sure list is in array format
        UpperCamelCase_							       =       padded_inputs.get("""input_values"""     )
        if isinstance(input_values[0]     ,	__UpperCamelCase     ):
               UpperCamelCase_							       =       [np.asarray(__UpperCamelCase     ,	dtype=np.floataa     ) for feature in input_values]

        # normalization
        if self.do_normalize:
               UpperCamelCase_							       =       [self.normalize(__UpperCamelCase     ) for feature in input_values]

        if return_tensors is not None:
               UpperCamelCase_							       =       padded_inputs.convert_to_tensors(__UpperCamelCase     )

        return padded_inputs





import warnings
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from .configuration_utils import ConfigMixin, register_to_config
from .utils import CONFIG_NAME, PIL_INTERPOLATION, deprecate

class 		lowercase_       (       __SCREAMING_SNAKE_CASE				):
 A__						:				List[str]         =    CONFIG_NAME

 @register_to_config
 def __init__(      self     ,	__UpperCamelCase = True     ,	__UpperCamelCase = 8     ,	__UpperCamelCase = "lanczos"     ,	__UpperCamelCase = True     ,	__UpperCamelCase = False     ,	):




        """simple docstring"""


        super().__init__()

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        if images.ndim == 3:
               UpperCamelCase_							       =       images[None, ...]
        UpperCamelCase_							       =       (images * 2_5_5).round().astype("""uint8"""     )
        if images.shape[-1] == 1:
               # special case for grayscale (single channel) images
               UpperCamelCase_							       =       [Image.fromarray(image.squeeze()     ,	mode="""L"""     ) for image in images]
        else:
               UpperCamelCase_							       =       [Image.fromarray(__UpperCamelCase     ) for image in images]

        return pil_images

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        if not isinstance(__UpperCamelCase     ,	__UpperCamelCase     ):
               UpperCamelCase_							       =       [images]
        UpperCamelCase_							       =       [np.array(__UpperCamelCase     ).astype(np.floataa     ) / 255.0 for image in images]
        UpperCamelCase_							       =       np.stack(__UpperCamelCase     ,	axis=0     )

        return images

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        if images.ndim == 3:
               UpperCamelCase_							       =       images[..., None]

        UpperCamelCase_							       =       torch.from_numpy(images.transpose(0     ,	3     ,	1     ,	2     )     )
        return images

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        UpperCamelCase_							       =       images.cpu().permute(0     ,	2     ,	3     ,	1     ).float().numpy()
        return images

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        return 2.0 * images - 1.0

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        return (images / 2 + 0.5).clamp(0     ,	1     )

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        UpperCamelCase_							       =       image.convert("""RGB"""     )
        return image

 def lowerCamelCase_				(      self     ,	__UpperCamelCase     ,	__UpperCamelCase = None     ,	__UpperCamelCase = None     ,	):




        """simple docstring"""


        if height is None:
               UpperCamelCase_							       =       image.height
        if width is None:
               UpperCamelCase_							       =       image.width

        UpperCamelCase_       ,				UpperCamelCase_							       =       (
            x - x % self.config.vae_scale_factor for x in (width, height)
        )  # resize to integer multiple of vae_scale_factor
        UpperCamelCase_							       =       image.resize((width, height)     ,	resample=PIL_INTERPOLATION[self.config.resample]     )
        return image

 def lowerCamelCase_				(      self     ,	__UpperCamelCase     ,	__UpperCamelCase = None     ,	__UpperCamelCase = None     ,	):




        """simple docstring"""


        UpperCamelCase_							       =       (PIL.Image.Image, np.ndarray, torch.Tensor)
        if isinstance(__UpperCamelCase     ,	__UpperCamelCase     ):
               UpperCamelCase_							       =       [image]
        elif not (isinstance(__UpperCamelCase     ,	__UpperCamelCase     ) and all(isinstance(__UpperCamelCase     ,	__UpperCamelCase     ) for i in image     )):
               raise ValueError(
                   f'''Input is in incorrect format: {[type(__UpperCamelCase     ) for i in image]}. Currently, we only support {", ".join(__UpperCamelCase     )}'''     )

        if isinstance(image[0]     ,	PIL.Image.Image     ):
               if self.config.do_convert_rgb:
                      UpperCamelCase_							       =       [self.convert_to_rgb(__UpperCamelCase     ) for i in image]
               if self.config.do_resize:
                      UpperCamelCase_							       =       [self.resize(__UpperCamelCase     ,	__UpperCamelCase     ,	__UpperCamelCase     ) for i in image]
               UpperCamelCase_							       =       self.pil_to_numpy(__UpperCamelCase     )  # to np
               UpperCamelCase_							       =       self.numpy_to_pt(__UpperCamelCase     )  # to pt

        elif isinstance(image[0]     ,	np.ndarray     ):
               UpperCamelCase_							       =       np.concatenate(__UpperCamelCase     ,	axis=0     ) if image[0].ndim == 4 else np.stack(__UpperCamelCase     ,	axis=0     )
               UpperCamelCase_							       =       self.numpy_to_pt(__UpperCamelCase     )
               UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_							       =       image.shape
               if self.config.do_resize and (
                   height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
               ):
                      raise ValueError(
                          f'''Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}'''
                          f'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''     )

        elif isinstance(image[0]     ,	torch.Tensor     ):
               UpperCamelCase_							       =       torch.cat(__UpperCamelCase     ,	axis=0     ) if image[0].ndim == 4 else torch.stack(__UpperCamelCase     ,	axis=0     )
               UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_							       =       image.shape

               # don't need any preprocess if the image is latents
               if channel == 4:
                      return image

               if self.config.do_resize and (
                   height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
               ):
                      raise ValueError(
                          f'''Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}'''
                          f'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''     )

        # expected range [0,1], normalize to [-1,1]
        UpperCamelCase_							       =       self.config.do_normalize
        if image.min() < 0:
               warnings.warn(
                   """Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] """
                   f'''when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]'''     ,	__UpperCamelCase     ,	)
               UpperCamelCase_							       =       False

        if do_normalize:
               UpperCamelCase_							       =       self.normalize(__UpperCamelCase     )

        return image

 def lowerCamelCase_				(      self     ,	__UpperCamelCase     ,	__UpperCamelCase = "pil"     ,	__UpperCamelCase = None     ,	):




        """simple docstring"""


        if not isinstance(__UpperCamelCase     ,	torch.Tensor     ):
               raise ValueError(
                   f'''Input for postprocessing is in incorrect format: {type(__UpperCamelCase     )}. We only support pytorch tensor'''     )
        if output_type not in ["latent", "pt", "np", "pil"]:
               UpperCamelCase_							       =       (
                   f'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
                   """`pil`, `np`, `pt`, `latent`"""
               )
               deprecate("""Unsupported output_type"""     ,	"""1.0.0"""     ,	__UpperCamelCase     ,	standard_warn=__UpperCamelCase     )
               UpperCamelCase_							       =       """np"""

        if output_type == "latent":
               return image

        if do_denormalize is None:
               UpperCamelCase_							       =       [self.config.do_normalize] * image.shape[0]

        UpperCamelCase_							       =       torch.stack(
            [self.denormalize(image[i]     ) if do_denormalize[i] else image[i] for i in range(image.shape[0]     )]     )

        if output_type == "pt":
               return image

        UpperCamelCase_							       =       self.pt_to_numpy(__UpperCamelCase     )

        if output_type == "np":
               return image

        if output_type == "pil":
               return self.numpy_to_pil(__UpperCamelCase     )



class 		lowercase_       (       __SCREAMING_SNAKE_CASE				):
 A__						:				List[Any]         =    CONFIG_NAME

 @register_to_config
 def __init__(      self     ,	__UpperCamelCase = True     ,	__UpperCamelCase = 8     ,	__UpperCamelCase = "lanczos"     ,	__UpperCamelCase = True     ,	):




        """simple docstring"""


        super().__init__()

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        if images.ndim == 3:
               UpperCamelCase_							       =       images[None, ...]
        UpperCamelCase_							       =       (images * 2_5_5).round().astype("""uint8"""     )
        if images.shape[-1] == 1:
               # special case for grayscale (single channel) images
               UpperCamelCase_							       =       [Image.fromarray(image.squeeze()     ,	mode="""L"""     ) for image in images]
        else:
               UpperCamelCase_							       =       [Image.fromarray(image[:, :, :3]     ) for image in images]

        return pil_images

 @staticmethod
 def lowerCamelCase_				(      __UpperCamelCase     ):




        """simple docstring"""


        return image[:, :, 1] * 2**8 + image[:, :, 2]

 def lowerCamelCase_				(      self     ,	__UpperCamelCase     ):




        """simple docstring"""


        if images.ndim == 3:
               UpperCamelCase_							       =       images[None, ...]
        UpperCamelCase_							       =       images[:, :, :, 3:]
        if images.shape[-1] == 6:
               UpperCamelCase_							       =       (images_depth * 2_5_5).round().astype("""uint8"""     )
               UpperCamelCase_							       =       [
                   Image.fromarray(self.rgblike_to_depthmap(__UpperCamelCase     )     ,	mode="""I;16"""     ) for image_depth in images_depth
               ]
        elif images.shape[-1] == 4:
               UpperCamelCase_							       =       (images_depth * 65_535.0).astype(np.uintaa     )
               UpperCamelCase_							       =       [Image.fromarray(__UpperCamelCase     ,	mode="""I;16"""     ) for image_depth in images_depth]
        else:
               raise Exception("""Not supported"""     )

        return pil_images

 def lowerCamelCase_				(      self     ,	__UpperCamelCase     ,	__UpperCamelCase = "pil"     ,	__UpperCamelCase = None     ,	):




        """simple docstring"""


        if not isinstance(__UpperCamelCase     ,	torch.Tensor     ):
               raise ValueError(
                   f'''Input for postprocessing is in incorrect format: {type(__UpperCamelCase     )}. We only support pytorch tensor'''     )
        if output_type not in ["latent", "pt", "np", "pil"]:
               UpperCamelCase_							       =       (
                   f'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
                   """`pil`, `np`, `pt`, `latent`"""
               )
               deprecate("""Unsupported output_type"""     ,	"""1.0.0"""     ,	__UpperCamelCase     ,	standard_warn=__UpperCamelCase     )
               UpperCamelCase_							       =       """np"""

        if do_denormalize is None:
               UpperCamelCase_							       =       [self.config.do_normalize] * image.shape[0]

        UpperCamelCase_							       =       torch.stack(
            [self.denormalize(image[i]     ) if do_denormalize[i] else image[i] for i in range(image.shape[0]     )]     )

        UpperCamelCase_							       =       self.pt_to_numpy(__UpperCamelCase     )

        if output_type == "np":
               if image.shape[-1] == 6:
                      UpperCamelCase_							       =       np.stack([self.rgblike_to_depthmap(im[:, :, 3:]     ) for im in image]     ,	axis=0     )
               else:
                      UpperCamelCase_							       =       image[:, :, :, 3:]
               return image[:, :, :, :3], image_depth

        if output_type == "pil":
               return self.numpy_to_pil(__UpperCamelCase     ), self.numpy_to_depth(__UpperCamelCase     )
        else:
               raise Exception(f'''This type {output_type} is not supported'''     )



