





'''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 ConditionalDetrImageProcessor




class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


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

							'''simple docstring'''



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


					def lowerCamelCase_			(						self				:			Dict						):

							'''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				:			Tuple      ,						lowerCamelCase_				:			Union[str, Any]      ,						lowerCamelCase_				:			Optional[Any]=False						):

							'''simple docstring'''



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

							else:
									SCREAMING_SNAKE_CASE :				Optional[int]		=			[]
									for image in image_inputs:
											SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				List[str]		=			self.get_expected_values([image]						)
											expected_values.append((expected_height, expected_width)						)
									SCREAMING_SNAKE_CASE :				int		=			max(lowerCamelCase_      ,						key=lambda lowerCamelCase_						: item[0]						)[0]
									SCREAMING_SNAKE_CASE :				Tuple		=			max(lowerCamelCase_      ,						key=lambda lowerCamelCase_						: item[1]						)[1]

							return expected_height, expected_width







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




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	ConditionalDetrImageProcessor if is_vision_available() else None


					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Any		=			ConditionalDetrImageProcessingTester(self						)


					@property
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							return self.image_processor_tester.prepare_image_processor_dict()


					def lowerCamelCase_			(						self				:			Optional[Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Tuple		=			self.image_processing_class(**self.image_processor_dict						)
							self.assertTrue(hasattr(lowerCamelCase_      ,						"""image_mean"""						)						)
							self.assertTrue(hasattr(lowerCamelCase_      ,						"""image_std"""						)						)
							self.assertTrue(hasattr(lowerCamelCase_      ,						"""do_normalize"""						)						)
							self.assertTrue(hasattr(lowerCamelCase_      ,						"""do_resize"""						)						)
							self.assertTrue(hasattr(lowerCamelCase_      ,						"""size"""						)						)


					def lowerCamelCase_			(						self				:			Optional[int]						):

							'''simple docstring'''



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

							SCREAMING_SNAKE_CASE :				int		=			self.image_processing_class.from_dict(
							    self.image_processor_dict      ,						size=42      ,						max_size=84      ,						pad_and_return_pixel_mask=lowerCamelCase_						)
							self.assertEqual(image_processor.size      ,						{"""shortest_edge""": 42, """longest_edge""": 84}						)
							self.assertEqual(image_processor.do_pad      ,						lowerCamelCase_						)


					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							pass


					def lowerCamelCase_			(						self				:			Optional[int]						):

							'''simple docstring'''



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

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

							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				int		=			self.image_processor_tester.get_expected_values(lowerCamelCase_						)

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

							# Test batched
							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Any		=			self.image_processor_tester.get_expected_values(lowerCamelCase_      ,						batched=lowerCamelCase_						)

							SCREAMING_SNAKE_CASE :				Any		=			image_processing(lowerCamelCase_      ,						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				:			int						):

							'''simple docstring'''



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

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

							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Optional[int]		=			self.image_processor_tester.get_expected_values(lowerCamelCase_						)

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

							# Test batched
							SCREAMING_SNAKE_CASE :				int		=			image_processing(lowerCamelCase_      ,						return_tensors="""pt"""						).pixel_values

							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Any		=			self.image_processor_tester.get_expected_values(lowerCamelCase_      ,						batched=lowerCamelCase_						)

							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'''



							SCREAMING_SNAKE_CASE :				Any		=			self.image_processing_class(**self.image_processor_dict						)
							# create random PyTorch tensors
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			prepare_image_inputs(self.image_processor_tester      ,						equal_resolution=lowerCamelCase_      ,						torchify=lowerCamelCase_						)
							for image in image_inputs:
									self.assertIsInstance(lowerCamelCase_      ,						torch.Tensor						)

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

							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Tuple		=			self.image_processor_tester.get_expected_values(lowerCamelCase_						)

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

							# Test batched
							SCREAMING_SNAKE_CASE :				List[str]		=			image_processing(lowerCamelCase_      ,						return_tensors="""pt"""						).pixel_values

							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.image_processor_tester.get_expected_values(lowerCamelCase_      ,						batched=lowerCamelCase_						)

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

							'''simple docstring'''



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

							SCREAMING_SNAKE_CASE :				List[Any]		=			{"""image_id""": 3_97_69, """annotations""": target}

							# encode them
							SCREAMING_SNAKE_CASE :				str		=			ConditionalDetrImageProcessor.from_pretrained("""microsoft/conditional-detr-resnet-50"""						)
							SCREAMING_SNAKE_CASE :				Optional[int]		=			image_processing(images=lowerCamelCase_      ,						annotations=lowerCamelCase_      ,						return_tensors="""pt"""						)

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

							SCREAMING_SNAKE_CASE :				Tuple		=			torch.tensor([0.2_796, 0.3_138, 0.3_481]						)
							self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]      ,						lowerCamelCase_      ,						atol=1e-4						)						)

							# verify area
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]      ,						lowerCamelCase_						)						)
							# verify boxes
							SCREAMING_SNAKE_CASE :				Dict		=			torch.Size([6, 4]						)
							self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape      ,						lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				Tuple		=			torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]      ,						lowerCamelCase_      ,						atol=1e-3						)						)
							# verify image_id
							SCREAMING_SNAKE_CASE :				str		=			torch.tensor([3_97_69]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]      ,						lowerCamelCase_						)						)
							# verify is_crowd
							SCREAMING_SNAKE_CASE :				str		=			torch.tensor([0, 0, 0, 0, 0, 0]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]      ,						lowerCamelCase_						)						)
							# verify class_labels
							SCREAMING_SNAKE_CASE :				str		=			torch.tensor([75, 75, 63, 65, 17, 17]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]      ,						lowerCamelCase_						)						)
							# verify orig_size
							SCREAMING_SNAKE_CASE :				Tuple		=			torch.tensor([4_80, 6_40]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]      ,						lowerCamelCase_						)						)
							# verify size
							SCREAMING_SNAKE_CASE :				List[str]		=			torch.tensor([8_00, 10_66]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]      ,						lowerCamelCase_						)						)



					@slow
					def lowerCamelCase_			(						self				:			Optional[int]						):

							'''simple docstring'''



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

							SCREAMING_SNAKE_CASE :				str		=			{"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target}

							SCREAMING_SNAKE_CASE :				Tuple		=			pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic"""						)

							# encode them
							SCREAMING_SNAKE_CASE :				int		=			ConditionalDetrImageProcessor(format="""coco_panoptic"""						)
							SCREAMING_SNAKE_CASE :				Dict		=			image_processing(images=lowerCamelCase_      ,						annotations=lowerCamelCase_      ,						masks_path=lowerCamelCase_      ,						return_tensors="""pt"""						)

							# verify pixel values
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			torch.Size([1, 3, 8_00, 10_66]						)
							self.assertEqual(encoding["""pixel_values"""].shape      ,						lowerCamelCase_						)

							SCREAMING_SNAKE_CASE :				Optional[Any]		=			torch.tensor([0.2_796, 0.3_138, 0.3_481]						)
							self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]      ,						lowerCamelCase_      ,						atol=1e-4						)						)

							# verify area
							SCREAMING_SNAKE_CASE :				Optional[int]		=			torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]      ,						lowerCamelCase_						)						)
							# verify boxes
							SCREAMING_SNAKE_CASE :				str		=			torch.Size([6, 4]						)
							self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape      ,						lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				List[str]		=			torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]      ,						lowerCamelCase_      ,						atol=1e-3						)						)
							# verify image_id
							SCREAMING_SNAKE_CASE :				Dict		=			torch.tensor([3_97_69]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]      ,						lowerCamelCase_						)						)
							# verify is_crowd
							SCREAMING_SNAKE_CASE :				Any		=			torch.tensor([0, 0, 0, 0, 0, 0]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]      ,						lowerCamelCase_						)						)
							# verify class_labels
							SCREAMING_SNAKE_CASE :				Dict		=			torch.tensor([17, 17, 63, 75, 75, 93]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]      ,						lowerCamelCase_						)						)
							# verify masks
							SCREAMING_SNAKE_CASE :				List[str]		=			82_28_73
							self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item()      ,						lowerCamelCase_						)
							# verify orig_size
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			torch.tensor([4_80, 6_40]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]      ,						lowerCamelCase_						)						)
							# verify size
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			torch.tensor([8_00, 10_66]						)
							self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]      ,						lowerCamelCase_						)						)










'''simple docstring'''




import unittest

from transformers.testing_utils import require_bsa
from transformers.utils import is_bsa_available

from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin


if is_bsa_available():
							from transformers import MarkupLMFeatureExtractor




class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


					def __init__(						self				:			str      ,						lowerCamelCase_				:			str						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				List[str]		=			parent



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

							'''simple docstring'''



							return {}







def 		__A		(  ):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE :				Any		=			"""<HTML>

    <HEAD>
    <TITLE>sample document</TITLE>
    </HEAD>

    <BODY BGCOLOR=\"FFFFFF\">
    <HR>
    <a href=\"http://google.com\">Goog</a>
    <H1>This is one header</H1>
    <H2>This is a another Header</H2>
    <P>Travel from
        <P>
        <B>SFO to JFK</B>
        <BR>
        <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>
        <HR>
        <div style=\"color:#0000FF\">
            <h3>Traveler <b> name </b> is
            <p> John Doe </p>
        </div>"""

		SCREAMING_SNAKE_CASE :				int		=			"""
    <!DOCTYPE html>
    <html>
    <body>

    <h1>My First Heading</h1>
    <p>My first paragraph.</p>

    </body>
    </html>
    """

		return [html_string_a, html_string_a]




@require_bsa
class      UpperCamelCase__					(					lowercase_						,					unittest.TestCase			):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	MarkupLMFeatureExtractor if is_bsa_available() else None


					def lowerCamelCase_			(						self				:			Optional[int]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				int		=			MarkupLMFeatureExtractionTester(self						)


					@property
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							return self.feature_extract_tester.prepare_feat_extract_dict()



					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.feature_extraction_class()

							# Test not batched input
							SCREAMING_SNAKE_CASE :				Optional[int]		=			get_html_strings()[0]
							SCREAMING_SNAKE_CASE :				List[Any]		=			feature_extractor(lowerCamelCase_						)

							# fmt: off
							SCREAMING_SNAKE_CASE :				Optional[int]		=			[["""sample document""", """Goog""", """This is one header""", """This is a another Header""", """Travel from""", """SFO to JFK""", """on May 2, 2015 at 2:00 pm. For details go to confirm.com""", """Traveler""", """name""", """is""", """John Doe"""]]
							SCREAMING_SNAKE_CASE :				List[str]		=			[["""/html/head/title""", """/html/body/a""", """/html/body/h1""", """/html/body/h2""", """/html/body/p""", """/html/body/p/p/b[1]""", """/html/body/p/p/b[2]/i""", """/html/body/p/p/div/h3""", """/html/body/p/p/div/h3/b""", """/html/body/p/p/div/h3""", """/html/body/p/p/div/h3/p"""]]
							# fmt: on

							self.assertEqual(encoding.nodes      ,						lowerCamelCase_						)
							self.assertEqual(encoding.xpaths      ,						lowerCamelCase_						)

							# Test batched
							SCREAMING_SNAKE_CASE :				str		=			get_html_strings()
							SCREAMING_SNAKE_CASE :				str		=			feature_extractor(lowerCamelCase_						)

							# fmt: off
							SCREAMING_SNAKE_CASE :				int		=			expected_nodes + [["""My First Heading""", """My first paragraph."""]]
							SCREAMING_SNAKE_CASE :				List[Any]		=			expected_xpaths + [["""/html/body/h1""", """/html/body/p"""]]

							self.assertEqual(len(encoding.nodes						)      ,						2						)
							self.assertEqual(len(encoding.xpaths						)      ,						2						)

							self.assertEqual(encoding.nodes      ,						lowerCamelCase_						)
							self.assertEqual(encoding.xpaths      ,						lowerCamelCase_						)



