


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        __snake_case(  *A_							,					**A_    ):
        pass


@is_pipeline_test
@require_vision
@require_torch
class 		_SCREAMING_SNAKE_CASE     (						unittest.TestCase     ):
     __SCREAMING_SNAKE_CASE     					=    MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING


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

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


     def        __snake_case(  self							,					A_							,					A_    ):
       _UpperCAmelCase		:							Union[str, Any]							=     object_detector(examples[0]							,					threshold=0.0    )

       _UpperCAmelCase		:							Dict							=     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        __snake_case(  self    ):
       pass


     @require_torch
     def        __snake_case(  self    ):
       _UpperCAmelCase		:							str							=     pipeline(
           """zero-shot-object-detection"""							,					model="""hf-internal-testing/tiny-random-owlvit-object-detection"""    )

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

       self.assertEqual(
           nested_simplify(A_							,					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		:							List[str]							=     object_detector(
           [
               {
                   """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                   """candidate_labels""": ["""cat""", """remote""", """couch"""],
               }
           ]							,					threshold=0.6_4							,					)

       self.assertEqual(
           nested_simplify(A_							,					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        __snake_case(  self    ):
       _UpperCAmelCase		:							List[Any]							=     pipeline("""zero-shot-object-detection"""    )

       _UpperCAmelCase		:							Union[str, Any]							=     object_detector(
           """http://images.cocodataset.org/val2017/000000039769.jpg"""							,					candidate_labels=["""cat""", """remote""", """couch"""]							,					)
       self.assertEqual(
           nested_simplify(A_							,					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		:							str							=     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.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        __snake_case(  self    ):
       pass


     @require_torch
     @slow
     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Tuple							=     0.2
       _UpperCAmelCase		:							Union[str, Any]							=     pipeline("""zero-shot-object-detection"""    )

       _UpperCAmelCase		:							Optional[int]							=     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.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        __snake_case(  self    ):
       _UpperCAmelCase		:							Dict							=     2
       _UpperCAmelCase		:							Union[str, Any]							=     pipeline("""zero-shot-object-detection"""    )

       _UpperCAmelCase		:							Any							=     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.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}},
           ]							,					)









# A Bipartite Graph is a graph whose vertices can be divided into two independent sets,
# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.




def    a__    (		snake_case__					:							Dict     ):
  _UpperCAmelCase		:							str							=     [False] * len(snake_case__     )
  _UpperCAmelCase		:							str							=     [-1] * len(snake_case__     )

  def dfs(snake_case__					:							Dict						,   snake_case__					:							Optional[Any]     ):
    _UpperCAmelCase		:							str							=     True
    _UpperCAmelCase		:							Optional[Any]							=     c
    for u in graph[v]:
      if not visited[u]:
        dfs(snake_case__						,   1 - c     )

  for i in range(len(snake_case__     )     ):
    if not visited[i]:
      dfs(snake_case__						,   0     )

  for i in range(len(snake_case__     )     ):
    for j in graph[i]:
      if color[i] == color[j]:
        return False

  return True


# Adjacency list of graph
SCREAMING_SNAKE_CASE__					:     Optional[Any]       =     {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []}





print(check_bipartite_dfs(graph))





