"""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 			UpperCamelCase_     :

			"""simple docstring"""





			@staticmethod
			def   UpperCAmelCase_				(			*UpperCAmelCase__      :    Any      ,      **UpperCAmelCase__      :    List[str]       )	->     Tuple:
								pass




@is_pipeline_test
@require_vision
@require_timm
@require_torch
class 			UpperCamelCase_     (			unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  List[Any]    =      MODEL_FOR_OBJECT_DETECTION_MAPPING




	def   UpperCAmelCase_				(			self      :    int      ,      UpperCAmelCase__      :    Tuple      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    Dict       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE						       =    ObjectDetectionPipeline(model=_A      ,      image_processor=_A       )
						return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]




	def   UpperCAmelCase_				(			self      :    Any      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    Union[str, Any]       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    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

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

						__SCREAMING_SNAKE_CASE						       =    [
						    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"],
						]
						__SCREAMING_SNAKE_CASE						       =    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   UpperCAmelCase_				(			self      :    List[str]       )	->     Union[str, Any]:
						pass




	@require_torch
	def   UpperCAmelCase_				(			self      :    List[str]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    "hf-internal-testing/tiny-detr-mobilenetsv3"

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

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

						self.assertEqual(
						    nested_simplify(_A      ,      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}},
						    ]      ,      )

						__SCREAMING_SNAKE_CASE						       =    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.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      :    int       )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    "facebook/detr-resnet-50"

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

						__SCREAMING_SNAKE_CASE						       =    object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"       )
						self.assertEqual(
						    nested_simplify(_A      ,      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}},
						    ]      ,      )

						__SCREAMING_SNAKE_CASE						       =    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.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      :    int       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    "facebook/detr-resnet-50"

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

						__SCREAMING_SNAKE_CASE						       =    object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"       )
						self.assertEqual(
						    nested_simplify(_A      ,      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}},
						    ]      ,      )

						__SCREAMING_SNAKE_CASE						       =    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.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      :    Optional[int]       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    0.9_985
						__SCREAMING_SNAKE_CASE						       =    "facebook/detr-resnet-50"

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

						__SCREAMING_SNAKE_CASE						       =    object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"      ,      threshold=_A       )
						self.assertEqual(
						    nested_simplify(_A      ,      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      :    str       )	->     Dict:
						__SCREAMING_SNAKE_CASE						       =    "Narsil/layoutlmv3-finetuned-funsd"
						__SCREAMING_SNAKE_CASE						       =    0.9_993

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

						__SCREAMING_SNAKE_CASE						       =    object_detector(
						    "https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"       )
						self.assertEqual(
						    nested_simplify(_A      ,      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"""







import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel

from ...utils import logging


a__    :							Optional[Any]            =					logging.get_logger(__name__)




class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	snake_case__    :  int    =      CLIPConfig

	snake_case__    :  str    =      ["CLIPEncoderLayer"]




	def __init__(			self      :    Optional[int]      ,      UpperCAmelCase__      :    CLIPConfig       )	->     Dict:
						super().__init__(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    CLIPVisionModelWithProjection(config.vision_config       )

						__SCREAMING_SNAKE_CASE						       =    nn.Linear(config.vision_config.projection_dim      ,      1       )
						__SCREAMING_SNAKE_CASE						       =    nn.Linear(config.vision_config.projection_dim      ,      1       )




	@torch.no_grad()
	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    int=0.5      ,      UpperCAmelCase__      :    Optional[int]=0.5       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    self.vision_model(UpperCAmelCase__       )[0]

						__SCREAMING_SNAKE_CASE						       =    self.p_head(UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    nsfw_detected.flatten()
						__SCREAMING_SNAKE_CASE						       =    nsfw_detected > p_threshold
						__SCREAMING_SNAKE_CASE						       =    nsfw_detected.tolist()

						if any(UpperCAmelCase__       ):
											logger.warning(
											    "Potential NSFW content was detected in one or more images. A black image will be returned instead."
											    " Try again with a different prompt and/or seed."       )

						for idx, nsfw_detected_ in enumerate(UpperCAmelCase__       ):
											if nsfw_detected_:
																__SCREAMING_SNAKE_CASE						       =    np.zeros(images[idx].shape       )

						__SCREAMING_SNAKE_CASE						       =    self.w_head(UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    watermark_detected.flatten()
						__SCREAMING_SNAKE_CASE						       =    watermark_detected > w_threshold
						__SCREAMING_SNAKE_CASE						       =    watermark_detected.tolist()

						if any(UpperCAmelCase__       ):
											logger.warning(
											    "Potential watermarked content was detected in one or more images. A black image will be returned instead."
											    " Try again with a different prompt and/or seed."       )

						for idx, watermark_detected_ in enumerate(UpperCAmelCase__       ):
											if watermark_detected_:
																__SCREAMING_SNAKE_CASE						       =    np.zeros(images[idx].shape       )

						return images, nsfw_detected, watermark_detected
