

"""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 	__UpperCamelCase    :

   @staticmethod
   def __lowerCamelCase		(		*_A    ,**_A      ):
       '''simple docstring'''

       pass







@is_pipeline_test
@require_vision
@require_timm
@require_torch
class 	__UpperCamelCase    (    unittest.TestCase						):
  _UpperCAmelCase          =		MODEL_FOR_OBJECT_DETECTION_MAPPING

  def __lowerCamelCase		(		self    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    ObjectDetectionPipeline(model=_A    ,image_processor=_A      )
      return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]

  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Optional[int]  =    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

      _lowerCAmelCase       :   List[Any]  =    datasets.load_dataset('hf-internal-testing/fixtures_image_utils'    ,'image'    ,split='test'      )

      _lowerCAmelCase       :   Optional[int]  =    [
          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'],
      ]
      _lowerCAmelCase       :   Dict  =    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      ):
      '''simple docstring'''

      pass

  @require_torch
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   int  =    'hf-internal-testing/tiny-detr-mobilenetsv3'

      _lowerCAmelCase       :   Optional[int]  =    AutoModelForObjectDetection.from_pretrained(_A      )
      _lowerCAmelCase       :   int  =    AutoFeatureExtractor.from_pretrained(_A      )
      _lowerCAmelCase       :   Tuple  =    ObjectDetectionPipeline(model=_A    ,feature_extractor=_A      )

      _lowerCAmelCase       :   str  =    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}},
          ]    ,)

      _lowerCAmelCase       :   Optional[Any]  =    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      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    'facebook/detr-resnet-50'

      _lowerCAmelCase       :   Optional[int]  =    AutoModelForObjectDetection.from_pretrained(_A      )
      _lowerCAmelCase       :   List[Any]  =    AutoFeatureExtractor.from_pretrained(_A      )
      _lowerCAmelCase       :   List[str]  =    ObjectDetectionPipeline(model=_A    ,feature_extractor=_A      )

      _lowerCAmelCase       :   List[Any]  =    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}},
          ]    ,)

      _lowerCAmelCase       :   Optional[Any]  =    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      ):
      '''simple docstring'''

      _lowerCAmelCase       :   List[str]  =    'facebook/detr-resnet-50'

      _lowerCAmelCase       :   Dict  =    pipeline('object-detection'    ,model=_A      )

      _lowerCAmelCase       :   Optional[Any]  =    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}},
          ]    ,)

      _lowerCAmelCase       :   Dict  =    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      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    0.9_9_8_5
      _lowerCAmelCase       :   str  =    'facebook/detr-resnet-50'

      _lowerCAmelCase       :   Optional[int]  =    pipeline('object-detection'    ,model=_A      )

      _lowerCAmelCase       :   Optional[Any]  =    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      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Dict  =    'Narsil/layoutlmv3-finetuned-funsd'
      _lowerCAmelCase       :   Tuple  =    0.9_9_9_3

      _lowerCAmelCase       :   str  =    pipeline('object-detection'    ,model=_A    ,threshold=_A      )

      _lowerCAmelCase       :   Any  =    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"""






import baseaa

def 		lowerCamelCase__		(					_lowerCamelCase   ):

    '''simple docstring'''



    return baseaa.aaaencode(string.encode('utf-8'   )   )

def 		lowerCamelCase__		(					_lowerCamelCase   ):

    '''simple docstring'''



    return baseaa.aaadecode(_lowerCamelCase   ).decode('utf-8'   )


if __name__ == "__main__":
 import doctest

 doctest.testmod()





