




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     __SCREAMING_SNAKE_CASE(       *_A		,      **_A      ):




																"""simple docstring"""



																pass
@is_pipeline_test
@require_vision
@require_timm
@require_torch
class       a__   ( unittest.TestCase				):
						_a							:   List[Any] =   MODEL_FOR_OBJECT_DETECTION_MAPPING






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	ObjectDetectionPipeline(model=_A		,      image_processor=_A      )
											return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	object_detector("./tests/fixtures/tests_samples/COCO/000000039769.png"		,      threshold=0.0      )

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

											import datasets

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

											__lowerCAmelCase           =	[
											    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"],
											]
											__lowerCAmelCase           =	object_detector(_A		,      threshold=0.0      )

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






						@require_tf
						@unittest.skip("Object detection not implemented in TF"      )
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											pass






						@require_torch
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"hf-internal-testing/tiny-detr-mobilenetsv3"

											__lowerCAmelCase           =	AutoModelForObjectDetection.from_pretrained(_A      )
											__lowerCAmelCase           =	AutoFeatureExtractor.from_pretrained(_A      )
											__lowerCAmelCase           =	ObjectDetectionPipeline(model=_A		,      feature_extractor=_A      )

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

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

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

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






						@require_torch
						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"facebook/detr-resnet-50"

											__lowerCAmelCase           =	AutoModelForObjectDetection.from_pretrained(_A      )
											__lowerCAmelCase           =	AutoFeatureExtractor.from_pretrained(_A      )
											__lowerCAmelCase           =	ObjectDetectionPipeline(model=_A		,      feature_extractor=_A      )

											__lowerCAmelCase           =	object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"      )
											self.assertEqual(
											    nested_simplify(_A		,      decimals=4      )		,      [
											        {"score": 0.99_82, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
											        {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
											        {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
											        {"score": 0.99_88, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
											        {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
											    ]		,      )

											__lowerCAmelCase           =	object_detector(
											    [
											        "http://images.cocodataset.org/val2017/000000039769.jpg",
											        "http://images.cocodataset.org/val2017/000000039769.jpg",
											    ]      )
											self.assertEqual(
											    nested_simplify(_A		,      decimals=4      )		,      [
											        [
											            {"score": 0.99_82, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
											            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
											            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
											            {"score": 0.99_88, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
											            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
											        ],
											        [
											            {"score": 0.99_82, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
											            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
											            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
											            {"score": 0.99_88, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
											            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
											        ],
											    ]		,      )






						@require_torch
						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"facebook/detr-resnet-50"

											__lowerCAmelCase           =	pipeline("object-detection"		,      model=_A      )

											__lowerCAmelCase           =	object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"      )
											self.assertEqual(
											    nested_simplify(_A		,      decimals=4      )		,      [
											        {"score": 0.99_82, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
											        {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
											        {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
											        {"score": 0.99_88, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
											        {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
											    ]		,      )

											__lowerCAmelCase           =	object_detector(
											    [
											        "http://images.cocodataset.org/val2017/000000039769.jpg",
											        "http://images.cocodataset.org/val2017/000000039769.jpg",
											    ]      )
											self.assertEqual(
											    nested_simplify(_A		,      decimals=4      )		,      [
											        [
											            {"score": 0.99_82, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
											            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
											            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
											            {"score": 0.99_88, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
											            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
											        ],
											        [
											            {"score": 0.99_82, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
											            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
											            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
											            {"score": 0.99_88, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
											            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
											        ],
											    ]		,      )






						@require_torch
						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	0.99_85
											__lowerCAmelCase           =	"facebook/detr-resnet-50"

											__lowerCAmelCase           =	pipeline("object-detection"		,      model=_A      )

											__lowerCAmelCase           =	object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"		,      threshold=_A      )
											self.assertEqual(
											    nested_simplify(_A		,      decimals=4      )		,      [
											        {"score": 0.99_88, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
											        {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
											    ]		,      )






						@require_torch
						@require_pytesseract
						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"Narsil/layoutlmv3-finetuned-funsd"
											__lowerCAmelCase           =	0.99_93

											__lowerCAmelCase           =	pipeline("object-detection"		,      model=_A		,      threshold=_A      )

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










UpperCamelCase__         =      """0.18.2"""

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
					if not is_onnx_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_onnx_objects import *  # noqa F403
else:
					from .pipelines import OnnxRuntimeModel

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_pt_objects import *  # noqa F403
else:
					from .models import (
					    AutoencoderKL,
					    ControlNetModel,
					    ModelMixin,
					    PriorTransformer,
					    TaFilmDecoder,
					    TransformeraDModel,
					    UNetaDModel,
					    UNetaDConditionModel,
					    UNetaDModel,
					    UNetaDConditionModel,
					    VQModel,
					)
					from .optimization import (
					    get_constant_schedule,
					    get_constant_schedule_with_warmup,
					    get_cosine_schedule_with_warmup,
					    get_cosine_with_hard_restarts_schedule_with_warmup,
					    get_linear_schedule_with_warmup,
					    get_polynomial_decay_schedule_with_warmup,
					    get_scheduler,
					)
					from .pipelines import (
					    AudioPipelineOutput,
					    ConsistencyModelPipeline,
					    DanceDiffusionPipeline,
					    DDIMPipeline,
					    DDPMPipeline,
					    DiffusionPipeline,
					    DiTPipeline,
					    ImagePipelineOutput,
					    KarrasVePipeline,
					    LDMPipeline,
					    LDMSuperResolutionPipeline,
					    PNDMPipeline,
					    RePaintPipeline,
					    ScoreSdeVePipeline,
					)
					from .schedulers import (
					    CMStochasticIterativeScheduler,
					    DDIMInverseScheduler,
					    DDIMParallelScheduler,
					    DDIMScheduler,
					    DDPMParallelScheduler,
					    DDPMScheduler,
					    DEISMultistepScheduler,
					    DPMSolverMultistepInverseScheduler,
					    DPMSolverMultistepScheduler,
					    DPMSolverSinglestepScheduler,
					    EulerAncestralDiscreteScheduler,
					    EulerDiscreteScheduler,
					    HeunDiscreteScheduler,
					    IPNDMScheduler,
					    KarrasVeScheduler,
					    KDPMaAncestralDiscreteScheduler,
					    KDPMaDiscreteScheduler,
					    PNDMScheduler,
					    RePaintScheduler,
					    SchedulerMixin,
					    ScoreSdeVeScheduler,
					    UnCLIPScheduler,
					    UniPCMultistepScheduler,
					    VQDiffusionScheduler,
					)
					from .training_utils import EMAModel

try:
					if not (is_torch_available() and is_scipy_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
					from .schedulers import LMSDiscreteScheduler

try:
					if not (is_torch_available() and is_torchsde_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
					from .schedulers import DPMSolverSDEScheduler

try:
					if not (is_torch_available() and is_transformers_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
					from .pipelines import (
					    AltDiffusionImgaImgPipeline,
					    AltDiffusionPipeline,
					    AudioLDMPipeline,
					    CycleDiffusionPipeline,
					    IFImgaImgPipeline,
					    IFImgaImgSuperResolutionPipeline,
					    IFInpaintingPipeline,
					    IFInpaintingSuperResolutionPipeline,
					    IFPipeline,
					    IFSuperResolutionPipeline,
					    ImageTextPipelineOutput,
					    KandinskyImgaImgPipeline,
					    KandinskyInpaintPipeline,
					    KandinskyPipeline,
					    KandinskyPriorPipeline,
					    KandinskyVaaControlnetImgaImgPipeline,
					    KandinskyVaaControlnetPipeline,
					    KandinskyVaaImgaImgPipeline,
					    KandinskyVaaInpaintPipeline,
					    KandinskyVaaPipeline,
					    KandinskyVaaPriorEmbaEmbPipeline,
					    KandinskyVaaPriorPipeline,
					    LDMTextToImagePipeline,
					    PaintByExamplePipeline,
					    SemanticStableDiffusionPipeline,
					    ShapEImgaImgPipeline,
					    ShapEPipeline,
					    StableDiffusionAttendAndExcitePipeline,
					    StableDiffusionControlNetImgaImgPipeline,
					    StableDiffusionControlNetInpaintPipeline,
					    StableDiffusionControlNetPipeline,
					    StableDiffusionDepthaImgPipeline,
					    StableDiffusionDiffEditPipeline,
					    StableDiffusionImageVariationPipeline,
					    StableDiffusionImgaImgPipeline,
					    StableDiffusionInpaintPipeline,
					    StableDiffusionInpaintPipelineLegacy,
					    StableDiffusionInstructPixaPixPipeline,
					    StableDiffusionLatentUpscalePipeline,
					    StableDiffusionLDMaDPipeline,
					    StableDiffusionModelEditingPipeline,
					    StableDiffusionPanoramaPipeline,
					    StableDiffusionParadigmsPipeline,
					    StableDiffusionPipeline,
					    StableDiffusionPipelineSafe,
					    StableDiffusionPixaPixZeroPipeline,
					    StableDiffusionSAGPipeline,
					    StableDiffusionUpscalePipeline,
					    StableUnCLIPImgaImgPipeline,
					    StableUnCLIPPipeline,
					    TextToVideoSDPipeline,
					    TextToVideoZeroPipeline,
					    UnCLIPImageVariationPipeline,
					    UnCLIPPipeline,
					    UniDiffuserModel,
					    UniDiffuserPipeline,
					    UniDiffuserTextDecoder,
					    VersatileDiffusionDualGuidedPipeline,
					    VersatileDiffusionImageVariationPipeline,
					    VersatileDiffusionPipeline,
					    VersatileDiffusionTextToImagePipeline,
					    VideoToVideoSDPipeline,
					    VQDiffusionPipeline,
					)

try:
					if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
					from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
					if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
					from .pipelines import StableDiffusionKDiffusionPipeline

try:
					if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
					from .pipelines import (
					    OnnxStableDiffusionImgaImgPipeline,
					    OnnxStableDiffusionInpaintPipeline,
					    OnnxStableDiffusionInpaintPipelineLegacy,
					    OnnxStableDiffusionPipeline,
					    OnnxStableDiffusionUpscalePipeline,
					    StableDiffusionOnnxPipeline,
					)

try:
					if not (is_torch_available() and is_librosa_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
					from .pipelines import AudioDiffusionPipeline, Mel

try:
					if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
					from .pipelines import SpectrogramDiffusionPipeline

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_flax_objects import *  # noqa F403
else:
					from .models.controlnet_flax import FlaxControlNetModel
					from .models.modeling_flax_utils import FlaxModelMixin
					from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
					from .models.vae_flax import FlaxAutoencoderKL
					from .pipelines import FlaxDiffusionPipeline
					from .schedulers import (
					    FlaxDDIMScheduler,
					    FlaxDDPMScheduler,
					    FlaxDPMSolverMultistepScheduler,
					    FlaxKarrasVeScheduler,
					    FlaxLMSDiscreteScheduler,
					    FlaxPNDMScheduler,
					    FlaxSchedulerMixin,
					    FlaxScoreSdeVeScheduler,
					)


try:
					if not (is_flax_available() and is_transformers_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
					from .pipelines import (
					    FlaxStableDiffusionControlNetPipeline,
					    FlaxStableDiffusionImgaImgPipeline,
					    FlaxStableDiffusionInpaintPipeline,
					    FlaxStableDiffusionPipeline,
					)

try:
					if not (is_note_seq_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from .utils.dummy_note_seq_objects import *  # noqa F403
else:
					from .pipelines import MidiProcessor




