



"""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 ChineseCLIPImageProcessor






class       UpperCamelCase__			(   unittest.TestCase      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=7						,	SCREAMING_SNAKE_CASE__=3						,	SCREAMING_SNAKE_CASE__=18						,	SCREAMING_SNAKE_CASE__=30						,	SCREAMING_SNAKE_CASE__=400						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=None						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=None						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]						,	SCREAMING_SNAKE_CASE__=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]						,	SCREAMING_SNAKE_CASE__=True						,	)      ->	List[Any]:
				A__											=					size if size is not None else {"height": 224, "width": 224}
				A__											=					crop_size if crop_size is not None else {"height": 18, "width": 18}
				A__											=					parent
				A__											=					batch_size
				A__											=					num_channels
				A__											=					image_size
				A__											=					min_resolution
				A__											=					max_resolution
				A__											=					do_resize
				A__											=					size
				A__											=					do_center_crop
				A__											=					crop_size
				A__											=					do_normalize
				A__											=					image_mean
				A__											=					image_std
				A__											=					do_convert_rgb


			def   snake_case__	(      self		)      ->	int:
				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   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__=False						,	SCREAMING_SNAKE_CASE__=False						,	SCREAMING_SNAKE_CASE__=False		)      ->	Tuple:
				assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

				if equal_resolution:
					A__											=					[]
					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:
					A__											=					[]
					for i in range(self.batch_size		):
						A__							,							A__											=					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
					A__											=					[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__						,	0						,	-1		)		) for x in image_inputs]

				if torchify:
					A__											=					[torch.from_numpy(SCREAMING_SNAKE_CASE__		) for x in image_inputs]

				return image_inputs






@require_torch
@require_vision
class       UpperCamelCase__			(   _lowerCAmelCase       ,		unittest.TestCase      ):

			"""simple docstring"""


			A__	:					List[Any]							  =     ChineseCLIPImageProcessor if is_vision_available() else None


			def   snake_case__	(      self		)      ->	str:
				A__											=					ChineseCLIPImageProcessingTester(self						,	do_center_crop=SCREAMING_SNAKE_CASE__		)


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


			def   snake_case__	(      self		)      ->	Tuple:
				A__											=					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   snake_case__	(      self		)      ->	List[str]:
				A__											=					self.image_processing_class.from_dict(self.image_processor_dict		)
				self.assertEqual(image_processor.size						,	{"height": 224, "width": 224}		)
				self.assertEqual(image_processor.crop_size						,	{"height": 18, "width": 18}		)

				A__											=					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   snake_case__	(      self		)      ->	str:
				pass


			def   snake_case__	(      self		)      ->	str:
				# Initialize image_processing
				A__											=					self.image_processing_class(**self.image_processor_dict		)
				# create random PIL images
				A__											=					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
				A__											=					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
				A__											=					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   snake_case__	(      self		)      ->	List[str]:
				# Initialize image_processing
				A__											=					self.image_processing_class(**self.image_processor_dict		)
				# create random numpy tensors
				A__											=					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
				A__											=					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
				A__											=					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   snake_case__	(      self		)      ->	int:
				# Initialize image_processing
				A__											=					self.image_processing_class(**self.image_processor_dict		)
				# create random PyTorch tensors
				A__											=					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
				A__											=					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
				A__											=					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       UpperCamelCase__			(   _lowerCAmelCase       ,		unittest.TestCase      ):

			"""simple docstring"""


			A__	:					str							  =     ChineseCLIPImageProcessor if is_vision_available() else None


			def   snake_case__	(      self		)      ->	Dict:
				A__											=					ChineseCLIPImageProcessingTester(self						,	num_channels=4						,	do_center_crop=SCREAMING_SNAKE_CASE__		)
				A__											=					3


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


			def   snake_case__	(      self		)      ->	Dict:
				A__											=					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   snake_case__	(      self		)      ->	Dict:
				pass







			def   snake_case__	(      self		)      ->	Optional[Any]:
				# Initialize image_processing
				A__											=					self.image_processing_class(**self.image_processor_dict		)
				# create random PIL images
				A__											=					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
				A__											=					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
				A__											=					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"],
				    )						,	)









"""simple docstring"""







def       _lowerCamelCase				(							UpperCAmelCase_ :       bytes     )	-> str:






	"""simple docstring"""





	return "".join([hex(UpperCAmelCase_     )[2:].zfill(2     ).upper() for byte in list(UpperCAmelCase_     )]     )

def       _lowerCamelCase				(							UpperCAmelCase_ :       str     )	-> bytes:






	"""simple docstring"""





	if (len(UpperCAmelCase_     ) % 2) != 0:
		raise ValueError(
		    "Base16 encoded data is invalid:\nData does not have an even number of hex digits."     )
	# Check the character set - the standard base16 alphabet
	# is uppercase according to RFC3548 section 6
	if not set(UpperCAmelCase_     ) <= set("0123456789ABCDEF"     ):
		raise ValueError(
		    "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters."     )
	# For every two hexadecimal digits (= a byte), turn it into an integer.
	# Then, string the result together into bytes, and return it.
	return bytes(int(data[i] + data[i + 1],	16     ) for i in range(0,	len(UpperCAmelCase_     ),	2     )     )


if __name__ == "__main__":
			import doctest

			doctest.testmod()




