




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






											@staticmethod
											def        __SCREAMING_SNAKE_CASE(     *_A	,    **_A    ):




																"""simple docstring"""



																pass
@is_pipeline_test
@require_vision
@require_torch
class     a__					(      unittest.TestCase   ):
						_a							:					List[Any]      =					MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A    ):




											"""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        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""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        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											pass






						@require_torch
						def        __SCREAMING_SNAKE_CASE(     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.72_35, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											        {"score": 0.72_18, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											        {"score": 0.71_84, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											        {"score": 0.67_48, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											        {"score": 0.66_56, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											        {"score": 0.66_14, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											        {"score": 0.64_56, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
											        {"score": 0.6_42, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
											        {"score": 0.64_19, "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.72_35, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											            {"score": 0.72_18, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											            {"score": 0.71_84, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
											            {"score": 0.67_48, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											            {"score": 0.66_56, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											            {"score": 0.66_14, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
											            {"score": 0.64_56, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
											            {"score": 0.6_42, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
											            {"score": 0.64_19, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
											        ]
											    ]	,    )






						@require_torch
						@slow
						def        __SCREAMING_SNAKE_CASE(     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.28_68, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											        {"score": 0.2_77, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											        {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											        {"score": 0.14_74, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
											        {"score": 0.12_08, "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.28_68, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											            {"score": 0.2_77, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											            {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											            {"score": 0.14_74, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
											            {"score": 0.12_08, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
											        ],
											        [
											            {"score": 0.28_68, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											            {"score": 0.2_77, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											            {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											            {"score": 0.14_74, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
											            {"score": 0.12_08, "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        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											pass






						@require_torch
						@slow
						def        __SCREAMING_SNAKE_CASE(     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.28_68, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											        {"score": 0.2_77, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											        {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
											    ]	,    )






						@require_torch
						@slow
						def        __SCREAMING_SNAKE_CASE(     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.28_68, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
											        {"score": 0.2_77, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
											    ]	,    )










from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
					from PIL import Image

					from ..image_utils import load_image

if is_tf_available():
					import tensorflow as tf

					from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
					from ..tf_utils import stable_softmax

if is_torch_available():
					from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

UpperCamelCase__					    =    logging.get_logger(__name__)
@add_end_docstrings(snake_case__   )
class     a__					(      snake_case__   ):






						def __init__(     self	,    *_A	,    **_A    ):




											"""simple docstring"""



											super().__init__(*_A	,    **_A    )
											requires_backends(self	,    "vision"    )
											self.check_model_type(
											    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
											    if self.framework == "tf"
											    else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A=None    ):




											"""simple docstring"""



											__lowerCAmelCase            = {}
											if top_k is not None:
																__lowerCAmelCase            = top_k
											return {}, {}, postprocess_params






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




											"""simple docstring"""



											return super().__call__(_A	,    **_A    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = load_image(_A    )
											__lowerCAmelCase            = self.image_processor(images=_A	,    return_tensors=self.framework    )
											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=5    ):




											"""simple docstring"""



											if top_k > self.model.config.num_labels:
																__lowerCAmelCase            = self.model.config.num_labels

											if self.framework == "pt":
																__lowerCAmelCase            = model_outputs.logits.softmax(-1    )[0]
																__lowerCAmelCase     ,							__lowerCAmelCase            = probs.topk(_A    )
											elif self.framework == "tf":
																__lowerCAmelCase            = stable_softmax(model_outputs.logits	,    axis=-1    )[0]
																__lowerCAmelCase            = tf.math.top_k(_A	,    k=_A    )
																__lowerCAmelCase     ,							__lowerCAmelCase            = topk.values.numpy(), topk.indices.numpy()
											else:
																raise ValueError(f"""Unsupported framework: {self.framework}"""    )

											__lowerCAmelCase            = scores.tolist()
											__lowerCAmelCase            = ids.tolist()
											return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_A	,    _A    )]




