





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import BlipImageProcessor





class   lowercase__     (				unittest.TestCase	):







  def __init__(						self     ,    _lowercase     ,    _lowercase=7     ,    _lowercase=3     ,    _lowercase=18     ,    _lowercase=30     ,    _lowercase=400     ,    _lowercase=True     ,    _lowercase=None     ,    _lowercase=True     ,    _lowercase=False     ,    _lowercase=[0.4814_5466, 0.457_8275, 0.4082_1073]     ,    _lowercase=[0.2686_2954, 0.2613_0258, 0.2757_7711]     ,    _lowercase=True     ,    ):
       lowerCAmelCase_       :							Tuple			 =		size if size is not None else {'''height''': 20, '''width''': 20}
       lowerCAmelCase_       :							Optional[Any]			 =		parent
       lowerCAmelCase_       :							Dict			 =		batch_size
       lowerCAmelCase_       :							Any			 =		num_channels
       lowerCAmelCase_       :							Optional[int]			 =		image_size
       lowerCAmelCase_       :							Tuple			 =		min_resolution
       lowerCAmelCase_       :							str			 =		max_resolution
       lowerCAmelCase_       :							Optional[int]			 =		do_resize
       lowerCAmelCase_       :							Optional[int]			 =		size
       lowerCAmelCase_       :							int			 =		do_normalize
       lowerCAmelCase_       :							Optional[int]			 =		image_mean
       lowerCAmelCase_       :							Dict			 =		image_std
       lowerCAmelCase_       :							Optional[int]			 =		do_pad
       lowerCAmelCase_       :							int			 =		do_convert_rgb







  def 						UpperCAmelCase__		(						self    ):
       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_convert_rgb": self.do_convert_rgb,
           "do_pad": self.do_pad,
       }







  def 						UpperCAmelCase__		(						self     ,    _lowercase=False     ,    _lowercase=False     ,    _lowercase=False    ):
       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       if equal_resolution:
            lowerCAmelCase_       :							Optional[Any]			 =		[]
            for i in range(self.batch_size    ):
                 image_inputs.append(
                     np.random.randint(
                         255     ,    size=(self.num_channels, self.max_resolution, self.max_resolution)     ,    dtype=np.uinta    )    )
       else:
            lowerCAmelCase_       :							Optional[Any]			 =		[]
            for i in range(self.batch_size    ):
                 lowerCAmelCase_       :							Any			 =		np.random.choice(np.arange(self.min_resolution     ,    self.max_resolution    )     ,    2    )
                 image_inputs.append(np.random.randint(255     ,    size=(self.num_channels, width, height)     ,    dtype=np.uinta    )    )

       if not numpify and not torchify:
            # PIL expects the channel dimension as last dimension
            lowerCAmelCase_       :							List[Any]			 =		[Image.fromarray(np.moveaxis(_lowercase     ,    0     ,    -1    )    ) for x in image_inputs]

       if torchify:
            lowerCAmelCase_       :							Optional[int]			 =		[torch.from_numpy(_lowercase    ) for x in image_inputs]

       return image_inputs





