


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(   *lowerCamelCase_      :     List[str]      ,							**lowerCamelCase_      :     List[str]    ):
                   pass


@is_pipeline_test
@require_vision
@require_timm
@require_torch
class   a__	(						unittest.TestCase    ):
       lowerCamelCase__:     Tuple							    =     MODEL_FOR_OBJECT_DETECTION_MAPPING

       def 						UpperCAmelCase(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Any      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     str    ):
            a_    :     List[str]										=    ObjectDetectionPipeline(model=__UpperCamelCase      ,							image_processor=__UpperCamelCase    )
            return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]

       def 						UpperCAmelCase(   self      :     Optional[Any]      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     Optional[int]    ):
            a_    :     Optional[Any]										=    object_detector("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      ,							threshold=0.0    )

            self.assertGreater(len(__UpperCamelCase    )      ,							0    )
            for detected_object in outputs:
                 self.assertEqual(
                     __UpperCamelCase      ,							{
                         """score""": ANY(__UpperCamelCase    ),
                         """label""": ANY(__UpperCamelCase    ),
                         """box""": {"""xmin""": ANY(__UpperCamelCase    ), """ymin""": ANY(__UpperCamelCase    ), """xmax""": ANY(__UpperCamelCase    ), """ymax""": ANY(__UpperCamelCase    )},
                     }      ,							)

            import datasets

            a_    :     Any										=    datasets.load_dataset("""hf-internal-testing/fixtures_image_utils"""      ,							"""image"""      ,							split="""test"""    )

            a_    :     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_    :     Dict										=    object_detector(__UpperCamelCase      ,							threshold=0.0    )

            self.assertEqual(len(__UpperCamelCase    )      ,							len(__UpperCamelCase    )    )
            for outputs in batch_outputs:
                 self.assertGreater(len(__UpperCamelCase    )      ,							0    )
                 for detected_object in outputs:
                      self.assertEqual(
                          __UpperCamelCase      ,							{
                              """score""": ANY(__UpperCamelCase    ),
                              """label""": ANY(__UpperCamelCase    ),
                              """box""": {"""xmin""": ANY(__UpperCamelCase    ), """ymin""": ANY(__UpperCamelCase    ), """xmax""": ANY(__UpperCamelCase    ), """ymax""": ANY(__UpperCamelCase    )},
                          }      ,							)

       @require_tf
       @unittest.skip("""Object detection not implemented in TF"""    )
       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            pass

       @require_torch
       def 						UpperCAmelCase(   self      :     Tuple    ):
            a_    :     Dict										=    """hf-internal-testing/tiny-detr-mobilenetsv3"""

            a_    :     Optional[int]										=    AutoModelForObjectDetection.from_pretrained(__UpperCamelCase    )
            a_    :     List[str]										=    AutoFeatureExtractor.from_pretrained(__UpperCamelCase    )
            a_    :     Any										=    ObjectDetectionPipeline(model=__UpperCamelCase      ,							feature_extractor=__UpperCamelCase    )

            a_    :     Tuple										=    object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""      ,							threshold=0.0    )

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

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

            self.assertEqual(
                nested_simplify(__UpperCamelCase      ,							decimals=4    )      ,							[
                    [
                        {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                        {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                    ],
                    [
                        {"""score""": 0.3_3_7_6, """label""": """LABEL_0""", """box""": {"""xmin""": 1_5_9, """ymin""": 1_2_0, """xmax""": 4_8_0, """ymax""": 3_5_9}},
                        {"""score""": 0.3_3_7_6, """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      :     int    ):
            a_    :     Union[str, Any]										=    """facebook/detr-resnet-50"""

            a_    :     Union[str, Any]										=    AutoModelForObjectDetection.from_pretrained(__UpperCamelCase    )
            a_    :     Optional[int]										=    AutoFeatureExtractor.from_pretrained(__UpperCamelCase    )
            a_    :     Dict										=    ObjectDetectionPipeline(model=__UpperCamelCase      ,							feature_extractor=__UpperCamelCase    )

            a_    :     int										=    object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""    )
            self.assertEqual(
                nested_simplify(__UpperCamelCase      ,							decimals=4    )      ,							[
                    {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                    {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                    {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                    {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                    {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                ]      ,							)

            a_    :     Optional[int]										=    object_detector(
                [
                    """http://images.cocodataset.org/val2017/000000039769.jpg""",
                    """http://images.cocodataset.org/val2017/000000039769.jpg""",
                ]    )
            self.assertEqual(
                nested_simplify(__UpperCamelCase      ,							decimals=4    )      ,							[
                    [
                        {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                        {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                        {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                        {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                        {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                    ],
                    [
                        {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                        {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                        {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                        {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                        {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                    ],
                ]      ,							)

       @require_torch
       @slow
       def 						UpperCAmelCase(   self      :     Any    ):
            a_    :     List[str]										=    """facebook/detr-resnet-50"""

            a_    :     Any										=    pipeline("""object-detection"""      ,							model=__UpperCamelCase    )

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

            a_    :     Dict										=    object_detector(
                [
                    """http://images.cocodataset.org/val2017/000000039769.jpg""",
                    """http://images.cocodataset.org/val2017/000000039769.jpg""",
                ]    )
            self.assertEqual(
                nested_simplify(__UpperCamelCase      ,							decimals=4    )      ,							[
                    [
                        {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                        {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                        {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                        {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                        {"""score""": 0.9_9_8_7, """label""": """cat""", """box""": {"""xmin""": 3_4_5, """ymin""": 2_3, """xmax""": 6_4_0, """ymax""": 3_6_8}},
                    ],
                    [
                        {"""score""": 0.9_9_8_2, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_0, """xmax""": 1_7_5, """ymax""": 1_1_7}},
                        {"""score""": 0.9_9_6_0, """label""": """remote""", """box""": {"""xmin""": 3_3_3, """ymin""": 7_2, """xmax""": 3_6_8, """ymax""": 1_8_7}},
                        {"""score""": 0.9_9_5_5, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_3_9, """ymax""": 4_7_3}},
                        {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                        {"""score""": 0.9_9_8_7, """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[str]    ):
            a_    :     Tuple										=    0.9_9_8_5
            a_    :     Tuple										=    """facebook/detr-resnet-50"""

            a_    :     List[str]										=    pipeline("""object-detection"""      ,							model=__UpperCamelCase    )

            a_    :     Any										=    object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""      ,							threshold=__UpperCamelCase    )
            self.assertEqual(
                nested_simplify(__UpperCamelCase      ,							decimals=4    )      ,							[
                    {"""score""": 0.9_9_8_8, """label""": """cat""", """box""": {"""xmin""": 1_3, """ymin""": 5_2, """xmax""": 3_1_4, """ymax""": 4_7_0}},
                    {"""score""": 0.9_9_8_7, """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      :     Union[str, Any]    ):
            a_    :     int										=    """Narsil/layoutlmv3-finetuned-funsd"""
            a_    :     Union[str, Any]										=    0.9_9_9_3

            a_    :     str										=    pipeline("""object-detection"""      ,							model=__UpperCamelCase      ,							threshold=__UpperCamelCase    )

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






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCamelCase        =     logging.get_logger(__name__)

__lowerCamelCase        =     {
    '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''',
}


class   a__	(						lowerCAmelCase_						,       lowerCAmelCase_    ):
       lowerCamelCase__:     str							    =     """convnextv2"""

       def __init__(   self      :     Tuple      ,							lowerCamelCase_      :     str=3      ,							lowerCamelCase_      :     Tuple=4      ,							lowerCamelCase_      :     Any=4      ,							lowerCamelCase_      :     Optional[int]=None      ,							lowerCamelCase_      :     Optional[int]=None      ,							lowerCamelCase_      :     Optional[int]="gelu"      ,							lowerCamelCase_      :     Optional[Any]=0.0_2      ,							lowerCamelCase_      :     str=1E-12      ,							lowerCamelCase_      :     Dict=0.0      ,							lowerCamelCase_      :     Union[str, Any]=2_2_4      ,							lowerCamelCase_      :     Dict=None      ,							lowerCamelCase_      :     Optional[Any]=None      ,							**lowerCamelCase_      :     List[Any]      ,							):
            super().__init__(**lowerCamelCase_    )

            a_    :     Dict										=    num_channels
            a_    :     Optional[int]										=    patch_size
            a_    :     int										=    num_stages
            a_    :     Any										=    [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes
            a_    :     Any										=    [3, 3, 9, 3] if depths is None else depths
            a_    :     Any										=    hidden_act
            a_    :     Dict										=    initializer_range
            a_    :     Optional[Any]										=    layer_norm_eps
            a_    :     int										=    drop_path_rate
            a_    :     Optional[Any]										=    image_size
            a_    :     Dict										=    ["""stem"""] + [F'''stage{idx}''' for idx in range(1      ,							len(self.depths    ) + 1    )]
            a_			,			a_    :     int										=    get_aligned_output_features_output_indices(
                out_features=lowerCamelCase_      ,							out_indices=lowerCamelCase_      ,							stage_names=self.stage_names    )



