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

							@staticmethod
							def    lowercase__      (	*__UpperCamelCase					:     List[str]   ,					**__UpperCamelCase					:     List[str]   ):
														pass





@is_pipeline_test
@require_vision
@require_timm
@require_torch
class       __A(				unittest.TestCase       ):
					SCREAMING_SNAKE_CASE			    =    MODEL_FOR_OBJECT_DETECTION_MAPPING

					def    lowercase__      (	self					:     Union[str, Any]   ,					__UpperCamelCase					:     Any   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     str   ):
												lowerCamelCase_							  =					ObjectDetectionPipeline(model=__UpperCamelCase   ,					image_processor=__UpperCamelCase   )
												return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]

					def    lowercase__      (	self					:     Optional[Any]   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     Optional[int]   ):
												lowerCamelCase_							  =					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

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

												lowerCamelCase_							  =					[
												    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_							  =					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    lowercase__      (	self					:     Optional[Any]   ):
												pass

					@require_torch
					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					"""hf-internal-testing/tiny-detr-mobilenetsv3"""

												lowerCamelCase_							  =					AutoModelForObjectDetection.from_pretrained(__UpperCamelCase   )
												lowerCamelCase_							  =					AutoFeatureExtractor.from_pretrained(__UpperCamelCase   )
												lowerCamelCase_							  =					ObjectDetectionPipeline(model=__UpperCamelCase   ,					feature_extractor=__UpperCamelCase   )

												lowerCamelCase_							  =					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""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
												        {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
												    ]   ,					)

												lowerCamelCase_							  =					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""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
												            {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
												        ],
												        [
												            {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
												            {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
												        ],
												    ]   ,					)

					@require_torch
					@slow
					def    lowercase__      (	self					:     int   ):
												lowerCamelCase_							  =					"""facebook/detr-resnet-50"""

												lowerCamelCase_							  =					AutoModelForObjectDetection.from_pretrained(__UpperCamelCase   )
												lowerCamelCase_							  =					AutoFeatureExtractor.from_pretrained(__UpperCamelCase   )
												lowerCamelCase_							  =					ObjectDetectionPipeline(model=__UpperCamelCase   ,					feature_extractor=__UpperCamelCase   )

												lowerCamelCase_							  =					object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""   )
												self.assertEqual(
												    nested_simplify(__UpperCamelCase   ,					decimals=4   )   ,					[
												        {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
												        {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
												        {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
												        {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
												        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
												    ]   ,					)

												lowerCamelCase_							  =					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""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
												            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
												            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
												            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
												            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
												        ],
												        [
												            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
												            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
												            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
												            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
												            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
												        ],
												    ]   ,					)

					@require_torch
					@slow
					def    lowercase__      (	self					:     Any   ):
												lowerCamelCase_							  =					"""facebook/detr-resnet-50"""

												lowerCamelCase_							  =					pipeline("""object-detection"""   ,					model=__UpperCamelCase   )

												lowerCamelCase_							  =					object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""   )
												self.assertEqual(
												    nested_simplify(__UpperCamelCase   ,					decimals=4   )   ,					[
												        {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
												        {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
												        {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
												        {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
												        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
												    ]   ,					)

												lowerCamelCase_							  =					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""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
												            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
												            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
												            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
												            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
												        ],
												        [
												            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
												            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
												            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
												            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
												            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
												        ],
												    ]   ,					)

					@require_torch
					@slow
					def    lowercase__      (	self					:     List[str]   ):
												lowerCamelCase_							  =					0.9985
												lowerCamelCase_							  =					"""facebook/detr-resnet-50"""

												lowerCamelCase_							  =					pipeline("""object-detection"""   ,					model=__UpperCamelCase   )

												lowerCamelCase_							  =					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""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
												        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
												    ]   ,					)



					@require_torch
					@require_pytesseract
					@slow
					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					"""Narsil/layoutlmv3-finetuned-funsd"""
												lowerCamelCase_							  =					0.9993

												lowerCamelCase_							  =					pipeline("""object-detection"""   ,					model=__UpperCamelCase   ,					threshold=__UpperCamelCase   )

												lowerCamelCase_							  =					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""": 2_9_4, """ymin""": 2_5_4, """xmax""": 3_4_3, """ymax""": 2_6_4}},
												        {"""score""": 0.9993, """label""": """I-ANSWER""", """box""": {"""xmin""": 2_9_4, """ymin""": 2_5_4, """xmax""": 3_4_3, """ymax""": 2_6_4}},
												    ]   ,					)







import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
		import torch

		from transformers.generation import DisjunctiveConstraint





@require_torch
class       __A(				unittest.TestCase       ):

					def    lowercase__      (	self					:     Optional[Any]   ):
												# For consistency across different places the DisjunctiveConstraint is called,
												# dc.token_ids is a list of integers. It is also initialized only by integers.

												lowerCamelCase_							  =					[[1, 2, 4], [1, 2, 3, 4]]
												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )
												self.assertTrue(isinstance(dc.token_ids   ,					__UpperCamelCase   )   )

												with self.assertRaises(__UpperCamelCase   ):
																			DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]   )   )

												with self.assertRaises(__UpperCamelCase   ):
																			DisjunctiveConstraint([torch.LongTensor([1, 2, 4]   ), torch.LongTensor([1, 2, 3, 4, 5]   )]   )

					def    lowercase__      (	self					:     List[Any]   ):
												# We can't have constraints that are complete subsets of another. This leads to a preverse
												# interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
												# It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
												# fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
												# will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
												lowerCamelCase_							  =					[[1, 2], [1, 2, 3, 4]]

												with self.assertRaises(__UpperCamelCase   ):
																			DisjunctiveConstraint(__UpperCamelCase   )  # fails here

					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					[[1, 2, 3], [1, 2, 4]]

												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(1   )
												lowerCamelCase_							  =					stepped is True and completed is False and reset is False
												self.assertTrue(__UpperCamelCase   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(2   )
												lowerCamelCase_							  =					stepped is True and completed is False and reset is False
												self.assertTrue(__UpperCamelCase   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1, 2]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(3   )
												lowerCamelCase_							  =					stepped is True and completed is True and reset is False
												self.assertTrue(__UpperCamelCase   )
												self.assertTrue(dc.completed   )  # Completed!
												self.assertTrue(dc.current_seq == [1, 2, 3]   )



					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(1   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(2   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1, 2]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(4   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1, 2, 4]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(5   )
												self.assertTrue(dc.completed   )  # Completed!
												self.assertTrue(dc.current_seq == [1, 2, 4, 5]   )

												dc.reset()

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(1   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.remaining() == 3   )
												self.assertTrue(dc.current_seq == [1]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(2   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.remaining() == 2   )
												self.assertTrue(dc.current_seq == [1, 2]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(5   )
												self.assertTrue(dc.completed   )  # Completed!
												self.assertTrue(dc.remaining() == 0   )
												self.assertTrue(dc.current_seq == [1, 2, 5]   )





