



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_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor
class    __UpperCamelCase			(       unittest.TestCase				):

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=1_3		,				SCREAMING_SNAKE_CASE=3		,				SCREAMING_SNAKE_CASE=2_2_4		,				SCREAMING_SNAKE_CASE=3_0		,				SCREAMING_SNAKE_CASE=4_0_0		,				SCREAMING_SNAKE_CASE=True		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=True		,				SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]		,				SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]		,				) ->			List[Any]:
  a__												= size if size is not None else {'''height''': 1_8, '''width''': 1_8}
  a__												= parent
  a__												= batch_size
  a__												= num_channels
  a__												= image_size
  a__												= min_resolution
  a__												= max_resolution
  a__												= do_resize
  a__												= size
  a__												= do_normalize
  a__												= image_mean
  a__												= image_std



 def 						_UpperCAmelCase       (	self      ) ->			List[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,
  }





@require_torch
@require_vision
class    __UpperCamelCase			(       _lowercase	,	unittest.TestCase				):

 """simple docstring"""



 _lowercase   :    Any      						=					ViTImageProcessor if is_vision_available() else None

 def 						_UpperCAmelCase       (	self      ) ->			Optional[Any]:
  a__												= EfficientFormerImageProcessorTester(self      )

 @property
 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  return self.image_proc_tester.prepare_image_processor_dict()

 def 						_UpperCAmelCase       (	self      ) ->			str:
  a__												= 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		,				'''size'''      )      )

 def 						_UpperCAmelCase       (	self      ) ->			Any:
  pass

 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  # Initialize image_processor
  a__												= self.image_processing_class(**self.image_processor_dict      )
  # create random PIL images
  a__												= prepare_image_inputs(self.image_proc_tester		,				equal_resolution=SCREAMING_SNAKE_CASE      )
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE		,				Image.Image      )

  # Test not batched input
  a__												= image_processor(image_inputs[0]		,				return_tensors='''pt'''      ).pixel_values
  self.assertEqual(
      encoded_images.shape		,				(
          1,
          self.image_proc_tester.num_channels,
          self.image_proc_tester.size['''height'''],
          self.image_proc_tester.size['''width'''],
      )		,				)

  # Test batched
  a__												= image_processor(SCREAMING_SNAKE_CASE		,				return_tensors='''pt'''      ).pixel_values
  self.assertEqual(
      encoded_images.shape		,				(
          self.image_proc_tester.batch_size,
          self.image_proc_tester.num_channels,
          self.image_proc_tester.size['''height'''],
          self.image_proc_tester.size['''width'''],
      )		,				)

 def 						_UpperCAmelCase       (	self      ) ->			Any:
  # Initialize image_processor
  a__												= self.image_processing_class(**self.image_processor_dict      )
  # create random numpy tensors
  a__												= prepare_image_inputs(self.image_proc_tester		,				equal_resolution=SCREAMING_SNAKE_CASE		,				numpify=SCREAMING_SNAKE_CASE      )
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE		,				np.ndarray      )

  # Test not batched input
  a__												= image_processor(image_inputs[0]		,				return_tensors='''pt'''      ).pixel_values
  self.assertEqual(
      encoded_images.shape		,				(
          1,
          self.image_proc_tester.num_channels,
          self.image_proc_tester.size['''height'''],
          self.image_proc_tester.size['''width'''],
      )		,				)

  # Test batched
  a__												= image_processor(SCREAMING_SNAKE_CASE		,				return_tensors='''pt'''      ).pixel_values
  self.assertEqual(
      encoded_images.shape		,				(
          self.image_proc_tester.batch_size,
          self.image_proc_tester.num_channels,
          self.image_proc_tester.size['''height'''],
          self.image_proc_tester.size['''width'''],
      )		,				)



 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  # Initialize image_processor
  a__												= self.image_processing_class(**self.image_processor_dict      )
  # create random PyTorch tensors
  a__												= prepare_image_inputs(self.image_proc_tester		,				equal_resolution=SCREAMING_SNAKE_CASE		,				torchify=SCREAMING_SNAKE_CASE      )
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE		,				torch.Tensor      )

  # Test not batched input
  a__												= image_processor(image_inputs[0]		,				return_tensors='''pt'''      ).pixel_values
  self.assertEqual(
      encoded_images.shape		,				(
          1,
          self.image_proc_tester.num_channels,
          self.image_proc_tester.size['''height'''],
          self.image_proc_tester.size['''width'''],
      )		,				)

  # Test batched
  a__												= image_processor(SCREAMING_SNAKE_CASE		,				return_tensors='''pt'''      ).pixel_values
  self.assertEqual(
      encoded_images.shape		,				(
          self.image_proc_tester.batch_size,
          self.image_proc_tester.num_channels,
          self.image_proc_tester.size['''height'''],
          self.image_proc_tester.size['''width'''],
      )		,				)










import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    Optional[Any]      						=					['''image_processor''', '''tokenizer''']
 _lowercase   :    Optional[int]      						=					'''ChineseCLIPImageProcessor'''
 _lowercase   :    List[Any]      						=					('''BertTokenizer''', '''BertTokenizerFast''')

 def __init__(	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE      ) ->			Union[str, Any]:
  a__												= 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		,				)
   a__												= kwargs.pop('''feature_extractor'''      )

  a__												= 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      )
  a__												= self.image_processor

 def __call__(	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:

  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:
   a__												= self.tokenizer(SCREAMING_SNAKE_CASE		,				return_tensors=SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

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

  if text is not None and images is not None:
   a__												= 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		,				*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:
  return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			int:
  return self.tokenizer.decode(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

 @property
 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  a__												= self.tokenizer.model_input_names
  a__												= self.image_processor.model_input_names
  return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names      )      )



 @property
 def 						_UpperCAmelCase       (	self      ) ->			Optional[int]:
  warnings.warn(
      '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''		,				SCREAMING_SNAKE_CASE		,				)
  return self.image_processor_class





