




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






						'''simple docstring'''



						@staticmethod
						def       A_					(						*lowercase						,   **lowercase						):
										pass






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






			'''simple docstring'''



			lowerCamelCase__   =    MODEL_FOR_OBJECT_DETECTION_MAPPING
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Dict					=					ObjectDetectionPipeline(model=__A						,   image_processor=__A						)
							return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]
			def       A_					(						self						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    List[str]					=					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  :    List[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  :    List[Any]					=					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						):
							pass
			@require_torch
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[int]					=					"hf-internal-testing/tiny-detr-mobilenetsv3"

							_lowerCamelCase  :    List[str]					=					AutoModelForObjectDetection.from_pretrained(__A						)
							_lowerCamelCase  :    str					=					AutoFeatureExtractor.from_pretrained(__A						)
							_lowerCamelCase  :    Tuple					=					ObjectDetectionPipeline(model=__A						,   feature_extractor=__A						)

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

							self.assertEqual(
							    nested_simplify(__A						,   decimals=4						)						,   [
							        {'score': 0.33_76, 'label': 'LABEL_0', 'box': {'xmin': 159, 'ymin': 120, 'xmax': 480, 'ymax': 359}},
							        {'score': 0.33_76, 'label': 'LABEL_0', 'box': {'xmin': 159, 'ymin': 120, 'xmax': 480, 'ymax': 359}},
							    ]						,   )

							_lowerCamelCase  :    List[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.33_76, 'label': 'LABEL_0', 'box': {'xmin': 159, 'ymin': 120, 'xmax': 480, 'ymax': 359}},
							            {'score': 0.33_76, 'label': 'LABEL_0', 'box': {'xmin': 159, 'ymin': 120, 'xmax': 480, 'ymax': 359}},
							        ],
							        [
							            {'score': 0.33_76, 'label': 'LABEL_0', 'box': {'xmin': 159, 'ymin': 120, 'xmax': 480, 'ymax': 359}},
							            {'score': 0.33_76, 'label': 'LABEL_0', 'box': {'xmin': 159, 'ymin': 120, 'xmax': 480, 'ymax': 359}},
							        ],
							    ]						,   )
			@require_torch
			@slow
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[Any]					=					"facebook/detr-resnet-50"

							_lowerCamelCase  :    Tuple					=					AutoModelForObjectDetection.from_pretrained(__A						)
							_lowerCamelCase  :    Union[str, Any]					=					AutoFeatureExtractor.from_pretrained(__A						)
							_lowerCamelCase  :    Dict					=					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.99_82, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
							        {'score': 0.99_60, 'label': 'remote', 'box': {'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
							        {'score': 0.99_55, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
							        {'score': 0.99_88, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
							        {'score': 0.99_87, 'label': 'cat', 'box': {'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}},
							    ]						,   )

							_lowerCamelCase  :    List[str]					=					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.99_82, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
							            {'score': 0.99_60, 'label': 'remote', 'box': {'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
							            {'score': 0.99_55, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
							            {'score': 0.99_88, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
							            {'score': 0.99_87, 'label': 'cat', 'box': {'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}},
							        ],
							        [
							            {'score': 0.99_82, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
							            {'score': 0.99_60, 'label': 'remote', 'box': {'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
							            {'score': 0.99_55, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
							            {'score': 0.99_88, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
							            {'score': 0.99_87, 'label': 'cat', 'box': {'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}},
							        ],
							    ]						,   )
			@require_torch
			@slow
			def       A_					(						self						):
							_lowerCamelCase  :    Dict					=					"facebook/detr-resnet-50"

							_lowerCamelCase  :    Optional[Any]					=					pipeline('object-detection'						,   model=__A						)

							_lowerCamelCase  :    Optional[int]					=					object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'						)
							self.assertEqual(
							    nested_simplify(__A						,   decimals=4						)						,   [
							        {'score': 0.99_82, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
							        {'score': 0.99_60, 'label': 'remote', 'box': {'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
							        {'score': 0.99_55, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
							        {'score': 0.99_88, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
							        {'score': 0.99_87, '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.99_82, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
							            {'score': 0.99_60, 'label': 'remote', 'box': {'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
							            {'score': 0.99_55, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
							            {'score': 0.99_88, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
							            {'score': 0.99_87, 'label': 'cat', 'box': {'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}},
							        ],
							        [
							            {'score': 0.99_82, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
							            {'score': 0.99_60, 'label': 'remote', 'box': {'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
							            {'score': 0.99_55, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
							            {'score': 0.99_88, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
							            {'score': 0.99_87, 'label': 'cat', 'box': {'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}},
							        ],
							    ]						,   )
			@require_torch
			@slow
			def       A_					(						self						):
							_lowerCamelCase  :    int					=					0.99_85
							_lowerCamelCase  :    List[Any]					=					"facebook/detr-resnet-50"

							_lowerCamelCase  :    int					=					pipeline('object-detection'						,   model=__A						)

							_lowerCamelCase  :    int					=					object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'						,   threshold=__A						)
							self.assertEqual(
							    nested_simplify(__A						,   decimals=4						)						,   [
							        {'score': 0.99_88, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
							        {'score': 0.99_87, 'label': 'cat', 'box': {'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}},
							    ]						,   )






			@require_torch
			@require_pytesseract
			@slow
			def       A_					(						self						):
							_lowerCamelCase  :    Any					=					"Narsil/layoutlmv3-finetuned-funsd"
							_lowerCamelCase  :    List[Any]					=					0.99_93

							_lowerCamelCase  :    Union[str, Any]					=					pipeline('object-detection'						,   model=__A						,   threshold=__A						)

							_lowerCamelCase  :    str					=					object_detector(
							    'https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'						)
							self.assertEqual(
							    nested_simplify(__A						,   decimals=4						)						,   [
							        {'score': 0.99_93, 'label': 'I-ANSWER', 'box': {'xmin': 294, 'ymin': 254, 'xmax': 343, 'ymax': 264}},
							        {'score': 0.99_93, 'label': 'I-ANSWER', 'box': {'xmin': 294, 'ymin': 254, 'xmax': 343, 'ymax': 264}},
							    ]						,   )





"""simple docstring"""






import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
			from PIL import Image
else:






			class lowerCAmelCase__			:






						'''simple docstring'''



						@staticmethod
						def       A_					(						*lowercase						,   **lowercase						):
										pass






@is_pipeline_test
@require_vision
class lowerCAmelCase__			(  unittest.TestCase		):






			'''simple docstring'''



			@require_torch
			def       A_					(						self						):
							_lowerCamelCase  :    int					=					pipeline(
							    model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'						,   )
							_lowerCamelCase  :    List[Any]					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
							_lowerCamelCase  :    List[str]					=					image_classifier(lowercase						,   candidate_labels=['a', 'b', 'c']						)

							# The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
							# python and torch versions.
							self.assertIn(
							    nested_simplify(lowercase						)						,   [
							        [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}],
							        [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'c'}, {'score': 0.3_33, 'label': 'b'}],
							    ]						,   )

							_lowerCamelCase  :    Optional[Any]					=					image_classifier([image] * 5						,   candidate_labels=['A', 'B', 'C']						,   batch_size=2						)
							self.assertEqual(
							    nested_simplify(lowercase						)						,   [
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							    ]						,   )
			@require_tf
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[Any]					=					pipeline(
							    model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'						,   framework='tf'						)
							_lowerCamelCase  :    Tuple					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
							_lowerCamelCase  :    Optional[int]					=					image_classifier(lowercase						,   candidate_labels=['a', 'b', 'c']						)

							self.assertEqual(
							    nested_simplify(lowercase						)						,   [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}]						,   )

							_lowerCamelCase  :    Optional[Any]					=					image_classifier([image] * 5						,   candidate_labels=['A', 'B', 'C']						,   batch_size=2						)
							self.assertEqual(
							    nested_simplify(lowercase						)						,   [
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							        [
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							            {'score': 0.3_33, 'label': ANY(lowercase						)},
							        ],
							    ]						,   )
			@slow
			@require_torch
			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					pipeline(
							    task='zero-shot-image-classification'						,   model='openai/clip-vit-base-patch32'						,   )
							# This is an image of 2 cats with remotes and no planes
							_lowerCamelCase  :    Optional[Any]					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
							_lowerCamelCase  :    Optional[Any]					=					image_classifier(lowercase						,   candidate_labels=['cat', 'plane', 'remote']						)

							self.assertEqual(
							    nested_simplify(lowercase						)						,   [
							        {'score': 0.5_11, 'label': 'remote'},
							        {'score': 0.4_85, 'label': 'cat'},
							        {'score': 0.0_04, 'label': 'plane'},
							    ]						,   )

							_lowerCamelCase  :    Dict					=					image_classifier([image] * 5						,   candidate_labels=['cat', 'plane', 'remote']						,   batch_size=2						)
							self.assertEqual(
							    nested_simplify(lowercase						)						,   [
							        [
							            {'score': 0.5_11, 'label': 'remote'},
							            {'score': 0.4_85, 'label': 'cat'},
							            {'score': 0.0_04, 'label': 'plane'},
							        ],
							    ]
							    * 5						,   )






			@slow
			@require_tf
			def       A_					(						self						):
							_lowerCamelCase  :    List[str]					=					pipeline(
							    task='zero-shot-image-classification'						,   model='openai/clip-vit-base-patch32'						,   framework='tf'						)
							# This is an image of 2 cats with remotes and no planes
							_lowerCamelCase  :    List[str]					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
							_lowerCamelCase  :    Tuple					=					image_classifier(lowercase						,   candidate_labels=['cat', 'plane', 'remote']						)
							self.assertEqual(
							    nested_simplify(lowercase						)						,   [
							        {'score': 0.5_11, 'label': 'remote'},
							        {'score': 0.4_85, 'label': 'cat'},
							        {'score': 0.0_04, 'label': 'plane'},
							    ]						,   )

							_lowerCamelCase  :    Dict					=					image_classifier([image] * 5						,   candidate_labels=['cat', 'plane', 'remote']						,   batch_size=2						)
							self.assertEqual(
							    nested_simplify(lowercase						)						,   [
							        [
							            {'score': 0.5_11, 'label': 'remote'},
							            {'score': 0.4_85, 'label': 'cat'},
							            {'score': 0.0_04, 'label': 'plane'},
							        ],
							    ]
							    * 5						,   )