



"""simple docstring"""
from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       import torch

       from transformers.modeling_outputs import BaseModelOutput

       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

UpperCAmelCase						:		Optional[Any]			= logging.get_logger(__name__)


@add_end_docstrings(__UpperCAmelCase   )
class    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase   ):

    def __init__(				self			:				Optional[int] ,  **lowerCAmelCase_			:				List[Any]):







      """simple docstring"""







      super().__init__(**lowerCAmelCase_)

      if self.framework == "tf":
        raise ValueError(F'''The {self.__class__} is only available in PyTorch.''')

      requires_backends(self ,  """vision""")
      self.check_model_type(lowerCAmelCase_)

    def __call__(				self			:				Dict ,  lowerCAmelCase_			:				Union[str, "Image.Image", List[Dict[str, Any]]] ,  lowerCAmelCase_			:				Union[str, List[str]] = None ,  **lowerCAmelCase_			:				Dict ,  ):







      """simple docstring"""







      if "text_queries" in kwargs:
        lowercase_								= kwargs.pop("""text_queries""")

      if isinstance(lowerCAmelCase_ ,  (str, Image.Image)):
        lowercase_								= {"""image""": image, """candidate_labels""": candidate_labels}
      else:
        lowercase_								= image
      lowercase_								= super().__call__(lowerCAmelCase_ ,  **lowerCAmelCase_)
      return results

    def 					_UpperCAmelCase	(				self			:				Optional[Any] ,  **lowerCAmelCase_			:				Optional[Any]):







      """simple docstring"""







      lowercase_								= {}
      if "threshold" in kwargs:
        lowercase_								= kwargs["""threshold"""]
      if "top_k" in kwargs:
        lowercase_								= kwargs["""top_k"""]
      return {}, {}, postprocess_params

    def 					_UpperCAmelCase	(				self			:				int ,  lowerCAmelCase_			:				List[Any]):







      """simple docstring"""







      lowercase_								= load_image(inputs["""image"""])
      lowercase_								= inputs["""candidate_labels"""]
      if isinstance(lowerCAmelCase_ ,  lowerCAmelCase_):
        lowercase_								= candidate_labels.split(""",""")

      lowercase_								= torch.tensor([[image.height, image.width]] ,  dtype=torch.intaa)
      for i, candidate_label in enumerate(lowerCAmelCase_):
        lowercase_								= self.tokenizer(lowerCAmelCase_ ,  return_tensors=self.framework)
        lowercase_								= self.image_processor(lowerCAmelCase_ ,  return_tensors=self.framework)
        yield {
            "is_last": i == len(lowerCAmelCase_) - 1,
            "target_size": target_size,
            "candidate_label": candidate_label,
            **text_inputs,
            **image_features,
        }

    def 					_UpperCAmelCase	(				self			:				List[Any] ,  lowerCAmelCase_			:				List[Any]):







      """simple docstring"""







      lowercase_								= model_inputs.pop("""target_size""")
      lowercase_								= model_inputs.pop("""candidate_label""")
      lowercase_								= model_inputs.pop("""is_last""")

      lowercase_								= self.model(**lowerCAmelCase_)

      lowercase_								= {"""target_size""": target_size, """candidate_label""": candidate_label, """is_last""": is_last, **outputs}
      return model_outputs

    def 					_UpperCAmelCase	(				self			:				int ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				Tuple=0.1 ,  lowerCAmelCase_			:				List[str]=None):







      """simple docstring"""







      lowercase_								= []
      for model_output in model_outputs:
        lowercase_								= model_output["""candidate_label"""]
        lowercase_								= BaseModelOutput(lowerCAmelCase_)
        lowercase_								= self.image_processor.post_process_object_detection(
            outputs=lowerCAmelCase_ ,  threshold=lowerCAmelCase_ ,  target_sizes=model_output["""target_size"""])[0]

        for index in outputs["scores"].nonzero():
          lowercase_								= outputs["""scores"""][index].item()
          lowercase_								= self._get_bounding_box(outputs["""boxes"""][index][0])

          lowercase_								= {"""score""": score, """label""": label, """box""": box}
          results.append(lowerCAmelCase_)

      lowercase_								= sorted(lowerCAmelCase_ ,  key=lambda lowerCAmelCase_: x["score"] ,  reverse=lowerCAmelCase_)
      if top_k:
        lowercase_								= results[:top_k]

      return results




    def 					_UpperCAmelCase	(				self			:				Union[str, Any] ,  lowerCAmelCase_			:				"torch.Tensor"):







      """simple docstring"""







      if self.framework != "pt":
        raise ValueError("""The ZeroShotObjectDetectionPipeline is only available in PyTorch.""")
      lowercase_					,    lowercase_					,    lowercase_					,    lowercase_								= box.int().tolist()
      lowercase_								= {
          """xmin""": xmin,
          """ymin""": ymin,
          """xmax""": xmax,
          """ymax""": ymax,
      }
      return bbox






