


'''simple docstring'''







import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
   from PIL import Image
else:







   class A__       :






         @staticmethod
         def __UpperCAmelCase       ( *SCREAMING_SNAKE_CASE			:Optional[int]     ,						**SCREAMING_SNAKE_CASE			:int	)       ->					Any:




                '''simple docstring'''
                pass







@is_pipeline_test
@require_vision
@require_timm
@require_torch
class A__       (						unittest.TestCase      ):
      __UpperCamelCase       :  Optional[int]	       =							MODEL_FOR_OBJECT_DETECTION_MAPPING






      def __UpperCAmelCase       ( self			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:Optional[int]     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:Optional[Any]	)       ->					Optional[Any]:




             '''simple docstring'''
             _a    : Dict     					=ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE     ,						image_processor=SCREAMING_SNAKE_CASE	)
             return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]






      def __UpperCAmelCase       ( self			:Dict     ,						SCREAMING_SNAKE_CASE			:List[Any]     ,						SCREAMING_SNAKE_CASE			:Any	)       ->					Any:




             '''simple docstring'''
             _a    : int     					=object_detector("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     ,						threshold=0.0	)

             self.assertGreater(len(SCREAMING_SNAKE_CASE	)     ,						0	)
             for detected_object in outputs:
                    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	)},
                        }     ,						)

             import datasets

             _a    : List[Any]     					=datasets.load_dataset("""hf-internal-testing/fixtures_image_utils"""     ,						"""image"""     ,						split="""test"""	)

             _a    : Optional[int]     					=[
                 Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	),
                 """http://images.cocodataset.org/val2017/000000039769.jpg""",
                 # RGBA
                 dataset[0]["""file"""],
                 # LA
                 dataset[1]["""file"""],
                 # L
                 dataset[2]["""file"""],
             ]
             _a    : Optional[int]     					=object_detector(SCREAMING_SNAKE_CASE     ,						threshold=0.0	)

             self.assertEqual(len(SCREAMING_SNAKE_CASE	)     ,						len(SCREAMING_SNAKE_CASE	)	)
             for outputs in batch_outputs:
                    self.assertGreater(len(SCREAMING_SNAKE_CASE	)     ,						0	)
                    for detected_object in outputs:
                           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	)},
                               }     ,						)






      @require_tf
      @unittest.skip("""Object detection not implemented in TF"""	)
      def __UpperCAmelCase       ( self			:Tuple	)       ->					Any:




             '''simple docstring'''
             pass






      @require_torch
      def __UpperCAmelCase       ( self			:Optional[Any]	)       ->					Dict:




             '''simple docstring'''
             _a    : Optional[int]     					="""hf-internal-testing/tiny-detr-mobilenetsv3"""

             _a    : List[str]     					=AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE	)
             _a    : int     					=AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE	)
             _a    : List[Any]     					=ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE     ,						feature_extractor=SCREAMING_SNAKE_CASE	)

             _a    : Optional[Any]     					=object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""     ,						threshold=0.0	)

             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                     {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                 ]     ,						)

             _a    : Optional[int]     					=object_detector(
                 [
                     """http://images.cocodataset.org/val2017/000000039769.jpg""",
                     """http://images.cocodataset.org/val2017/000000039769.jpg""",
                 ]     ,						threshold=0.0     ,						)

             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     [
                         {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                         {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                     ],
                     [
                         {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                         {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                     ],
                 ]     ,						)






      @require_torch
      @slow
      def __UpperCAmelCase       ( self			:Any	)       ->					int:




             '''simple docstring'''
             _a    : int     					="""facebook/detr-resnet-50"""

             _a    : Any     					=AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE	)
             _a    : Dict     					=AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE	)
             _a    : Dict     					=ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE     ,						feature_extractor=SCREAMING_SNAKE_CASE	)

             _a    : Union[str, Any]     					=object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""	)
             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                     {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                     {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                     {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                     {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                 ]     ,						)

             _a    : str     					=object_detector(
                 [
                     """http://images.cocodataset.org/val2017/000000039769.jpg""",
                     """http://images.cocodataset.org/val2017/000000039769.jpg""",
                 ]	)
             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     [
                         {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                         {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                         {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                         {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                         {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                     ],
                     [
                         {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                         {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                         {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                         {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                         {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                     ],
                 ]     ,						)






      @require_torch
      @slow
      def __UpperCAmelCase       ( self			:str	)       ->					List[Any]:




             '''simple docstring'''
             _a    : Dict     					="""facebook/detr-resnet-50"""

             _a    : List[str]     					=pipeline("""object-detection"""     ,						model=SCREAMING_SNAKE_CASE	)

             _a    : Any     					=object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""	)
             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                     {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                     {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                     {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                     {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                 ]     ,						)

             _a    : List[Any]     					=object_detector(
                 [
                     """http://images.cocodataset.org/val2017/000000039769.jpg""",
                     """http://images.cocodataset.org/val2017/000000039769.jpg""",
                 ]	)
             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     [
                         {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                         {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                         {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                         {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                         {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                     ],
                     [
                         {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                         {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                         {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                         {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                         {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                     ],
                 ]     ,						)






      @require_torch
      @slow
      def __UpperCAmelCase       ( self			:List[Any]	)       ->					Tuple:




             '''simple docstring'''
             _a    : List[str]     					=0.9_985
             _a    : int     					="""facebook/detr-resnet-50"""

             _a    : Optional[Any]     					=pipeline("""object-detection"""     ,						model=SCREAMING_SNAKE_CASE	)

             _a    : List[Any]     					=object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""     ,						threshold=SCREAMING_SNAKE_CASE	)
             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                     {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                 ]     ,						)






      @require_torch
      @require_pytesseract
      @slow
      def __UpperCAmelCase       ( self			:Dict	)       ->					Optional[Any]:




             '''simple docstring'''
             _a    : str     					="""Narsil/layoutlmv3-finetuned-funsd"""
             _a    : str     					=0.9_993

             _a    : List[str]     					=pipeline("""object-detection"""     ,						model=SCREAMING_SNAKE_CASE     ,						threshold=SCREAMING_SNAKE_CASE	)

             _a    : Optional[Any]     					=object_detector(
                 """https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"""	)
             self.assertEqual(
                 nested_simplify(SCREAMING_SNAKE_CASE     ,						decimals=4	)     ,						[
                     {"""score""": 0.9_993, """label""": """I-ANSWER""", """box""": {"""xmin""": 2_9_4, """ymin""": 2_5_4, """xmax""": 3_4_3, """ymax""": 2_6_4}},
                     {"""score""": 0.9_993, """label""": """I-ANSWER""", """box""": {"""xmin""": 2_9_4, """ymin""": 2_5_4, """xmax""": 3_4_3, """ymax""": 2_6_4}},
                 ]     ,						)






'''simple docstring'''







# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works
from ...utils import deprecate
from ..controlnet.multicontrolnet import MultiControlNetModel  # noqa: F401
from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline  # noqa: F401


deprecate(
    '''stable diffusion controlnet''',
    '''0.22.0''',
    '''Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.''',
    standard_warn=False,
    stacklevel=3,
)


