





import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, 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       __A					:

						'''simple docstring'''







						@staticmethod
						def 	__lowerCamelCase       (					*__lowerCAmelCase ,						**__lowerCAmelCase							):



							'''simple docstring'''







							pass






@is_pipeline_test
@require_vision
@require_torch
class       __A					(					unittest.TestCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__              =	pipeline(
				    '''zero-shot-object-detection''' ,						model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

				lowerCamelCase__              =	[
				    {
				        '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
				        '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
				    }
				]
				return object_detector, examples





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__              =	object_detector(examples[0] ,						threshold=0.0							)

				lowerCamelCase__              =	len(_A							)
				self.assertGreater(_A ,						0							)
				self.assertEqual(
				    _A ,						[
				        {
				            '''score''': ANY(_A							),
				            '''label''': ANY(_A							),
				            '''box''': {'''xmin''': ANY(_A							), '''ymin''': ANY(_A							), '''xmax''': ANY(_A							), '''ymax''': ANY(_A							)},
				        }
				        for i in range(_A							)
				    ] ,						)





			@require_tf
			@unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			@require_torch
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	pipeline(
				    '''zero-shot-object-detection''' ,						model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

				lowerCamelCase__              =	object_detector(
				    '''./tests/fixtures/tests_samples/COCO/000000039769.png''' ,						candidate_labels=['''cat''', '''remote''', '''couch'''] ,						threshold=0.64 ,						)

				self.assertEqual(
				    nested_simplify(_A ,						decimals=4							) ,						[
				        {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
				        {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
				        {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
				        {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
				        {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
				        {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
				        {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
				        {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
				        {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
				    ] ,						)

				lowerCamelCase__              =	object_detector(
				    [
				        {
				            '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
				            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
				        }
				    ] ,						threshold=0.64 ,						)

				self.assertEqual(
				    nested_simplify(_A ,						decimals=4							) ,						[
				        [
				            {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
				            {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
				            {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
				            {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
				            {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
				            {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
				            {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
				            {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
				            {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
				        ]
				    ] ,						)





			@require_torch
			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	pipeline('''zero-shot-object-detection'''							)

				lowerCamelCase__              =	object_detector(
				    '''http://images.cocodataset.org/val2017/000000039769.jpg''' ,						candidate_labels=['''cat''', '''remote''', '''couch'''] ,						)
				self.assertEqual(
				    nested_simplify(_A ,						decimals=4							) ,						[
				        {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
				        {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
				        {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
				        {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
				        {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
				    ] ,						)

				lowerCamelCase__              =	object_detector(
				    [
				        {
				            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
				            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
				        },
				        {
				            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
				            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
				        },
				    ] ,						)
				self.assertEqual(
				    nested_simplify(_A ,						decimals=4							) ,						[
				        [
				            {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
				            {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
				            {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
				            {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
				            {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
				        ],
				        [
				            {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
				            {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
				            {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
				            {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
				            {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
				        ],
				    ] ,						)





			@require_tf
			@unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			@require_torch
			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	0.2
				lowerCamelCase__              =	pipeline('''zero-shot-object-detection'''							)

				lowerCamelCase__              =	object_detector(
				    '''http://images.cocodataset.org/val2017/000000039769.jpg''' ,						candidate_labels=['''cat''', '''remote''', '''couch'''] ,						threshold=_A ,						)
				self.assertEqual(
				    nested_simplify(_A ,						decimals=4							) ,						[
				        {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
				        {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
				        {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
				    ] ,						)





			@require_torch
			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	2
				lowerCamelCase__              =	pipeline('''zero-shot-object-detection'''							)

				lowerCamelCase__              =	object_detector(
				    '''http://images.cocodataset.org/val2017/000000039769.jpg''' ,						candidate_labels=['''cat''', '''remote''', '''couch'''] ,						top_k=_A ,						)
				self.assertEqual(
				    nested_simplify(_A ,						decimals=4							) ,						[
				        {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
				        {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
				    ] ,						)







# This is the module that test_patching.py uses to test patch_submodule()

import os  # noqa: this is just for tests
import os as renamed_os  # noqa: this is just for tests
from os import path  # noqa: this is just for tests
from os import path as renamed_path  # noqa: this is just for tests
from os.path import join  # noqa: this is just for tests
from os.path import join as renamed_join  # noqa: this is just for tests


_a						=   open  # noqa: we just need to have a builtin inside this module to test it properly



