


"""simple docstring"""



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


__snake_case             =   logging.get_logger(__name__)







class _lowerCAmelCase    (				snake_case_     ):
   __UpperCAmelCase			:	Tuple							  =  ['''input_values''', '''attention_mask''']







   def __init__(     self						,     UpperCamelCase__ = 1						,     UpperCamelCase__ = 1_6000						,     UpperCamelCase__ = 0.0						,     UpperCamelCase__ = False						,     UpperCamelCase__ = 80						,     UpperCamelCase__ = 16						,     UpperCamelCase__ = 64						,     UpperCamelCase__ = "hann_window"						,     UpperCamelCase__ = 1.0						,     UpperCamelCase__ = 80						,     UpperCamelCase__ = 7600						,     UpperCamelCase__ = 1e-10						,     UpperCamelCase__ = 2						,     UpperCamelCase__ = True						,     **UpperCamelCase__						,     )     ->		Optional[Any]:

       '''simple docstring'''




       super().__init__(feature_size=UpperCamelCase__						,     sampling_rate=UpperCamelCase__						,     padding_value=UpperCamelCase__						,     **UpperCamelCase__						)
       snake_case   :				List[str]				 =   do_normalize
       snake_case   :				Tuple				 =   return_attention_mask

       snake_case   :				Optional[Any]				 =   num_mel_bins
       snake_case   :				List[Any]				 =   hop_length
       snake_case   :				Optional[int]				 =   win_length
       snake_case   :				Dict				 =   win_function
       snake_case   :				List[Any]				 =   frame_signal_scale
       snake_case   :				Tuple				 =   fmin
       snake_case   :				Optional[Any]				 =   fmax
       snake_case   :				List[Any]				 =   mel_floor
       snake_case   :				Any				 =   reduction_factor

       snake_case   :				Union[str, Any]				 =   win_length * sampling_rate // 1000
       snake_case   :				Any				 =   hop_length * sampling_rate // 1000
       snake_case   :				Any				 =   optimal_fft_length(self.sample_size						)
       snake_case   :				List[Any]				 =   (self.n_fft // 2) + 1

       snake_case   :				int				 =   window_function(window_length=self.sample_size						,     name=self.win_function						,     periodic=UpperCamelCase__						)

       snake_case   :				List[Any]				 =   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"						,     UpperCamelCase__						,     )
       if reduction_factor != 2.0:
           warnings.warn(
               "The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers"						,     UpperCamelCase__						,     )







   @staticmethod
   # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
   def 			lowerCamelCase		(     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__ = 0.0						)     ->		List[np.ndarray]:

       '''simple docstring'''




       if attention_mask is not None:
           snake_case   :				List[str]				 =   np.array(UpperCamelCase__						,     np.intaa						)
           snake_case   :				Union[str, Any]				 =   []

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

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

       return normed_input_values







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     )     ->		np.ndarray:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   spectrogram(
           UpperCamelCase__						,     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						,     UpperCamelCase__ = None						,     UpperCamelCase__ = None						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     UpperCamelCase__ = None						,     UpperCamelCase__ = None						,     UpperCamelCase__ = None						,     **UpperCamelCase__						,     )     ->		BatchFeature:

       '''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:
           snake_case   :				Optional[int]				 =   self._process_audio(
               UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     **UpperCamelCase__						,     )
       else:
           snake_case   :				Dict				 =   None

       if audio_target is not None:
           snake_case   :				Optional[int]				 =   self._process_audio(
               UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     **UpperCamelCase__						,     )

           if inputs is None:
               return inputs_target
           else:
               snake_case   :				Tuple				 =   inputs_target["input_values"]
               snake_case   :				Dict				 =   inputs_target.get("attention_mask"						)
               if decoder_attention_mask is not None:
                   snake_case   :				Dict				 =   decoder_attention_mask

       return inputs







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__ = False						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     UpperCamelCase__ = None						,     UpperCamelCase__ = None						,     **UpperCamelCase__						,     )     ->		BatchFeature:

       '''simple docstring'''




       snake_case   :				int				 =   isinstance(UpperCamelCase__						,     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}'						)
       snake_case   :				List[Any]				 =   is_batched_numpy or (
           isinstance(UpperCamelCase__						,     (list, tuple)						) and (isinstance(speech[0]						,     (np.ndarray, tuple, list)						))
       )

       if is_batched:
           snake_case   :				Optional[Any]				 =   [np.asarray(UpperCamelCase__						,     dtype=np.floataa						) for speech in speech]
       elif not is_batched and not isinstance(UpperCamelCase__						,     np.ndarray						):
           snake_case   :				List[str]				 =   np.asarray(UpperCamelCase__						,     dtype=np.floataa						)
       elif isinstance(UpperCamelCase__						,     np.ndarray						) and speech.dtype is np.dtype(np.floataa						):
           snake_case   :				Optional[Any]				 =   speech.astype(np.floataa						)

       # always return batch
       if not is_batched:
           snake_case   :				Optional[int]				 =   [speech]

       # needed to make pad() work on spectrogram inputs
       snake_case   :				str				 =   self.feature_size

       # convert into correct format for padding
       if is_target:
           snake_case   :				Optional[int]				 =   [self._extract_mel_features(UpperCamelCase__						) for waveform in speech]
           snake_case   :				Tuple				 =   BatchFeature({"input_values": features}						)
           snake_case   :				Tuple				 =   self.num_mel_bins
       else:
           snake_case   :				Optional[int]				 =   BatchFeature({"input_values": speech}						)

       snake_case   :				List[Any]				 =   self.pad(
           UpperCamelCase__						,     padding=UpperCamelCase__						,     max_length=UpperCamelCase__						,     truncation=UpperCamelCase__						,     pad_to_multiple_of=UpperCamelCase__						,     return_attention_mask=UpperCamelCase__						,     **UpperCamelCase__						,     )

       snake_case   :				str				 =   feature_size_hack

       # convert input values to correct format
       snake_case   :				List[str]				 =   padded_inputs["input_values"]
       if not isinstance(input_values[0]						,     np.ndarray						):
           snake_case   :				Optional[Any]				 =   [np.asarray(UpperCamelCase__						,     dtype=np.floataa						) for array in input_values]
       elif (
           not isinstance(UpperCamelCase__						,     np.ndarray						)
           and isinstance(input_values[0]						,     np.ndarray						)
           and input_values[0].dtype is np.dtype(np.floataa						)
       ):
           snake_case   :				Tuple				 =   [array.astype(np.floataa						) for array in input_values]
       elif isinstance(UpperCamelCase__						,     np.ndarray						) and input_values.dtype is np.dtype(np.floataa						):
           snake_case   :				List[str]				 =   input_values.astype(np.floataa						)

       # convert attention_mask to correct format
       snake_case   :				List[str]				 =   padded_inputs.get("attention_mask"						)
       if attention_mask is not None:
           snake_case   :				Dict				 =   [np.asarray(UpperCamelCase__						,     dtype=np.intaa						) for array in attention_mask]

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

       if return_tensors is not None:
           snake_case   :				Any				 =   padded_inputs.convert_to_tensors(UpperCamelCase__						)

       return padded_inputs







   def 			lowerCamelCase		(     self						)     ->		Dict[str, Any]:

       '''simple docstring'''




       snake_case   :				List[str]				 =   super().to_dict()

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

       return output









"""simple docstring"""



import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:







  class _lowerCAmelCase    :







     @staticmethod
     def 			lowerCamelCase		(     *UpperCamelCase__						,     **UpperCamelCase__						)     ->		Union[str, Any]:

         '''simple docstring'''




         pass







@is_pipeline_test
@require_vision
@require_torch
class _lowerCAmelCase    (				unittest.TestCase     ):
   __UpperCAmelCase			:	List[Any]							  =  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Optional[Any]:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   pipeline(
           "zero-shot-object-detection"						,     model="hf-internal-testing/tiny-random-owlvit-object-detection"						)

       snake_case   :				int				 =   [
           {
               "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
               "candidate_labels": ["cat", "remote", "couch"],
           }
       ]
       return object_detector, examples







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				str				 =   object_detector(examples[0]						,     threshold=0.0						)

       snake_case   :				str				 =   len(UpperCamelCase__						)
       self.assertGreater(UpperCamelCase__						,     0						)
       self.assertEqual(
           UpperCamelCase__						,     [
               {
                   "score": ANY(UpperCamelCase__						),
                   "label": ANY(UpperCamelCase__						),
                   "box": {"xmin": ANY(UpperCamelCase__						), "ymin": ANY(UpperCamelCase__						), "xmax": ANY(UpperCamelCase__						), "ymax": ANY(UpperCamelCase__						)},
               }
               for i in range(UpperCamelCase__						)
           ]						,     )







   @require_tf
   @unittest.skip("Zero Shot Object Detection not implemented in TF"						)
   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       pass







   @require_torch
   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Dict				 =   pipeline(
           "zero-shot-object-detection"						,     model="hf-internal-testing/tiny-random-owlvit-object-detection"						)

       snake_case   :				Optional[Any]				 =   object_detector(
           "./tests/fixtures/tests_samples/COCO/000000039769.png"						,     candidate_labels=["cat", "remote", "couch"]						,     threshold=0.64						,     )

       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               {"score": 0.7235, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.7218, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.7184, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.6748, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6656, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6614, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6456, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
               {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
               {"score": 0.6419, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
           ]						,     )

       snake_case   :				Dict				 =   object_detector(
           [
               {
                   "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                   "candidate_labels": ["cat", "remote", "couch"],
               }
           ]						,     threshold=0.64						,     )

       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               [
                   {"score": 0.7235, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                   {"score": 0.7218, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                   {"score": 0.7184, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                   {"score": 0.6748, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                   {"score": 0.6656, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                   {"score": 0.6614, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                   {"score": 0.6456, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                   {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
                   {"score": 0.6419, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
               ]
           ]						,     )







   @require_torch
   @slow
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   pipeline("zero-shot-object-detection"						)

       snake_case   :				Tuple				 =   object_detector(
           "http://images.cocodataset.org/val2017/000000039769.jpg"						,     candidate_labels=["cat", "remote", "couch"]						,     )
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               {"score": 0.2868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
               {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
               {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
               {"score": 0.1474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
               {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
           ]						,     )

       snake_case   :				List[Any]				 =   object_detector(
           [
               {
                   "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                   "candidate_labels": ["cat", "remote", "couch"],
               },
               {
                   "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                   "candidate_labels": ["cat", "remote", "couch"],
               },
           ]						,     )
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               [
                   {"score": 0.2868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                   {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                   {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                   {"score": 0.1474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                   {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
               ],
               [
                   {"score": 0.2868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                   {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                   {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                   {"score": 0.1474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                   {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
               ],
           ]						,     )







   @require_tf
   @unittest.skip("Zero Shot Object Detection not implemented in TF"						)
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       pass







   @require_torch
   @slow
   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   0.2
       snake_case   :				List[str]				 =   pipeline("zero-shot-object-detection"						)

       snake_case   :				List[Any]				 =   object_detector(
           "http://images.cocodataset.org/val2017/000000039769.jpg"						,     candidate_labels=["cat", "remote", "couch"]						,     threshold=UpperCamelCase__						,     )
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               {"score": 0.2868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
               {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
               {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
           ]						,     )







   @require_torch
   @slow
   def 			lowerCamelCase		(     self						)     ->		Optional[int]:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   2
       snake_case   :				Optional[Any]				 =   pipeline("zero-shot-object-detection"						)

       snake_case   :				Optional[int]				 =   object_detector(
           "http://images.cocodataset.org/val2017/000000039769.jpg"						,     candidate_labels=["cat", "remote", "couch"]						,     top_k=UpperCamelCase__						,     )
       self.assertEqual(
           nested_simplify(UpperCamelCase__						,     decimals=4						)						,     [
               {"score": 0.2868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
               {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
           ]						,     )





