


import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
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 DeformableDetrImageProcessor


class        _a							(			unittest.TestCase			):

							def __init__(							self:	List[Any]    ,						UpperCamelCase_:	List[str]    ,						UpperCamelCase_:	Tuple=7    ,						UpperCamelCase_:	Dict=3    ,						UpperCamelCase_:	Dict=30    ,						UpperCamelCase_:	Union[str, Any]=400    ,						UpperCamelCase_:	List[str]=True    ,						UpperCamelCase_:	Optional[Any]=None    ,						UpperCamelCase_:	Union[str, Any]=True    ,						UpperCamelCase_:	Optional[int]=[0.5, 0.5, 0.5]    ,						UpperCamelCase_:	Optional[Any]=[0.5, 0.5, 0.5]    ,						UpperCamelCase_:	Any=True    ,						UpperCamelCase_:	List[str]=1 / 255    ,						UpperCamelCase_:	Union[str, Any]=True    ,						)			->   Any:





									"""simple docstring"""







									lowercase__            =      size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1_333}
									lowercase__            =      parent
									lowercase__            =      batch_size
									lowercase__            =      num_channels
									lowercase__            =      min_resolution
									lowercase__            =      max_resolution
									lowercase__            =      do_resize
									lowercase__            =      size
									lowercase__            =      do_normalize
									lowercase__            =      image_mean
									lowercase__            =      image_std
									lowercase__            =      do_rescale
									lowercase__            =      rescale_factor
									lowercase__            =      do_pad

							def       lowerCamelCase_ (							self:	List[Any]							)			->   List[str]:





									"""simple docstring"""







									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_rescale": self.do_rescale,
									    "rescale_factor": self.rescale_factor,
									    "do_pad": self.do_pad,
									}


							def       lowerCamelCase_ (							self:	int    ,						UpperCamelCase_:	Optional[int]    ,						UpperCamelCase_:	Tuple=False							)			->   Union[str, Any]:





									"""simple docstring"""







									if not batched:
											lowercase__            =      image_inputs[0]
											if isinstance(UpperCamelCase_    ,						Image.Image							):
													lowercase__     ,		lowercase__            =      image.size
											else:
													lowercase__     ,		lowercase__            =      image.shape[1], image.shape[2]
											if w < h:
													lowercase__            =      int(self.size['''shortest_edge'''] * h / w							)
													lowercase__            =      self.size['''shortest_edge''']
											elif w > h:
													lowercase__            =      self.size['''shortest_edge''']
													lowercase__            =      int(self.size['''shortest_edge'''] * w / h							)
											else:
													lowercase__            =      self.size['''shortest_edge''']
													lowercase__            =      self.size['''shortest_edge''']

									else:
											lowercase__            =      []
											for image in image_inputs:
													lowercase__     ,		lowercase__            =      self.get_expected_values([image]							)
													expected_values.append((expected_height, expected_width)							)
											lowercase__            =      max(UpperCamelCase_    ,						key=lambda UpperCamelCase_							: item[0]							)[0]
											lowercase__            =      max(UpperCamelCase_    ,						key=lambda UpperCamelCase_							: item[1]							)[1]

									return expected_height, expected_width


