





'''simple docstring'''


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 PaddingStrategy, TensorType, logging


_SCREAMING_SNAKE_CASE									=				logging.get_logger(__name__)

class  lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase    :				List[Any]								=       ["input_features", "attention_mask"]




 def __init__(						self      ,  _lowerCAmelCase=80      ,  _lowerCAmelCase=16000      ,  _lowerCAmelCase=80      ,  _lowerCAmelCase=0.0      ,  _lowerCAmelCase=True      ,  _lowerCAmelCase=True      ,  _lowerCAmelCase=True      ,  **_lowerCAmelCase      ,  )     ->				Tuple:
     super().__init__(feature_size=_lowerCAmelCase      ,  sampling_rate=_lowerCAmelCase      ,  padding_value=_lowerCAmelCase      ,  **_lowerCAmelCase		)
     _lowerCAmelCase						      =   num_mel_bins
     _lowerCAmelCase						      =   do_ceptral_normalize
     _lowerCAmelCase						      =   normalize_means
     _lowerCAmelCase						      =   normalize_vars
     _lowerCAmelCase						      =   True




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  )     ->				np.ndarray:
     _lowerCAmelCase						      =   waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
     _lowerCAmelCase						      =   torch.from_numpy(_lowerCAmelCase		).unsqueeze(0		)
     _lowerCAmelCase						      =   ta_kaldi.fbank(_lowerCAmelCase      ,  num_mel_bins=self.num_mel_bins      ,  sample_frequency=self.sampling_rate		)
     return features.numpy()




 @staticmethod
 def 				_snake_case (						_lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase = True      ,  _lowerCAmelCase = True      ,  _lowerCAmelCase = 0.0      ,  )     ->				np.ndarray:
     # make sure we normalize float32 arrays
     if normalize_means:
         _lowerCAmelCase						      =   x[:input_length].mean(axis=0		)
         _lowerCAmelCase						      =   np.subtract(_lowerCAmelCase      ,  _lowerCAmelCase		)
     if normalize_vars:
         _lowerCAmelCase						      =   x[:input_length].std(axis=0		)
         _lowerCAmelCase						      =   np.divide(_lowerCAmelCase      ,  _lowerCAmelCase		)

     if input_length < x.shape[0]:
         _lowerCAmelCase						      =   padding_value

     # make sure array is in float32
     _lowerCAmelCase						      =   x.astype(np.floataa		)

     return x




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase = None		)     ->				List[np.ndarray]:
     _lowerCAmelCase						      =   attention_mask.sum(-1		) if attention_mask is not None else [x.shape[0] for x in input_features]
     return [
         self.utterance_cmvn(_lowerCAmelCase      ,  _lowerCAmelCase      ,  self.normalize_means      ,  self.normalize_vars      ,  self.padding_value		)
         for x, n in zip(_lowerCAmelCase      ,  _lowerCAmelCase		)
     ]





 def __call__(						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase = False      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = False      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  **_lowerCAmelCase      ,  )     ->				BatchFeature:

     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."		)

     _lowerCAmelCase						      =   isinstance(_lowerCAmelCase      ,  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}'''		)
     _lowerCAmelCase						      =   is_batched_numpy or (
         isinstance(_lowerCAmelCase      ,  (list, tuple)		) and (isinstance(raw_speech[0]      ,  (np.ndarray, tuple, list)		))
     )

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

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

     # extract fbank features
     _lowerCAmelCase						      =   [self._extract_fbank_features(_lowerCAmelCase		) for waveform in raw_speech]

     # convert into correct format for padding
     _lowerCAmelCase						      =   BatchFeature({"input_features": features}		)

     _lowerCAmelCase						      =   self.pad(
         _lowerCAmelCase      ,  padding=_lowerCAmelCase      ,  max_length=_lowerCAmelCase      ,  truncation=_lowerCAmelCase      ,  pad_to_multiple_of=_lowerCAmelCase      ,  return_attention_mask=_lowerCAmelCase      ,  **_lowerCAmelCase      ,  )

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

     _lowerCAmelCase						      =   padded_inputs.get("attention_mask"		)
     if attention_mask is not None:
         _lowerCAmelCase						      =   [np.asarray(_lowerCAmelCase      ,  dtype=np.intaa		) for array in attention_mask]

     # Utterance-level cepstral mean and variance normalization
     if self.do_ceptral_normalize:
         _lowerCAmelCase						      =   (
             np.array(_lowerCAmelCase      ,  dtype=np.intaa		)
             if self._get_padding_strategies(_lowerCAmelCase      ,  max_length=_lowerCAmelCase		) is not PaddingStrategy.DO_NOT_PAD
             else None
         )
         _lowerCAmelCase						      =   self.normalize(
             padded_inputs["input_features"]      ,  attention_mask=_lowerCAmelCase		)

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

     return padded_inputs









'''simple docstring'''


from typing import Dict, Iterable, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


_SCREAMING_SNAKE_CASE									=				logging.get_logger(__name__)

class  lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase    :				int								=       ["pixel_values"]




 def __init__(						self      ,  _lowerCAmelCase = True      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = PILImageResampling.BICUBIC      ,  _lowerCAmelCase = True      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = True      ,  _lowerCAmelCase = 1 / 255      ,  _lowerCAmelCase = True      ,  _lowerCAmelCase = IMAGENET_DEFAULT_MEAN      ,  _lowerCAmelCase = IMAGENET_DEFAULT_STD      ,  **_lowerCAmelCase      ,  )     ->				None:
     super().__init__(**_lowerCAmelCase		)
     _lowerCAmelCase						      =   size if size is not None else {"shortest_edge": 224}
     _lowerCAmelCase						      =   get_size_dict(_lowerCAmelCase      ,  default_to_square=_lowerCAmelCase		)
     _lowerCAmelCase						      =   crop_size if crop_size is not None else {"height": 224, "width": 224}
     _lowerCAmelCase						      =   get_size_dict(_lowerCAmelCase      ,  param_name="crop_size"		)

     _lowerCAmelCase						      =   do_resize
     _lowerCAmelCase						      =   size
     _lowerCAmelCase						      =   resample
     _lowerCAmelCase						      =   do_center_crop
     _lowerCAmelCase						      =   crop_size
     _lowerCAmelCase						      =   do_rescale
     _lowerCAmelCase						      =   rescale_factor
     _lowerCAmelCase						      =   do_normalize
     _lowerCAmelCase						      =   image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
     _lowerCAmelCase						      =   image_std if image_std is not None else IMAGENET_DEFAULT_STD




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase = PILImageResampling.BICUBIC      ,  _lowerCAmelCase = None      ,  **_lowerCAmelCase      ,  )     ->				np.ndarray:
     _lowerCAmelCase						      =   get_size_dict(_lowerCAmelCase      ,  default_to_square=_lowerCAmelCase		)
     # size_dict is a dict with either keys "height" and "width" or "shortest_edge"
     if "shortest_edge" in size:
         _lowerCAmelCase						      =   int((256 / 224) * size["shortest_edge"]		)
         _lowerCAmelCase						      =   get_resize_output_image_size(_lowerCAmelCase      ,  size=_lowerCAmelCase      ,  default_to_square=_lowerCAmelCase		)
         _lowerCAmelCase						      =   {"height": output_size[0], "width": output_size[1]}
     if "height" not in size_dict or "width" not in size_dict:
         raise ValueError(
             f'''Size dict must have keys \'height\' and \'width\' or \'shortest_edge\'. Got {size_dict.keys()}'''		)
     return resize(
         _lowerCAmelCase      ,  size=(size_dict["height"], size_dict["width"])      ,  resample=_lowerCAmelCase      ,  data_format=_lowerCAmelCase      ,  **_lowerCAmelCase		)




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase = None      ,  **_lowerCAmelCase      ,  )     ->				np.ndarray:
     _lowerCAmelCase						      =   get_size_dict(_lowerCAmelCase		)
     if "height" not in size or "width" not in size:
         raise ValueError(f'''Size dict must have keys \'height\' and \'width\'. Got {size.keys()}'''		)
     return center_crop(_lowerCAmelCase      ,  size=(size["height"], size["width"])      ,  data_format=_lowerCAmelCase      ,  **_lowerCAmelCase		)




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase = None      ,  **_lowerCAmelCase      ,  )     ->				np.ndarray:
     return rescale(_lowerCAmelCase      ,  scale=_lowerCAmelCase      ,  data_format=_lowerCAmelCase      ,  **_lowerCAmelCase		)




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase = None      ,  **_lowerCAmelCase      ,  )     ->				np.ndarray:
     return normalize(_lowerCAmelCase      ,  mean=_lowerCAmelCase      ,  std=_lowerCAmelCase      ,  data_format=_lowerCAmelCase      ,  **_lowerCAmelCase		)





 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = None      ,  _lowerCAmelCase = ChannelDimension.FIRST      ,  **_lowerCAmelCase      ,  )     ->				BatchFeature:
     _lowerCAmelCase						      =   do_resize if do_resize is not None else self.do_resize
     _lowerCAmelCase						      =   resample if resample is not None else self.resample
     _lowerCAmelCase						      =   do_center_crop if do_center_crop is not None else self.do_center_crop
     _lowerCAmelCase						      =   do_rescale if do_rescale is not None else self.do_rescale
     _lowerCAmelCase						      =   rescale_factor if rescale_factor is not None else self.rescale_factor
     _lowerCAmelCase						      =   do_normalize if do_normalize is not None else self.do_normalize
     _lowerCAmelCase						      =   image_mean if image_mean is not None else self.image_mean
     _lowerCAmelCase						      =   image_std if image_std is not None else self.image_std

     _lowerCAmelCase						      =   size if size is not None else self.size
     _lowerCAmelCase						      =   get_size_dict(_lowerCAmelCase      ,  default_to_square=_lowerCAmelCase		)
     _lowerCAmelCase						      =   crop_size if crop_size is not None else self.crop_size
     _lowerCAmelCase						      =   get_size_dict(_lowerCAmelCase      ,  param_name="crop_size"		)

     _lowerCAmelCase						      =   make_list_of_images(_lowerCAmelCase		)

     if not valid_images(_lowerCAmelCase		):
         raise ValueError(
             "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
             "torch.Tensor, tf.Tensor or jax.ndarray."		)

     if do_resize and size is None:
         raise ValueError("Size must be specified if do_resize is True."		)

     if do_center_crop and crop_size is None:
         raise ValueError("Crop size must be specified if do_center_crop is True."		)

     if do_rescale and rescale_factor is None:
         raise ValueError("Rescale factor must be specified if do_rescale is True."		)

     if do_normalize and (image_mean is None or image_std is None):
         raise ValueError("Image mean and std must be specified if do_normalize is True."		)

     # All transformations expect numpy arrays.
     _lowerCAmelCase						      =   [to_numpy_array(_lowerCAmelCase		) for image in images]

     if do_resize:
         _lowerCAmelCase						      =   [self.resize(_lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase		) for image in images]

     if do_center_crop:
         _lowerCAmelCase						      =   [self.center_crop(_lowerCAmelCase      ,  _lowerCAmelCase		) for image in images]

     if do_rescale:
         _lowerCAmelCase						      =   [self.rescale(_lowerCAmelCase      ,  _lowerCAmelCase		) for image in images]

     if do_normalize:
         _lowerCAmelCase						      =   [self.normalize(_lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase		) for image in images]

     _lowerCAmelCase						      =   [to_channel_dimension_format(_lowerCAmelCase      ,  _lowerCAmelCase		) for image in images]

     _lowerCAmelCase						      =   {"pixel_values": images}
     return BatchFeature(data=_lowerCAmelCase      ,  tensor_type=_lowerCAmelCase		)


