



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


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import TvltImageProcessor


def 	_A	(   _UpperCamelCase      ,					_UpperCamelCase=10      ,					_UpperCamelCase=10      ,					_UpperCamelCase=False      ,					_UpperCamelCase=False			):
				_UpperCAmelCase						:  Dict     					=	[]
				for i in range(image_processor_tester.num_frames			):
								video.append(np.random.randint(255      ,					size=(image_processor_tester.num_channels, width, height)      ,					dtype=np.uinta			)			)

				if not numpify and not torchify:
								# PIL expects the channel dimension as last dimension
								_UpperCAmelCase						:  Optional[int]     					=	[Image.fromarray(np.moveaxis(a__      ,					0      ,					-1			)			) for frame in video]

				if torchify:
								_UpperCAmelCase						:  str     					=	[torch.from_numpy(a__			) for frame in video]

				return video


def 	_A	(   _UpperCamelCase      ,					_UpperCamelCase=False      ,					_UpperCamelCase=False      ,					_UpperCamelCase=False			):
				assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

				_UpperCAmelCase						:  Any     					=	[]
				for i in range(image_processor_tester.batch_size			):
								if equal_resolution:
												_UpperCAmelCase						:  Any     					=	image_processor_tester.max_resolution
								else:
												_UpperCAmelCase					, _UpperCAmelCase						:  List[str]     					=	np.random.choice(
												    np.arange(image_processor_tester.min_resolution      ,					image_processor_tester.max_resolution			)      ,					2			)
												_UpperCAmelCase						:  List[str]     					=	prepare_video(
												    image_processor_tester=a__      ,					width=a__      ,					height=a__      ,					numpify=a__      ,					torchify=a__      ,					)
								video_inputs.append(a__			)

				return video_inputs





class 							lowerCAmelCase_		(		unittest.TestCase     ):







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


						'''simple docstring'''







						_UpperCAmelCase						:  int     					=	size if size is not None else {'''shortest_edge''': 18}
						_UpperCAmelCase						:  Optional[Any]     					=	crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

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







		def 					a_  (    self  :   Optional[Any]					)       ->		Dict:


						'''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,
						    "do_center_crop": self.do_center_crop,
						    "crop_size": self.crop_size,
						}





@require_torch
@require_vision
class 							lowerCAmelCase_		(		A__							,		unittest.TestCase     ):
		SCREAMING_SNAKE_CASE_       :			int					       =	TvltImageProcessor if is_vision_available() else None







		def 					a_  (    self  :   Optional[Any]					)       ->		Union[str, Any]:


						'''simple docstring'''







						_UpperCAmelCase						:  List[Any]     					=	TvltImageProcessorTester(self					)







		@property
		def 					a_  (    self  :   Optional[Any]					)       ->		Optional[Any]:


						'''simple docstring'''







						return self.image_processor_tester.prepare_image_processor_dict()







		def 					a_  (    self  :   List[str]					)       ->		Any:


						'''simple docstring'''







						_UpperCAmelCase						:  int     					=	self.image_processing_class(**self.image_processor_dict					)
						self.assertTrue(hasattr(UpperCAmelCase_       ,		'''image_mean'''					)					)
						self.assertTrue(hasattr(UpperCAmelCase_       ,		'''image_std'''					)					)
						self.assertTrue(hasattr(UpperCAmelCase_       ,		'''do_normalize'''					)					)
						self.assertTrue(hasattr(UpperCAmelCase_       ,		'''do_resize'''					)					)
						self.assertTrue(hasattr(UpperCAmelCase_       ,		'''do_center_crop'''					)					)
						self.assertTrue(hasattr(UpperCAmelCase_       ,		'''size'''					)					)







		def 					a_  (    self  :   Tuple					)       ->		Union[str, Any]:


						'''simple docstring'''







						_UpperCAmelCase						:  List[str]     					=	self.image_processing_class(**self.image_processor_dict					)
						# create random PIL videos
						_UpperCAmelCase						:  str     					=	prepare_video_inputs(self.image_processor_tester       ,		equal_resolution=UpperCAmelCase_					)
						for video in video_inputs:
										self.assertIsInstance(UpperCAmelCase_       ,		UpperCAmelCase_					)
										self.assertIsInstance(video[0]       ,		Image.Image					)

						# Test not batched input
						_UpperCAmelCase						:  Any     					=	image_processor(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
						_UpperCAmelCase						:  Tuple     					=	image_processor(UpperCAmelCase_       ,		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 					a_  (    self  :   Dict					)       ->		Tuple:


						'''simple docstring'''







						_UpperCAmelCase						:  str     					=	self.image_processing_class(**self.image_processor_dict					)
						# create random numpy tensors
						_UpperCAmelCase						:  int     					=	prepare_video_inputs(self.image_processor_tester       ,		equal_resolution=UpperCAmelCase_       ,		numpify=UpperCAmelCase_					)
						for video in video_inputs:
										self.assertIsInstance(UpperCAmelCase_       ,		UpperCAmelCase_					)
										self.assertIsInstance(video[0]       ,		np.ndarray					)

						# Test not batched input
						_UpperCAmelCase						:  int     					=	image_processor(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
						_UpperCAmelCase						:  str     					=	image_processor(UpperCAmelCase_       ,		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 					a_  (    self  :   str					)       ->		Optional[Any]:


						'''simple docstring'''







						_UpperCAmelCase						:  Dict     					=	self.image_processing_class(**self.image_processor_dict					)
						# create random PyTorch tensors
						_UpperCAmelCase						:  int     					=	prepare_video_inputs(self.image_processor_tester       ,		equal_resolution=UpperCAmelCase_       ,		torchify=UpperCAmelCase_					)
						for video in video_inputs:
										self.assertIsInstance(UpperCAmelCase_       ,		UpperCAmelCase_					)
										self.assertIsInstance(video[0]       ,		torch.Tensor					)

						# Test not batched input
						_UpperCAmelCase						:  Optional[int]     					=	image_processor(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
						_UpperCAmelCase						:  Dict     					=	image_processor(UpperCAmelCase_       ,		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'''],
						    )       ,		)


'''simple docstring'''







from ..utils import DummyObject, requires_backends







class 		snake_case_       (						metaclass=A__						):



   """simple docstring"""





   __lowerCAmelCase	:							List[str]								=['''torch''', '''librosa''']







   def __init__( self			,   *UpperCamelCase			,   **UpperCamelCase):
          requires_backends(self			,   ["torch", "librosa"])







   @classmethod
   def     __UpperCAmelCase      ( cls			,   *UpperCamelCase			,   **UpperCamelCase):
          requires_backends(cls			,   ["torch", "librosa"])







   @classmethod
   def     __UpperCAmelCase      ( cls			,   *UpperCamelCase			,   **UpperCamelCase):
          requires_backends(cls			,   ["torch", "librosa"])







class 		snake_case_       (						metaclass=A__						):



   """simple docstring"""





   __lowerCAmelCase	:							Union[str, Any]								=['''torch''', '''librosa''']







   def __init__( self			,   *UpperCamelCase			,   **UpperCamelCase):
          requires_backends(self			,   ["torch", "librosa"])







   @classmethod
   def     __UpperCAmelCase      ( cls			,   *UpperCamelCase			,   **UpperCamelCase):
          requires_backends(cls			,   ["torch", "librosa"])







   @classmethod
   def     __UpperCAmelCase      ( cls			,   *UpperCamelCase			,   **UpperCamelCase):
          requires_backends(cls			,   ["torch", "librosa"])

