


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


__lowerCAmelCase	: List[Any]  				=logging.get_logger(__name__)
class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			int						=		['''input_features''', '''attention_mask''']


				def __init__(       self			:int			,			lowerCAmelCase__			:List[Any]=80			,			lowerCAmelCase__			:Union[str, Any]=16_000			,			lowerCAmelCase__			:List[str]=80			,			lowerCAmelCase__			:str=0.0			,			lowerCAmelCase__			:Union[str, Any]=True			,			lowerCAmelCase__			:List[Any]=True			,			lowerCAmelCase__			:Union[str, Any]=True			,			**lowerCAmelCase__			:Tuple			,			)		->			List[str]:
										super().__init__(feature_size=lowerCAmelCase__			,			sampling_rate=lowerCAmelCase__			,			padding_value=lowerCAmelCase__			,			**lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Any					=		num_mel_bins
										__SCREAMING_SNAKE_CASE						:     List[str]					=		do_ceptral_normalize
										__SCREAMING_SNAKE_CASE						:     Any					=		normalize_means
										__SCREAMING_SNAKE_CASE						:     Dict					=		normalize_vars
										__SCREAMING_SNAKE_CASE						:     Any					=		True


				def 		__magic_name__(       self			:str			,			lowerCAmelCase__			:np.ndarray			,			)		->			np.ndarray:
										__SCREAMING_SNAKE_CASE						:     Any					=		waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
										__SCREAMING_SNAKE_CASE						:     str					=		torch.from_numpy(lowerCAmelCase__	).unsqueeze(0	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		ta_kaldi.fbank(lowerCAmelCase__			,			num_mel_bins=self.num_mel_bins			,			sample_frequency=self.sampling_rate	)
										return features.numpy()


				@staticmethod
				def 		__magic_name__(       lowerCAmelCase__			:np.ndarray			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:Optional[bool] = True			,			lowerCAmelCase__			:Optional[bool] = True			,			lowerCAmelCase__			:float = 0.0			,			)		->			np.ndarray:
										# make sure we normalize float32 arrays
										if normalize_means:
																__SCREAMING_SNAKE_CASE						:     int					=		x[:input_length].mean(axis=0	)
																__SCREAMING_SNAKE_CASE						:     List[str]					=		np.subtract(lowerCAmelCase__			,			lowerCAmelCase__	)
										if normalize_vars:
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		x[:input_length].std(axis=0	)
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		np.divide(lowerCAmelCase__			,			lowerCAmelCase__	)

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

										# make sure array is in float32
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		x.astype(np.floataa	)

										return x


				def 		__magic_name__(       self			:str			,			lowerCAmelCase__			:List[np.ndarray]			,			lowerCAmelCase__			:Optional[np.ndarray] = None	)		->			List[np.ndarray]:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		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			:int			,			lowerCAmelCase__			:Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]			,			lowerCAmelCase__			:Union[bool, str, PaddingStrategy] = False			,			lowerCAmelCase__			:Optional[int] = None			,			lowerCAmelCase__			:bool = False			,			lowerCAmelCase__			:Optional[int] = None			,			lowerCAmelCase__			:Optional[Union[str, TensorType]] = None			,			lowerCAmelCase__			:Optional[int] = None			,			lowerCAmelCase__			:Optional[bool] = None			,			**lowerCAmelCase__			:int			,			)		->			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.'''	)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		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}'''	)
										__SCREAMING_SNAKE_CASE						:     List[str]					=		is_batched_numpy or (
										    isinstance(lowerCAmelCase__			,			(list, tuple)	) and (isinstance(raw_speech[0]			,			(np.ndarray, tuple, list)	))
										)

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

										# always return batch
										if not is_batched:
																__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		[raw_speech]

										# extract fbank features
										__SCREAMING_SNAKE_CASE						:     Dict					=		[self._extract_fbank_features(lowerCAmelCase__	) for waveform in raw_speech]

										# convert into correct format for padding
										__SCREAMING_SNAKE_CASE						:     Tuple					=		BatchFeature({'''input_features''': features}	)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		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
										__SCREAMING_SNAKE_CASE						:     str					=		padded_inputs.get('''input_features'''	)
										if isinstance(input_features[0]			,			lowerCAmelCase__	):
																__SCREAMING_SNAKE_CASE						:     int					=		[np.asarray(lowerCAmelCase__			,			dtype=np.floataa	) for feature in input_features]

										__SCREAMING_SNAKE_CASE						:     Dict					=		padded_inputs.get('''attention_mask'''	)
										if attention_mask is not None:
																__SCREAMING_SNAKE_CASE						:     Tuple					=		[np.asarray(lowerCAmelCase__			,			dtype=np.intaa	) for array in attention_mask]

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

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

										return padded_inputs




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import VivitImageProcessor
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Tuple=7			,			lowerCAmelCase__			:List[Any]=3			,			lowerCAmelCase__			:Any=10			,			lowerCAmelCase__			:Optional[int]=18			,			lowerCAmelCase__			:Dict=30			,			lowerCAmelCase__			:Tuple=400			,			lowerCAmelCase__			:List[Any]=True			,			lowerCAmelCase__			:Tuple=None			,			lowerCAmelCase__			:str=True			,			lowerCAmelCase__			:List[str]=[0.5, 0.5, 0.5]			,			lowerCAmelCase__			:List[str]=[0.5, 0.5, 0.5]			,			lowerCAmelCase__			:Optional[Any]=None			,			)		->			Optional[Any]:
										__SCREAMING_SNAKE_CASE						:     Dict					=		size if size is not None else {'''shortest_edge''': 18}
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

										__SCREAMING_SNAKE_CASE						:     Tuple					=		parent
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		batch_size
										__SCREAMING_SNAKE_CASE						:     List[str]					=		num_channels
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		num_frames
										__SCREAMING_SNAKE_CASE						:     Tuple					=		image_size
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		min_resolution
										__SCREAMING_SNAKE_CASE						:     Any					=		max_resolution
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		do_resize
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		size
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		do_normalize
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		image_mean
										__SCREAMING_SNAKE_CASE						:     List[str]					=		image_std
										__SCREAMING_SNAKE_CASE						:     str					=		crop_size



				def 		__magic_name__(       self			:Tuple	)		->			Any:
										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "crop_size": self.crop_size,
										}




