





import unittest

import numpy as np

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import DPTImageProcessor

class 		SCREAMING_SNAKE_CASE__				(       unittest.TestCase  ):


      def __init__(self       :      Tuple				,			a__       :      int				,			a__       :      Any=7				,			a__       :      Any=3				,			a__       :      Union[str, Any]=18				,			a__       :      str=30				,			a__       :      Optional[int]=400				,			a__       :      Any=True				,			a__       :      Tuple=None				,			a__       :      str=True				,			a__       :      str=[0.5, 0.5, 0.5]				,			a__       :      Any=[0.5, 0.5, 0.5]				,			):





         """simple docstring"""



         __snake_case							       =   size if size is not None else {'''height''': 18, '''width''': 18}
         __snake_case							       =   parent
         __snake_case							       =   batch_size
         __snake_case							       =   num_channels
         __snake_case							       =   image_size
         __snake_case							       =   min_resolution
         __snake_case							       =   max_resolution
         __snake_case							       =   do_resize
         __snake_case							       =   size
         __snake_case							       =   do_normalize
         __snake_case							       =   image_mean
         __snake_case							       =   image_std


      def    a (self       :      str      ):





         """simple docstring"""



         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
         }






@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase						,      unittest.TestCase  ):
      A_						:				List[str]							=    DPTImageProcessor if is_vision_available() else None


      def    a (self       :      Optional[int]      ):





         """simple docstring"""



         __snake_case							       =   DPTImageProcessingTester(self      )


      @property
      def    a (self       :      int      ):





         """simple docstring"""



         return self.image_processor_tester.prepare_image_processor_dict()


      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         self.assertTrue(hasattr(a__				,			'''image_mean'''      )      )
         self.assertTrue(hasattr(a__				,			'''image_std'''      )      )
         self.assertTrue(hasattr(a__				,			'''do_normalize'''      )      )
         self.assertTrue(hasattr(a__				,			'''do_resize'''      )      )
         self.assertTrue(hasattr(a__				,			'''size'''      )      )


      def    a (self       :      Optional[int]      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class.from_dict(self.image_processor_dict      )
         self.assertEqual(image_processor.size				,			{'''height''': 18, '''width''': 18}      )

         __snake_case							       =   self.image_processing_class.from_dict(self.image_processor_dict				,			size=42      )
         self.assertEqual(image_processor.size				,			{'''height''': 42, '''width''': 42}      )


      def    a (self       :      Tuple      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         # create random PIL images
         __snake_case							       =   prepare_image_inputs(self.image_processor_tester				,			equal_resolution=a__      )
         for image in image_inputs:
            self.assertIsInstance(a__				,			Image.Image      )

         # Test not batched input
         __snake_case							       =   image_processing(image_inputs[0]				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )				,			)

         # Test batched
         __snake_case							       =   image_processing(a__				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )				,			)


      def    a (self       :      Any      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         # create random numpy tensors
         __snake_case							       =   prepare_image_inputs(self.image_processor_tester				,			equal_resolution=a__				,			numpify=a__      )
         for image in image_inputs:
            self.assertIsInstance(a__				,			np.ndarray      )

         # Test not batched input
         __snake_case							       =   image_processing(image_inputs[0]				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )				,			)

         # Test batched
         __snake_case							       =   image_processing(a__				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )				,			)


      def    a (self       :      str      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         # create random PyTorch tensors
         __snake_case							       =   prepare_image_inputs(self.image_processor_tester				,			equal_resolution=a__				,			torchify=a__      )
         for image in image_inputs:
            self.assertIsInstance(a__				,			torch.Tensor      )

         # Test not batched input
         __snake_case							       =   image_processing(image_inputs[0]				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )				,			)

         # Test batched
         __snake_case							       =   image_processing(a__				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )				,			)






'''simple docstring'''






import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin

