'''simple docstring'''

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 SwinaSRImageProcessor
      from transformers.image_transforms import get_image_size




class    lowerCamelCase__  (   unittest.TestCase     ):



   def __init__(    self					,      lowerCAmelCase__					,      lowerCAmelCase__=7					,      lowerCAmelCase__=3					,      lowerCAmelCase__=18					,      lowerCAmelCase__=30					,      lowerCAmelCase__=400					,      lowerCAmelCase__=True					,      lowerCAmelCase__=1 / 255					,      lowerCAmelCase__=True					,      lowerCAmelCase__=8					,      )				->     Union[str, Any]:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =parent
        _UpperCamelCase  :Tuple       =batch_size
        _UpperCamelCase  :Optional[Any]       =num_channels
        _UpperCamelCase  :List[Any]       =image_size
        _UpperCamelCase  :Optional[int]       =min_resolution
        _UpperCamelCase  :Optional[int]       =max_resolution
        _UpperCamelCase  :List[Any]       =do_rescale
        _UpperCamelCase  :Union[str, Any]       =rescale_factor
        _UpperCamelCase  :Any       =do_pad
        _UpperCamelCase  :Any       =pad_size



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        return {
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
            "do_pad": self.do_pad,
            "pad_size": self.pad_size,
        }




   def  _UpperCamelCase		(    self					,      lowerCAmelCase__=False					,      lowerCAmelCase__=False					,      lowerCAmelCase__=False     )				->     Optional[Any]:
        """simple docstring"""





        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
             _UpperCamelCase  :Tuple       =[]
             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:
             _UpperCamelCase  :List[str]       =[]
             for i in range(self.batch_size     ):
                  _UpperCamelCase       ,      _UpperCamelCase  :List[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
             _UpperCamelCase  :List[str]       =[Image.fromarray(np.moveaxis(lowerCAmelCase__					,      0					,      -1     )     ) for x in image_inputs]

        if torchify:
             _UpperCamelCase  :Dict       =[torch.from_numpy(lowerCAmelCase__     ) for x in image_inputs]

        return image_inputs




@require_torch
@require_vision
class    lowerCamelCase__  (   __snake_case       ,				unittest.TestCase     ):
   __UpperCAmelCase           =						SwinaSRImageProcessor if is_vision_available() else None



   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =SwinaSRImageProcessingTester(self     )



   @property
   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        return self.image_processor_tester.prepare_image_processor_dict()



   def  _UpperCamelCase		(    self     )				->     str:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =self.image_processing_class(**self.image_processor_dict     )
        self.assertTrue(hasattr(lowerCAmelCase__					,      """do_rescale"""     )     )
        self.assertTrue(hasattr(lowerCAmelCase__					,      """rescale_factor"""     )     )
        self.assertTrue(hasattr(lowerCAmelCase__					,      """do_pad"""     )     )
        self.assertTrue(hasattr(lowerCAmelCase__					,      """pad_size"""     )     )



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        pass



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__     )				->     str:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :Union[str, Any]       =get_image_size(lowerCAmelCase__     )
        _UpperCamelCase  :Tuple       =self.image_processor_tester.pad_size

        _UpperCamelCase  :List[Any]       =(old_height // size + 1) * size - old_height
        _UpperCamelCase  :List[Any]       =(old_width // size + 1) * size - old_width
        return old_height + pad_height, old_width + pad_width



   def  _UpperCamelCase		(    self     )				->     Union[str, Any]:
        """simple docstring"""





        _UpperCamelCase  :Optional[Any]       =self.image_processing_class(**self.image_processor_dict     )
        # create random PIL images
        _UpperCamelCase  :str       =self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase__     )
        for image in image_inputs:
             self.assertIsInstance(lowerCAmelCase__					,      Image.Image     )

        # Test not batched input
        _UpperCamelCase  :Tuple       =image_processor(image_inputs[0]					,      return_tensors="""pt"""     ).pixel_values
        _UpperCamelCase       ,      _UpperCamelCase  :str       =self.calculate_expected_size(np.array(image_inputs[0]     )     )
        self.assertEqual(
            encoded_images.shape					,      (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )					,      )



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =self.image_processing_class(**self.image_processor_dict     )
        # create random numpy tensors
        _UpperCamelCase  :Any       =self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase__					,      numpify=lowerCAmelCase__     )
        for image in image_inputs:
             self.assertIsInstance(lowerCAmelCase__					,      np.ndarray     )

        # Test not batched input
        _UpperCamelCase  :Any       =image_processor(image_inputs[0]					,      return_tensors="""pt"""     ).pixel_values
        _UpperCamelCase       ,      _UpperCamelCase  :List[str]       =self.calculate_expected_size(image_inputs[0]     )
        self.assertEqual(
            encoded_images.shape					,      (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )					,      )




   def  _UpperCamelCase		(    self     )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase  :int       =self.image_processing_class(**self.image_processor_dict     )
        # create random PyTorch tensors
        _UpperCamelCase  :List[str]       =self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase__					,      torchify=lowerCAmelCase__     )
        for image in image_inputs:
             self.assertIsInstance(lowerCAmelCase__					,      torch.Tensor     )

        # Test not batched input
        _UpperCamelCase  :Optional[int]       =image_processor(image_inputs[0]					,      return_tensors="""pt"""     ).pixel_values
        _UpperCamelCase       ,      _UpperCamelCase  :Optional[int]       =self.calculate_expected_size(image_inputs[0]     )
        self.assertEqual(
            encoded_images.shape					,      (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )					,      )







'''simple docstring'''







import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
			from PIL import Image

class _lowercase      (							unittest.TestCase							, _lowercase						):




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""	)
														self.tool.setup()
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""     ,				remote=UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							int	):
														lowerCamelCase__   :      List[Any]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[Any]			    =			self.tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Tuple			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Dict			    =			self.remote_tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      str			    =			self.tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[int]			    =			self.remote_tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)

