


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       lowerCamelCase__   :





       @staticmethod
       def      __A					(*UpperCAmelCase		,     **UpperCAmelCase       )     ->   List[Any]:
        pass







@is_pipeline_test
@require_vision
@require_timm
@require_torch
class       lowerCamelCase__   (       unittest.TestCase):
     SCREAMING_SNAKE_CASE__      			=			MODEL_FOR_OBJECT_DETECTION_MAPPING





     def      __A					(self		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       )     ->   int:
      _lowercase							      =ObjectDetectionPipeline(model=UpperCAmelCase		,     image_processor=UpperCAmelCase       )
      return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]





     def      __A					(self		,     UpperCAmelCase		,     UpperCAmelCase       )     ->   List[str]:
      _lowercase							      =object_detector('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		,     threshold=0.0       )

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

      import datasets

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

      _lowercase							      =[
          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'''],
      ]
      _lowercase							      =object_detector(UpperCAmelCase		,     threshold=0.0       )

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





     @require_tf
     @unittest.skip('''Object detection not implemented in TF'''       )
     def      __A					(self       )     ->   Union[str, Any]:
      pass





     @require_torch
     def      __A					(self       )     ->   Union[str, Any]:
      _lowercase							      ='''hf-internal-testing/tiny-detr-mobilenetsv3'''

      _lowercase							      =AutoModelForObjectDetection.from_pretrained(UpperCAmelCase       )
      _lowercase							      =AutoFeatureExtractor.from_pretrained(UpperCAmelCase       )
      _lowercase							      =ObjectDetectionPipeline(model=UpperCAmelCase		,     feature_extractor=UpperCAmelCase       )

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

      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}},
              {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}},
          ]		,     )

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

      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              [
                  {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}},
                  {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}},
              ],
              [
                  {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}},
                  {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}},
              ],
          ]		,     )





     @require_torch
     @slow
     def      __A					(self       )     ->   Optional[int]:
      _lowercase							      ='''facebook/detr-resnet-50'''

      _lowercase							      =AutoModelForObjectDetection.from_pretrained(UpperCAmelCase       )
      _lowercase							      =AutoFeatureExtractor.from_pretrained(UpperCAmelCase       )
      _lowercase							      =ObjectDetectionPipeline(model=UpperCAmelCase		,     feature_extractor=UpperCAmelCase       )

      _lowercase							      =object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''       )
      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}},
              {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}},
              {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}},
              {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}},
              {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}},
          ]		,     )

      _lowercase							      =object_detector(
          [
              '''http://images.cocodataset.org/val2017/000000039769.jpg''',
              '''http://images.cocodataset.org/val2017/000000039769.jpg''',
          ]       )
      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              [
                  {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}},
                  {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}},
                  {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}},
                  {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}},
                  {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}},
              ],
              [
                  {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}},
                  {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}},
                  {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}},
                  {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}},
                  {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}},
              ],
          ]		,     )





     @require_torch
     @slow
     def      __A					(self       )     ->   Dict:
      _lowercase							      ='''facebook/detr-resnet-50'''

      _lowercase							      =pipeline('''object-detection'''		,     model=UpperCAmelCase       )

      _lowercase							      =object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''       )
      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}},
              {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}},
              {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}},
              {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}},
              {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}},
          ]		,     )

      _lowercase							      =object_detector(
          [
              '''http://images.cocodataset.org/val2017/000000039769.jpg''',
              '''http://images.cocodataset.org/val2017/000000039769.jpg''',
          ]       )
      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              [
                  {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}},
                  {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}},
                  {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}},
                  {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}},
                  {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}},
              ],
              [
                  {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}},
                  {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}},
                  {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}},
                  {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}},
                  {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}},
              ],
          ]		,     )





     @require_torch
     @slow
     def      __A					(self       )     ->   Any:
      _lowercase							      =0.9985
      _lowercase							      ='''facebook/detr-resnet-50'''

      _lowercase							      =pipeline('''object-detection'''		,     model=UpperCAmelCase       )

      _lowercase							      =object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''		,     threshold=UpperCAmelCase       )
      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}},
              {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}},
          ]		,     )





     @require_torch
     @require_pytesseract
     @slow
     def      __A					(self       )     ->   Tuple:
      _lowercase							      ='''Narsil/layoutlmv3-finetuned-funsd'''
      _lowercase							      =0.9993

      _lowercase							      =pipeline('''object-detection'''		,     model=UpperCAmelCase		,     threshold=UpperCAmelCase       )

      _lowercase							      =object_detector(
          '''https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'''       )
      self.assertEqual(
          nested_simplify(UpperCAmelCase		,     decimals=4       )		,     [
              {'''score''': 0.9993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 2_9_4, '''ymin''': 2_5_4, '''xmax''': 3_4_3, '''ymax''': 2_6_4}},
              {'''score''': 0.9993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 2_9_4, '''ymin''': 2_5_4, '''xmax''': 3_4_3, '''ymax''': 2_6_4}},
          ]		,     )





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase__   	=							{
    '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''],
    '''tokenization_xlm''': ['''XLMTokenizer'''],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__   	=							[
      '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''XLMForMultipleChoice''',
      '''XLMForQuestionAnswering''',
      '''XLMForQuestionAnsweringSimple''',
      '''XLMForSequenceClassification''',
      '''XLMForTokenClassification''',
      '''XLMModel''',
      '''XLMPreTrainedModel''',
      '''XLMWithLMHeadModel''',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__   	=							[
      '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TFXLMForMultipleChoice''',
      '''TFXLMForQuestionAnsweringSimple''',
      '''TFXLMForSequenceClassification''',
      '''TFXLMForTokenClassification''',
      '''TFXLMMainLayer''',
      '''TFXLMModel''',
      '''TFXLMPreTrainedModel''',
      '''TFXLMWithLMHeadModel''',
  ]


if TYPE_CHECKING:
  from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
  from .tokenization_xlm import XLMTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlm import (
        XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLMForMultipleChoice,
        XLMForQuestionAnswering,
        XLMForQuestionAnsweringSimple,
        XLMForSequenceClassification,
        XLMForTokenClassification,
        XLMModel,
        XLMPreTrainedModel,
        XLMWithLMHeadModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlm import (
        TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLMForMultipleChoice,
        TFXLMForQuestionAnsweringSimple,
        TFXLMForSequenceClassification,
        TFXLMForTokenClassification,
        TFXLMMainLayer,
        TFXLMModel,
        TFXLMPreTrainedModel,
        TFXLMWithLMHeadModel,
    )

else:
  import sys

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

