



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_image_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import EfficientNetImageProcessor





class    __lowerCAmelCase					(	unittest.TestCase			):




				def __init__(				self		,							_snake_case		,							_snake_case=13		,							_snake_case=3		,							_snake_case=18		,							_snake_case=30		,							_snake_case=400		,							_snake_case=True		,							_snake_case=None		,							_snake_case=True		,							_snake_case=[0.5, 0.5, 0.5]		,							_snake_case=[0.5, 0.5, 0.5]		,							):



									"""simple docstring"""





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




				def 				snake_case  (				self						):



									"""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    __lowerCAmelCase					(	lowerCamelCase__			,		unittest.TestCase			):
				__lowerCamelCase					=							EfficientNetImageProcessor if is_vision_available() else None




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       EfficientNetImageProcessorTester(self						)




				@property
				def 				snake_case  (				self						):



									"""simple docstring"""





									return self.image_processor_tester.prepare_image_processor_dict()




				def 				snake_case  (				self						):



									"""simple docstring"""





									_lowerCAmelCase         =       self.image_processing_class(**self.image_processor_dict						)
									self.assertTrue(hasattr(_snake_case		,							"""image_mean"""						)						)
									self.assertTrue(hasattr(_snake_case		,							"""image_std"""						)						)
									self.assertTrue(hasattr(_snake_case		,							"""do_normalize"""						)						)
									self.assertTrue(hasattr(_snake_case		,							"""do_resize"""						)						)
									self.assertTrue(hasattr(_snake_case		,							"""size"""						)						)




				def 				snake_case  (				self						):



									"""simple docstring"""





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

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




				def 				snake_case  (				self						):



									"""simple docstring"""





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

									# Test not batched input
									_lowerCAmelCase         =       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
									_lowerCAmelCase         =       image_processing(_snake_case		,							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 				snake_case  (				self						):



									"""simple docstring"""





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

									# Test not batched input
									_lowerCAmelCase         =       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
									_lowerCAmelCase         =       image_processing(_snake_case		,							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 				snake_case  (				self						):



									"""simple docstring"""





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

									# Test not batched input
									_lowerCAmelCase         =       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
									_lowerCAmelCase         =       image_processing(_snake_case		,							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"""],
									    )		,							)







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  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=10			,  __lowerCamelCase=10			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)		->Tuple:


 '''simple docstring'''
 snake_case :			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
  snake_case :			Optional[int]							=   [Image.fromarray(np.moveaxis(__lowerCamelCase			,  0			,  -1							)							) for frame in video]

 if torchify:
  snake_case :			int							=   [torch.from_numpy(__lowerCamelCase							) for frame in video]

 return video






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=False			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)		->Optional[Any]:


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

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

 return video_inputs

class   lowerCamelCase						(	unittest.TestCase  ):

       def __init__(      self						,    a__						,    a__=7						,    a__=3						,    a__=4						,    a__=18						,    a__=30						,    a__=4_00						,    a__=True						,    a__=None						,    a__=True						,    a__=[0.5, 0.5, 0.5]						,    a__=[0.5, 0.5, 0.5]						,    a__=True						,    a__=None						,    ):






        '''simple docstring'''






        snake_case :			Tuple							=   size if size is not None else {'''shortest_edge''': 18}
        snake_case :			List[str]							=   crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

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



       def 					UpperCAmelCase					(      self       ):






        '''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						(	SCREAMING_SNAKE_CASE,      unittest.TestCase  ):
       _UpperCamelCase     		=   TvltImageProcessor if is_vision_available() else None

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   TvltImageProcessorTester(self       )

       @property
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        return self.image_processor_tester.prepare_image_processor_dict()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   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__						,    '''do_center_crop'''       )       )
        self.assertTrue(hasattr(a__						,    '''size'''       )       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






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

        # Test not batched input
        snake_case :			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
        snake_case :			Optional[Any]							=   image_processor(a__						,    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       ):






        '''simple docstring'''






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

        # Test not batched input
        snake_case :			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
        snake_case :			Tuple							=   image_processor(a__						,    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       ):






        '''simple docstring'''






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

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






