






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  __lowercase (		*SCREAMING_SNAKE_CASE :       List[Any]							,       **SCREAMING_SNAKE_CASE :       Dict						):


       '''simple docstring'''






       pass





@is_pipeline_test
@require_vision
@require_torch
class       __a  (   unittest.TestCase	):
 _lowerCAmelCase	:	Any				=			MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







 def  __lowercase (		self :       List[str]							,       SCREAMING_SNAKE_CASE :       int							,       SCREAMING_SNAKE_CASE :       Union[str, Any]							,       SCREAMING_SNAKE_CASE :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       List[Any]	    =  pipeline(
          "zero-shot-object-detection"							,       model="hf-internal-testing/tiny-random-owlvit-object-detection"						)

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







 def  __lowercase (		self :       List[str]							,       SCREAMING_SNAKE_CASE :       int							,       SCREAMING_SNAKE_CASE :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  object_detector(examples[0]							,       threshold=0.0						)

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







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


      '''simple docstring'''






      pass







 @require_torch
 def  __lowercase (		self :       Union[str, Any]						):


      '''simple docstring'''






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

      UpperCamelCase__      :       int	    =  object_detector(
          "./tests/fixtures/tests_samples/COCO/000000039769.png"							,       candidate_labels=["cat", "remote", "couch"]							,       threshold=0.6_4							,       )

      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE							,       decimals=4						)							,       [
              {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
              {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
              {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
              {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
              {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
              {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
              {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
              {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}},
              {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
          ]							,       )

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

      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE							,       decimals=4						)							,       [
              [
                  {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                  {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                  {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                  {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                  {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                  {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                  {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
                  {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}},
                  {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
              ]
          ]							,       )







 @require_torch
 @slow
 def  __lowercase (		self :       str						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  pipeline("zero-shot-object-detection"						)

      UpperCamelCase__      :       Any	    =  object_detector(
          "http://images.cocodataset.org/val2017/000000039769.jpg"							,       candidate_labels=["cat", "remote", "couch"]							,       )
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE							,       decimals=4						)							,       [
              {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
              {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
              {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
              {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
          ]							,       )

      UpperCamelCase__      :       Tuple	    =  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(SCREAMING_SNAKE_CASE							,       decimals=4						)							,       [
              [
                  {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                  {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                  {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
              ],
              [
                  {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                  {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                  {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
              ],
          ]							,       )







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


      '''simple docstring'''






      pass







 @require_torch
 @slow
 def  __lowercase (		self :       Tuple						):


      '''simple docstring'''






      UpperCamelCase__      :       List[str]	    =  0.2
      UpperCamelCase__      :       Optional[Any]	    =  pipeline("zero-shot-object-detection"						)

      UpperCamelCase__      :       Optional[int]	    =  object_detector(
          "http://images.cocodataset.org/val2017/000000039769.jpg"							,       candidate_labels=["cat", "remote", "couch"]							,       threshold=SCREAMING_SNAKE_CASE							,       )
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE							,       decimals=4						)							,       [
              {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
              {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
          ]							,       )







 @require_torch
 @slow
 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       Dict	    =  2
      UpperCamelCase__      :       Union[str, Any]	    =  pipeline("zero-shot-object-detection"						)

      UpperCamelCase__      :       int	    =  object_detector(
          "http://images.cocodataset.org/val2017/000000039769.jpg"							,       candidate_labels=["cat", "remote", "couch"]							,       top_k=SCREAMING_SNAKE_CASE							,       )
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE							,       decimals=4						)							,       [
              {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
          ]							,       )







from __future__ import annotations







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase ,   __lowerCAmelCase = None ,   __lowerCAmelCase = None      )			->     None:
     if start is None:
          UpperCamelCase__      :       Union[str, Any]	    =  0

     if end is None:
          UpperCamelCase__      :       List[Any]	    =  len(__lowerCAmelCase      ) - 1

     if start >= end:
          return

     UpperCamelCase__      :       Union[str, Any]	    =  (start + end) // 2

     slowsort(__lowerCAmelCase ,   __lowerCAmelCase ,   __lowerCAmelCase      )
     slowsort(__lowerCAmelCase ,   mid + 1 ,   __lowerCAmelCase      )

     if sequence[end] < sequence[mid]:
          UpperCamelCase__    ,					UpperCamelCase__      :       Optional[int]	    =  sequence[mid], sequence[end]

     slowsort(__lowerCAmelCase ,   __lowerCAmelCase ,   end - 1      )


if __name__ == "__main__":
 from doctest import testmod

 testmod()