
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 ChineseCLIPImageProcessor


class SCREAMING_SNAKE_CASE  (		unittest.TestCase							):






			"""simple docstring"""




			def __init__(    self ,				__UpperCamelCase ,				__UpperCamelCase=7 ,				__UpperCamelCase=3 ,				__UpperCamelCase=18 ,				__UpperCamelCase=30 ,				__UpperCamelCase=4_00 ,				__UpperCamelCase=True ,				__UpperCamelCase=None ,				__UpperCamelCase=True ,				__UpperCamelCase=None ,				__UpperCamelCase=True ,				__UpperCamelCase=[0.4814_5466, 0.457_8275, 0.4082_1073] ,				__UpperCamelCase=[0.2686_2954, 0.2613_0258, 0.2757_7711] ,				__UpperCamelCase=True ,				):

				"""simple docstring"""


				snake_case_      		= size if size is not None else {'height': 2_24, 'width': 2_24}
				snake_case_      		= crop_size if crop_size is not None else {'height': 18, 'width': 18}
				snake_case_      		= parent
				snake_case_      		= batch_size
				snake_case_      		= num_channels
				snake_case_      		= image_size
				snake_case_      		= min_resolution
				snake_case_      		= max_resolution
				snake_case_      		= do_resize
				snake_case_      		= size
				snake_case_      		= do_center_crop
				snake_case_      		= crop_size
				snake_case_      		= do_normalize
				snake_case_      		= image_mean
				snake_case_      		= image_std
				snake_case_      		= do_convert_rgb


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				return {
				    "do_resize": self.do_resize,
				    "size": self.size,
				    "do_center_crop": self.do_center_crop,
				    "crop_size": self.crop_size,
				    "do_normalize": self.do_normalize,
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				    "do_convert_rgb": self.do_convert_rgb,
				}







			def 			__lowerCAmelCase (    self ,				__UpperCamelCase=False ,				__UpperCamelCase=False ,				__UpperCamelCase=False							):

				"""simple docstring"""


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

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

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

				if torchify:
					snake_case_      		= [torch.from_numpy(SCREAMING_SNAKE_CASE_							) for x in image_inputs]

				return image_inputs







@require_torch
@require_vision
class SCREAMING_SNAKE_CASE  (		__lowerCamelCase							,						unittest.TestCase							):






			"""simple docstring"""




			__A						  =  ChineseCLIPImageProcessor if is_vision_available() else None


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= ChineseCLIPImageProcessingTester(self ,				do_center_crop=SCREAMING_SNAKE_CASE_							)


			@property
			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				return self.image_processor_tester.prepare_image_processor_dict()


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


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


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.image_processing_class.from_dict(self.image_processor_dict							)
				self.assertEqual(image_processor.size ,				{'height': 2_24, 'width': 2_24}							)
				self.assertEqual(image_processor.crop_size ,				{'height': 18, 'width': 18}							)

				snake_case_      		= 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 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				pass


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.image_processing_class(**self.image_processor_dict							)
				# create random PIL images
				snake_case_      		= self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_							)
				for image in image_inputs:
					self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,				Image.Image							)

				# Test not batched input
				snake_case_      		= 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.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)

				# Test batched
				snake_case_      		= image_processing(SCREAMING_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.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


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

				# Test not batched input
				snake_case_      		= 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.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)

				# Test batched
				snake_case_      		= image_processing(SCREAMING_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.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)







			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


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

				# Test not batched input
				snake_case_      		= 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.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)

				# Test batched
				snake_case_      		= image_processing(SCREAMING_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.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)







@require_torch
@require_vision
class SCREAMING_SNAKE_CASE  (		__lowerCamelCase							,						unittest.TestCase							):






			"""simple docstring"""




			__A						  =  ChineseCLIPImageProcessor if is_vision_available() else None


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= ChineseCLIPImageProcessingTester(self ,				num_channels=4 ,				do_center_crop=SCREAMING_SNAKE_CASE_							)
				snake_case_      		= 3


			@property
			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				return self.image_processor_tester.prepare_image_processor_dict()


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


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


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				pass







			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.image_processing_class(**self.image_processor_dict							)
				# create random PIL images
				snake_case_      		= self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_							)
				for image in image_inputs:
					self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,				Image.Image							)

				# Test not batched input
				snake_case_      		= image_processing(image_inputs[0] ,				return_tensors='pt'							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,				(
				        1,
				        self.expected_encoded_image_num_channels,
				        self.image_processor_tester.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)

				# Test batched
				snake_case_      		= image_processing(SCREAMING_SNAKE_CASE_ ,				return_tensors='pt'							).pixel_values
				self.assertEqual(
				    encoded_images.shape ,				(
				        self.image_processor_tester.batch_size,
				        self.expected_encoded_image_num_channels,
				        self.image_processor_tester.crop_size['height'],
				        self.image_processor_tester.crop_size['width'],
				    ) ,				)



from ..utils import DummyObject, requires_backends


class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)