"""simple docstring"""
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    SCREAMING_SNAKE_CASE__ :

           @staticmethod
           def 					_UpperCAmelCase	(				*lowerCAmelCase_			:				Tuple ,  **lowerCAmelCase_			:				Tuple):







             """simple docstring"""







             pass


@is_pipeline_test
@require_vision
@require_torch
class    SCREAMING_SNAKE_CASE__ (							unittest.TestCase   ):
    lowercase__      =   MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

    def 					_UpperCAmelCase	(				self			:				Optional[Any] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				List[Any]):







      """simple docstring"""







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

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

    def 					_UpperCAmelCase	(				self			:				Optional[Any] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				Optional[Any]):







      """simple docstring"""







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

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

    @require_tf
    @unittest.skip("""Zero Shot Object Detection not implemented in TF""")
    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      pass

    @require_torch
    def 					_UpperCAmelCase	(				self			:				List[Any]):







      """simple docstring"""







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

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

      self.assertEqual(
          nested_simplify(lowerCAmelCase_ ,  decimals=4) ,  [
              {"""score""": 0.7_235, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
              {"""score""": 0.7_218, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
              {"""score""": 0.7_184, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
              {"""score""": 0.6_748, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
              {"""score""": 0.6_656, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
              {"""score""": 0.6_614, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
              {"""score""": 0.6_456, """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.6_419, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
          ] ,  )

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

      self.assertEqual(
          nested_simplify(lowerCAmelCase_ ,  decimals=4) ,  [
              [
                  {"""score""": 0.7_235, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                  {"""score""": 0.7_218, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                  {"""score""": 0.7_184, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                  {"""score""": 0.6_748, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                  {"""score""": 0.6_656, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                  {"""score""": 0.6_614, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                  {"""score""": 0.6_456, """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.6_419, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
              ]
          ] ,  )

    @require_torch
    @slow
    def 					_UpperCAmelCase	(				self			:				Dict):







      """simple docstring"""







      lowercase_								= pipeline("""zero-shot-object-detection""")

      lowercase_								= object_detector(
          """http://images.cocodataset.org/val2017/000000039769.jpg""" ,  candidate_labels=["""cat""", """remote""", """couch"""] ,  )
      self.assertEqual(
          nested_simplify(lowerCAmelCase_ ,  decimals=4) ,  [
              {"""score""": 0.2_868, """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.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
              {"""score""": 0.1_474, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
              {"""score""": 0.1_208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
          ] ,  )

      lowercase_								= 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(lowerCAmelCase_ ,  decimals=4) ,  [
              [
                  {"""score""": 0.2_868, """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.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
                  {"""score""": 0.1_474, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
                  {"""score""": 0.1_208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
              ],
              [
                  {"""score""": 0.2_868, """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.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
                  {"""score""": 0.1_474, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
                  {"""score""": 0.1_208, """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 					_UpperCAmelCase	(				self			:				List[Any]):







      """simple docstring"""







      pass

    @require_torch
    @slow
    def 					_UpperCAmelCase	(				self			:				List[str]):







      """simple docstring"""







      lowercase_								= 0.2
      lowercase_								= pipeline("""zero-shot-object-detection""")

      lowercase_								= object_detector(
          """http://images.cocodataset.org/val2017/000000039769.jpg""" ,  candidate_labels=["""cat""", """remote""", """couch"""] ,  threshold=lowerCAmelCase_ ,  )
      self.assertEqual(
          nested_simplify(lowerCAmelCase_ ,  decimals=4) ,  [
              {"""score""": 0.2_868, """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.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
          ] ,  )




    @require_torch
    @slow
    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      lowercase_								= 2
      lowercase_								= pipeline("""zero-shot-object-detection""")

      lowercase_								= object_detector(
          """http://images.cocodataset.org/val2017/000000039769.jpg""" ,  candidate_labels=["""cat""", """remote""", """couch"""] ,  top_k=lowerCAmelCase_ ,  )
      self.assertEqual(
          nested_simplify(lowerCAmelCase_ ,  decimals=4) ,  [
              {"""score""": 0.2_868, """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}},
          ] ,  )

