




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    __lowerCAmelCase       (							unittest.TestCase):






       def __init__(   self:					int       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					List[str]=7       ,					_lowerCAmelCase:					List[Any]=3       ,					_lowerCAmelCase:					List[Any]=18       ,					_lowerCAmelCase:					Optional[int]=30       ,					_lowerCAmelCase:					str=4_00       ,					_lowerCAmelCase:					Tuple=True       ,					_lowerCAmelCase:					Any=None       ,					_lowerCAmelCase:					str=True       ,					_lowerCAmelCase:					Tuple=None       ,					_lowerCAmelCase:					Optional[Any]=True       ,					_lowerCAmelCase:					Union[str, Any]=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]       ,					_lowerCAmelCase:					Any=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]       ,					_lowerCAmelCase:					int=True       ,					):
         lowercase    :List[Any]      	=						size if size is not None else {"height": 2_24, "width": 2_24}
         lowercase    :Optional[Any]      	=						crop_size if crop_size is not None else {"height": 18, "width": 18}
         lowercase    :Dict      	=						parent
         lowercase    :Tuple      	=						batch_size
         lowercase    :Union[str, Any]      	=						num_channels
         lowercase    :Any      	=						image_size
         lowercase    :List[str]      	=						min_resolution
         lowercase    :Optional[int]      	=						max_resolution
         lowercase    :Any      	=						do_resize
         lowercase    :str      	=						size
         lowercase    :int      	=						do_center_crop
         lowercase    :Union[str, Any]      	=						crop_size
         lowercase    :List[Any]      	=						do_normalize
         lowercase    :Tuple      	=						image_mean
         lowercase    :Optional[int]      	=						image_std
         lowercase    :List[Any]      	=						do_convert_rgb






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]				):
         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 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					List[Any]=False       ,					_lowerCAmelCase:					Any=False       ,					_lowerCAmelCase:					Optional[int]=False				):
         assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

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

         if torchify:
           lowercase    :Any      	=						[torch.from_numpy(_lowerCAmelCase				) for x in image_inputs]

         return image_inputs







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






       def 					SCREAMING_SNAKE_CASE    (   self:					Any				):
         lowercase    :int      	=						ChineseCLIPImageProcessingTester(self       ,					do_center_crop=_lowerCAmelCase				)






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         return self.image_processor_tester.prepare_image_processor_dict()






       def 					SCREAMING_SNAKE_CASE    (   self:					str				):
         lowercase    :Any      	=						self.image_processing_class(**self.image_processor_dict				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_resize"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"size"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_center_crop"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"center_crop"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_normalize"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"image_mean"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"image_std"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_convert_rgb"				)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         lowercase    :List[str]      	=						self.image_processing_class.from_dict(self.image_processor_dict				)
         self.assertEqual(image_processor.size       ,					{"height": 2_24, "width": 2_24}				)
         self.assertEqual(image_processor.crop_size       ,					{"height": 18, "width": 18}				)

         lowercase    :Optional[Any]      	=						self.image_processing_class.from_dict(self.image_processor_dict       ,					size=42       ,					crop_size=84				)
         self.assertEqual(image_processor.size       ,					{"shortest_edge": 42}				)
         self.assertEqual(image_processor.crop_size       ,					{"height": 84, "width": 84}				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         pass






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         # Initialize image_processing
         lowercase    :Optional[Any]      	=						self.image_processing_class(**self.image_processor_dict				)
         # create random PIL images
         lowercase    :Any      	=						self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase				)
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase       ,					Image.Image				)

         # Test not batched input
         lowercase    :Dict      	=						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
         lowercase    :List[Any]      	=						image_processing(_lowerCAmelCase       ,					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 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         # Initialize image_processing
         lowercase    :str      	=						self.image_processing_class(**self.image_processor_dict				)
         # create random numpy tensors
         lowercase    :Optional[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
         lowercase    :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
         lowercase    :Optional[Any]      	=						image_processing(_lowerCAmelCase       ,					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 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         # Initialize image_processing
         lowercase    :Union[str, Any]      	=						self.image_processing_class(**self.image_processor_dict				)
         # create random PyTorch tensors
         lowercase    :Optional[Any]      	=						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
         lowercase    :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
         lowercase    :Any      	=						image_processing(_lowerCAmelCase       ,					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    __lowerCAmelCase       (							lowerCAmelCase      , unittest.TestCase):
       _a 					=						ChineseCLIPImageProcessor if is_vision_available() else None






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :Dict      	=						ChineseCLIPImageProcessingTester(self       ,					num_channels=4       ,					do_center_crop=_lowerCAmelCase				)
         lowercase    :str      	=						3






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         return self.image_processor_tester.prepare_image_processor_dict()






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         lowercase    :str      	=						self.image_processing_class(**self.image_processor_dict				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_resize"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"size"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_center_crop"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"center_crop"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_normalize"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"image_mean"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"image_std"				)				)
         self.assertTrue(hasattr(_lowerCAmelCase       ,					"do_convert_rgb"				)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         pass






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         # Initialize image_processing
         lowercase    :Any      	=						self.image_processing_class(**self.image_processor_dict				)
         # create random PIL images
         lowercase    :Optional[Any]      	=						self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase				)
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase       ,					Image.Image				)

         # Test not batched input
         lowercase    :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
         lowercase    :int      	=						image_processing(_lowerCAmelCase       ,					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"],
             )       ,					)









import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class    __lowerCAmelCase       (							lowerCAmelCase):
       _a 					=						['''image_processor''', '''tokenizer''']
       _a 					=						'''LayoutLMv2ImageProcessor'''
       _a 					=						('''LayoutXLMTokenizer''', '''LayoutXLMTokenizerFast''')






       def __init__(   self:					int       ,					_lowerCAmelCase:					Any=None       ,					_lowerCAmelCase:					List[Any]=None       ,					**_lowerCAmelCase:					Optional[Any]				):
         if "feature_extractor" in kwargs:
           warnings.warn(
               "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
               " instead."       ,					_lowerCAmelCase       ,					)
           lowercase    :Optional[Any]      	=						kwargs.pop("feature_extractor"				)

         lowercase    :Union[str, Any]      	=						image_processor if image_processor is not None else feature_extractor
         if image_processor is None:
           raise ValueError("You need to specify an `image_processor`."				)
         if tokenizer is None:
           raise ValueError("You need to specify a `tokenizer`."				)

         super().__init__(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def __call__(   self:					List[str]       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None       ,					_lowerCAmelCase:					Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None       ,					_lowerCAmelCase:					Union[List[List[int]], List[List[List[int]]]] = None       ,					_lowerCAmelCase:					Optional[Union[List[int], List[List[int]]]] = None       ,					_lowerCAmelCase:					bool = True       ,					_lowerCAmelCase:					Union[bool, str, PaddingStrategy] = False       ,					_lowerCAmelCase:					Union[bool, str, TruncationStrategy] = None       ,					_lowerCAmelCase:					Optional[int] = None       ,					_lowerCAmelCase:					int = 0       ,					_lowerCAmelCase:					Optional[int] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = True       ,					_lowerCAmelCase:					Optional[Union[str, TensorType]] = None       ,					**_lowerCAmelCase:					Tuple       ,					):
         # verify input
         if self.image_processor.apply_ocr and (boxes is not None):
           raise ValueError(
               "You cannot provide bounding boxes "
               "if you initialized the image processor with apply_ocr set to True."				)

         if self.image_processor.apply_ocr and (word_labels is not None):
           raise ValueError(
               "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."				)

         if return_overflowing_tokens is True and return_offsets_mapping is False:
           raise ValueError("You cannot return overflowing tokens without returning the offsets mapping."				)

         # first, apply the image processor
         lowercase    :int      	=						self.image_processor(images=_lowerCAmelCase       ,					return_tensors=_lowerCAmelCase				)

         # second, apply the tokenizer
         if text is not None and self.image_processor.apply_ocr and text_pair is None:
           if isinstance(_lowerCAmelCase       ,					_lowerCAmelCase				):
             lowercase    :int      	=						[text]  # add batch dimension (as the image processor always adds a batch dimension)
           lowercase    :int      	=						features["words"]

         lowercase    :Optional[Any]      	=						self.tokenizer(
             text=text if text is not None else features["words"]       ,					text_pair=text_pair if text_pair is not None else None       ,					boxes=boxes if boxes is not None else features["boxes"]       ,					word_labels=_lowerCAmelCase       ,					add_special_tokens=_lowerCAmelCase       ,					padding=_lowerCAmelCase       ,					truncation=_lowerCAmelCase       ,					max_length=_lowerCAmelCase       ,					stride=_lowerCAmelCase       ,					pad_to_multiple_of=_lowerCAmelCase       ,					return_token_type_ids=_lowerCAmelCase       ,					return_attention_mask=_lowerCAmelCase       ,					return_overflowing_tokens=_lowerCAmelCase       ,					return_special_tokens_mask=_lowerCAmelCase       ,					return_offsets_mapping=_lowerCAmelCase       ,					return_length=_lowerCAmelCase       ,					verbose=_lowerCAmelCase       ,					return_tensors=_lowerCAmelCase       ,					**_lowerCAmelCase       ,					)

         # add pixel values
         lowercase    :Any      	=						features.pop("pixel_values"				)
         if return_overflowing_tokens is True:
           lowercase    :Any      	=						self.get_overflowing_images(_lowerCAmelCase       ,					encoded_inputs["overflow_to_sample_mapping"]				)
         lowercase    :Optional[int]      	=						images

         return encoded_inputs






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					List[Any]				):
         # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
         lowercase    :Dict      	=						[]
         for sample_idx in overflow_to_sample_mapping:
           images_with_overflow.append(images[sample_idx]				)

         if len(_lowerCAmelCase				) != len(_lowerCAmelCase				):
           raise ValueError(
               "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
               F" {len(_lowerCAmelCase				)} and {len(_lowerCAmelCase				)}"				)

         return images_with_overflow






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict       ,					*_lowerCAmelCase:					str       ,					**_lowerCAmelCase:					List[Any]				):
         return self.tokenizer.batch_decode(*_lowerCAmelCase       ,					**_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str       ,					*_lowerCAmelCase:					Tuple       ,					**_lowerCAmelCase:					Union[str, Any]				):
         return self.tokenizer.decode(*_lowerCAmelCase       ,					**_lowerCAmelCase				)






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         return ["input_ids", "bbox", "attention_mask", "image"]






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         warnings.warn(
             "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."       ,					_lowerCAmelCase       ,					)
         return self.image_processor_class






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         warnings.warn(
             "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."       ,					_lowerCAmelCase       ,					)
         return self.image_processor



