






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	):


		'''simple docstring'''

		__A    =							[]
		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    =							[Image.fromarray(np.moveaxis(lowerCAmelCase__					,     0					,     -1	)	) for frame in video]

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

		return video



def      UpperCAmelCase						( lowerCAmelCase__					,     lowerCAmelCase__=False					,     lowerCAmelCase__=False					,     lowerCAmelCase__=False	):


		'''simple docstring'''

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

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

		return video_inputs





class        a__      (	unittest.TestCase		):






		def __init__(      self							, lowercase__							, lowercase__=7							, lowercase__=3							, lowercase__=4							, lowercase__=18							, lowercase__=30							, lowercase__=400							, lowercase__=True							, lowercase__=None							, lowercase__=True							, lowercase__=[0.5, 0.5, 0.5]							, lowercase__=[0.5, 0.5, 0.5]							, lowercase__=True							, lowercase__=None							, )				->				Optional[Any]:
				__A    =							size if size is not None else {"shortest_edge": 18}
				__A    =							crop_size if crop_size is not None else {"height": 18, "width": 18}

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






		def 					_lowerCamelCase					(      self						)				->				Optional[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        a__      (	lowerCAmelCase__	,       unittest.TestCase		):
		UpperCAmelCase_							:       Tuple 					= TvltImageProcessor if is_vision_available() else None






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






		@property
		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				return self.image_processor_tester.prepare_image_processor_dict()






		def 					_lowerCamelCase					(      self						)				->				List[str]:
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				self.assertTrue(hasattr(lowercase__							, "image_mean"						)						)
				self.assertTrue(hasattr(lowercase__							, "image_std"						)						)
				self.assertTrue(hasattr(lowercase__							, "do_normalize"						)						)
				self.assertTrue(hasattr(lowercase__							, "do_resize"						)						)
				self.assertTrue(hasattr(lowercase__							, "do_center_crop"						)						)
				self.assertTrue(hasattr(lowercase__							, "size"						)						)






		def 					_lowerCamelCase					(      self						)				->				List[Any]:
				# Initialize image_processor
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				# create random PIL videos
				__A    =							prepare_video_inputs(self.image_processor_tester							, equal_resolution=lowercase__						)
				for video in video_inputs:
						self.assertIsInstance(lowercase__							, lowercase__						)
						self.assertIsInstance(video[0]							, Image.Image						)

				# Test not batched input
				__A    =							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    =							image_processor(lowercase__							, 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						)				->				Any:
				# Initialize image_processor
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				# create random numpy tensors
				__A    =							prepare_video_inputs(self.image_processor_tester							, equal_resolution=lowercase__							, numpify=lowercase__						)
				for video in video_inputs:
						self.assertIsInstance(lowercase__							, lowercase__						)
						self.assertIsInstance(video[0]							, np.ndarray						)

				# Test not batched input
				__A    =							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    =							image_processor(lowercase__							, 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						)				->				Any:
				# Initialize image_processor
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				# create random PyTorch tensors
				__A    =							prepare_video_inputs(self.image_processor_tester							, equal_resolution=lowercase__							, torchify=lowercase__						)
				for video in video_inputs:
						self.assertIsInstance(lowercase__							, lowercase__						)
						self.assertIsInstance(video[0]							, torch.Tensor						)

				# Test not batched input
				__A    =							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    =							image_processor(lowercase__							, 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

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 SwinaSRImageProcessor
   from transformers.image_transforms import get_image_size


class 		lowerCAmelCase_				(  unittest.TestCase ):





     def __init__(	self							, _UpperCAmelCase							, _UpperCAmelCase=7							, _UpperCAmelCase=3							, _UpperCAmelCase=18							, _UpperCAmelCase=30							, _UpperCAmelCase=400							, _UpperCAmelCase=True							, _UpperCAmelCase=1 / 255							, _UpperCAmelCase=True							, _UpperCAmelCase=8							, )		->				Tuple:
        lowerCamelCase									=parent
        lowerCamelCase									=batch_size
        lowerCamelCase									=num_channels
        lowerCamelCase									=image_size
        lowerCamelCase									=min_resolution
        lowerCamelCase									=max_resolution
        lowerCamelCase									=do_rescale
        lowerCamelCase									=rescale_factor
        lowerCamelCase									=do_pad
        lowerCamelCase									=pad_size





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        return {
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
            "do_pad": self.do_pad,
            "pad_size": self.pad_size,
        }





     def 						lowerCamelCase							(	self							, _UpperCAmelCase=False							, _UpperCAmelCase=False							, _UpperCAmelCase=False  )		->				int:
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
           lowerCamelCase									=[]
           for i in range(self.batch_size  ):
              image_inputs.append(
                  np.random.randint(
                      255							, size=(self.num_channels, self.max_resolution, self.max_resolution)							, dtype=np.uinta  )  )
        else:
           lowerCamelCase									=[]
           for i in range(self.batch_size  ):
              lowerCamelCase				,       lowerCamelCase									=np.random.choice(np.arange(self.min_resolution							, self.max_resolution  )							, 2  )
              image_inputs.append(np.random.randint(255							, size=(self.num_channels, width, height)							, dtype=np.uinta  )  )

        if not numpify and not torchify:
           # PIL expects the channel dimension as last dimension
           lowerCamelCase									=[Image.fromarray(np.moveaxis(_UpperCAmelCase							, 0							, -1  )  ) for x in image_inputs]

        if torchify:
           lowerCamelCase									=[torch.from_numpy(_UpperCAmelCase  ) for x in image_inputs]

        return image_inputs




@require_torch
@require_vision
class 		lowerCAmelCase_				(  UpperCamelCase				,     unittest.TestCase ):
     __lowerCamelCase       :    Optional[Any] 	=     SwinaSRImageProcessor if is_vision_available() else None





     def 						lowerCamelCase							(	self  )		->				Dict:
        lowerCamelCase									=SwinaSRImageProcessingTester(self  )





     @property
     def 						lowerCamelCase							(	self  )		->				Any:
        return self.image_processor_tester.prepare_image_processor_dict()





     def 						lowerCamelCase							(	self  )		->				Any:
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_rescale"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """rescale_factor"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_pad"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """pad_size"""  )  )





     def 						lowerCamelCase							(	self  )		->				Any:
        pass





     def 						lowerCamelCase							(	self							, _UpperCAmelCase  )		->				int:
        lowerCamelCase				,       lowerCamelCase									=get_image_size(_UpperCAmelCase  )
        lowerCamelCase									=self.image_processor_tester.pad_size

        lowerCamelCase									=(old_height // size + 1) * size - old_height
        lowerCamelCase									=(old_width // size + 1) * size - old_width
        return old_height + pad_height, old_width + pad_width





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random PIL images
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, Image.Image  )

        # Test not batched input
        lowerCamelCase									=image_processor(image_inputs[0]							, return_tensors="""pt"""  ).pixel_values
        lowerCamelCase				,       lowerCamelCase									=self.calculate_expected_size(np.array(image_inputs[0]  )  )
        self.assertEqual(
            encoded_images.shape							, (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )							, )





     def 						lowerCamelCase							(	self  )		->				Any:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random numpy tensors
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase							, numpify=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, np.ndarray  )

        # Test not batched input
        lowerCamelCase									=image_processor(image_inputs[0]							, return_tensors="""pt"""  ).pixel_values
        lowerCamelCase				,       lowerCamelCase									=self.calculate_expected_size(image_inputs[0]  )
        self.assertEqual(
            encoded_images.shape							, (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )							, )





     def 						lowerCamelCase							(	self  )		->				Dict:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random PyTorch tensors
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase							, torchify=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, torch.Tensor  )

        # Test not batched input
        lowerCamelCase									=image_processor(image_inputs[0]							, return_tensors="""pt"""  ).pixel_values
        lowerCamelCase				,       lowerCamelCase									=self.calculate_expected_size(image_inputs[0]  )
        self.assertEqual(
            encoded_images.shape							, (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )							, )