@require_torch
@require_vision
class        _lowercase      (			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			List[str]						=		VivitImageProcessor if is_vision_available() else None


				def 		__magic_name__(       self			:List[str]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     str					=		VivitImageProcessingTester(self	)


				@property
				def 		__magic_name__(       self			:int	)		->			Union[str, Any]:
										return self.image_processor_tester.prepare_image_processor_dict()


				def 		__magic_name__(       self			:List[str]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.image_processing_class(**self.image_processor_dict	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''image_mean'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''image_std'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_normalize'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_resize'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_center_crop'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''size'''	)	)


				def 		__magic_name__(       self			:Optional[Any]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.image_processing_class.from_dict(self.image_processor_dict	)
										self.assertEqual(image_processor.size			,			{'''shortest_edge''': 18}	)
										self.assertEqual(image_processor.crop_size			,			{'''height''': 18, '''width''': 18}	)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.image_processing_class.from_dict(self.image_processor_dict			,			size=42			,			crop_size=84	)
										self.assertEqual(image_processor.size			,			{'''shortest_edge''': 42}	)
										self.assertEqual(image_processor.crop_size			,			{'''height''': 84, '''width''': 84}	)


				def 		__magic_name__(       self			:List[Any]	)		->			Union[str, Any]:
										# Initialize image_processing
										__SCREAMING_SNAKE_CASE						:     Dict					=		self.image_processing_class(**self.image_processor_dict	)
										# create random PIL videos
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		prepare_video_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__	)
										for video in video_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			lowerCAmelCase__	)
																self.assertIsInstance(video[0]			,			Image.Image	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     List[str]					=		image_processing(video_inputs[0]			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)


				def 		__magic_name__(       self			:str	)		->			int:
										# Initialize image_processing
										__SCREAMING_SNAKE_CASE						:     Any					=		self.image_processing_class(**self.image_processor_dict	)
										# create random numpy tensors
										__SCREAMING_SNAKE_CASE						:     List[str]					=		prepare_video_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__			,			numpify=lowerCAmelCase__	)
										for video in video_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			lowerCAmelCase__	)
																self.assertIsInstance(video[0]			,			np.ndarray	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		image_processing(video_inputs[0]			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Any					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)



				def 		__magic_name__(       self			:Any	)		->			List[str]:
										# Initialize image_processing
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.image_processing_class(**self.image_processor_dict	)
										# create random PyTorch tensors
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		prepare_video_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__			,			torchify=lowerCAmelCase__	)
										for video in video_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			lowerCAmelCase__	)
																self.assertIsInstance(video[0]			,			torch.Tensor	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Tuple					=		image_processing(video_inputs[0]			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)
