




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





									@staticmethod
									def 							A (      *a_						:    Any      ,		**a_						:    List[str]							):



												"""simple docstring"""




												pass



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





			def 							A (      self						:    Optional[Any]      ,		a_						:    int      ,		a_						:    Dict      ,		a_						:    List[Any]							):



						"""simple docstring"""




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





			def 							A (      self						:    str      ,		a_						:    Dict      ,		a_						:    Dict							):



						"""simple docstring"""




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

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

						__snake_case			=		[
						    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"],
						]
						__snake_case			=		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						:    Union[str, Any]							):



						"""simple docstring"""




						pass





			@require_torch
			def 							A (      self						:    List[str]							):



						"""simple docstring"""




						__snake_case			=		"hf-internal-testing/tiny-detr-mobilenetsv3"

						__snake_case			=		AutoModelForObjectDetection.from_pretrained(a_							)
						__snake_case			=		AutoFeatureExtractor.from_pretrained(a_							)
						__snake_case			=		ObjectDetectionPipeline(model=a_      ,		feature_extractor=a_							)

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

						self.assertEqual(
						    nested_simplify(a_      ,		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}},
						    ]      ,		)

						__snake_case			=		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.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 							A (      self						:    str							):



						"""simple docstring"""




						__snake_case			=		"facebook/detr-resnet-50"

						__snake_case			=		AutoModelForObjectDetection.from_pretrained(a_							)
						__snake_case			=		AutoFeatureExtractor.from_pretrained(a_							)
						__snake_case			=		ObjectDetectionPipeline(model=a_      ,		feature_extractor=a_							)

						__snake_case			=		object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"							)
						self.assertEqual(
						    nested_simplify(a_      ,		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}},
						    ]      ,		)

						__snake_case			=		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.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 							A (      self						:    int							):



						"""simple docstring"""




						__snake_case			=		"facebook/detr-resnet-50"

						__snake_case			=		pipeline("object-detection"      ,		model=a_							)

						__snake_case			=		object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"							)
						self.assertEqual(
						    nested_simplify(a_      ,		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}},
						    ]      ,		)

						__snake_case			=		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.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 							A (      self						:    Optional[Any]							):



						"""simple docstring"""




						__snake_case			=		0.9985
						__snake_case			=		"facebook/detr-resnet-50"

						__snake_case			=		pipeline("object-detection"      ,		model=a_							)

						__snake_case			=		object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"      ,		threshold=a_							)
						self.assertEqual(
						    nested_simplify(a_      ,		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 							A (      self						:    Tuple							):



						"""simple docstring"""




						__snake_case			=		"Narsil/layoutlmv3-finetuned-funsd"
						__snake_case			=		0.9993

						__snake_case			=		pipeline("object-detection"      ,		model=a_      ,		threshold=a_							)

						__snake_case			=		object_detector(
						    "https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"							)
						self.assertEqual(
						    nested_simplify(a_      ,		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 typing import Dict

from .base import GenericTensor, Pipeline


class 	__snake_case   ( SCREAMING_SNAKE_CASE ):
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_=None    ,a_=None    ,a_=None    ,**a_						):







						"""simple docstring"""
						if tokenize_kwargs is None:
										lowerCAmelCase__		  =  {}

						if truncation is not None:
										if "truncation" in tokenize_kwargs:
														raise ValueError(
														    'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)'						)
										lowerCAmelCase__		  =  truncation

						lowerCAmelCase__		  =  tokenize_kwargs

						lowerCAmelCase__		  =  {}
						if return_tensors is not None:
										lowerCAmelCase__		  =  return_tensors

						return preprocess_params, {}, postprocess_params
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,**a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.framework
						lowerCAmelCase__		  =  self.tokenizer(a_    ,return_tensors=a_    ,**a_						)
						return model_inputs
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model(**a_						)
						return model_outputs
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_=False						):







						"""simple docstring"""
						# [0] is the first available tensor, logits or last_hidden_state.
						if return_tensors:
										return model_outputs[0]
						if self.framework == "pt":
										return model_outputs[0].tolist()
						elif self.framework == "tf":
										return model_outputs[0].numpy().tolist()



		def __call__(							self    ,*a_    ,**a_						):







						"""simple docstring"""
						return super().__call__(*a_    ,**a_						)
