

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 VideoMAEImageProcessor



class 					lowercase		( unittest.TestCase      ):



 '''simple docstring'''


 def __init__(self , __a , __a=7 , __a=3 , __a=10 , __a=18 , __a=30 , __a=400 , __a=True , __a=None , __a=True , __a=[0.5, 0.5, 0.5] , __a=[0.5, 0.5, 0.5] , __a=None , )				->      str:






  """simple docstring"""





  UpperCAmelCase__           =       size if size is not None else {'shortest_edge': 18}
  UpperCAmelCase__           =       crop_size if crop_size is not None else {'height': 18, 'width': 18}

  UpperCAmelCase__           =       parent
  UpperCAmelCase__           =       batch_size
  UpperCAmelCase__           =       num_channels
  UpperCAmelCase__           =       num_frames
  UpperCAmelCase__           =       image_size
  UpperCAmelCase__           =       min_resolution
  UpperCAmelCase__           =       max_resolution
  UpperCAmelCase__           =       do_resize
  UpperCAmelCase__           =       size
  UpperCAmelCase__           =       do_normalize
  UpperCAmelCase__           =       image_mean
  UpperCAmelCase__           =       image_std
  UpperCAmelCase__           =       crop_size




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







@require_torch
@require_vision
class 					lowercase		( a__				,       unittest.TestCase      ):



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE      							=       VideoMAEImageProcessor if is_vision_available() else None




 def 						UpperCamelCase__							(self				)				->      Dict:






  """simple docstring"""





  UpperCAmelCase__           =       VideoMAEImageProcessingTester(self				)




 @property
 def 						UpperCamelCase__							(self				)				->      List[str]:






  """simple docstring"""





  return self.image_processor_tester.prepare_image_processor_dict()




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






  """simple docstring"""





  UpperCAmelCase__           =       self.image_processing_class(**self.image_processor_dict				)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , 'image_mean'				)				)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , 'image_std'				)				)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , 'do_normalize'				)				)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , 'do_resize'				)				)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , 'do_center_crop'				)				)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , 'size'				)				)




 def 						UpperCamelCase__							(self				)				->      int:






  """simple docstring"""





  UpperCAmelCase__           =       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}				)

  UpperCAmelCase__           =       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 						UpperCamelCase__							(self				)				->      Union[str, Any]:






  """simple docstring"""





  pass




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






  """simple docstring"""





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

  # Test not batched input
  UpperCAmelCase__           =       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
  UpperCAmelCase__           =       image_processing(SCREAMING_SNAKE_CASE_ , 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 						UpperCamelCase__							(self				)				->      Dict:






  """simple docstring"""





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

  # Test not batched input
  UpperCAmelCase__           =       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
  UpperCAmelCase__           =       image_processing(SCREAMING_SNAKE_CASE_ , 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 						UpperCamelCase__							(self				)				->      Optional[Any]:






  """simple docstring"""





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

  # Test not batched input
  UpperCAmelCase__           =       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
  UpperCAmelCase__           =       image_processing(SCREAMING_SNAKE_CASE_ , 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__:						Optional[int]     ) ->				Tuple:
 if is_torch_available():
  import torch

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

 return isinstance(snake_case__						,					np.ndarray     )







class 					lowercase		( _UpperCamelCase      ):



 '''simple docstring'''


 def 						UpperCamelCase__							(self				)				->      int:






  """simple docstring"""





  UpperCAmelCase__           =       fields(self				)

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

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

  if other_fields_are_none and isinstance(__a , __a				):
   for key, value in first_field.items():
    UpperCAmelCase__           =       value
  else:
   for field in class_fields:
    UpperCAmelCase__           =       getattr(self , field.name				)
    if v is not None:
     UpperCAmelCase__           =       v




 def __delitem__(self , *__a , **__a				)				->      Optional[int]:






  """simple docstring"""





  raise Exception(F"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."				)




 def 						UpperCamelCase__							(self , *__a , **__a				)				->      str:






  """simple docstring"""





  raise Exception(F"You cannot use ``setdefault`` on a {self.__class__.__name__} instance."				)




 def 						UpperCamelCase__							(self , *__a , **__a				)				->      Optional[int]:






  """simple docstring"""





  raise Exception(F"You cannot use ``pop`` on a {self.__class__.__name__} instance."				)




 def 						UpperCamelCase__							(self , *__a , **__a				)				->      Tuple:






  """simple docstring"""





  raise Exception(F"You cannot use ``update`` on a {self.__class__.__name__} instance."				)




 def __getitem__(self , __a				)				->      Tuple:






  """simple docstring"""





  if isinstance(__a , __a				):
   UpperCAmelCase__           =       dict(self.items()				)
   return inner_dict[k]
  else:
   return self.to_tuple()[k]




 def __setattr__(self , __a , __a				)				->      Any:






  """simple docstring"""





  if name in self.keys() and value is not None:
   # Don't call self.__setitem__ to avoid recursion errors
   super().__setitem__(__a , __a				)
  super().__setattr__(__a , __a				)




 def __setitem__(self , __a , __a				)				->      Union[str, Any]:






  """simple docstring"""





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




 def 						UpperCamelCase__							(self				)				->      Tuple[Any]:






  """simple docstring"""





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



