




'''simple docstring'''





import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image
else:




		class  _snake_case		:




					@staticmethod
					def    lowerCAmelCase__				(					*a__				,							**a__							)				->				List[str]:

												'''simple docstring'''




												pass




@is_pipeline_test
@require_vision
@require_timm
@require_torch
class  _snake_case		(	unittest.TestCase							):
			lowerCAmelCase_     :      str						       =    MODEL_FOR_OBJECT_DETECTION_MAPPING




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__							)				->				Tuple:

										'''simple docstring'''




										snake_case_						=      ObjectDetectionPipeline(model=a__				,							image_processor=a__							)
										return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]




			def    lowerCAmelCase__				(					self				,							a__				,							a__							)				->				List[Any]:

										'''simple docstring'''




										snake_case_						=      object_detector("./tests/fixtures/tests_samples/COCO/000000039769.png"				,							threshold=0.0							)

										self.assertGreater(len(a__							)				,							0							)
										for detected_object in outputs:
																	self.assertEqual(
																	    a__				,							{
																	        "score": ANY(a__							),
																	        "label": ANY(a__							),
																	        "box": {"xmin": ANY(a__							), "ymin": ANY(a__							), "xmax": ANY(a__							), "ymax": ANY(a__							)},
																	    }				,							)

										import datasets

										snake_case_						=      datasets.load_dataset("hf-internal-testing/fixtures_image_utils"				,							"image"				,							split="test"							)

										snake_case_						=      [
										    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							),
										    "http://images.cocodataset.org/val2017/000000039769.jpg",
										    # RGBA
										    dataset[0]["file"],
										    # LA
										    dataset[1]["file"],
										    # L
										    dataset[2]["file"],
										]
										snake_case_						=      object_detector(a__				,							threshold=0.0							)

										self.assertEqual(len(a__							)				,							len(a__							)							)
										for outputs in batch_outputs:
																	self.assertGreater(len(a__							)				,							0							)
																	for detected_object in outputs:
																								self.assertEqual(
																								    a__				,							{
																								        "score": ANY(a__							),
																								        "label": ANY(a__							),
																								        "box": {"xmin": ANY(a__							), "ymin": ANY(a__							), "xmax": ANY(a__							), "ymax": ANY(a__							)},
																								    }				,							)




			@require_tf
			@unittest.skip("Object detection not implemented in TF"							)
			def    lowerCAmelCase__				(					self							)				->				List[Any]:

										'''simple docstring'''




										pass




			@require_torch
			def    lowerCAmelCase__				(					self							)				->				List[str]:

										'''simple docstring'''




										snake_case_						=      "hf-internal-testing/tiny-detr-mobilenetsv3"

										snake_case_						=      AutoModelForObjectDetection.from_pretrained(a__							)
										snake_case_						=      AutoFeatureExtractor.from_pretrained(a__							)
										snake_case_						=      ObjectDetectionPipeline(model=a__				,							feature_extractor=a__							)

										snake_case_						=      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"				,							threshold=0.0							)

										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
										        {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
										    ]				,							)

										snake_case_						=      object_detector(
										    [
										        "http://images.cocodataset.org/val2017/000000039769.jpg",
										        "http://images.cocodataset.org/val2017/000000039769.jpg",
										    ]				,							threshold=0.0				,							)

										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        [
										            {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
										            {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
										        ],
										        [
										            {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
										            {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
										        ],
										    ]				,							)




			@require_torch
			@slow
			def    lowerCAmelCase__				(					self							)				->				Tuple:

										'''simple docstring'''




										snake_case_						=      "facebook/detr-resnet-50"

										snake_case_						=      AutoModelForObjectDetection.from_pretrained(a__							)
										snake_case_						=      AutoFeatureExtractor.from_pretrained(a__							)
										snake_case_						=      ObjectDetectionPipeline(model=a__				,							feature_extractor=a__							)

										snake_case_						=      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"							)
										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
										        {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
										        {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
										        {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
										        {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
										    ]				,							)

										snake_case_						=      object_detector(
										    [
										        "http://images.cocodataset.org/val2017/000000039769.jpg",
										        "http://images.cocodataset.org/val2017/000000039769.jpg",
										    ]							)
										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        [
										            {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
										            {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
										            {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
										            {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
										            {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
										        ],
										        [
										            {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
										            {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
										            {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
										            {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
										            {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
										        ],
										    ]				,							)




			@require_torch
			@slow
			def    lowerCAmelCase__				(					self							)				->				Optional[Any]:

										'''simple docstring'''




										snake_case_						=      "facebook/detr-resnet-50"

										snake_case_						=      pipeline("object-detection"				,							model=a__							)

										snake_case_						=      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"							)
										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
										        {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
										        {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
										        {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
										        {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
										    ]				,							)

										snake_case_						=      object_detector(
										    [
										        "http://images.cocodataset.org/val2017/000000039769.jpg",
										        "http://images.cocodataset.org/val2017/000000039769.jpg",
										    ]							)
										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        [
										            {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
										            {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
										            {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
										            {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
										            {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
										        ],
										        [
										            {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
										            {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
										            {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
										            {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
										            {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
										        ],
										    ]				,							)




			@require_torch
			@slow
			def    lowerCAmelCase__				(					self							)				->				Union[str, Any]:

										'''simple docstring'''




										snake_case_						=      0.9_9_8_5
										snake_case_						=      "facebook/detr-resnet-50"

										snake_case_						=      pipeline("object-detection"				,							model=a__							)

										snake_case_						=      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"				,							threshold=a__							)
										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
										        {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
										    ]				,							)




			@require_torch
			@require_pytesseract
			@slow
			def    lowerCAmelCase__				(					self							)				->				Tuple:

										'''simple docstring'''




										snake_case_						=      "Narsil/layoutlmv3-finetuned-funsd"
										snake_case_						=      0.9_9_9_3

										snake_case_						=      pipeline("object-detection"				,							model=a__				,							threshold=a__							)

										snake_case_						=      object_detector(
										    "https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"							)
										self.assertEqual(
										    nested_simplify(a__				,							decimals=4							)				,							[
										        {"score": 0.9_9_9_3, "label": "I-ANSWER", "box": {"xmin": 294, "ymin": 254, "xmax": 343, "ymax": 264}},
										        {"score": 0.9_9_9_3, "label": "I-ANSWER", "box": {"xmin": 294, "ymin": 254, "xmax": 343, "ymax": 264}},
										    ]				,							)











a__:      Dict									=		{0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}

a__:      str									=		{0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}


def 	UpperCamelCase__(       UpperCamelCase__			:       dict[int, list[int]] ,     UpperCamelCase__			:       int ,     UpperCamelCase__			:       list[bool]  )->list[int]:
       A__											=						True
       A__											=						[]

       for neighbour in graph[vert]:
              if not visited[neighbour]:
                     order += topology_sort(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )

       order.append(UpperCamelCase__  )

       return order


def 	UpperCamelCase__(       UpperCamelCase__			:       dict[int, list[int]] ,     UpperCamelCase__			:       int ,     UpperCamelCase__			:       list[bool]  )->list[int]:
       A__											=						True
       A__											=						[vert]

       for neighbour in reversed_graph[vert]:
              if not visited[neighbour]:
                     component += find_components(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )

       return component


def 	UpperCamelCase__(       UpperCamelCase__			:       dict[int, list[int]]  )->list[list[int]]:
       A__											=						len(UpperCamelCase__  ) * [False]
       A__											=						{vert: [] for vert in range(len(UpperCamelCase__  )  )}

       for vert, neighbours in graph.items():
              for neighbour in neighbours:
                     reversed_graph[neighbour].append(UpperCamelCase__  )

       A__											=						[]
       for i, was_visited in enumerate(UpperCamelCase__  ):
              if not was_visited:
                     order += topology_sort(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )

       A__											=						[]
       A__											=						len(UpperCamelCase__  ) * [False]

       for i in range(len(UpperCamelCase__  )  ):
              A__											=						order[len(UpperCamelCase__  ) - i - 1]
              if not visited[vert]:
                     A__											=						find_components(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )
                     components_list.append(UpperCamelCase__  )

       return components_list





