






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






           '''simple docstring'''




           @staticmethod
           def       __lowerCAmelCase					(      *__UpperCAmelCase      :						str ,					**__UpperCAmelCase      :						Tuple  )    ->int:




            """simple docstring"""


            pass






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






      '''simple docstring'''




      __snake_case       =  MODEL_FOR_OBJECT_DETECTION_MAPPING







      def       __lowerCAmelCase					(      self      :						Optional[Any] ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						Optional[int] ,					__UpperCAmelCase      :						Union[str, Any]  )    ->List[str]:




       """simple docstring"""


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







      def       __lowerCAmelCase					(      self      :						List[Any] ,					__UpperCAmelCase      :						List[Any] ,					__UpperCAmelCase      :						int  )    ->Tuple:




       """simple docstring"""


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

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

       a		  =      [
           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'''],
       ]
       a		  =      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       __lowerCAmelCase					(      self      :						Any  )    ->str:




       """simple docstring"""


       pass







      @require_torch
      def       __lowerCAmelCase					(      self      :						Optional[int]  )    ->Optional[Any]:




       """simple docstring"""


       a		  =      '''hf-internal-testing/tiny-detr-mobilenetsv3'''

       a		  =      AutoModelForObjectDetection.from_pretrained(__UpperCAmelCase  )
       a		  =      AutoFeatureExtractor.from_pretrained(__UpperCAmelCase  )
       a		  =      ObjectDetectionPipeline(model=__UpperCAmelCase ,					feature_extractor=__UpperCAmelCase  )

       a		  =      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''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
               {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
           ] ,					)

       a		  =      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''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
                   {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
               ],
               [
                   {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
                   {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}},
               ],
           ] ,					)







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




       """simple docstring"""


       a		  =      '''facebook/detr-resnet-50'''

       a		  =      AutoModelForObjectDetection.from_pretrained(__UpperCAmelCase  )
       a		  =      AutoFeatureExtractor.from_pretrained(__UpperCAmelCase  )
       a		  =      ObjectDetectionPipeline(model=__UpperCAmelCase ,					feature_extractor=__UpperCAmelCase  )

       a		  =      object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''  )
       self.assertEqual(
           nested_simplify(__UpperCAmelCase ,					decimals=4  ) ,					[
               {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
               {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
               {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
               {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
               {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
           ] ,					)

       a		  =      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''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
               [
                   {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
           ] ,					)







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




       """simple docstring"""


       a		  =      '''facebook/detr-resnet-50'''

       a		  =      pipeline('''object-detection''' ,					model=__UpperCAmelCase  )

       a		  =      object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg'''  )
       self.assertEqual(
           nested_simplify(__UpperCAmelCase ,					decimals=4  ) ,					[
               {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
               {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
               {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
               {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
               {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
           ] ,					)

       a		  =      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''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
               [
                   {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}},
                   {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}},
                   {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}},
                   {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
                   {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
               ],
           ] ,					)







      @require_torch
      @slow
      def       __lowerCAmelCase					(      self      :						Optional[int]  )    ->Dict:




       """simple docstring"""


       a		  =      0.9985
       a		  =      '''facebook/detr-resnet-50'''

       a		  =      pipeline('''object-detection''' ,					model=__UpperCAmelCase  )

       a		  =      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''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}},
               {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}},
           ] ,					)







      @require_torch
      @require_pytesseract
      @slow
      def       __lowerCAmelCase					(      self      :						List[Any]  )    ->Optional[Any]:




       """simple docstring"""


       a		  =      '''Narsil/layoutlmv3-finetuned-funsd'''
       a		  =      0.9993

       a		  =      pipeline('''object-detection''' ,					model=__UpperCAmelCase ,					threshold=__UpperCAmelCase  )

       a		  =      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''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}},
               {'''score''': 0.9993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}},
           ] ,					)










from math import factorial

UpperCAmelCase__				   =		{str(digit): factorial(digit) for digit in range(10)}
def 						_a    (      a :int )							->			int:
 if not isinstance(a	,							a ):
  raise TypeError('''Parameter number must be int''' )

 if number < 0:
  raise ValueError('''Parameter number must be greater than or equal to 0''' )

 # Converts number in string to iterate on its digits and adds its factorial.
 return sum(DIGIT_FACTORIAL[digit] for digit in str(a ) )
def 						_a    (      a :int = 60	,							a :int = 1_000_000 )							->			int:

 if not isinstance(a	,							a ) or not isinstance(a	,							a ):
  raise TypeError('''Parameters chain_length and number_limit must be int''' )

 if chain_length <= 0 or number_limit <= 0:
  raise ValueError(
      '''Parameters chain_length and number_limit must be greater than 0''' )

 # the counter for the chains with the exact desired length
 a		  =      0
 # the cached sizes of the previous chains
 a		  =      {}

 for start_chain_element in range(1	,							a ):
  # The temporary set will contain the elements of the chain
  a		  =      set()
  a		  =      0

  # Stop computing the chain when you find a cached size, a repeating item or the
  # length is greater then the desired one.
  a		  =      start_chain_element
  while (
      chain_element not in chain_sets_lengths
      and chain_element not in chain_set
      and chain_set_length <= chain_length
  ):
   chain_set.add(a )
   chain_set_length += 1
   a		  =      digit_factorial_sum(a )

  if chain_element in chain_sets_lengths:
   chain_set_length += chain_sets_lengths[chain_element]

  a		  =      chain_set_length

  # If chain contains the exact amount of elements increase the counter
  if chain_set_length == chain_length:
   chains_counter += 1

 return chains_counter


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     print(f"""{solution()}""")


