


"""simple docstring"""


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

import numpy as np
import torch

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


__UpperCamelCase						:				str             =      logging.get_logger(__name__)



class 			lowerCamelCase__						(   UpperCAmelCase_			):
						lowerCAmelCase       =     ["""input_features""", """is_longer"""]







						def __init__(							self   :    Optional[Any]			,			_lowercase   :    int=64			,			_lowercase   :    Any=48_000			,			_lowercase   :    Optional[int]=480			,			_lowercase   :    Any=10			,			_lowercase   :    Optional[int]=1_024			,			_lowercase   :    str=0.0			,			_lowercase   :    List[str]=False			,			_lowercase   :    float = 0			,			_lowercase   :    float = 14_000			,			_lowercase   :    int = None			,			_lowercase   :    str = "fusion"			,			_lowercase   :    str = "repeatpad"			,			**_lowercase   :    List[Any]			,			):
								super().__init__(
								    feature_size=_lowercase			,			sampling_rate=_lowercase			,			padding_value=_lowercase			,			return_attention_mask=_lowercase			,			**_lowercase			,			)
								A					       = top_db
								A					       = truncation
								A					       = padding
								A					       = fft_window_size
								A					       = (fft_window_size >> 1) + 1
								A					       = hop_length
								A					       = max_length_s
								A					       = max_length_s * sampling_rate
								A					       = sampling_rate
								A					       = frequency_min
								A					       = frequency_max
								A					       = mel_filter_bank(
								    num_frequency_bins=self.nb_frequency_bins			,			num_mel_filters=_lowercase			,			min_frequency=_lowercase			,			max_frequency=_lowercase			,			sampling_rate=_lowercase			,			norm=_lowercase			,			mel_scale='htk'			,			)
								A					       = mel_filter_bank(
								    num_frequency_bins=self.nb_frequency_bins			,			num_mel_filters=_lowercase			,			min_frequency=_lowercase			,			max_frequency=_lowercase			,			sampling_rate=_lowercase			,			norm='slaney'			,			mel_scale='slaney'			,			)







						def __a						(							self   :    Union[str, Any]		):
								A					       = copy.deepcopy(self.__dict__		)
								A					       = self.__class__.__name__
								if "mel_filters" in output:
										del output["mel_filters"]
								if "mel_filters_slaney" in output:
										del output["mel_filters_slaney"]
								return output







						def __a						(							self   :    List[str]			,			_lowercase   :    np.array			,			_lowercase   :    Optional[np.array] = None		):
								A					       = spectrogram(
								    _lowercase			,			window_function(self.fft_window_size			,			'hann'		)			,			frame_length=self.fft_window_size			,			hop_length=self.hop_length			,			power=2.0			,			mel_filters=_lowercase			,			log_mel='dB'			,			)
								return log_mel_spectrogram.T







						def __a						(							self   :    Tuple			,			_lowercase   :    int			,			_lowercase   :    List[str]			,			_lowercase   :    str		):
								A					       = np.array_split(list(range(0			,			total_frames - chunk_frames + 1		)		)			,			3		)
								if len(ranges[1]		) == 0:
										# if the audio is too short, we just use the first chunk
										A					       = [0]
								if len(ranges[2]		) == 0:
										# if the audio is too short, we just use the first chunk
										A					       = [0]
								# randomly choose index for each part
								A					       = np.random.choice(ranges[0]		)
								A					       = np.random.choice(ranges[1]		)
								A					       = np.random.choice(ranges[2]		)

								A					       = mel[idx_front : idx_front + chunk_frames, :]
								A					       = mel[idx_middle : idx_middle + chunk_frames, :]
								A					       = mel[idx_back : idx_back + chunk_frames, :]

								A					       = torch.tensor(mel[None, None, :]		)
								A					       = torch.nn.functional.interpolate(
								    _lowercase			,			size=[chunk_frames, 64]			,			mode='bilinear'			,			align_corners=_lowercase		)
								A					       = mel_shrink[0][0].numpy()
								A					       = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]			,			axis=0		)
								return mel_fusion







						def __a						(							self   :    Any			,			_lowercase   :    np.array			,			_lowercase   :    Any			,			_lowercase   :    List[str]			,			_lowercase   :    List[Any]		):
								if waveform.shape[0] > max_length:
										if truncation == "rand_trunc":
												A					       = True
												# random crop to max_length (for compatibility) -> this should be handled by self.pad
												A					       = len(_lowercase		) - max_length
												A					       = np.random.randint(0			,			overflow + 1		)
												A					       = waveform[idx : idx + max_length]
												A					       = self._np_extract_fbank_features(_lowercase			,			self.mel_filters_slaney		)[None, :]
										elif truncation == "fusion":
												A					       = self._np_extract_fbank_features(_lowercase			,			self.mel_filters		)
												A					       = max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
												A					       = mel.shape[0]
												if chunk_frames == total_frames:
														# there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
														# In this case, we just use the whole audio.
														A					       = np.stack([mel, mel, mel, mel]			,			axis=0		)
														A					       = False
												else:
														A					       = self._random_mel_fusion(_lowercase			,			_lowercase			,			_lowercase		)
														A					       = True
										else:
												raise NotImplementedError(f'data_truncating {truncation} not implemented'		)

								else:
										A					       = False
										# only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
										if waveform.shape[0] < max_length:
												if padding == "repeat":
														A					       = int(max_length / len(_lowercase		)		)
														A					       = np.stack(np.tile(_lowercase			,			n_repeat + 1		)		)[:max_length]
												if padding == "repeatpad":
														A					       = int(max_length / len(_lowercase		)		)
														A					       = np.stack(np.tile(_lowercase			,			_lowercase		)		)
												A					       = np.pad(_lowercase			,			(0, max_length - waveform.shape[0])			,			mode='constant'			,			constant_values=0		)

										if truncation == "fusion":
												A					       = self._np_extract_fbank_features(_lowercase			,			self.mel_filters		)
												A					       = np.stack([input_mel, input_mel, input_mel, input_mel]			,			axis=0		)
										else:
												A					       = self._np_extract_fbank_features(_lowercase			,			self.mel_filters_slaney		)[None, :]

								return input_mel, longer







						def __call__(							self   :    List[str]			,			_lowercase   :    Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]			,			_lowercase   :    str = None			,			_lowercase   :    Optional[str] = None			,			_lowercase   :    Optional[int] = None			,			_lowercase   :    Optional[int] = None			,			_lowercase   :    Optional[Union[str, TensorType]] = None			,			**_lowercase   :    str			,			):
								A					       = truncation if truncation is not None else self.truncation
								A					       = padding if padding else self.padding

								if sampling_rate is not None:
										if sampling_rate != self.sampling_rate:
												raise ValueError(
												    f'The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a'
												    f' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input'
												    f' was sampled with {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.'		)

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

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

								# always return batch
								if not is_batched:
										A					       = [np.asarray(_lowercase		)]

								# convert to mel spectrogram, truncate and pad if needed.
								A					       = [
								    self._get_input_mel(_lowercase			,			max_length if max_length else self.nb_max_samples			,			_lowercase			,			_lowercase		)
								    for waveform in raw_speech
								]

								A					       = []
								A					       = []
								for mel, longer in padded_inputs:
										input_mel.append(_lowercase		)
										is_longer.append(_lowercase		)

								if truncation == "fusion" and sum(_lowercase		) == 0:
										# if no audio is longer than 10s, then randomly select one audio to be longer
										A					       = np.random.randint(0			,			len(_lowercase		)		)
										A					       = True

								if isinstance(input_mel[0]			,			_lowercase		):
										A					       = [np.asarray(_lowercase			,			dtype=np.floataa		) for feature in input_mel]

								# is_longer is a list of bool
								A					       = [[longer] for longer in is_longer]

								A					       = {'input_features': input_mel, 'is_longer': is_longer}
								A					       = BatchFeature(_lowercase		)

								if return_tensors is not None:
										A					       = input_features.convert_to_tensors(_lowercase		)

								return input_features