@require_torch
@require_vision
class        _a							(			UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			List[str]        = DeformableDetrImageProcessor if is_vision_available() else None

							def       lowerCamelCase_ (							self:	int							)			->   List[Any]:





									"""simple docstring"""







									lowercase__            =      DeformableDetrImageProcessingTester(self							)

							@property
							def       lowerCamelCase_ (							self:	Dict							)			->   List[Any]:





									"""simple docstring"""







									return self.image_processor_tester.prepare_image_processor_dict()

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__            =      self.image_processing_class(**self.image_processor_dict							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''image_mean'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''image_std'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''do_normalize'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''do_resize'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''do_rescale'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''do_pad'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''size'''							)							)

							def       lowerCamelCase_ (							self:	List[str]							)			->   List[Any]:





									"""simple docstring"""







									lowercase__            =      self.image_processing_class.from_dict(self.image_processor_dict							)
									self.assertEqual(image_processor.size    ,						{'''shortest_edge''': 18, '''longest_edge''': 1_333}							)
									self.assertEqual(image_processor.do_pad    ,						UpperCamelCase_							)

									lowercase__            =      self.image_processing_class.from_dict(
									    self.image_processor_dict    ,						size=42    ,						max_size=84    ,						pad_and_return_pixel_mask=UpperCamelCase_							)
									self.assertEqual(image_processor.size    ,						{'''shortest_edge''': 42, '''longest_edge''': 84}							)
									self.assertEqual(image_processor.do_pad    ,						UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	int							)			->   List[str]:





									"""simple docstring"""







									pass

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





									"""simple docstring"""







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

									# Test not batched input
									lowercase__            =      image_processing(image_inputs[0]    ,						return_tensors='''pt'''							).pixel_values

									lowercase__     ,		lowercase__            =      self.image_processor_tester.get_expected_values(UpperCamelCase_							)

									self.assertEqual(
									    encoded_images.shape    ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,						)

									# Test batched
									lowercase__     ,		lowercase__            =      self.image_processor_tester.get_expected_values(UpperCamelCase_    ,						batched=UpperCamelCase_							)

									lowercase__            =      image_processing(UpperCamelCase_    ,						return_tensors='''pt'''							).pixel_values
									self.assertEqual(
									    encoded_images.shape    ,						(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )    ,						)

							def       lowerCamelCase_ (							self:	List[str]							)			->   Optional[Any]:





									"""simple docstring"""







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

									# Test not batched input
									lowercase__            =      image_processing(image_inputs[0]    ,						return_tensors='''pt'''							).pixel_values

									lowercase__     ,		lowercase__            =      self.image_processor_tester.get_expected_values(UpperCamelCase_							)

									self.assertEqual(
									    encoded_images.shape    ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,						)

									# Test batched
									lowercase__            =      image_processing(UpperCamelCase_    ,						return_tensors='''pt'''							).pixel_values

									lowercase__     ,		lowercase__            =      self.image_processor_tester.get_expected_values(UpperCamelCase_    ,						batched=UpperCamelCase_							)

									self.assertEqual(
									    encoded_images.shape    ,						(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )    ,						)

							def       lowerCamelCase_ (							self:	List[str]							)			->   str:





									"""simple docstring"""







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

									# Test not batched input
									lowercase__            =      image_processing(image_inputs[0]    ,						return_tensors='''pt'''							).pixel_values

									lowercase__     ,		lowercase__            =      self.image_processor_tester.get_expected_values(UpperCamelCase_							)

									self.assertEqual(
									    encoded_images.shape    ,						(1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,						)

									# Test batched
									lowercase__            =      image_processing(UpperCamelCase_    ,						return_tensors='''pt'''							).pixel_values

									lowercase__     ,		lowercase__            =      self.image_processor_tester.get_expected_values(UpperCamelCase_    ,						batched=UpperCamelCase_							)

									self.assertEqual(
									    encoded_images.shape    ,						(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )    ,						)

							@slow
							def       lowerCamelCase_ (							self:	List[str]							)			->   Union[str, Any]:





									"""simple docstring"""







									lowercase__            =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
									with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt'''    ,						'''r'''							) as f:
											lowercase__            =      json.loads(f.read()							)

									lowercase__            =      {'''image_id''': 39_769, '''annotations''': target}

									# encode them
									lowercase__            =      DeformableDetrImageProcessor()
									lowercase__            =      image_processing(images=UpperCamelCase_    ,						annotations=UpperCamelCase_    ,						return_tensors='''pt'''							)

									# verify pixel values
									lowercase__            =      torch.Size([1, 3, 800, 1_066]							)
									self.assertEqual(encoding['''pixel_values'''].shape    ,						UpperCamelCase_							)

									lowercase__            =      torch.tensor([0.2796, 0.3138, 0.3481]							)
									self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]    ,						UpperCamelCase_    ,						atol=1E-4							)							)

									# verify area
									lowercase__            =      torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']    ,						UpperCamelCase_							)							)
									# verify boxes
									lowercase__            =      torch.Size([6, 4]							)
									self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape    ,						UpperCamelCase_							)
									lowercase__            =      torch.tensor([0.5503, 0.2765, 0.0604, 0.2215]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]    ,						UpperCamelCase_    ,						atol=1E-3							)							)
									# verify image_id
									lowercase__            =      torch.tensor([39_769]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']    ,						UpperCamelCase_							)							)
									# verify is_crowd
									lowercase__            =      torch.tensor([0, 0, 0, 0, 0, 0]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']    ,						UpperCamelCase_							)							)
									# verify class_labels
									lowercase__            =      torch.tensor([75, 75, 63, 65, 17, 17]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']    ,						UpperCamelCase_							)							)
									# verify orig_size
									lowercase__            =      torch.tensor([480, 640]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']    ,						UpperCamelCase_							)							)
									# verify size
									lowercase__            =      torch.tensor([800, 1_066]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']    ,						UpperCamelCase_							)							)


							@slow
							def       lowerCamelCase_ (							self:	Any							)			->   int:





									"""simple docstring"""







									lowercase__            =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
									with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'''    ,						'''r'''							) as f:
											lowercase__            =      json.loads(f.read()							)

									lowercase__            =      {'''file_name''': '''000000039769.png''', '''image_id''': 39_769, '''segments_info''': target}

									lowercase__            =      pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''							)

									# encode them
									lowercase__            =      DeformableDetrImageProcessor(format='''coco_panoptic'''							)
									lowercase__            =      image_processing(images=UpperCamelCase_    ,						annotations=UpperCamelCase_    ,						masks_path=UpperCamelCase_    ,						return_tensors='''pt'''							)

									# verify pixel values
									lowercase__            =      torch.Size([1, 3, 800, 1_066]							)
									self.assertEqual(encoding['''pixel_values'''].shape    ,						UpperCamelCase_							)

									lowercase__            =      torch.tensor([0.2796, 0.3138, 0.3481]							)
									self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]    ,						UpperCamelCase_    ,						atol=1E-4							)							)

									# verify area
									lowercase__            =      torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']    ,						UpperCamelCase_							)							)
									# verify boxes
									lowercase__            =      torch.Size([6, 4]							)
									self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape    ,						UpperCamelCase_							)
									lowercase__            =      torch.tensor([0.2625, 0.5437, 0.4688, 0.8625]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]    ,						UpperCamelCase_    ,						atol=1E-3							)							)
									# verify image_id
									lowercase__            =      torch.tensor([39_769]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']    ,						UpperCamelCase_							)							)
									# verify is_crowd
									lowercase__            =      torch.tensor([0, 0, 0, 0, 0, 0]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']    ,						UpperCamelCase_							)							)
									# verify class_labels
									lowercase__            =      torch.tensor([17, 17, 63, 75, 75, 93]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']    ,						UpperCamelCase_							)							)
									# verify masks
									lowercase__            =      822_873
									self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()    ,						UpperCamelCase_							)
									# verify orig_size
									lowercase__            =      torch.tensor([480, 640]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']    ,						UpperCamelCase_							)							)
									# verify size
									lowercase__            =      torch.tensor([800, 1_066]							)
									self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']    ,						UpperCamelCase_							)							)






def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')     )
def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      credit_card_number
		lowercase__            =      0
		lowercase__            =      len(SCREAMING_SNAKE_CASE     ) - 2
		for i in range(SCREAMING_SNAKE_CASE						,				-1						,				-2     ):
				#  double the value of every second digit
				lowercase__            =      int(cc_number[i]     )
				digit *= 2
				# If doubling of a number results in a two digit number
				# i.e greater than 9(e.g., 6 × 2 = 12),
				# then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6),
				# to get a single digit number.
				if digit > 9:
						digit %= 10
						digit += 1
				lowercase__            =      cc_number[:i] + str(SCREAMING_SNAKE_CASE     ) + cc_number[i + 1 :]
				total += digit

		# Sum up the remaining digits
		for i in range(len(SCREAMING_SNAKE_CASE     ) - 1						,				-1						,				-2     ):
				total += int(cc_number[i]     )

		return total % 10 == 0
def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      f'{credit_card_number} is an invalid credit card number because'
		if not credit_card_number.isdigit():
				print(f'{error_message} it has nonnumerical characters.'     )
				return False

		if not 13 <= len(SCREAMING_SNAKE_CASE     ) <= 16:
				print(f'{error_message} of its length.'     )
				return False

		if not validate_initial_digits(SCREAMING_SNAKE_CASE     ):
				print(f'{error_message} of its first two digits.'     )
				return False

		if not luhn_validation(SCREAMING_SNAKE_CASE     ):
				print(f'{error_message} it fails the Luhn check.'     )
				return False

		print(f'{credit_card_number} is a valid credit card number.'     )
		return True


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			validate_credit_card_number('4111111111111111')
			validate_credit_card_number('32323')


