






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

       @staticmethod
       def   A__						(				*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)  ->				List[str]:



             """simple docstring"""






             pass






@is_pipeline_test
@require_vision
@require_timm
@require_torch
class     a_						(      unittest.TestCase    ):
  lowercase							=				MODEL_FOR_OBJECT_DETECTION_MAPPING

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				Dict:



        """simple docstring"""






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

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				Union[str, Any]:



        """simple docstring"""






        UpperCamelCase      = 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

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

        UpperCamelCase      = [
            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"""],
        ]
        UpperCamelCase      = 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   A__						(				self				)  ->				Optional[int]:



        """simple docstring"""






        pass

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



        """simple docstring"""






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

        UpperCamelCase      = AutoModelForObjectDetection.from_pretrained(_a				)
        UpperCamelCase      = AutoFeatureExtractor.from_pretrained(_a				)
        UpperCamelCase      = ObjectDetectionPipeline(model=_a      ,		feature_extractor=_a				)

        UpperCamelCase      = 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}},
            ]      ,		)

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



        """simple docstring"""






        UpperCamelCase      = """facebook/detr-resnet-50"""

        UpperCamelCase      = AutoModelForObjectDetection.from_pretrained(_a				)
        UpperCamelCase      = AutoFeatureExtractor.from_pretrained(_a				)
        UpperCamelCase      = ObjectDetectionPipeline(model=_a      ,		feature_extractor=_a				)

        UpperCamelCase      = 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}},
            ]      ,		)

        UpperCamelCase      = 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   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase      = """facebook/detr-resnet-50"""

        UpperCamelCase      = pipeline("""object-detection"""      ,		model=_a				)

        UpperCamelCase      = 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}},
            ]      ,		)

        UpperCamelCase      = 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   A__						(				self				)  ->				Any:



        """simple docstring"""






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

        UpperCamelCase      = pipeline("""object-detection"""      ,		model=_a				)

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



        """simple docstring"""






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

        UpperCamelCase      = pipeline("""object-detection"""      ,		model=_a      ,		threshold=_a				)

        UpperCamelCase      = 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}},
            ]      ,		)








'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE__     =       {
    'configuration_nllb_moe': [
        'NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'NllbMoeConfig',
    ]
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     SCREAMING_SNAKE_CASE__     =       [
         'NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST',
         'NllbMoeForConditionalGeneration',
         'NllbMoeModel',
         'NllbMoePreTrainedModel',
         'NllbMoeTop2Router',
         'NllbMoeSparseMLP',
     ]


if TYPE_CHECKING:
     from .configuration_nllb_moe import (
         NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
         NllbMoeConfig,
     )

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_nllb_moe import (
              NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
              NllbMoeForConditionalGeneration,
              NllbMoeModel,
              NllbMoePreTrainedModel,
              NllbMoeSparseMLP,
              NllbMoeTopaRouter,
          )


else:
     import sys

     SCREAMING_SNAKE_CASE__     =       _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
