





'''simple docstring'''






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      lowercase  (  lowerCAmelCase :						Union[str, Any]   , lowerCAmelCase :						str=10   , lowerCAmelCase :						str=10   , lowerCAmelCase :						Tuple=False   , lowerCAmelCase :						List[str]=False):



				"""simple docstring"""


				_A :				List[str]				=  []
				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
								_A :				Optional[Any]				=  [Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE   , 0   , -1)) for frame in video]

				if torchify:
								_A :				List[str]				=  [torch.from_numpy(_SCREAMING_SNAKE_CASE) for frame in video]

				return video





def      lowercase  (  lowerCAmelCase :						Tuple   , lowerCAmelCase :						List[str]=False   , lowerCAmelCase :						List[str]=False   , lowerCAmelCase :						Any=False):



				"""simple docstring"""


				assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

				return video_inputs



class  lowerCamelCase__				(						unittest.TestCase						):


	"""simple docstring"""





	def __init__(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__=7   ,     UpperCAmelCase__=3   ,     UpperCAmelCase__=4   ,     UpperCAmelCase__=1_8   ,     UpperCAmelCase__=3_0   ,     UpperCAmelCase__=4_0_0   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=None   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=[0.5, 0.5, 0.5]   ,     UpperCAmelCase__=[0.5, 0.5, 0.5]   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=None   ,     )       ->				Tuple:
					_A :				List[str]				=  size if size is not None else {'''shortest_edge''': 1_8}
					_A :				Optional[int]				=  crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}

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




	def 				_lowerCamelCase				(					self			)       ->				int:
					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__				(						lowerCAmelCase_  ,							unittest.TestCase						):


	"""simple docstring"""





	__magic_name__							   =		TvltImageProcessor if is_vision_available() else None




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				Union[str, Any]				=  TvltImageProcessorTester(self			)




	@property
	def 				_lowerCamelCase				(					self			)       ->				Dict:
					return self.image_processor_tester.prepare_image_processor_dict()




	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					_A :				int				=  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 				_lowerCamelCase				(					self			)       ->				List[str]:
					_A :				str				=  self.image_processing_class(**self.image_processor_dict			)
					# create random PIL videos
					_A :				Tuple				=  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
					_A :				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
					_A :				Optional[int]				=  image_processor(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 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Any				=  self.image_processing_class(**self.image_processor_dict			)
					# create random numpy tensors
					_A :				Tuple				=  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
					_A :				Dict				=  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
					_A :				Dict				=  image_processor(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 				_lowerCamelCase				(					self			)       ->				Optional[Any]:
					_A :				Optional[int]				=  self.image_processing_class(**self.image_processor_dict			)
					# create random PyTorch tensors
					_A :				str				=  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
					_A :				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
					_A :				Tuple				=  image_processor(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'''],
					    )   ,     )







import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput

@dataclass
class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     __snake_case       :      float
     __snake_case       :      Optional[float]						      =     None
     __snake_case       :      Optional[float]						      =     None

class 						UpperCamelCase__					( unittest.TestCase  ):



     '''simple docstring'''

     def 							SCREAMING_SNAKE_CASE__    (				self		:       Any			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30			)
           self.assertEqual(x.a		,30			)
           self.assertIsNone(x.b			)
           self.assertIsNone(x.c			)
           with self.assertRaises(lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		x.d






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[Any]			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30		,b=10			)
           self.assertEqual(x[0]		,30			)
           self.assertEqual(x[1]		,10			)
           self.assertEqual(x[:2]		,(30, 10)			)
           self.assertEqual(x[:]		,(30, 10)			)

           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30		,c=10			)
           self.assertEqual(x[0]		,30			)
           self.assertEqual(x[1]		,10			)
           self.assertEqual(x[:2]		,(30, 10)			)
           self.assertEqual(x[:]		,(30, 10)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       str			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30		,b=10			)
           self.assertEqual(x["""a"""]		,30			)
           self.assertEqual(x["""b"""]		,10			)
           with self.assertRaises(lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		x["""c"""]

           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30		,c=10			)
           self.assertEqual(x["""a"""]		,30			)
           self.assertEqual(x["""c"""]		,10			)
           with self.assertRaises(lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		x["""b"""]






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30			)
           self.assertEqual(list(x.keys()			)		,["""a"""]			)
           self.assertEqual(list(x.values()			)		,[30]			)
           self.assertEqual(list(x.items()			)		,[("""a""", 30)]			)
           self.assertEqual(list(lowerCamelCase__			)		,["""a"""]			)

           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30		,b=10			)
           self.assertEqual(list(x.keys()			)		,["""a""", """b"""]			)
           self.assertEqual(list(x.values()			)		,[30, 10]			)
           self.assertEqual(list(x.items()			)		,[("""a""", 30), ("""b""", 10)]			)
           self.assertEqual(list(lowerCamelCase__			)		,["""a""", """b"""]			)

           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30		,c=10			)
           self.assertEqual(list(x.keys()			)		,["""a""", """c"""]			)
           self.assertEqual(list(x.values()			)		,[30, 10]			)
           self.assertEqual(list(x.items()			)		,[("""a""", 30), ("""c""", 10)]			)
           self.assertEqual(list(lowerCamelCase__			)		,["""a""", """c"""]			)

           with self.assertRaises(lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		x.update({"""d""": 20}			)
           with self.assertRaises(lowerCamelCase__			):
                 del x["a"]
           with self.assertRaises(lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		x.pop("""a"""			)
           with self.assertRaises(lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		x.setdefault("""d"""		,32			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       int			)	->						List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30			)
           SCREAMING_SNAKE_CASE						=		10
           self.assertEqual(x.a		,10			)
           self.assertEqual(x["""a"""]		,10			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[str]			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=30			)
           SCREAMING_SNAKE_CASE						=		10
           self.assertEqual(x.a		,10			)
           self.assertEqual(x["""a"""]		,10			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]			)	->						Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest({"""a""": 30, """b""": 10}			)
           self.assertEqual(list(x.keys()			)		,["""a""", """b"""]			)
           self.assertEqual(x.a		,30			)
           self.assertEqual(x.b		,10			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[int]			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ModelOutputTest([("""a""", 30), ("""b""", 10)]			)
           self.assertEqual(list(x.keys()			)		,["""a""", """b"""]			)
           self.assertEqual(x.a		,30			)
           self.assertEqual(x.b		,10			)

           with self.assertRaises(lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		ModelOutputTest([("""a""", 30), (10, 10)]			)

           SCREAMING_SNAKE_CASE						=		ModelOutputTest(a=(30, 30)			)
           self.assertEqual(list(x.keys()			)		,["""a"""]			)
           self.assertEqual(x.a		,(30, 30)			)


