

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


									"""simple docstring"""





									@staticmethod
									def        A__  ( *_A					,						**_A							):
														pass





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


							"""simple docstring"""





							__UpperCAmelCase       =MODEL_FOR_OBJECT_DETECTION_MAPPING




							def        A__  ( self					,						_A					,						_A					,						_A							):
												__lowerCAmelCase					=							ObjectDetectionPipeline(model=_A					,						image_processor=_A							)
												return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]




							def        A__  ( self					,						_A					,						_A							):
												__lowerCAmelCase					=							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					=							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(_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					=							'hf-internal-testing/tiny-detr-mobilenetsv3'

												__lowerCAmelCase					=							AutoModelForObjectDetection.from_pretrained(_A							)
												__lowerCAmelCase					=							AutoFeatureExtractor.from_pretrained(_A							)
												__lowerCAmelCase					=							ObjectDetectionPipeline(model=_A					,						feature_extractor=_A							)

												__lowerCAmelCase					=							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': 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(_A					,						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        A__  ( self							):
												__lowerCAmelCase					=							'facebook/detr-resnet-50'

												__lowerCAmelCase					=							AutoModelForObjectDetection.from_pretrained(_A							)
												__lowerCAmelCase					=							AutoFeatureExtractor.from_pretrained(_A							)
												__lowerCAmelCase					=							ObjectDetectionPipeline(model=_A					,						feature_extractor=_A							)

												__lowerCAmelCase					=							object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'							)
												self.assertEqual(
												    nested_simplify(_A					,						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(_A					,						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        A__  ( self							):
												__lowerCAmelCase					=							'facebook/detr-resnet-50'

												__lowerCAmelCase					=							pipeline('object-detection'					,						model=_A							)

												__lowerCAmelCase					=							object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'							)
												self.assertEqual(
												    nested_simplify(_A					,						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(_A					,						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        A__  ( self							):
												__lowerCAmelCase					=							0.9985
												__lowerCAmelCase					=							'facebook/detr-resnet-50'

												__lowerCAmelCase					=							pipeline('object-detection'					,						model=_A							)

												__lowerCAmelCase					=							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': 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        A__  ( self							):
												__lowerCAmelCase					=							'Narsil/layoutlmv3-finetuned-funsd'
												__lowerCAmelCase					=							0.9993

												__lowerCAmelCase					=							pipeline('object-detection'					,						model=_A					,						threshold=_A							)

												__lowerCAmelCase					=							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': 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

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import ViTImageProcessor





class     snake_case_			(				unittest.TestCase							):


							"""simple docstring"""





							def __init__( self					,						_A					,						_A=1_3					,						_A=3					,						_A=2_2_4					,						_A=3_0					,						_A=4_0_0					,						_A=True					,						_A=None					,						_A=True					,						_A=[0.5, 0.5, 0.5]					,						_A=[0.5, 0.5, 0.5]					,						):
												__lowerCAmelCase					=							size if size is not None else {'height': 1_8, 'width': 1_8}
												__lowerCAmelCase					=							parent
												__lowerCAmelCase					=							batch_size
												__lowerCAmelCase					=							num_channels
												__lowerCAmelCase					=							image_size
												__lowerCAmelCase					=							min_resolution
												__lowerCAmelCase					=							max_resolution
												__lowerCAmelCase					=							do_resize
												__lowerCAmelCase					=							size
												__lowerCAmelCase					=							do_normalize
												__lowerCAmelCase					=							image_mean
												__lowerCAmelCase					=							image_std




							def        A__  ( self							):
												return {
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_normalize": self.do_normalize,
												    "do_resize": self.do_resize,
												    "size": self.size,
												}





@require_torch
@require_vision
class     snake_case_			(				_a			, unittest.TestCase							):


							"""simple docstring"""





							__UpperCAmelCase       =ViTImageProcessor if is_vision_available() else None




							def        A__  ( self							):
												__lowerCAmelCase					=							EfficientFormerImageProcessorTester(self							)




							@property
							def        A__  ( self							):
												return self.image_proc_tester.prepare_image_processor_dict()




							def        A__  ( self							):
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												self.assertTrue(hasattr(_A					,						'image_mean'							)							)
												self.assertTrue(hasattr(_A					,						'image_std'							)							)
												self.assertTrue(hasattr(_A					,						'do_normalize'							)							)
												self.assertTrue(hasattr(_A					,						'do_resize'							)							)
												self.assertTrue(hasattr(_A					,						'size'							)							)




							def        A__  ( self							):
												pass




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random PIL images
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						Image.Image							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random numpy tensors
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A					,						numpify=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						np.ndarray							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random PyTorch tensors
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A					,						torchify=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						torch.Tensor							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)






