

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


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import VivitImageProcessor







class 		lowerCAmelCase__  ( unittest.TestCase   ):



 """simple docstring"""



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


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = size if size is not None else {"""shortest_edge""": 18}
      __SCREAMING_SNAKE_CASE      = crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

      __SCREAMING_SNAKE_CASE      = parent
      __SCREAMING_SNAKE_CASE      = batch_size
      __SCREAMING_SNAKE_CASE      = num_channels
      __SCREAMING_SNAKE_CASE      = num_frames
      __SCREAMING_SNAKE_CASE      = image_size
      __SCREAMING_SNAKE_CASE      = min_resolution
      __SCREAMING_SNAKE_CASE      = max_resolution
      __SCREAMING_SNAKE_CASE      = do_resize
      __SCREAMING_SNAKE_CASE      = size
      __SCREAMING_SNAKE_CASE      = do_normalize
      __SCREAMING_SNAKE_CASE      = image_mean
      __SCREAMING_SNAKE_CASE      = image_std
      __SCREAMING_SNAKE_CASE      = crop_size


 def  UpperCAmelCase__						(						self   :			Dict     )    ->					str:


      """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,
          "crop_size": self.crop_size,
      }







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



 """simple docstring"""



 lowerCAmelCase__       = VivitImageProcessor if is_vision_available() else None
 def  UpperCAmelCase__						(						self   :			str     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = VivitImageProcessingTester(self     )
 @property
 def  UpperCAmelCase__						(						self   :			Dict     )    ->					Dict:


      """simple docstring"""




      return self.image_processor_tester.prepare_image_processor_dict()
 def  UpperCAmelCase__						(						self   :			int     )    ->					Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.image_processing_class(**self.image_processor_dict     )
      self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,				"""image_mean"""     )     )
      self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,				"""image_std"""     )     )
      self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,				"""do_normalize"""     )     )
      self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,				"""do_resize"""     )     )
      self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,				"""do_center_crop"""     )     )
      self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,				"""size"""     )     )
 def  UpperCAmelCase__						(						self   :			Dict     )    ->					Any:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.image_processing_class.from_dict(self.image_processor_dict     )
      self.assertEqual(image_processor.size   ,				{"""shortest_edge""": 18}     )
      self.assertEqual(image_processor.crop_size   ,				{"""height""": 18, """width""": 18}     )

      __SCREAMING_SNAKE_CASE      = 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  UpperCAmelCase__						(						self   :			List[Any]     )    ->					List[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.image_processing_class(**self.image_processor_dict     )
      # create random PIL videos
      __SCREAMING_SNAKE_CASE      = prepare_video_inputs(self.image_processor_tester   ,				equal_resolution=__SCREAMING_SNAKE_CASE     )
      for video in video_inputs:
           self.assertIsInstance(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
           self.assertIsInstance(video[0]   ,				Image.Image     )

      # Test not batched input
      __SCREAMING_SNAKE_CASE      = image_processing(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      = image_processing(__SCREAMING_SNAKE_CASE   ,				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  UpperCAmelCase__						(						self   :			Optional[int]     )    ->					Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.image_processing_class(**self.image_processor_dict     )
      # create random numpy tensors
      __SCREAMING_SNAKE_CASE      = prepare_video_inputs(self.image_processor_tester   ,				equal_resolution=__SCREAMING_SNAKE_CASE   ,				numpify=__SCREAMING_SNAKE_CASE     )
      for video in video_inputs:
           self.assertIsInstance(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
           self.assertIsInstance(video[0]   ,				np.ndarray     )

      # Test not batched input
      __SCREAMING_SNAKE_CASE      = image_processing(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      = image_processing(__SCREAMING_SNAKE_CASE   ,				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  UpperCAmelCase__						(						self   :			Tuple     )    ->					Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.image_processing_class(**self.image_processor_dict     )
      # create random PyTorch tensors
      __SCREAMING_SNAKE_CASE      = prepare_video_inputs(self.image_processor_tester   ,				equal_resolution=__SCREAMING_SNAKE_CASE   ,				torchify=__SCREAMING_SNAKE_CASE     )
      for video in video_inputs:
           self.assertIsInstance(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
           self.assertIsInstance(video[0]   ,				torch.Tensor     )

      # Test not batched input
      __SCREAMING_SNAKE_CASE      = image_processing(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      = image_processing(__SCREAMING_SNAKE_CASE   ,				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 ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class 		lowerCAmelCase__  ( a   ):



 """simple docstring"""



 lowerCAmelCase__       = ["image_processor", "tokenizer"]
 lowerCAmelCase__       = "CLIPImageProcessor"
 lowerCAmelCase__       = ("XLMRobertaTokenizer", "XLMRobertaTokenizerFast")
 def __init__(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			Any=None   ,				__SCREAMING_SNAKE_CASE   :			str=None   ,				**__SCREAMING_SNAKE_CASE   :			Tuple     )    ->					Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = None
      if "feature_extractor" in kwargs:
           warnings.warn(
               """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
               """ instead."""   ,				__SCREAMING_SNAKE_CASE   ,				)
           __SCREAMING_SNAKE_CASE      = kwargs.pop("""feature_extractor"""     )

      __SCREAMING_SNAKE_CASE      = 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__(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
 def __call__(						self   :			Union[str, Any]   ,				__SCREAMING_SNAKE_CASE   :			Union[str, Any]=None   ,				__SCREAMING_SNAKE_CASE   :			Any=None   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]=None   ,				**__SCREAMING_SNAKE_CASE   :			str     )    ->					int:


      """simple docstring"""




      if text is None and images is None:
           raise ValueError("""You have to specify either text or images. Both cannot be none."""     )

      if text is not None:
           __SCREAMING_SNAKE_CASE      = self.tokenizer(__SCREAMING_SNAKE_CASE   ,				return_tensors=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )

      if images is not None:
           __SCREAMING_SNAKE_CASE      = self.image_processor(__SCREAMING_SNAKE_CASE   ,				return_tensors=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )

      if text is not None and images is not None:
           __SCREAMING_SNAKE_CASE      = image_features.pixel_values
           return encoding
      elif text is not None:
           return encoding
      else:
           return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE     )   ,				tensor_type=__SCREAMING_SNAKE_CASE     )
 def  UpperCAmelCase__						(						self   :			List[str]   ,				*__SCREAMING_SNAKE_CASE   :			Tuple   ,				**__SCREAMING_SNAKE_CASE   :			List[Any]     )    ->					Optional[int]:


      """simple docstring"""




      return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )
 def  UpperCAmelCase__						(						self   :			List[Any]   ,				*__SCREAMING_SNAKE_CASE   :			Union[str, Any]   ,				**__SCREAMING_SNAKE_CASE   :			str     )    ->					Optional[Any]:


      """simple docstring"""




      return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )


 @property
 def  UpperCAmelCase__						(						self   :			List[Any]     )    ->					List[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.tokenizer.model_input_names
      __SCREAMING_SNAKE_CASE      = self.image_processor.model_input_names
      return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )






