
'''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  _UpperCAmelCase :

         """simple docstring"""

         @staticmethod
         def 							_lowerCAmelCase  (      *lowerCAmelCase_							,		**lowerCAmelCase_ ):

           '''simple docstring'''

           pass


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

     """simple docstring"""

     a_    	=					MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

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

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


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Dict      =							object_detector(examples[0]							,		threshold=0.0 )

       a_      :	Optional[Any]      =							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 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       pass


     @require_torch
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

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

       a_      :	Any      =							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.7235, """label""": """cat""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
               {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
               {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
               {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
               {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
               {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
               {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
               {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 2_74, """xmax""": 93, """ymax""": 2_97}},
               {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
           ]							,		)

       a_      :	Optional[int]      =							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.7235, """label""": """cat""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
                   {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
                   {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
                   {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
                   {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
                   {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
                   {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
                   {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 2_74, """xmax""": 93, """ymax""": 2_97}},
                   {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
               ]
           ]							,		)


     @require_torch
     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	str      =							pipeline("""zero-shot-object-detection""" )

       a_      :	List[str]      =							object_detector(
           """http://images.cocodataset.org/val2017/000000039769.jpg"""							,		candidate_labels=["""cat""", """remote""", """couch"""]							,		)
       self.assertEqual(
           nested_simplify(lowerCAmelCase_							,		decimals=4 )							,		[
               {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
               {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
               {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
               {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 3_35, """ymin""": 74, """xmax""": 3_71, """ymax""": 1_87}},
               {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_42, """ymax""": 4_76}},
           ]							,		)

       a_      :	Any      =							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.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
                   {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
                   {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
                   {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 3_35, """ymin""": 74, """xmax""": 3_71, """ymax""": 1_87}},
                   {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_42, """ymax""": 4_76}},
               ],
               [
                   {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
                   {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
                   {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
                   {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 3_35, """ymin""": 74, """xmax""": 3_71, """ymax""": 1_87}},
                   {"""score""": 0.1208, """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 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       pass


     @require_torch
     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	str      =							0.2
       a_      :	str      =							pipeline("""zero-shot-object-detection""" )

       a_      :	List[Any]      =							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.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
               {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
               {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
           ]							,		)






     @require_torch
     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Tuple      =							2
       a_      :	Optional[Any]      =							pipeline("""zero-shot-object-detection""" )

       a_      :	List[str]      =							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.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
               {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
           ]							,		)




'''simple docstring'''

from datasets.utils.patching import _PatchedModuleObj, patch_submodule

from . import _test_patching






def 	_snake_case						(      ):
  """simple docstring"""







  import os as original_os
  from os import path as original_path
  from os import rename as original_rename
  from os.path import dirname as original_dirname
  from os.path import join as original_join

  assert _test_patching.os is original_os
  assert _test_patching.path is original_path
  assert _test_patching.join is original_join

  assert _test_patching.renamed_os is original_os
  assert _test_patching.renamed_path is original_path
  assert _test_patching.renamed_join is original_join

  a_      :	List[str]      =							"""__test_patch_submodule_mock__"""
  with patch_submodule(_test_patching     ,		"""os.path.join"""     ,		A_   ):
    # Every way to access os.path.join must be patched, and the rest must stay untouched

    # check os.path.join
    assert isinstance(_test_patching.os     ,		_PatchedModuleObj   )
    assert isinstance(_test_patching.os.path     ,		_PatchedModuleObj   )
    assert _test_patching.os.path.join is mock

    # check path.join
    assert isinstance(_test_patching.path     ,		_PatchedModuleObj   )
    assert _test_patching.path.join is mock

    # check join
    assert _test_patching.join is mock

    # check that the other attributes are untouched
    assert _test_patching.os.rename is original_rename
    assert _test_patching.path.dirname is original_dirname
    assert _test_patching.os.path.dirname is original_dirname

    # Even renamed modules or objects must be patched

    # check renamed_os.path.join
    assert isinstance(_test_patching.renamed_os     ,		_PatchedModuleObj   )
    assert isinstance(_test_patching.renamed_os.path     ,		_PatchedModuleObj   )
    assert _test_patching.renamed_os.path.join is mock

    # check renamed_path.join
    assert isinstance(_test_patching.renamed_path     ,		_PatchedModuleObj   )
    assert _test_patching.renamed_path.join is mock

    # check renamed_join
    assert _test_patching.renamed_join is mock

    # check that the other attributes are untouched
    assert _test_patching.renamed_os.rename is original_rename
    assert _test_patching.renamed_path.dirname is original_dirname
    assert _test_patching.renamed_os.path.dirname is original_dirname

  # check that everthing is back to normal when the patch is over

  assert _test_patching.os is original_os
  assert _test_patching.path is original_path
  assert _test_patching.join is original_join

  assert _test_patching.renamed_os is original_os
  assert _test_patching.renamed_path is original_path
  assert _test_patching.renamed_join is original_join






def 	_snake_case						(      ):
  """simple docstring"""







  assert _test_patching.open is open

  a_      :	Tuple      =							"""__test_patch_submodule_builtin_mock__"""
  # _test_patching has "open" in its globals
  assert _test_patching.open is open
  with patch_submodule(_test_patching     ,		"""open"""     ,		A_   ):
    assert _test_patching.open is mock

  # check that everthing is back to normal when the patch is over

  assert _test_patching.open is open






def 	_snake_case						(      ):
  """simple docstring"""







  a_      :	Any      =							"""__test_patch_submodule_missing_mock__"""
  with patch_submodule(_test_patching     ,		"""pandas.read_csv"""     ,		A_   ):
    pass






def 	_snake_case						(      ):
  """simple docstring"""







  a_      :	int      =							"""__test_patch_submodule_missing_builtin_mock__"""
  # _test_patching doesn't have "len" in its globals
  assert getattr(_test_patching     ,		"""len"""     ,		A_   ) is None
  with patch_submodule(_test_patching     ,		"""len"""     ,		A_   ):
    assert _test_patching.len is mock
  assert _test_patching.len is len






def 	_snake_case						(      ):
  """simple docstring"""







  a_      :	List[str]      =							"""__test_patch_submodule_start_and_stop_mock__"""
  a_      :	int      =							patch_submodule(_test_patching     ,		"""open"""     ,		A_   )
  assert _test_patching.open is open
  patch.start()
  assert _test_patching.open is mock
  patch.stop()
  assert _test_patching.open is open






def 	_snake_case						(      ):
  """simple docstring"""







  from os import rename as original_rename
  from os.path import dirname as original_dirname
  from os.path import join as original_join

  a_      :	Any      =							"""__test_patch_submodule_successive_join__"""
  a_      :	Optional[Any]      =							"""__test_patch_submodule_successive_dirname__"""
  a_      :	Optional[int]      =							"""__test_patch_submodule_successive_rename__"""
  assert _test_patching.os.path.join is original_join
  assert _test_patching.os.path.dirname is original_dirname
  assert _test_patching.os.rename is original_rename

  with patch_submodule(_test_patching     ,		"""os.path.join"""     ,		A_   ):
    with patch_submodule(_test_patching     ,		"""os.rename"""     ,		A_   ):
      with patch_submodule(_test_patching     ,		"""os.path.dirname"""     ,		A_   ):
        assert _test_patching.os.path.join is mock_join
        assert _test_patching.os.path.dirname is mock_dirname
        assert _test_patching.os.rename is mock_rename

    # try another order
  with patch_submodule(_test_patching     ,		"""os.rename"""     ,		A_   ):
    with patch_submodule(_test_patching     ,		"""os.path.join"""     ,		A_   ):
      with patch_submodule(_test_patching     ,		"""os.path.dirname"""     ,		A_   ):
        assert _test_patching.os.path.join is mock_join
        assert _test_patching.os.path.dirname is mock_dirname
        assert _test_patching.os.rename is mock_rename

  assert _test_patching.os.path.join is original_join
  assert _test_patching.os.path.dirname is original_dirname
  assert _test_patching.os.rename is original_rename






def 	_snake_case						(      ):
  """simple docstring"""







  a_      :	Optional[int]      =							"""__test_patch_submodule_doesnt_exist_mock__"""
  with patch_submodule(_test_patching     ,		"""__module_that_doesn_exist__.__attribute_that_doesn_exist__"""     ,		A_   ):
    pass
  with patch_submodule(_test_patching     ,		"""os.__attribute_that_doesn_exist__"""     ,		A_   ):
    pass


