import warnings
from typing import Any, Dict, List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


lowerCAmelCase__     						=			logging.get_logger(__name__)
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= ['input_values', 'attention_mask']




 def __init__(      self :  Union[str, Any]      ,   lowercase__ :  int = 1      ,   lowercase__ :  int = 16_000      ,   lowercase__ :  float = 0.0      ,   lowercase__ :  bool = False      ,   lowercase__ :  int = 80      ,   lowercase__ :  int = 16      ,   lowercase__ :  int = 64      ,   lowercase__ :  str = "hann_window"      ,   lowercase__ :  float = 1.0      ,   lowercase__ :  float = 80      ,   lowercase__ :  float = 7_600      ,   lowercase__ :  float = 1e-10      ,   lowercase__ :  int = 2      ,   lowercase__ :  bool = True      ,   **lowercase__ :  Optional[int]      ,   ):







        '''simple docstring'''



        super().__init__(feature_size=lowercase__      ,   sampling_rate=lowercase__      ,   padding_value=lowercase__      ,   **lowercase__)
        lowerCAmelCase__					     = do_normalize
        lowerCAmelCase__					     = return_attention_mask

        lowerCAmelCase__					     = num_mel_bins
        lowerCAmelCase__					     = hop_length
        lowerCAmelCase__					     = win_length
        lowerCAmelCase__					     = win_function
        lowerCAmelCase__					     = frame_signal_scale
        lowerCAmelCase__					     = fmin
        lowerCAmelCase__					     = fmax
        lowerCAmelCase__					     = mel_floor
        lowerCAmelCase__					     = reduction_factor

        lowerCAmelCase__					     = win_length * sampling_rate // 1_000
        lowerCAmelCase__					     = hop_length * sampling_rate // 1_000
        lowerCAmelCase__					     = optimal_fft_length(self.sample_size)
        lowerCAmelCase__					     = (self.n_fft // 2) + 1

        lowerCAmelCase__					     = window_function(window_length=self.sample_size      ,   name=self.win_function      ,   periodic=lowercase__)

        lowerCAmelCase__					     = mel_filter_bank(
            num_frequency_bins=self.n_freqs      ,   num_mel_filters=self.num_mel_bins      ,   min_frequency=self.fmin      ,   max_frequency=self.fmax      ,   sampling_rate=self.sampling_rate      ,   norm='slaney'      ,   mel_scale='slaney'      ,   )

        if frame_signal_scale != 1.0:
               warnings.warn(
                   'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers'      ,   lowercase__      ,   )
        if reduction_factor != 2.0:
               warnings.warn(
                   'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers'      ,   lowercase__      ,   )




 @staticmethod
 # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
 def 		__snake_case						(      lowercase__ :  List[np.ndarray]      ,   lowercase__ :  List[np.ndarray]      ,   lowercase__ :  float = 0.0):







        '''simple docstring'''



        if attention_mask is not None:
               lowerCAmelCase__					     = np.array(lowercase__      ,   np.intaa)
               lowerCAmelCase__					     = []

               for vector, length in zip(lowercase__      ,   attention_mask.sum(-1)):
                      lowerCAmelCase__					     = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7)
                      if length < normed_slice.shape[0]:
                             lowerCAmelCase__					     = padding_value

                      normed_input_values.append(lowercase__)
        else:
               lowerCAmelCase__					     = [(x - x.mean()) / np.sqrt(x.var() + 1e-7) for x in input_values]

        return normed_input_values




 def 		__snake_case						(      self :  List[Any]      ,   lowercase__ :  np.ndarray      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = spectrogram(
            lowercase__      ,   window=self.window      ,   frame_length=self.sample_size      ,   hop_length=self.sample_stride      ,   fft_length=self.n_fft      ,   mel_filters=self.mel_filters      ,   mel_floor=self.mel_floor      ,   log_mel='log10'      ,   )
        return log_mel_spec.T




 def __call__(      self :  str      ,   lowercase__ :  Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None      ,   lowercase__ :  Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None      ,   lowercase__ :  Union[bool, str, PaddingStrategy] = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  bool = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   lowercase__ :  Optional[int] = None      ,   **lowercase__ :  Union[str, Any]      ,   ):







        '''simple docstring'''



        if audio is None and audio_target is None:
               raise ValueError('You must provide either `audio` or `audio_target` values.')

        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 audio 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.')

        if audio is not None:
               lowerCAmelCase__					     = self._process_audio(
                   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   **lowercase__      ,   )
        else:
               lowerCAmelCase__					     = None

        if audio_target is not None:
               lowerCAmelCase__					     = self._process_audio(
                   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   **lowercase__      ,   )

               if inputs is None:
                      return inputs_target
               else:
                      lowerCAmelCase__					     = inputs_target['input_values']
                      lowerCAmelCase__					     = inputs_target.get('attention_mask')
                      if decoder_attention_mask is not None:
                             lowerCAmelCase__					     = decoder_attention_mask

        return inputs




 def 		__snake_case						(      self :  Tuple      ,   lowercase__ :  Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]      ,   lowercase__ :  bool = False      ,   lowercase__ :  Union[bool, str, PaddingStrategy] = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  bool = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   **lowercase__ :  Optional[Any]      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = isinstance(lowercase__      ,   np.ndarray) and len(speech.shape) > 1
        if is_batched_numpy and len(speech.shape) > 2:
               raise ValueError(F"""Only mono-channel audio is supported for input to {self}""")
        lowerCAmelCase__					     = is_batched_numpy or (
            isinstance(lowercase__      ,   (list, tuple)) and (isinstance(speech[0]      ,   (np.ndarray, tuple, list)))
        )

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

        # always return batch
        if not is_batched:
               lowerCAmelCase__					     = [speech]

        # needed to make pad() work on spectrogram inputs
        lowerCAmelCase__					     = self.feature_size

        # convert into correct format for padding
        if is_target:
               lowerCAmelCase__					     = [self._extract_mel_features(lowercase__) for waveform in speech]
               lowerCAmelCase__					     = BatchFeature({'input_values': features})
               lowerCAmelCase__					     = self.num_mel_bins
        else:
               lowerCAmelCase__					     = BatchFeature({'input_values': speech})

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

        lowerCAmelCase__					     = feature_size_hack

        # convert input values to correct format
        lowerCAmelCase__					     = padded_inputs['input_values']
        if not isinstance(input_values[0]      ,   np.ndarray):
               lowerCAmelCase__					     = [np.asarray(lowercase__      ,   dtype=np.floataa) for array in input_values]
        elif (
            not isinstance(lowercase__      ,   np.ndarray)
            and isinstance(input_values[0]      ,   np.ndarray)
            and input_values[0].dtype is np.dtype(np.floataa)
        ):
               lowerCAmelCase__					     = [array.astype(np.floataa) for array in input_values]
        elif isinstance(lowercase__      ,   np.ndarray) and input_values.dtype is np.dtype(np.floataa):
               lowerCAmelCase__					     = input_values.astype(np.floataa)

        # convert attention_mask to correct format
        lowerCAmelCase__					     = padded_inputs.get('attention_mask')
        if attention_mask is not None:
               lowerCAmelCase__					     = [np.asarray(lowercase__      ,   dtype=np.intaa) for array in attention_mask]

        # zero-mean and unit-variance normalization
        if not is_target and self.do_normalize:
               lowerCAmelCase__					     = (
                   attention_mask
                   if self._get_padding_strategies(lowercase__      ,   max_length=lowercase__) is not PaddingStrategy.DO_NOT_PAD
                   else None
               )
               lowerCAmelCase__					     = self.zero_mean_unit_var_norm(
                   padded_inputs['input_values']      ,   attention_mask=lowercase__      ,   padding_value=self.padding_value)

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

        return padded_inputs




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__					     = super().to_dict()

        # Don't serialize these as they are derived from the other properties.
        lowerCAmelCase__					     = ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs']
        for name in names:
               if name in output:
                      del output[name]

        return output

from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
    import torch

if is_tf_available():
    import tensorflow as tf
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= ['image_processor']
 UpperCAmelCase_     			= 'SamImageProcessor'




 def __init__(      self :  Tuple      ,   lowercase__ :  Dict):







        '''simple docstring'''



        super().__init__(lowercase__)
        lowerCAmelCase__					     = self.image_processor
        lowerCAmelCase__					     = -10
        lowerCAmelCase__					     = self.image_processor.size['longest_edge']




 def __call__(      self :  List[Any]      ,   lowercase__ :  Optional[int]=None      ,   lowercase__ :  Any=None      ,   lowercase__ :  Tuple=None      ,   lowercase__ :  List[str]=None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   **lowercase__ :  Dict      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = self.image_processor(
            lowercase__      ,   return_tensors=lowercase__      ,   **lowercase__      ,   )

        # pop arguments that are not used in the foward but used nevertheless
        lowerCAmelCase__					     = encoding_image_processor['original_sizes']

        if hasattr(lowercase__      ,   'numpy'):  # Checks if Torch or TF tensor
               lowerCAmelCase__					     = original_sizes.numpy()

        lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self._check_and_preprocess_points(
            input_points=lowercase__      ,   input_labels=lowercase__      ,   input_boxes=lowercase__      ,   )

        lowerCAmelCase__					     = self._normalize_and_convert(
            lowercase__      ,   lowercase__      ,   input_points=lowercase__      ,   input_labels=lowercase__      ,   input_boxes=lowercase__      ,   return_tensors=lowercase__      ,   )

        return encoding_image_processor




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  List[str]      ,   lowercase__ :  List[str]      ,   lowercase__ :  str=None      ,   lowercase__ :  Optional[int]=None      ,   lowercase__ :  str=None      ,   lowercase__ :  Optional[Any]="pt"      ,   ):







        '''simple docstring'''



        if input_points is not None:
               if len(lowercase__) != len(lowercase__):
                      lowerCAmelCase__					     = [
                          self._normalize_coordinates(self.target_size      ,   lowercase__      ,   original_sizes[0]) for point in input_points
                      ]
               else:
                      lowerCAmelCase__					     = [
                          self._normalize_coordinates(self.target_size      ,   lowercase__      ,   lowercase__)
                          for point, original_size in zip(lowercase__      ,   lowercase__)
                      ]
               # check that all arrays have the same shape
               if not all(point.shape == input_points[0].shape for point in input_points):
                      if input_labels is not None:
                             lowerCAmelCase__     , lowerCAmelCase__					     = self._pad_points_and_labels(lowercase__      ,   lowercase__)

               lowerCAmelCase__					     = np.array(lowercase__)

        if input_labels is not None:
               lowerCAmelCase__					     = np.array(lowercase__)

        if input_boxes is not None:
               if len(lowercase__) != len(lowercase__):
                      lowerCAmelCase__					     = [
                          self._normalize_coordinates(self.target_size      ,   lowercase__      ,   original_sizes[0]      ,   is_bounding_box=lowercase__)
                          for box in input_boxes
                      ]
               else:
                      lowerCAmelCase__					     = [
                          self._normalize_coordinates(self.target_size      ,   lowercase__      ,   lowercase__      ,   is_bounding_box=lowercase__)
                          for box, original_size in zip(lowercase__      ,   lowercase__)
                      ]
               lowerCAmelCase__					     = np.array(lowercase__)

        if input_boxes is not None:
               if return_tensors == "pt":
                      lowerCAmelCase__					     = torch.from_numpy(lowercase__)
                      # boxes batch size of 1 by default
                      lowerCAmelCase__					     = input_boxes.unsqueeze(1) if len(input_boxes.shape) != 3 else input_boxes
               elif return_tensors == "tf":
                      lowerCAmelCase__					     = tf.convert_to_tensor(lowercase__)
                      # boxes batch size of 1 by default
                      lowerCAmelCase__					     = tf.expand_dims(lowercase__      ,   1) if len(input_boxes.shape) != 3 else input_boxes
               encoding_image_processor.update({'input_boxes': input_boxes})
        if input_points is not None:
               if return_tensors == "pt":
                      lowerCAmelCase__					     = torch.from_numpy(lowercase__)
                      # point batch size of 1 by default
                      lowerCAmelCase__					     = input_points.unsqueeze(1) if len(input_points.shape) != 4 else input_points
               elif return_tensors == "tf":
                      lowerCAmelCase__					     = tf.convert_to_tensor(lowercase__)
                      # point batch size of 1 by default
                      lowerCAmelCase__					     = tf.expand_dims(lowercase__      ,   1) if len(input_points.shape) != 4 else input_points
               encoding_image_processor.update({'input_points': input_points})
        if input_labels is not None:
               if return_tensors == "pt":
                      lowerCAmelCase__					     = torch.from_numpy(lowercase__)
                      # point batch size of 1 by default
                      lowerCAmelCase__					     = input_labels.unsqueeze(1) if len(input_labels.shape) != 3 else input_labels
               elif return_tensors == "tf":
                      lowerCAmelCase__					     = tf.convert_to_tensor(lowercase__)
                      # point batch size of 1 by default
                      lowerCAmelCase__					     = tf.expand_dims(lowercase__      ,   1) if len(input_labels.shape) != 3 else input_labels
               encoding_image_processor.update({'input_labels': input_labels})

        return encoding_image_processor




 def 		__snake_case						(      self :  str      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = max([point.shape[0] for point in input_points])
        lowerCAmelCase__					     = []
        for i, point in enumerate(lowercase__):
               if point.shape[0] != expected_nb_points:
                      lowerCAmelCase__					     = np.concatenate(
                          [point, np.zeros((expected_nb_points - point.shape[0], 2)) + self.point_pad_value]      ,   axis=0)
                      lowerCAmelCase__					     = np.append(input_labels[i]      ,   [self.point_pad_value])
               processed_input_points.append(lowercase__)
        lowerCAmelCase__					     = processed_input_points
        return input_points, input_labels




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  int      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  int      ,   lowercase__ :  Optional[Any]=False):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = original_size
        lowerCAmelCase__     , lowerCAmelCase__					     = self.image_processor._get_preprocess_shape(lowercase__      ,   longest_edge=lowercase__)
        lowerCAmelCase__					     = deepcopy(lowercase__).astype(lowercase__)

        if is_bounding_box:
               lowerCAmelCase__					     = coords.reshape(-1      ,   2      ,   2)

        lowerCAmelCase__					     = coords[..., 0] * (new_w / old_w)
        lowerCAmelCase__					     = coords[..., 1] * (new_h / old_h)

        if is_bounding_box:
               lowerCAmelCase__					     = coords.reshape(-1      ,   4)

        return coords




 def 		__snake_case						(      self :  Dict      ,   lowercase__ :  Optional[Any]=None      ,   lowercase__ :  Tuple=None      ,   lowercase__ :  int=None      ,   ):







        '''simple docstring'''



        if input_points is not None:
               if hasattr(lowercase__      ,   'numpy'):  # Checks for TF or Torch tensor
                      lowerCAmelCase__					     = input_points.numpy().tolist()

               if not isinstance(lowercase__      ,   lowercase__) or not isinstance(input_points[0]      ,   lowercase__):
                      raise ValueError('Input points must be a list of list of floating points.')
               lowerCAmelCase__					     = [np.array(lowercase__) for input_point in input_points]
        else:
               lowerCAmelCase__					     = None

        if input_labels is not None:
               if hasattr(lowercase__      ,   'numpy'):
                      lowerCAmelCase__					     = input_labels.numpy().tolist()

               if not isinstance(lowercase__      ,   lowercase__) or not isinstance(input_labels[0]      ,   lowercase__):
                      raise ValueError('Input labels must be a list of list integers.')
               lowerCAmelCase__					     = [np.array(lowercase__) for label in input_labels]
        else:
               lowerCAmelCase__					     = None

        if input_boxes is not None:
               if hasattr(lowercase__      ,   'numpy'):
                      lowerCAmelCase__					     = input_boxes.numpy().tolist()

               if (
                   not isinstance(lowercase__      ,   lowercase__)
                   or not isinstance(input_boxes[0]      ,   lowercase__)
                   or not isinstance(input_boxes[0][0]      ,   lowercase__)
               ):
                      raise ValueError('Input boxes must be a list of list of list of floating points.')
               lowerCAmelCase__					     = [np.array(lowercase__).astype(np.floataa) for box in input_boxes]
        else:
               lowerCAmelCase__					     = None

        return input_points, input_labels, input_boxes




 @property
 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.image_processor.model_input_names
        return list(dict.fromkeys(lowercase__))




 def 		__snake_case						(      self :  int      ,   *lowercase__ :  int      ,   **lowercase__ :  int):







        '''simple docstring'''



        return self.image_processor.post_process_masks(*lowercase__      ,   **lowercase__)
