




import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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 DonutImageProcessor







class __snake_case			(			unittest.TestCase		):







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

        """simple docstring"""


        UpperCAmelCase_          =							parent
        UpperCAmelCase_          =							batch_size
        UpperCAmelCase_          =							num_channels
        UpperCAmelCase_          =							image_size
        UpperCAmelCase_          =							min_resolution
        UpperCAmelCase_          =							max_resolution
        UpperCAmelCase_          =							do_resize
        UpperCAmelCase_          =							size if size is not None else {'''height''': 18, '''width''': 20}
        UpperCAmelCase_          =							do_thumbnail
        UpperCAmelCase_          =							do_align_axis
        UpperCAmelCase_          =							do_pad
        UpperCAmelCase_          =							do_normalize
        UpperCAmelCase_          =							image_mean
        UpperCAmelCase_          =							image_std







   def lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_thumbnail": self.do_thumbnail,
            "do_align_long_axis": self.do_align_axis,
            "do_pad": self.do_pad,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
        }







@require_torch
@require_vision
class __snake_case			(			a ,  unittest.TestCase		):
   UpperCAmelCase__  :    int          =  DonutImageProcessor if is_vision_available() else None







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        UpperCAmelCase_          =							DonutImageProcessingTester(self)







   @property
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        return self.image_processor_tester.prepare_image_processor_dict()







   def lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


        UpperCAmelCase_          =							self.image_processing_class(**self.image_processor_dict)
        self.assertTrue(hasattr(_snake_case					,						'''do_resize'''))
        self.assertTrue(hasattr(_snake_case					,						'''size'''))
        self.assertTrue(hasattr(_snake_case					,						'''do_thumbnail'''))
        self.assertTrue(hasattr(_snake_case					,						'''do_align_long_axis'''))
        self.assertTrue(hasattr(_snake_case					,						'''do_pad'''))
        self.assertTrue(hasattr(_snake_case					,						'''do_normalize'''))
        self.assertTrue(hasattr(_snake_case					,						'''image_mean'''))
        self.assertTrue(hasattr(_snake_case					,						'''image_std'''))







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							self.image_processing_class.from_dict(self.image_processor_dict)
        self.assertEqual(image_processor.size					,						{'''height''': 18, '''width''': 20})

        UpperCAmelCase_          =							self.image_processing_class.from_dict(self.image_processor_dict					,						size=42)
        self.assertEqual(image_processor.size					,						{'''height''': 42, '''width''': 42})

        # Previous config had dimensions in (width, height) order
        UpperCAmelCase_          =							self.image_processing_class.from_dict(self.image_processor_dict					,						size=(42, 84))
        self.assertEqual(image_processor.size					,						{'''height''': 84, '''width''': 42})







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        pass







   @is_flaky()
   def lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


        UpperCAmelCase_          =							self.image_processing_class(**self.image_processor_dict)
        # create random PIL images
        UpperCAmelCase_          =							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
        UpperCAmelCase_          =							image_processing(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
        UpperCAmelCase_          =							image_processing(_snake_case					,						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'''],
            )					,						)







   @is_flaky()
   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							self.image_processing_class(**self.image_processor_dict)
        # create random numpy tensors
        UpperCAmelCase_          =							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
        UpperCAmelCase_          =							image_processing(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
        UpperCAmelCase_          =							image_processing(_snake_case					,						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'''],
            )					,						)







   @is_flaky()
   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        UpperCAmelCase_          =							self.image_processing_class(**self.image_processor_dict)
        # create random PyTorch tensors
        UpperCAmelCase_          =							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
        UpperCAmelCase_          =							image_processing(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
        UpperCAmelCase_          =							image_processing(_snake_case					,						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'''],
            )					,						)










'''simple docstring'''







A__:    Tuple					    =       '''
# Installazione di Transformers
! pip install transformers datasets
# Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e
# rimuovi la modalità commento al comando seguente.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

A__:    Tuple					    =       [{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
A__:    Any					    =       {
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}


