
'''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 							A_  (    __SCREAMING_SNAKE_CASE    :	List[Any]      ,  __SCREAMING_SNAKE_CASE    :	Optional[int]=10      ,  __SCREAMING_SNAKE_CASE    :	Tuple=10      ,  __SCREAMING_SNAKE_CASE    :	Union[str, Any]=False      ,  __SCREAMING_SNAKE_CASE    :	List[Any]=False			)					->							Optional[Any]:





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

  if torchify:
    __A		:  Optional[int]       				=		[torch.from_numpy(__snake_case			) for frame in video]

  return video







def 							A_  (    __SCREAMING_SNAKE_CASE    :	Optional[Any]      ,  __SCREAMING_SNAKE_CASE    :	str=False      ,  __SCREAMING_SNAKE_CASE    :	List[Any]=False      ,  __SCREAMING_SNAKE_CASE    :	Any=False			)					->							Dict:





  """simple docstring"""

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

  __A		:  Optional[Any]       				=		[]
  for i in range(image_processor_tester.batch_size			):
    if equal_resolution:
      __A		:  int       				=		image_processor_tester.max_resolution
    else:
      __A				,       __A		:  Optional[int]       				=		np.random.choice(
          np.arange(image_processor_tester.min_resolution      ,  image_processor_tester.max_resolution			)      ,  2			)
      __A		:  Tuple       				=		prepare_video(
          image_processor_tester=__snake_case      ,  width=__snake_case      ,  height=__snake_case      ,  numpify=__snake_case      ,  torchify=__snake_case      ,  )
    video_inputs.append(__snake_case			)

  return video_inputs







class 			__A					(			unittest.TestCase      ):
 def __init__( self      :     List[Any]					, lowerCamelCase      :     Union[str, Any]					, lowerCamelCase      :     int=7					, lowerCamelCase      :     Union[str, Any]=3					, lowerCamelCase      :     List[str]=4					, lowerCamelCase      :     Optional[int]=18					, lowerCamelCase      :     List[str]=30					, lowerCamelCase      :     Optional[int]=4_00					, lowerCamelCase      :     Any=True					, lowerCamelCase      :     Tuple=None					, lowerCamelCase      :     List[str]=True					, lowerCamelCase      :     str=[0.5, 0.5, 0.5]					, lowerCamelCase      :     str=[0.5, 0.5, 0.5]					, lowerCamelCase      :     Tuple=True					, lowerCamelCase      :     Any=None					, ):



   """simple docstring"""





   __A		:  List[Any]       				=		size if size is not None else {"""shortest_edge""": 18}
   __A		:  List[Any]       				=		crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

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






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







@require_torch
@require_vision
class 			__A					(			_UpperCAmelCase     ,    unittest.TestCase      ):
 lowerCamelCase												=TvltImageProcessor if is_vision_available() else None
 def 		lowercase_( self      :     List[str]							):



   """simple docstring"""





   __A		:  List[str]       				=		TvltImageProcessorTester(self							)
 @property
 def 		lowercase_( self      :     List[Any]							):



   """simple docstring"""





   return self.image_processor_tester.prepare_image_processor_dict()
 def 		lowercase_( self      :     Union[str, Any]							):



   """simple docstring"""





   __A		:  str       				=		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 		lowercase_( self      :     Dict							):



   """simple docstring"""





   __A		:  Dict       				=		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		:  Tuple       				=		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"""],
       )					, )
 def 		lowercase_( self      :     List[Any]							):



   """simple docstring"""





   __A		:  Optional[Any]       				=		self.image_processing_class(**self.image_processor_dict							)
   # create random numpy tensors
   __A		:  int       				=		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		:  List[str]       				=		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		:  str       				=		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 		lowercase_( self      :     int							):



   """simple docstring"""





   __A		:  Optional[Any]       				=		self.image_processing_class(**self.image_processor_dict							)
   # create random PyTorch tensors
   __A		:  List[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		:  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		:  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"""],
       )					, )







from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available
def 				UpperCamelCase_	(					__snake_case      :    Dict  )->   str:




						'''simple docstring'''
						if is_torch_available():
												import torch

												if isinstance(__snake_case						,      torch.Tensor  ):
																		return True

						return isinstance(__snake_case						,      np.ndarray  )




class      __snake_case				(   _UpperCAmelCase):
					def 	a			(						self       :						Optional[Any]  )							->					Optional[Any]:



											'''simple docstring'''





											__magic_name__					=      fields(self  )

											# Safety and consistency checks
											if not len(_lowerCAmelCase  ):
																	raise ValueError(F'''{self.__class__.__name__} has no fields.'''  )

											__magic_name__					=      getattr(self	,      class_fields[0].name  )
											__magic_name__					=      all(getattr(self	,      field.name  ) is None for field in class_fields[1:]  )

											if other_fields_are_none and isinstance(_lowerCAmelCase	,      _lowerCAmelCase  ):
																	for key, value in first_field.items():
																							__magic_name__					=      value
											else:
																	for field in class_fields:
																							__magic_name__					=      getattr(self	,      field.name  )
																							if v is not None:
																													__magic_name__					=      v
					def __delitem__(						self       :						Dict	,      *_lowerCAmelCase       :						List[str]	,      **_lowerCAmelCase       :						List[str]  )							->					List[str]:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.'''  )
					def 	a			(						self       :						Optional[int]	,      *_lowerCAmelCase       :						Optional[Any]	,      **_lowerCAmelCase       :						Optional[Any]  )							->					str:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.'''  )
					def 	a			(						self       :						Union[str, Any]	,      *_lowerCAmelCase       :						List[str]	,      **_lowerCAmelCase       :						Any  )							->					str:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``pop`` on a {self.__class__.__name__} instance.'''  )
					def 	a			(						self       :						Any	,      *_lowerCAmelCase       :						str	,      **_lowerCAmelCase       :						Optional[int]  )							->					List[Any]:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``update`` on a {self.__class__.__name__} instance.'''  )
					def __getitem__(						self       :						Optional[Any]	,      _lowerCAmelCase       :						Any  )							->					Tuple:



											'''simple docstring'''





											if isinstance(_lowerCAmelCase	,      _lowerCAmelCase  ):
																	__magic_name__					=      dict(self.items()  )
																	return inner_dict[k]
											else:
																	return self.to_tuple()[k]
					def __setattr__(						self       :						str	,      _lowerCAmelCase       :						Optional[Any]	,      _lowerCAmelCase       :						Optional[int]  )							->					int:



											'''simple docstring'''





											if name in self.keys() and value is not None:
																	# Don't call self.__setitem__ to avoid recursion errors
																	super().__setitem__(_lowerCAmelCase	,      _lowerCAmelCase  )
											super().__setattr__(_lowerCAmelCase	,      _lowerCAmelCase  )
					def __setitem__(						self       :						Optional[Any]	,      _lowerCAmelCase       :						Optional[Any]	,      _lowerCAmelCase       :						List[str]  )							->					Optional[int]:



											'''simple docstring'''





											super().__setitem__(_lowerCAmelCase	,      _lowerCAmelCase  )
											# Don't call self.__setattr__ to avoid recursion errors
											super().__setattr__(_lowerCAmelCase	,      _lowerCAmelCase  )







					def 	a			(						self       :						str  )							->					Tuple[Any]:



											'''simple docstring'''





											return tuple(self[k] for k in self.keys()  )

