

"""simple docstring"""






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 DetrImageProcessor







class       snake_case__  ( unittest.TestCase     ):







			def __init__(			self			: Dict			,  lowercase			: List[Any]			,  lowercase			: List[str]=7			,  lowercase			: Dict=3			,  lowercase			: Tuple=30			,  lowercase			: Optional[Any]=4_00			,  lowercase			: str=True			,  lowercase			: str=None			,  lowercase			: List[str]=True			,  lowercase			: Any=1 / 2_55			,  lowercase			: Union[str, Any]=True			,  lowercase			: Tuple=[0.5, 0.5, 0.5]			,  lowercase			: Optional[int]=[0.5, 0.5, 0.5]			,  lowercase			: Optional[Any]=True			,  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[int]      =				size if size is not None else {"shortest_edge": 18, "longest_edge": 13_33}
								UpperCAmelCase			:   Optional[int]      =				parent
								UpperCAmelCase			:   Any      =				batch_size
								UpperCAmelCase			:   Union[str, Any]      =				num_channels
								UpperCAmelCase			:   Tuple      =				min_resolution
								UpperCAmelCase			:   Any      =				max_resolution
								UpperCAmelCase			:   Optional[int]      =				do_resize
								UpperCAmelCase			:   List[Any]      =				size
								UpperCAmelCase			:   Any      =				do_rescale
								UpperCAmelCase			:   str      =				rescale_factor
								UpperCAmelCase			:   int      =				do_normalize
								UpperCAmelCase			:   Union[str, Any]      =				image_mean
								UpperCAmelCase			:   Any      =				image_std
								UpperCAmelCase			:   Tuple      =				do_pad







			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



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







			def 			__lowerCAmelCase				(			self			: Dict			,  lowercase			: int			,  lowercase			: Any=False  ):





								'''simple docstring'''



								if not batched:
													UpperCAmelCase			:   Union[str, Any]      =				image_inputs[0]
													if isinstance(UpperCamelCase__			,  Image.Image  ):
																		UpperCAmelCase      ,							UpperCAmelCase			:   Union[str, Any]      =				image.size
													else:
																		UpperCAmelCase      ,							UpperCAmelCase			:   Optional[int]      =				image.shape[1], image.shape[2]
													if w < h:
																		UpperCAmelCase			:   Optional[Any]      =				int(self.size["shortest_edge"] * h / w  )
																		UpperCAmelCase			:   int      =				self.size["shortest_edge"]
													elif w > h:
																		UpperCAmelCase			:   Union[str, Any]      =				self.size["shortest_edge"]
																		UpperCAmelCase			:   List[str]      =				int(self.size["shortest_edge"] * w / h  )
													else:
																		UpperCAmelCase			:   Union[str, Any]      =				self.size["shortest_edge"]
																		UpperCAmelCase			:   Any      =				self.size["shortest_edge"]

								else:
													UpperCAmelCase			:   int      =				[]
													for image in image_inputs:
																		UpperCAmelCase      ,							UpperCAmelCase			:   int      =				self.get_expected_values([image]  )
																		expected_values.append((expected_height, expected_width)  )
													UpperCAmelCase			:   Dict      =				max(UpperCamelCase__			,  key=lambda lowercase  : item[0]  )[0]
													UpperCAmelCase			:   List[Any]      =				max(UpperCamelCase__			,  key=lambda lowercase  : item[1]  )[1]

								return expected_height, expected_width







@require_torch
@require_vision
class       snake_case__  ( lowerCAmelCase_					,  unittest.TestCase     ):
			SCREAMING_SNAKE_CASE__     				=						DetrImageProcessor if is_vision_available() else None







			def 			__lowerCAmelCase				(			self			: Tuple  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[Any]      =				DetrImageProcessingTester(self  )







			@property
			def 			__lowerCAmelCase				(			self			: Tuple  ):





								'''simple docstring'''



								return self.image_processor_tester.prepare_image_processor_dict()







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				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_rescale"  )  )
								self.assertTrue(hasattr(UpperCamelCase__			,  "rescale_factor"  )  )
								self.assertTrue(hasattr(UpperCamelCase__			,  "do_resize"  )  )
								self.assertTrue(hasattr(UpperCamelCase__			,  "size"  )  )
								self.assertTrue(hasattr(UpperCamelCase__			,  "do_pad"  )  )







			def 			__lowerCAmelCase				(			self			: int  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				self.image_processing_class.from_dict(self.image_processor_dict  )
								self.assertEqual(image_processor.size			,  {"shortest_edge": 18, "longest_edge": 13_33}  )
								self.assertEqual(image_processor.do_pad			,  UpperCamelCase__  )

								UpperCAmelCase			:   Union[str, Any]      =				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			: Optional[Any]  ):





								'''simple docstring'''



								pass







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[Any]      =				self.image_processing_class(**self.image_processor_dict  )
								# create random PIL images
								UpperCAmelCase			:   Dict      =				prepare_image_inputs(self.image_processor_tester			,  equal_resolution=UpperCamelCase__  )
								for image in image_inputs:
													self.assertIsInstance(UpperCamelCase__			,  Image.Image  )

								# Test not batched input
								UpperCAmelCase			:   Optional[Any]      =				image_processing(image_inputs[0]			,  return_tensors="pt"  ).pixel_values

								UpperCAmelCase      ,							UpperCAmelCase			:   Optional[Any]      =				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
								UpperCAmelCase      ,							UpperCAmelCase			:   List[Any]      =				self.image_processor_tester.get_expected_values(UpperCamelCase__			,  batched=UpperCamelCase__  )

								UpperCAmelCase			:   str      =				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			: Dict  ):





								'''simple docstring'''



								UpperCAmelCase			:   Tuple      =				self.image_processing_class(**self.image_processor_dict  )
								# create random numpy tensors
								UpperCAmelCase			:   str      =				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
								UpperCAmelCase			:   int      =				image_processing(image_inputs[0]			,  return_tensors="pt"  ).pixel_values

								UpperCAmelCase      ,							UpperCAmelCase			:   int      =				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
								UpperCAmelCase			:   Union[str, Any]      =				image_processing(UpperCamelCase__			,  return_tensors="pt"  ).pixel_values

								UpperCAmelCase      ,							UpperCAmelCase			:   Optional[int]      =				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			: int  ):





								'''simple docstring'''



								UpperCAmelCase			:   Any      =				self.image_processing_class(**self.image_processor_dict  )
								# create random PyTorch tensors
								UpperCAmelCase			:   Dict      =				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
								UpperCAmelCase			:   List[str]      =				image_processing(image_inputs[0]			,  return_tensors="pt"  ).pixel_values

								UpperCAmelCase      ,							UpperCAmelCase			:   Optional[Any]      =				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
								UpperCAmelCase			:   int      =				image_processing(UpperCamelCase__			,  return_tensors="pt"  ).pixel_values

								UpperCAmelCase      ,							UpperCAmelCase			:   Union[str, Any]      =				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			: Tuple  ):





								'''simple docstring'''



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

								UpperCAmelCase			:   Any      =				{"image_id": 3_97_69, "annotations": target}

								# encode them
								UpperCAmelCase			:   Tuple      =				DetrImageProcessor.from_pretrained("facebook/detr-resnet-50"  )
								UpperCAmelCase			:   Dict      =				image_processing(images=UpperCamelCase__			,  annotations=UpperCamelCase__			,  return_tensors="pt"  )

								# verify pixel values
								UpperCAmelCase			:   List[Any]      =				torch.Size([1, 3, 8_00, 10_66]  )
								self.assertEqual(encoding["pixel_values"].shape			,  UpperCamelCase__  )

								UpperCAmelCase			:   Tuple      =				torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]  )
								self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]			,  UpperCamelCase__			,  atol=1E-4  )  )

								# verify area
								UpperCAmelCase			:   Any      =				torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["area"]			,  UpperCamelCase__  )  )
								# verify boxes
								UpperCAmelCase			:   Tuple      =				torch.Size([6, 4]  )
								self.assertEqual(encoding["labels"][0]["boxes"].shape			,  UpperCamelCase__  )
								UpperCAmelCase			:   str      =				torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]			,  UpperCamelCase__			,  atol=1E-3  )  )
								# verify image_id
								UpperCAmelCase			:   str      =				torch.tensor([3_97_69]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]			,  UpperCamelCase__  )  )
								# verify is_crowd
								UpperCAmelCase			:   str      =				torch.tensor([0, 0, 0, 0, 0, 0]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]			,  UpperCamelCase__  )  )
								# verify class_labels
								UpperCAmelCase			:   Optional[int]      =				torch.tensor([75, 75, 63, 65, 17, 17]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]			,  UpperCamelCase__  )  )
								# verify orig_size
								UpperCAmelCase			:   Dict      =				torch.tensor([4_80, 6_40]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]			,  UpperCamelCase__  )  )
								# verify size
								UpperCAmelCase			:   str      =				torch.tensor([8_00, 10_66]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["size"]			,  UpperCamelCase__  )  )







			@slow
			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
								with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"			,  "r"  ) as f:
													UpperCAmelCase			:   Any      =				json.loads(f.read()  )

								UpperCAmelCase			:   int      =				{"file_name": "000000039769.png", "image_id": 3_97_69, "segments_info": target}

								UpperCAmelCase			:   List[str]      =				pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic"  )

								# encode them
								UpperCAmelCase			:   str      =				DetrImageProcessor.from_pretrained("facebook/detr-resnet-50-panoptic"  )
								UpperCAmelCase			:   Any      =				image_processing(images=UpperCamelCase__			,  annotations=UpperCamelCase__			,  masks_path=UpperCamelCase__			,  return_tensors="pt"  )

								# verify pixel values
								UpperCAmelCase			:   List[Any]      =				torch.Size([1, 3, 8_00, 10_66]  )
								self.assertEqual(encoding["pixel_values"].shape			,  UpperCamelCase__  )

								UpperCAmelCase			:   Optional[int]      =				torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]  )
								self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]			,  UpperCamelCase__			,  atol=1E-4  )  )

								# verify area
								UpperCAmelCase			:   str      =				torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["area"]			,  UpperCamelCase__  )  )
								# verify boxes
								UpperCAmelCase			:   Tuple      =				torch.Size([6, 4]  )
								self.assertEqual(encoding["labels"][0]["boxes"].shape			,  UpperCamelCase__  )
								UpperCAmelCase			:   int      =				torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]			,  UpperCamelCase__			,  atol=1E-3  )  )
								# verify image_id
								UpperCAmelCase			:   Tuple      =				torch.tensor([3_97_69]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]			,  UpperCamelCase__  )  )
								# verify is_crowd
								UpperCAmelCase			:   Union[str, Any]      =				torch.tensor([0, 0, 0, 0, 0, 0]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]			,  UpperCamelCase__  )  )
								# verify class_labels
								UpperCAmelCase			:   Any      =				torch.tensor([17, 17, 63, 75, 75, 93]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]			,  UpperCamelCase__  )  )
								# verify masks
								UpperCAmelCase			:   Optional[Any]      =				82_28_73
								self.assertEqual(encoding["labels"][0]["masks"].sum().item()			,  UpperCamelCase__  )
								# verify orig_size
								UpperCAmelCase			:   Optional[Any]      =				torch.tensor([4_80, 6_40]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]			,  UpperCamelCase__  )  )
								# verify size
								UpperCAmelCase			:   List[str]      =				torch.tensor([8_00, 10_66]  )
								self.assertTrue(torch.allclose(encoding["labels"][0]["size"]			,  UpperCamelCase__  )  )









"""simple docstring"""






def   lowercase_	(    _lowercase       :		int							,      _lowercase       :		int	):




					'''simple docstring'''






					return int(input_a == input_a == 0	)



def   lowercase_	(    ):




					'''simple docstring'''






					print("Truth Table of NOR Gate:"	)
					print("|   Input 1   |  Input 2  | Output |"	)
					print(F"""|      0      |    0      |  {nor_gate(0							,      0	)}     |"""	)
					print(F"""|      0      |    1      |  {nor_gate(0							,      1	)}     |"""	)
					print(F"""|      1      |    0      |  {nor_gate(1							,      0	)}     |"""	)
					print(F"""|      1      |    1      |  {nor_gate(1							,      1	)}     |"""	)


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	main()


