


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





          """simple docstring"""






          @staticmethod
          def  lowercase_							(				*SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	Any:
               pass





@is_pipeline_test
@require_vision
@require_timm
@require_torch
class   UpperCAmelCase_    (unittest.TestCase    ):





    """simple docstring"""






    lowerCamelCase    :   Union[str, Any]					=     MODEL_FOR_OBJECT_DETECTION_MAPPING







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )							->	str:
         __lowerCamelCase  :      Dict														=						ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_     ,	image_processor=SCREAMING_SNAKE_CASE_      )
         return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )							->	Tuple:
         __lowerCamelCase  :      Optional[int]														=						object_detector('./tests/fixtures/tests_samples/COCO/000000039769.png'     ,	threshold=0.0      )

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

         import datasets

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

         __lowerCamelCase  :      str														=						[
             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  :      Any														=						object_detector(SCREAMING_SNAKE_CASE_     ,	threshold=0.0      )

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







    @require_tf
    @unittest.skip('Object detection not implemented in TF'      )
    def  lowercase_							(				self      )							->	Any:
         pass







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

         __lowerCamelCase  :      Optional[Any]														=						AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Optional[int]														=						AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Any														=						ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_     ,	feature_extractor=SCREAMING_SNAKE_CASE_      )

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

         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                 {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
             ]     ,	)

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

         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 [
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                 ],
                 [
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                 ],
             ]     ,	)







    @require_torch
    @slow
    def  lowercase_							(				self      )							->	Dict:
         __lowerCamelCase  :      int														=						'facebook/detr-resnet-50'

         __lowerCamelCase  :      Dict														=						AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Optional[int]														=						ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_     ,	feature_extractor=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Tuple														=						object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                 {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                 {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                 {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                 {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
             ]     ,	)

         __lowerCamelCase  :      Dict														=						object_detector(
             [
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
             ]      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
             ]     ,	)







    @require_torch
    @slow
    def  lowercase_							(				self      )							->	List[Any]:
         __lowerCamelCase  :      Optional[int]														=						'facebook/detr-resnet-50'

         __lowerCamelCase  :      str														=						pipeline('object-detection'     ,	model=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      int														=						object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                 {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                 {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                 {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                 {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
             ]     ,	)

         __lowerCamelCase  :      str														=						object_detector(
             [
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
             ]      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
             ]     ,	)







    @require_torch
    @slow
    def  lowercase_							(				self      )							->	List[str]:
         __lowerCamelCase  :      List[Any]														=						0.9_9_8_5
         __lowerCamelCase  :      Optional[int]														=						'facebook/detr-resnet-50'

         __lowerCamelCase  :      int														=						pipeline('object-detection'     ,	model=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      List[str]														=						object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'     ,	threshold=SCREAMING_SNAKE_CASE_      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                 {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
             ]     ,	)







    @require_torch
    @require_pytesseract
    @slow
    def  lowercase_							(				self      )							->	Tuple:
         __lowerCamelCase  :      Optional[Any]														=						'Narsil/layoutlmv3-finetuned-funsd'
         __lowerCamelCase  :      Dict														=						0.9_9_9_3

         __lowerCamelCase  :      int														=						pipeline('object-detection'     ,	model=SCREAMING_SNAKE_CASE_     ,	threshold=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      int														=						object_detector(
             'https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_9_3, 'label': 'I-ANSWER', 'box': {'xmin': 2_94, 'ymin': 2_54, 'xmax': 3_43, 'ymax': 2_64}},
                 {'score': 0.9_9_9_3, 'label': 'I-ANSWER', 'box': {'xmin': 2_94, 'ymin': 2_54, 'xmax': 3_43, 'ymax': 2_64}},
             ]     ,	)












'''simple docstring'''



def       _UpperCamelCase			(_lowerCamelCase				:  int						)-> int:




     '''simple docstring'''





     __snake_case            =	abs(_lowerCamelCase						)
     __snake_case            =	0
     while n > 0:
          res += n % 10
          n //= 10
     return res







def       _UpperCamelCase			(_lowerCamelCase				:  int						)-> int:




     '''simple docstring'''





     __snake_case            =	abs(_lowerCamelCase						)
     return n if n < 10 else n % 10 + sum_of_digits(n // 10						)







def       _UpperCamelCase			(_lowerCamelCase				:  int						)-> int:




     '''simple docstring'''





     return sum(int(_lowerCamelCase						) for c in str(abs(_lowerCamelCase						)						)						)







def       _UpperCamelCase			()-> None:




     '''simple docstring'''





     from collections.abc import Callable
     from timeit import timeit

     def benchmark_a_function(_lowerCamelCase				:  Callable ,						_lowerCamelCase				:  int						) -> None:
          __snake_case            =	f'''{func.__name__}({value})'''
          __snake_case            =	timeit(f'''__main__.{call}''' ,						setup='''import __main__'''						)
          print(f'''{call:56} = {func(_lowerCamelCase						)} -- {timing:.4f} seconds'''						)

     for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76):
          for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact):
               benchmark_a_function(_lowerCamelCase ,						_lowerCamelCase						)
          print()


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()






