






"""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 ChineseCLIPImageProcessor


class      _lowercase (     unittest.TestCase						):


      def __init__(    self				:	List[Any]							,   a				:	Any							,   a				:	Any=7							,   a				:	Union[str, Any]=3							,   a				:	Union[str, Any]=1_8							,   a				:	str=3_0							,   a				:	List[str]=4_0_0							,   a				:	Any=True							,   a				:	str=None							,   a				:	Optional[Any]=True							,   a				:	Any=None							,   a				:	Optional[int]=True							,   a				:	Optional[Any]=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]							,   a				:	Tuple=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]							,   a				:	Union[str, Any]=True							,   ):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=size if size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
            __snake_case      :    Dict  			=crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
            __snake_case      :    Optional[Any]  			=parent
            __snake_case      :    List[str]  			=batch_size
            __snake_case      :    Optional[int]  			=num_channels
            __snake_case      :    List[Any]  			=image_size
            __snake_case      :    Optional[Any]  			=min_resolution
            __snake_case      :    str  			=max_resolution
            __snake_case      :    Dict  			=do_resize
            __snake_case      :    Optional[Any]  			=size
            __snake_case      :    List[str]  			=do_center_crop
            __snake_case      :    Optional[int]  			=crop_size
            __snake_case      :    Dict  			=do_normalize
            __snake_case      :    List[str]  			=image_mean
            __snake_case      :    Tuple  			=image_std
            __snake_case      :    int  			=do_convert_rgb


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            return {
                "do_resize": self.do_resize,
                "size": self.size,
                "do_center_crop": self.do_center_crop,
                "crop_size": self.crop_size,
                "do_normalize": self.do_normalize,
                "image_mean": self.image_mean,
                "image_std": self.image_std,
                "do_convert_rgb": self.do_convert_rgb,
            }







      def _UpperCamelCase						(    self				:	Optional[Any]							,   a				:	Tuple=False							,   a				:	List[Any]=False							,   a				:	Optional[Any]=False						):





            """simple docstring"""







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

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

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

            if torchify:
                  __snake_case      :    List[str]  			=[torch.from_numpy(a						) for x in image_inputs]

            return image_inputs



@require_torch
@require_vision
class      _lowercase (     lowerCAmelCase		,  unittest.TestCase						):
      _a   :     Any								=   ChineseCLIPImageProcessor if is_vision_available() else None


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            __snake_case      :    Union[str, Any]  			=ChineseCLIPImageProcessingTester(self							,   do_center_crop=a						)


      @property
      def _UpperCamelCase						(    self				:	Dict						):





            """simple docstring"""







            return self.image_processor_tester.prepare_image_processor_dict()


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.image_processing_class(**self.image_processor_dict						)
            self.assertTrue(hasattr(a							,   '''do_resize'''						)						)
            self.assertTrue(hasattr(a							,   '''size'''						)						)
            self.assertTrue(hasattr(a							,   '''do_center_crop'''						)						)
            self.assertTrue(hasattr(a							,   '''center_crop'''						)						)
            self.assertTrue(hasattr(a							,   '''do_normalize'''						)						)
            self.assertTrue(hasattr(a							,   '''image_mean'''						)						)
            self.assertTrue(hasattr(a							,   '''image_std'''						)						)
            self.assertTrue(hasattr(a							,   '''do_convert_rgb'''						)						)


      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Tuple  			=self.image_processing_class.from_dict(self.image_processor_dict						)
            self.assertEqual(image_processor.size							,   {'''height''': 2_2_4, '''width''': 2_2_4}						)
            self.assertEqual(image_processor.crop_size							,   {'''height''': 1_8, '''width''': 1_8}						)

            __snake_case      :    Optional[Any]  			=self.image_processing_class.from_dict(self.image_processor_dict							,   size=4_2							,   crop_size=8_4						)
            self.assertEqual(image_processor.size							,   {'''shortest_edge''': 4_2}						)
            self.assertEqual(image_processor.crop_size							,   {'''height''': 8_4, '''width''': 8_4}						)


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            pass


      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







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

            # Test not batched input
            __snake_case      :    Tuple  			=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.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    str  			=image_processing(a							,   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.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=self.image_processing_class(**self.image_processor_dict						)
            # create random numpy tensors
            __snake_case      :    str  			=self.image_processor_tester.prepare_inputs(equal_resolution=a							,   numpify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   np.ndarray						)

            # Test not batched input
            __snake_case      :    List[str]  			=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.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Any  			=image_processing(a							,   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.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )







      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PyTorch tensors
            __snake_case      :    Tuple  			=self.image_processor_tester.prepare_inputs(equal_resolution=a							,   torchify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   torch.Tensor						)

            # Test not batched input
            __snake_case      :    List[Any]  			=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.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[Any]  			=image_processing(a							,   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.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )



@require_torch
@require_vision
class      _lowercase (     lowerCAmelCase		,  unittest.TestCase						):
      _a   :     List[str]								=   ChineseCLIPImageProcessor if is_vision_available() else None


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=ChineseCLIPImageProcessingTester(self							,   num_channels=4							,   do_center_crop=a						)
            __snake_case      :    Optional[Any]  			=3


      @property
      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            return self.image_processor_tester.prepare_image_processor_dict()


      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    Union[str, Any]  			=self.image_processing_class(**self.image_processor_dict						)
            self.assertTrue(hasattr(a							,   '''do_resize'''						)						)
            self.assertTrue(hasattr(a							,   '''size'''						)						)
            self.assertTrue(hasattr(a							,   '''do_center_crop'''						)						)
            self.assertTrue(hasattr(a							,   '''center_crop'''						)						)
            self.assertTrue(hasattr(a							,   '''do_normalize'''						)						)
            self.assertTrue(hasattr(a							,   '''image_mean'''						)						)
            self.assertTrue(hasattr(a							,   '''image_std'''						)						)
            self.assertTrue(hasattr(a							,   '''do_convert_rgb'''						)						)


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            pass







      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PIL images
            __snake_case      :    int  			=self.image_processor_tester.prepare_inputs(equal_resolution=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   Image.Image						)

            # Test not batched input
            __snake_case      :    Union[str, Any]  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.expected_encoded_image_num_channels,
                    self.image_processor_tester.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[int]  			=image_processing(a							,   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.crop_size['''height'''],
                    self.image_processor_tester.crop_size['''width'''],
                )							,   )










"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


UpperCamelCase_	:				str										= {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]}

try:
    if not is_sentencepiece_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_	:				Union[str, Any]										= ["""LayoutXLMTokenizer"""]

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_	:				Optional[int]										= ["""LayoutXLMTokenizerFast"""]

if TYPE_CHECKING:
    from .processing_layoutxlm import LayoutXLMProcessor

    try:
        if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_layoutxlm import LayoutXLMTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast

else:
    import sys

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


