




'''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 TvltImageProcessor


def 					UpperCamelCase						(	snake_case						,  snake_case=10						,  snake_case=10						,  snake_case=False						,  snake_case=False ):




   '''simple docstring'''

   SCREAMING_SNAKE_CASE__      :					List[str]							    =			[]
   for i in range(image_processor_tester.num_frames ):
      video.append(np.random.randint(2_55						,  size=(image_processor_tester.num_channels, width, height)						,  dtype=np.uinta ) )

   if not numpify and not torchify:
      # PIL expects the channel dimension as last dimension
      SCREAMING_SNAKE_CASE__      :					str							    =			[Image.fromarray(np.moveaxis(snake_case						,  0						,  -1 ) ) for frame in video]

   if torchify:
      SCREAMING_SNAKE_CASE__      :					int							    =			[torch.from_numpy(snake_case ) for frame in video]

   return video


def 					UpperCamelCase						(	snake_case						,  snake_case=False						,  snake_case=False						,  snake_case=False ):




   '''simple docstring'''

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

   SCREAMING_SNAKE_CASE__      :					str							    =			[]
   for i in range(image_processor_tester.batch_size ):
      if equal_resolution:
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			image_processor_tester.max_resolution
      else:
         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					Any							    =			np.random.choice(
             np.arange(image_processor_tester.min_resolution						,  image_processor_tester.max_resolution )						,  2 )
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			prepare_video(
             image_processor_tester=snake_case						,  width=snake_case						,  height=snake_case						,  numpify=snake_case						,  torchify=snake_case						,  )
      video_inputs.append(snake_case )

   return video_inputs






class     lowercase  ( unittest.TestCase):






      def __init__(self			,	a__			,	a__=7			,	a__=3			,	a__=4			,	a__=1_8			,	a__=3_0			,	a__=4_0_0			,	a__=True			,	a__=None			,	a__=True			,	a__=[0.5, 0.5, 0.5]			,	a__=[0.5, 0.5, 0.5]			,	a__=True			,	a__=None			,	)							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE__      :					int							    =			size if size is not None else {'''shortest_edge''': 1_8}
         SCREAMING_SNAKE_CASE__      :					int							    =			crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}

         SCREAMING_SNAKE_CASE__      :					List[str]							    =			parent
         SCREAMING_SNAKE_CASE__      :					str							    =			batch_size
         SCREAMING_SNAKE_CASE__      :					str							    =			num_channels
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			num_frames
         SCREAMING_SNAKE_CASE__      :					Any							    =			image_size
         SCREAMING_SNAKE_CASE__      :					Dict							    =			min_resolution
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			max_resolution
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			do_resize
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			size
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			do_normalize
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			image_mean
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			image_std
         SCREAMING_SNAKE_CASE__      :					str							    =			do_center_crop
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			crop_size






      def  A__(self       )							-> str:
         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
             "do_center_crop": self.do_center_crop,
             "crop_size": self.crop_size,
         }






@require_torch
@require_vision
class     lowercase  ( UpperCAmelCase_					,      unittest.TestCase):
      __lowercase					=	TvltImageProcessor if is_vision_available() else None






      def  A__(self       )							-> Optional[Any]:
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			TvltImageProcessorTester(self       )






      @property
      def  A__(self       )							-> Optional[Any]:
         return self.image_processor_tester.prepare_image_processor_dict()






      def  A__(self       )							-> Any:
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			self.image_processing_class(**self.image_processor_dict       )
         self.assertTrue(hasattr(a__			,	'''image_mean'''       )       )
         self.assertTrue(hasattr(a__			,	'''image_std'''       )       )
         self.assertTrue(hasattr(a__			,	'''do_normalize'''       )       )
         self.assertTrue(hasattr(a__			,	'''do_resize'''       )       )
         self.assertTrue(hasattr(a__			,	'''do_center_crop'''       )       )
         self.assertTrue(hasattr(a__			,	'''size'''       )       )






      def  A__(self       )							-> Dict:
         # Initialize image_processor
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			self.image_processing_class(**self.image_processor_dict       )
         # create random PIL videos
         SCREAMING_SNAKE_CASE__      :					str							    =			prepare_video_inputs(self.image_processor_tester			,	equal_resolution=a__       )
         for video in video_inputs:
            self.assertIsInstance(a__			,	a__       )
            self.assertIsInstance(video[0]			,	Image.Image       )

         # Test not batched input
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			image_processor(video_inputs[0]			,	return_tensors='''pt'''       ).pixel_values
         self.assertEqual(
             encoded_videos.shape			,	(
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )			,	)

         # Test batched
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			image_processor(a__			,	return_tensors='''pt'''       ).pixel_values
         self.assertEqual(
             encoded_videos.shape			,	(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )			,	)






      def  A__(self       )							-> Any:
         # Initialize image_processor
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			self.image_processing_class(**self.image_processor_dict       )
         # create random numpy tensors
         SCREAMING_SNAKE_CASE__      :					int							    =			prepare_video_inputs(self.image_processor_tester			,	equal_resolution=a__			,	numpify=a__       )
         for video in video_inputs:
            self.assertIsInstance(a__			,	a__       )
            self.assertIsInstance(video[0]			,	np.ndarray       )

         # Test not batched input
         SCREAMING_SNAKE_CASE__      :					Any							    =			image_processor(video_inputs[0]			,	return_tensors='''pt'''       ).pixel_values
         self.assertEqual(
             encoded_videos.shape			,	(
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )			,	)

         # Test batched
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			image_processor(a__			,	return_tensors='''pt'''       ).pixel_values
         self.assertEqual(
             encoded_videos.shape			,	(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )			,	)






      def  A__(self       )							-> List[Any]:
         # Initialize image_processor
         SCREAMING_SNAKE_CASE__      :					int							    =			self.image_processing_class(**self.image_processor_dict       )
         # create random PyTorch tensors
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			prepare_video_inputs(self.image_processor_tester			,	equal_resolution=a__			,	torchify=a__       )
         for video in video_inputs:
            self.assertIsInstance(a__			,	a__       )
            self.assertIsInstance(video[0]			,	torch.Tensor       )

         # Test not batched input
         SCREAMING_SNAKE_CASE__      :					int							    =			image_processor(video_inputs[0]			,	return_tensors='''pt'''       ).pixel_values
         self.assertEqual(
             encoded_videos.shape			,	(
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )			,	)

         # Test batched
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			image_processor(a__			,	return_tensors='''pt'''       ).pixel_values
         self.assertEqual(
             encoded_videos.shape			,	(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )			,	)







'''simple docstring'''





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     lowercase  ( UpperCAmelCase_):
      __lowercase					=	['''image_processor''', '''tokenizer''']
      __lowercase					=	'''LayoutLMv2ImageProcessor'''
      __lowercase					=	('''LayoutLMv2Tokenizer''', '''LayoutLMv2TokenizerFast''')






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

         SCREAMING_SNAKE_CASE__      :					List[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__(a__			,	a__       )






      def __call__(self			,	a__			,	a__ = None			,	a__ = None			,	a__ = None			,	a__ = None			,	a__ = True			,	a__ = False			,	a__ = False			,	a__ = None			,	a__ = 0			,	a__ = None			,	a__ = None			,	a__ = None			,	a__ = False			,	a__ = False			,	a__ = False			,	a__ = False			,	a__ = True			,	a__ = None			,	**a__			,	)							-> BatchEncoding:
         # 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
         SCREAMING_SNAKE_CASE__      :					str							    =			self.image_processor(images=a__			,	return_tensors=a__       )

         # second, apply the tokenizer
         if text is not None and self.image_processor.apply_ocr and text_pair is None:
            if isinstance(a__			,	a__       ):
               SCREAMING_SNAKE_CASE__      :					Tuple							    =			[text]  # add batch dimension (as the image processor always adds a batch dimension)
            SCREAMING_SNAKE_CASE__      :					Any							    =			features['''words''']

         SCREAMING_SNAKE_CASE__      :					int							    =			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=a__			,	add_special_tokens=a__			,	padding=a__			,	truncation=a__			,	max_length=a__			,	stride=a__			,	pad_to_multiple_of=a__			,	return_token_type_ids=a__			,	return_attention_mask=a__			,	return_overflowing_tokens=a__			,	return_special_tokens_mask=a__			,	return_offsets_mapping=a__			,	return_length=a__			,	verbose=a__			,	return_tensors=a__			,	**a__			,	)

         # add pixel values
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			features.pop('''pixel_values'''       )
         if return_overflowing_tokens is True:
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			self.get_overflowing_images(a__			,	encoded_inputs['''overflow_to_sample_mapping''']       )
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			images

         return encoded_inputs






      def  A__(self			,	a__			,	a__       )							-> str:
         # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
         SCREAMING_SNAKE_CASE__      :					int							    =			[]
         for sample_idx in overflow_to_sample_mapping:
            images_with_overflow.append(images[sample_idx]       )

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

         return images_with_overflow






      def  A__(self			,	*a__			,	**a__       )							-> List[Any]:
         return self.tokenizer.batch_decode(*a__			,	**a__       )






      def  A__(self			,	*a__			,	**a__       )							-> Optional[Any]:
         return self.tokenizer.decode(*a__			,	**a__       )






      @property
      def  A__(self       )							-> Tuple:
         return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]






      @property
      def  A__(self       )							-> str:
         warnings.warn(
             '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''			,	a__			,	)
         return self.image_processor_class






      @property
      def  A__(self       )							-> Dict:
         warnings.warn(
             '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''			,	a__			,	)
         return self.image_processor