class 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	lowerCamelCase__			 =       """MCTCTFeatureExtractor"""
	lowerCamelCase__			 =       """AutoTokenizer"""







	def __init__(						self			:     Dict		,			__snake_case			:     Optional[int]		,			__snake_case			:     List[str]		)			->		str:
								super().__init__(__snake_case		,			__snake_case		)
								UpperCAmelCase :       List[Any] 							=		self.feature_extractor
								UpperCAmelCase :       Union[str, Any] 							=		False







	def __call__(						self			:     Any		,			*__snake_case			:     List[str]		,			**__snake_case			:     Any		)			->		List[Any]:
								# For backward compatibility
								if self._in_target_context_manager:
															return self.current_processor(*__snake_case		,			**__snake_case		)

								if "raw_speech" in kwargs:
															warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''		)
															UpperCAmelCase :       int 							=		kwargs.pop('''raw_speech'''		)
								else:
															UpperCAmelCase :       Union[str, Any] 							=		kwargs.pop('''audio'''		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		kwargs.pop('''sampling_rate'''		,			__snake_case		)
								UpperCAmelCase :       Dict 							=		kwargs.pop('''text'''		,			__snake_case		)
								if len(__snake_case		) > 0:
															UpperCAmelCase :       Any 							=		args[0]
															UpperCAmelCase :       Optional[int] 							=		args[1:]

								if audio is None and text is None:
															raise ValueError('''You need to specify either an `audio` or `text` input to process.'''		)

								if audio is not None:
															UpperCAmelCase :       List[str] 							=		self.feature_extractor(__snake_case		,			*__snake_case		,			sampling_rate=__snake_case		,			**__snake_case		)
								if text is not None:
															UpperCAmelCase :       int 							=		self.tokenizer(__snake_case		,			**__snake_case		)

								if text is None:
															return inputs
								elif audio is None:
															return encodings
								else:
															UpperCAmelCase :       str 							=		encodings['''input_ids''']
															return inputs







	def      A	(						self			:     List[Any]		,			*__snake_case			:     List[Any]		,			**__snake_case			:     List[Any]		)			->		str:
								return self.tokenizer.batch_decode(*__snake_case		,			**__snake_case		)







	def      A	(						self			:     List[Any]		,			*__snake_case			:     int		,			**__snake_case			:     Optional[int]		)			->		Any:
								# For backward compatibility
								if self._in_target_context_manager:
															return self.current_processor.pad(*__snake_case		,			**__snake_case		)

								UpperCAmelCase :       List[Any] 							=		kwargs.pop('''input_features'''		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		kwargs.pop('''labels'''		,			__snake_case		)
								if len(__snake_case		) > 0:
															UpperCAmelCase :       List[str] 							=		args[0]
															UpperCAmelCase :       List[Any] 							=		args[1:]

								if input_features is not None:
															UpperCAmelCase :       Tuple 							=		self.feature_extractor.pad(__snake_case		,			*__snake_case		,			**__snake_case		)
								if labels is not None:
															UpperCAmelCase :       Optional[int] 							=		self.tokenizer.pad(__snake_case		,			**__snake_case		)

								if labels is None:
															return input_features
								elif input_features is None:
															return labels
								else:
															UpperCAmelCase :       List[str] 							=		labels['''input_ids''']
															return input_features







	def      A	(						self			:     Union[str, Any]		,			*__snake_case			:     Optional[Any]		,			**__snake_case			:     Optional[int]		)			->		Optional[Any]:
								return self.tokenizer.decode(*__snake_case		,			**__snake_case		)







	@contextmanager
	def      A	(						self			:     Any		)			->		Optional[int]:
								warnings.warn(
								    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
								    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
								    '''your audio inputs, or in a separate call.'''		)
								UpperCAmelCase :       Dict 							=		True
								UpperCAmelCase :       List[Any] 							=		self.tokenizer
								yield
								UpperCAmelCase :       Tuple 							=		self.feature_extractor
								UpperCAmelCase :       List[Any] 							=		False





