


"""simple docstring"""







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


__lowercase          = logging.get_logger(__name__)


if is_vision_available():
 import PIL






class 		_A    (    lowercase_    ):







    """simple docstring"""




    UpperCAmelCase			:				Tuple      =			["""pixel_values"""]






    def __init__(   self							:       Tuple							,					__UpperCAmelCase							:       str = True							,					__UpperCAmelCase							:       Any = None							,					__UpperCAmelCase							:       Optional[int] = PILImageResampling.BILINEAR							,					__UpperCAmelCase							:       Tuple = True							,					__UpperCAmelCase							:       Optional[Any] = False							,					__UpperCAmelCase							:       Any = True							,					__UpperCAmelCase							:       Optional[int] = True							,					__UpperCAmelCase							:       int = 1 / 255							,					__UpperCAmelCase							:       str = True							,					__UpperCAmelCase							:       List[Any] = None							,					__UpperCAmelCase							:       Tuple = None							,					**__UpperCAmelCase							:       int							,					):
      super().__init__(**a__)

      a		:						Any 				=     size if size is not None else {"height": 2560, "width": 1920}
      if isinstance(a__							,					(tuple, list)):
        # The previous feature extractor size parameter was in (width, height) format
        a		:						str 				=     size[::-1]
      a		:						Tuple 				=     get_size_dict(a__)

      a		:						Optional[Any] 				=     do_resize
      a		:						List[Any] 				=     size
      a		:						List[Any] 				=     resample
      a		:						str 				=     do_thumbnail
      a		:						Optional[int] 				=     do_align_long_axis
      a		:						int 				=     do_pad
      a		:						Optional[int] 				=     do_rescale
      a		:						Union[str, Any] 				=     rescale_factor
      a		:						int 				=     do_normalize
      a		:						List[str] 				=     image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
      a		:						Union[str, Any] 				=     image_std if image_std is not None else IMAGENET_STANDARD_STD






    def       __snake_case				(   self							:       Tuple							,					__UpperCAmelCase							:       int							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       Tuple = None):
      a				,						a		:						List[str] 				=     get_image_size(a__)
      a				,						a		:						Optional[Any] 				=     size["height"], size["width"]

      if (output_width < output_height and input_width > input_height) or (
          output_width > output_height and input_width < input_height
      ):
        a		:						Union[str, Any] 				=     np.rotaa(a__							,					3)

      if data_format is not None:
        a		:						int 				=     to_channel_dimension_format(a__							,					a__)

      return image






    def       __snake_case				(   self							:       Tuple							,					__UpperCAmelCase							:       Any							,					__UpperCAmelCase							:       Optional[int]							,					__UpperCAmelCase							:       str = False							,					__UpperCAmelCase							:       Optional[Any] = None							,					):
      a				,						a		:						Optional[int] 				=     size["height"], size["width"]
      a				,						a		:						str 				=     get_image_size(a__)

      a		:						List[str] 				=     output_width - input_width
      a		:						str 				=     output_height - input_height

      if random_padding:
        a		:						Tuple 				=     np.random.randint(low=0							,					high=delta_height + 1)
        a		:						Union[str, Any] 				=     np.random.randint(low=0							,					high=delta_width + 1)
      else:
        a		:						str 				=     delta_height // 2
        a		:						Union[str, Any] 				=     delta_width // 2

      a		:						Optional[int] 				=     delta_height - pad_top
      a		:						Optional[int] 				=     delta_width - pad_left

      a		:						Any 				=     ((pad_top, pad_bottom), (pad_left, pad_right))
      return pad(a__							,					a__							,					data_format=a__)






    def       __snake_case				(   self							:       Optional[Any]							,					*__UpperCAmelCase							:       Optional[int]							,					**__UpperCAmelCase							:       int):
      logger.info("pad is deprecated and will be removed in version 4.27. Please use pad_image instead.")
      return self.pad_image(*a__							,					**a__)






    def       __snake_case				(   self							:       Tuple							,					__UpperCAmelCase							:       List[Any]							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Any = PILImageResampling.BICUBIC							,					__UpperCAmelCase							:       int = None							,					**__UpperCAmelCase							:       Dict							,					):
      a				,						a		:						Dict 				=     get_image_size(a__)
      a				,						a		:						Optional[int] 				=     size["height"], size["width"]

      # We always resize to the smallest of either the input or output size.
      a		:						List[Any] 				=     min(a__							,					a__)
      a		:						List[Any] 				=     min(a__							,					a__)

      if height == input_height and width == input_width:
        return image

      if input_height > input_width:
        a		:						List[Any] 				=     int(input_width * height / input_height)
      elif input_width > input_height:
        a		:						str 				=     int(input_height * width / input_width)

      return resize(
          a__							,					size=(height, width)							,					resample=a__							,					reducing_gap=2.0							,					data_format=a__							,					**a__)






    def       __snake_case				(   self							:       List[str]							,					__UpperCAmelCase							:       List[str]							,					__UpperCAmelCase							:       Union[str, Any]							,					__UpperCAmelCase							:       Union[str, Any] = PILImageResampling.BICUBIC							,					__UpperCAmelCase							:       Tuple = None							,					**__UpperCAmelCase							:       List[Any]							,					):
      a		:						Optional[int] 				=     get_size_dict(a__)
      a		:						Any 				=     min(size["height"]							,					size["width"])
      a		:						Dict 				=     get_resize_output_image_size(a__							,					size=a__							,					default_to_square=a__)
      a		:						int 				=     resize(a__							,					size=a__							,					resample=a__							,					data_format=a__							,					**a__)
      return resized_image






    def       __snake_case				(   self							:       Optional[int]							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       Dict							,					__UpperCAmelCase							:       List[str] = None							,					**__UpperCAmelCase							:       Optional[Any]							,					):
      return rescale(a__							,					scale=a__							,					data_format=a__							,					**a__)






    def       __snake_case				(   self							:       int							,					__UpperCAmelCase							:       List[Any]							,					__UpperCAmelCase							:       Any							,					__UpperCAmelCase							:       int							,					__UpperCAmelCase							:       Tuple = None							,					**__UpperCAmelCase							:       Dict							,					):
      return normalize(a__							,					mean=a__							,					std=a__							,					data_format=a__							,					**a__)






    def       __snake_case				(   self							:       List[Any]							,					__UpperCAmelCase							:       int							,					__UpperCAmelCase							:       Any = None							,					__UpperCAmelCase							:       List[str] = None							,					__UpperCAmelCase							:       List[Any] = None							,					__UpperCAmelCase							:       Union[str, Any] = None							,					__UpperCAmelCase							:       Any = None							,					__UpperCAmelCase							:       str = None							,					__UpperCAmelCase							:       Optional[int] = False							,					__UpperCAmelCase							:       Dict = None							,					__UpperCAmelCase							:       Any = None							,					__UpperCAmelCase							:       Tuple = None							,					__UpperCAmelCase							:       Tuple = None							,					__UpperCAmelCase							:       Dict = None							,					__UpperCAmelCase							:       List[str] = None							,					__UpperCAmelCase							:       List[Any] = ChannelDimension.FIRST							,					**__UpperCAmelCase							:       int							,					):
      a		:						List[Any] 				=     do_resize if do_resize is not None else self.do_resize
      a		:						str 				=     size if size is not None else self.size
      if isinstance(a__							,					(tuple, list)):
        # Previous feature extractor had size in (width, height) format
        a		:						Union[str, Any] 				=     size[::-1]
      a		:						Union[str, Any] 				=     get_size_dict(a__)
      a		:						List[str] 				=     resample if resample is not None else self.resample
      a		:						str 				=     do_thumbnail if do_thumbnail is not None else self.do_thumbnail
      a		:						List[str] 				=     do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
      a		:						Optional[Any] 				=     do_pad if do_pad is not None else self.do_pad
      a		:						List[str] 				=     do_rescale if do_rescale is not None else self.do_rescale
      a		:						Optional[int] 				=     rescale_factor if rescale_factor is not None else self.rescale_factor
      a		:						Union[str, Any] 				=     do_normalize if do_normalize is not None else self.do_normalize
      a		:						Any 				=     image_mean if image_mean is not None else self.image_mean
      a		:						Union[str, Any] 				=     image_std if image_std is not None else self.image_std

      a		:						Optional[int] 				=     make_list_of_images(a__)

      if not valid_images(a__):
        raise ValueError(
            "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
            "torch.Tensor, tf.Tensor or jax.ndarray.")

      if do_resize and size is None:
        raise ValueError("Size must be specified if do_resize is True.")

      if do_rescale and rescale_factor is None:
        raise ValueError("Rescale factor must be specified if do_rescale is True.")

      if do_pad and size is None:
        raise ValueError("Size must be specified if do_pad is True.")

      if do_normalize and (image_mean is None or image_std is None):
        raise ValueError("Image mean and std must be specified if do_normalize is True.")

      # All transformations expect numpy arrays.
      a		:						List[str] 				=     [to_numpy_array(a__) for image in images]

      if do_align_long_axis:
        a		:						Dict 				=     [self.align_long_axis(a__							,					size=a__) for image in images]

      if do_resize:
        a		:						Union[str, Any] 				=     [self.resize(image=a__							,					size=a__							,					resample=a__) for image in images]

      if do_thumbnail:
        a		:						Dict 				=     [self.thumbnail(image=a__							,					size=a__) for image in images]

      if do_pad:
        a		:						int 				=     [self.pad_image(image=a__							,					size=a__							,					random_padding=a__) for image in images]

      if do_rescale:
        a		:						List[Any] 				=     [self.rescale(image=a__							,					scale=a__) for image in images]

      if do_normalize:
        a		:						Any 				=     [self.normalize(image=a__							,					mean=a__							,					std=a__) for image in images]

      a		:						Dict 				=     [to_channel_dimension_format(a__							,					a__) for image in images]

      a		:						str 				=     {"pixel_values": images}
      return BatchFeature(data=a__							,					tensor_type=a__)