@require_torch
@require_vision
class   lowercase__     (				__A  ,       unittest.TestCase	):
  __UpperCamelCase						  =	BlipImageProcessor if is_vision_available() else None







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							str			 =		BlipImageProcessingTester(self    )







  @property
  def 						UpperCAmelCase__		(						self    ):
       return self.image_processor_tester.prepare_image_processor_dict()







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.image_processing_class(**self.image_processor_dict    )
       self.assertTrue(hasattr(_lowercase     ,    """do_resize"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """size"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """do_normalize"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """image_mean"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """image_std"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """do_convert_rgb"""    )    )







  def 						UpperCAmelCase__		(						self    ):
       pass







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							List[Any]			 =		self.image_processing_class(**self.image_processor_dict    )
       # create random PIL images
       lowerCAmelCase_       :							List[str]			 =		self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase    )
       for image in image_inputs:
            self.assertIsInstance(_lowercase     ,    Image.Image    )

       # Test not batched input
       lowerCAmelCase_       :							Optional[Any]			 =		image_processor(image_inputs[0]     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )

       # Test batched
       lowerCAmelCase_       :							Optional[Any]			 =		image_processor(_lowercase     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							List[Any]			 =		self.image_processing_class(**self.image_processor_dict    )
       # create random numpy tensors
       lowerCAmelCase_       :							Tuple			 =		self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase     ,    numpify=_lowercase    )
       for image in image_inputs:
            self.assertIsInstance(_lowercase     ,    np.ndarray    )

       # Test not batched input
       lowerCAmelCase_       :							Optional[int]			 =		image_processor(image_inputs[0]     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )

       # Test batched
       lowerCAmelCase_       :							Dict			 =		image_processor(_lowercase     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Tuple			 =		self.image_processing_class(**self.image_processor_dict    )
       # create random PyTorch tensors
       lowerCAmelCase_       :							int			 =		self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase     ,    torchify=_lowercase    )
       for image in image_inputs:
            self.assertIsInstance(_lowercase     ,    torch.Tensor    )

       # Test not batched input
       lowerCAmelCase_       :							int			 =		image_processor(image_inputs[0]     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )

       # Test batched
       lowerCAmelCase_       :							Dict			 =		image_processor(_lowercase     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )





@require_torch
@require_vision
class   lowercase__     (				__A  ,       unittest.TestCase	):
  __UpperCamelCase						  =	BlipImageProcessor if is_vision_available() else None







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							List[Any]			 =		BlipImageProcessingTester(self     ,    num_channels=4    )
       lowerCAmelCase_       :							List[str]			 =		3







  @property
  def 						UpperCAmelCase__		(						self    ):
       return self.image_processor_tester.prepare_image_processor_dict()







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Dict			 =		self.image_processing_class(**self.image_processor_dict    )
       self.assertTrue(hasattr(_lowercase     ,    """do_resize"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """size"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """do_normalize"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """image_mean"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """image_std"""    )    )
       self.assertTrue(hasattr(_lowercase     ,    """do_convert_rgb"""    )    )







  def 						UpperCAmelCase__		(						self    ):
       pass







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Any			 =		self.image_processing_class(**self.image_processor_dict    )
       # create random PIL images
       lowerCAmelCase_       :							Dict			 =		self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase    )
       for image in image_inputs:
            self.assertIsInstance(_lowercase     ,    Image.Image    )

       # Test not batched input
       lowerCAmelCase_       :							int			 =		image_processor(image_inputs[0]     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               1,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )

       # Test batched
       lowerCAmelCase_       :							List[str]			 =		image_processor(_lowercase     ,    return_tensors="""pt"""    ).pixel_values
       self.assertEqual(
           encoded_images.shape     ,    (
               self.image_processor_tester.batch_size,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.size["""height"""],
               self.image_processor_tester.size["""width"""],
           )     ,    )








"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCamelCase_						:					Tuple      				=			{"""configuration_detr""": ["""DETR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DetrConfig""", """DetrOnnxConfig"""]}

try:
    if not is_vision_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_						:					Tuple      				=			["""DetrFeatureExtractor"""]
    UpperCamelCase_						:					int      				=			["""DetrImageProcessor"""]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_						:					Dict      				=			[
        """DETR_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """DetrForObjectDetection""",
        """DetrForSegmentation""",
        """DetrModel""",
        """DetrPreTrainedModel""",
    ]


if TYPE_CHECKING:
    from .configuration_detr import DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, DetrConfig, DetrOnnxConfig

    try:
        if not is_vision_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_detr import DetrFeatureExtractor
        from .image_processing_detr import DetrImageProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_detr import (
            DETR_PRETRAINED_MODEL_ARCHIVE_LIST,
            DetrForObjectDetection,
            DetrForSegmentation,
            DetrModel,
            DetrPreTrainedModel,
        )

else:
    import sys

    UpperCamelCase_						:					List[str]      				=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


