"""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, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import ViltImageProcessor

class SCREAMING_SNAKE_CASE     (    unittest.TestCase	):







 """simple docstring"""


 def __init__(   self		:      Dict					,   _snake_case		:      Optional[int]					,   _snake_case		:      Optional[int]=7					,   _snake_case		:      int=3					,   _snake_case		:      Optional[int]=18					,   _snake_case		:      List[str]=30					,   _snake_case		:      List[Any]=400					,   _snake_case		:      Union[str, Any]=True					,   _snake_case		:      int=None					,   _snake_case		:      Dict=2					,   _snake_case		:      int=True					,   _snake_case		:      Dict=[0.5, 0.5, 0.5]					,   _snake_case		:      Union[str, Any]=[0.5, 0.5, 0.5]					,   )				->			Tuple:




    '''simple docstring'''






    a__     				=		size if size is not None else {'shortest_edge': 30}
    a__     				=		parent
    a__     				=		batch_size
    a__     				=		num_channels
    a__     				=		image_size
    a__     				=		min_resolution
    a__     				=		max_resolution
    a__     				=		do_resize
    a__     				=		size
    a__     				=		size_divisor
    a__     				=		do_normalize
    a__     				=		image_mean
    a__     				=		image_std


 def        _lowerCAmelCase				(   self		:      List[str]				)				->			int:




    '''simple docstring'''






    return {
        "image_mean": self.image_mean,
        "image_std": self.image_std,
        "do_normalize": self.do_normalize,
        "do_resize": self.do_resize,
        "size": self.size,
        "size_divisor": self.size_divisor,
    }



 def        _lowerCAmelCase				(   self		:      Optional[Any]					,   _snake_case		:      List[str]					,   _snake_case		:      Tuple=False				)				->			Union[str, Any]:




    '''simple docstring'''






    if not batched:
       a__     				=		self.size['shortest_edge']
       a__     				=		image_inputs[0]
       if isinstance(_snake_case					,   Image.Image				):
          a__				,       a__     				=		image.size
       else:
          a__				,       a__     				=		image.shape[1], image.shape[2]
       a__     				=		size / min(_snake_case					,   _snake_case				)
       if h < w:
          a__				,       a__     				=		size, scale * w
       else:
          a__				,       a__     				=		scale * h, size

       a__     				=		int((1333 / 800) * size				)
       if max(_snake_case					,   _snake_case				) > max_size:
          a__     				=		max_size / max(_snake_case					,   _snake_case				)
          a__     				=		newh * scale
          a__     				=		neww * scale

       a__				,       a__     				=		int(newh + 0.5				), int(neww + 0.5				)
       a__				,       a__     				=		(
           newh // self.size_divisor * self.size_divisor,
           neww // self.size_divisor * self.size_divisor,
       )

    else:
       a__     				=		[]
       for image in image_inputs:
          a__				,       a__     				=		self.get_expected_values([image]				)
          expected_values.append((expected_height, expected_width)				)
       a__     				=		max(_snake_case					,   key=lambda _snake_case				: item[0]				)[0]
       a__     				=		max(_snake_case					,   key=lambda _snake_case				: item[1]				)[1]

    return expected_height, expected_width

@require_torch
@require_vision
class SCREAMING_SNAKE_CASE     (    a   ,      unittest.TestCase	):







 """simple docstring"""


 a_       :	Tuple  			=ViltImageProcessor if is_vision_available() else None


 def        _lowerCAmelCase				(   self		:      Union[str, Any]				)				->			Union[str, Any]:




    '''simple docstring'''






    a__     				=		ViltImageProcessingTester(self				)


 @property
 def        _lowerCAmelCase				(   self		:      int				)				->			Union[str, Any]:




    '''simple docstring'''






    return self.image_processor_tester.prepare_image_processor_dict()


 def        _lowerCAmelCase				(   self		:      List[str]				)				->			Optional[int]:




    '''simple docstring'''






    a__     				=		self.image_processing_class(**self.image_processor_dict				)
    self.assertTrue(hasattr(_snake_case					,   'image_mean'				)				)
    self.assertTrue(hasattr(_snake_case					,   'image_std'				)				)
    self.assertTrue(hasattr(_snake_case					,   'do_normalize'				)				)
    self.assertTrue(hasattr(_snake_case					,   'do_resize'				)				)
    self.assertTrue(hasattr(_snake_case					,   'size'				)				)
    self.assertTrue(hasattr(_snake_case					,   'size_divisor'				)				)


 def        _lowerCAmelCase				(   self		:      str				)				->			Optional[Any]:




    '''simple docstring'''






    a__     				=		self.image_processing_class.from_dict(self.image_processor_dict				)
    self.assertEqual(image_processor.size					,   {'shortest_edge': 30}				)

    a__     				=		self.image_processing_class.from_dict(self.image_processor_dict					,   size=42				)
    self.assertEqual(image_processor.size					,   {'shortest_edge': 42}				)


 def        _lowerCAmelCase				(   self		:      Optional[Any]				)				->			Any:




    '''simple docstring'''






    pass


 def        _lowerCAmelCase				(   self		:      List[str]				)				->			Tuple:




    '''simple docstring'''






    a__     				=		self.image_processing_class(**self.image_processor_dict				)
    # create random PIL images
    a__     				=		prepare_image_inputs(self.image_processor_tester					,   equal_resolution=_snake_case				)
    for image in image_inputs:
       self.assertIsInstance(_snake_case					,   Image.Image				)

    # Test not batched input
    a__     				=		image_processing(image_inputs[0]					,   return_tensors='pt'				).pixel_values

    a__				,       a__     				=		self.image_processor_tester.get_expected_values(_snake_case				)
    self.assertEqual(
        encoded_images.shape					,   (1, self.image_processor_tester.num_channels, expected_height, expected_width)					,   )

    # Test batched
    a__     				=		image_processing(_snake_case					,   return_tensors='pt'				).pixel_values

    a__				,       a__     				=		self.image_processor_tester.get_expected_values(_snake_case					,   batched=_snake_case				)
    self.assertEqual(
        encoded_images.shape					,   (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            expected_height,
            expected_width,
        )					,   )


 def        _lowerCAmelCase				(   self		:      Optional[Any]				)				->			Optional[Any]:




    '''simple docstring'''






    a__     				=		self.image_processing_class(**self.image_processor_dict				)
    # create random numpy tensors
    a__     				=		prepare_image_inputs(self.image_processor_tester					,   equal_resolution=_snake_case					,   numpify=_snake_case				)
    for image in image_inputs:
       self.assertIsInstance(_snake_case					,   np.ndarray				)

    # Test not batched input
    a__     				=		image_processing(image_inputs[0]					,   return_tensors='pt'				).pixel_values

    a__				,       a__     				=		self.image_processor_tester.get_expected_values(_snake_case				)
    self.assertEqual(
        encoded_images.shape					,   (1, self.image_processor_tester.num_channels, expected_height, expected_width)					,   )

    # Test batched
    a__     				=		image_processing(_snake_case					,   return_tensors='pt'				).pixel_values

    a__				,       a__     				=		self.image_processor_tester.get_expected_values(_snake_case					,   batched=_snake_case				)
    self.assertEqual(
        encoded_images.shape					,   (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            expected_height,
            expected_width,
        )					,   )



 def        _lowerCAmelCase				(   self		:      Optional[int]				)				->			Union[str, Any]:




    '''simple docstring'''






    a__     				=		self.image_processing_class(**self.image_processor_dict				)
    # create random PyTorch tensors
    a__     				=		prepare_image_inputs(self.image_processor_tester					,   equal_resolution=_snake_case					,   torchify=_snake_case				)
    for image in image_inputs:
       self.assertIsInstance(_snake_case					,   torch.Tensor				)

    # Test not batched input
    a__     				=		image_processing(image_inputs[0]					,   return_tensors='pt'				).pixel_values

    a__				,       a__     				=		self.image_processor_tester.get_expected_values(_snake_case				)
    self.assertEqual(
        encoded_images.shape					,   (1, self.image_processor_tester.num_channels, expected_height, expected_width)					,   )

    # Test batched
    a__     				=		image_processing(_snake_case					,   return_tensors='pt'				).pixel_values

    a__				,       a__     				=		self.image_processor_tester.get_expected_values(_snake_case					,   batched=_snake_case				)
    self.assertEqual(
        encoded_images.shape					,   (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            expected_height,
            expected_width,
        )					,   )



"""simple docstring"""



from typing import TYPE_CHECKING

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


__magic_name__    				=					{
    "configuration_donut_swin": ["DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "DonutSwinConfig"],
    "processing_donut": ["DonutProcessor"],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __magic_name__    				=					[
         "DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST",
         "DonutSwinModel",
         "DonutSwinPreTrainedModel",
     ]

try:
     if not is_vision_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __magic_name__    				=					["DonutFeatureExtractor"]
     __magic_name__    				=					["DonutImageProcessor"]


if TYPE_CHECKING:
     from .configuration_donut_swin import DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, DonutSwinConfig
     from .processing_donut import DonutProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_donut_swin import (
              DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
              DonutSwinModel,
              DonutSwinPreTrainedModel,
          )

     try:
          if not is_vision_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .feature_extraction_donut import DonutFeatureExtractor
          from .image_processing_donut import DonutImageProcessor

else:
     import sys

     __magic_name__    				=					_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