import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class 		__magic_name__				( lowercase_					):


  """simple docstring"""





  _UpperCamelCase						 = ["image_processor", "tokenizer"]
  _UpperCamelCase						 = "AutoImageProcessor"
  _UpperCamelCase						 = "AutoTokenizer"


  def __init__(					self	,		a__=None	,		a__=None	,		**a__		):
    _lowerCamelCase      =						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__	,		)
      _lowerCamelCase      =						kwargs.pop('''feature_extractor'''		)

    _lowerCamelCase      =						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__		)
    _lowerCamelCase      =						self.image_processor
    _lowerCamelCase      =						False


  def __call__(					self	,		*a__	,		**a__		):
    # For backward compatibility
    if self._in_target_context_manager:
      return self.current_processor(*a__	,		**a__		)

    _lowerCamelCase      =						kwargs.pop('''images'''	,		a__		)
    _lowerCamelCase      =						kwargs.pop('''text'''	,		a__		)
    if len(a__		) > 0:
      _lowerCamelCase      =						args[0]
      _lowerCamelCase      =						args[1:]

    if images is None and text is None:
      raise ValueError('''You need to specify either an `images` or `text` input to process.'''		)

    if images is not None:
      _lowerCamelCase      =						self.image_processor(a__	,		*a__	,		**a__		)
    if text is not None:
      _lowerCamelCase      =						self.tokenizer(a__	,		**a__		)

    if text is None:
      return inputs
    elif images is None:
      return encodings
    else:
      _lowerCamelCase      =						encodings['''input_ids''']
      return inputs


  def     _UpperCAmelCase  (					self	,		*a__	,		**a__		):
    return self.tokenizer.batch_decode(*a__	,		**a__		)


  def     _UpperCAmelCase  (					self	,		*a__	,		**a__		):
    return self.tokenizer.decode(*a__	,		**a__		)


  @contextmanager
  def     _UpperCAmelCase  (					self		):
    warnings.warn(
        '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
        '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
        '''your images inputs, or in a separate call.'''		)
    _lowerCamelCase      =						True
    _lowerCamelCase      =						self.tokenizer
    yield
    _lowerCamelCase      =						self.image_processor
    _lowerCamelCase      =						False


  @property
  def     _UpperCAmelCase  (					self		):
    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     _UpperCAmelCase  (					self		):
    warnings.warn(
        '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''	,		a__	,		)
    return self.image_processor