"""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 __a						(							*_lowercase   :    int			,			**_lowercase   :    Optional[int]		):
									pass



@is_pipeline_test
@require_vision
@require_torch
class 			lowerCamelCase__						(   unittest.TestCase			):
						lowerCAmelCase       =     MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







						def __a						(							self   :    int			,			_lowercase   :    Optional[Any]			,			_lowercase   :    int			,			_lowercase   :    Union[str, Any]		):
								A					       = pipeline(
								    'zero-shot-object-detection'			,			model='hf-internal-testing/tiny-random-owlvit-object-detection'		)

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







						def __a						(							self   :    Tuple			,			_lowercase   :    int			,			_lowercase   :    Tuple		):
								A					       = object_detector(examples[0]			,			threshold=0.0		)

								A					       = len(_lowercase		)
								self.assertGreater(_lowercase			,			0		)
								self.assertEqual(
								    _lowercase			,			[
								        {
								            'score': ANY(_lowercase		),
								            'label': ANY(_lowercase		),
								            'box': {'xmin': ANY(_lowercase		), 'ymin': ANY(_lowercase		), 'xmax': ANY(_lowercase		), 'ymax': ANY(_lowercase		)},
								        }
								        for i in range(_lowercase		)
								    ]			,			)







						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'		)
						def __a						(							self   :    Optional[int]		):
								pass







						@require_torch
						def __a						(							self   :    Optional[Any]		):
								A					       = pipeline(
								    'zero-shot-object-detection'			,			model='hf-internal-testing/tiny-random-owlvit-object-detection'		)

								A					       = object_detector(
								    './tests/fixtures/tests_samples/COCO/000000039769.png'			,			candidate_labels=['cat', 'remote', 'couch']			,			threshold=0.6_4			,			)

								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.7_2_3_5, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								        {'score': 0.7_2_1_8, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								        {'score': 0.7_1_8_4, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								        {'score': 0.6_7_4_8, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								        {'score': 0.6_6_5_6, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								        {'score': 0.6_6_1_4, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								        {'score': 0.6_4_5_6, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								        {'score': 0.6_4_2, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
								        {'score': 0.6_4_1_9, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								    ]			,			)

								A					       = object_detector(
								    [
								        {
								            'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        }
								    ]			,			threshold=0.6_4			,			)

								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        [
								            {'score': 0.7_2_3_5, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								            {'score': 0.7_2_1_8, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								            {'score': 0.7_1_8_4, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								            {'score': 0.6_7_4_8, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								            {'score': 0.6_6_5_6, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								            {'score': 0.6_6_1_4, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								            {'score': 0.6_4_5_6, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								            {'score': 0.6_4_2, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
								            {'score': 0.6_4_1_9, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								        ]
								    ]			,			)







						@require_torch
						@slow
						def __a						(							self   :    List[str]		):
								A					       = pipeline('zero-shot-object-detection'		)

								A					       = object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'			,			candidate_labels=['cat', 'remote', 'couch']			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								        {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								        {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								        {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
								        {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
								    ]			,			)

								A					       = 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(_lowercase			,			decimals=4		)			,			[
								        [
								            {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								            {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								            {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								            {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
								            {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
								        ],
								        [
								            {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								            {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								            {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								            {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
								            {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
								        ],
								    ]			,			)







						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'		)
						def __a						(							self   :    Optional[int]		):
								pass







						@require_torch
						@slow
						def __a						(							self   :    Optional[Any]		):
								A					       = 0.2
								A					       = pipeline('zero-shot-object-detection'		)

								A					       = object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'			,			candidate_labels=['cat', 'remote', 'couch']			,			threshold=_lowercase			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								        {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								        {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								    ]			,			)







						@require_torch
						@slow
						def __a						(							self   :    Optional[int]		):
								A					       = 2
								A					       = pipeline('zero-shot-object-detection'		)

								A					       = object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'			,			candidate_labels=['cat', 'remote', 'couch']			,			top_k=_lowercase			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								        {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								    ]			,			)





