

from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
      import PIL

a_     :    Any     		=   logging.get_logger(__name__)


def 							__lowerCAmelCase     (							_UpperCamelCase		:	List[str]						)		->				List[List[ImageInput]]:
 '''simple docstring'''


 if isinstance(_UpperCamelCase		,				(list, tuple)						) and isinstance(videos[0]		,				(list, tuple)						) and is_valid_image(videos[0][0]						):
  return videos

 elif isinstance(_UpperCamelCase		,				(list, tuple)						) and is_valid_image(videos[0]						):
  return [videos]

 elif is_valid_image(_UpperCamelCase						):
  return [[videos]]

 raise ValueError(f"""Could not make batched video from {videos}"""						)




class   UpperCamelCase			(  SCREAMING_SNAKE_CASE ):
    __UpperCamelCase    =["pixel_values"]

    def __init__(      self			:   List[str]				,     snake_case__			:   bool = True				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   PILImageResampling = PILImageResampling.BILINEAR				,     snake_case__			:   bool = True				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   bool = True				,     snake_case__			:   Union[int, float] = 1 / 2_5_5				,     snake_case__			:   bool = True				,     snake_case__			:   bool = True				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     **snake_case__			:   Optional[int]				,     ):

     """simple docstring"""






     super().__init__(**snake_case__     )
     SCREAMING_SNAKE_CASE		    =      size if size is not None else {'shortest_edge': 2_5_6}
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     default_to_square=snake_case__     )
     SCREAMING_SNAKE_CASE		    =      crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     param_name='crop_size'     )

     SCREAMING_SNAKE_CASE		    =      do_resize
     SCREAMING_SNAKE_CASE		    =      size
     SCREAMING_SNAKE_CASE		    =      do_center_crop
     SCREAMING_SNAKE_CASE		    =      crop_size
     SCREAMING_SNAKE_CASE		    =      resample
     SCREAMING_SNAKE_CASE		    =      do_rescale
     SCREAMING_SNAKE_CASE		    =      rescale_factor
     SCREAMING_SNAKE_CASE		    =      offset
     SCREAMING_SNAKE_CASE		    =      do_normalize
     SCREAMING_SNAKE_CASE		    =      image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
     SCREAMING_SNAKE_CASE		    =      image_std if image_std is not None else IMAGENET_STANDARD_STD

    def UpperCamelCase      (      self			:   Any				,     snake_case__			:   np.ndarray				,     snake_case__			:   Dict[str, int]				,     snake_case__			:   PILImageResampling = PILImageResampling.BILINEAR				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   Union[str, Any]				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     default_to_square=snake_case__     )
     if "shortest_edge" in size:
      SCREAMING_SNAKE_CASE		    =      get_resize_output_image_size(snake_case__				,     size['shortest_edge']				,     default_to_square=snake_case__     )
     elif "height" in size and "width" in size:
      SCREAMING_SNAKE_CASE		    =      (size['height'], size['width'])
     else:
      raise ValueError(F"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"""     )
     return resize(snake_case__				,     size=snake_case__				,     resample=snake_case__				,     data_format=snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   Tuple				,     snake_case__			:   np.ndarray				,     snake_case__			:   Dict[str, int]				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   Optional[Any]				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__     )
     if "height" not in size or "width" not in size:
      raise ValueError(F"""Size must have 'height' and 'width' as keys. Got {size.keys()}"""     )
     return center_crop(snake_case__				,     size=(size['height'], size['width'])				,     data_format=snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   int				,     snake_case__			:   np.ndarray				,     snake_case__			:   Union[int, float]				,     snake_case__			:   bool = True				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   int				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      image.astype(np.floataa     )
     if offset:
      SCREAMING_SNAKE_CASE		    =      image - (scale / 2)
     return rescale(snake_case__				,     scale=snake_case__				,     data_format=snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   Optional[int]				,     snake_case__			:   np.ndarray				,     snake_case__			:   Union[float, List[float]]				,     snake_case__			:   Union[float, List[float]]				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   str				,     ):

     """simple docstring"""






     return normalize(snake_case__				,     mean=snake_case__				,     std=snake_case__				,     data_format=snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   Tuple				,     snake_case__			:   ImageInput				,     snake_case__			:   bool = None				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   PILImageResampling = None				,     snake_case__			:   bool = None				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   bool = None				,     snake_case__			:   float = None				,     snake_case__			:   bool = None				,     snake_case__			:   bool = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[ChannelDimension] = ChannelDimension.FIRST				,     ):

     """simple docstring"""






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

     if do_center_crop and crop_size is None:
      raise ValueError('Crop size must be specified if do_center_crop is True.'     )

     if do_rescale and rescale_factor is None:
      raise ValueError('Rescale factor must be specified if do_rescale 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.'     )

     if offset and not do_rescale:
      raise ValueError('For offset, do_rescale must also be set to True.'     )

     # All transformations expect numpy arrays.
     SCREAMING_SNAKE_CASE		    =      to_numpy_array(snake_case__     )

     if do_resize:
      SCREAMING_SNAKE_CASE		    =      self.resize(image=snake_case__				,     size=snake_case__				,     resample=snake_case__     )

     if do_center_crop:
      SCREAMING_SNAKE_CASE		    =      self.center_crop(snake_case__				,     size=snake_case__     )

     if do_rescale:
      SCREAMING_SNAKE_CASE		    =      self.rescale(image=snake_case__				,     scale=snake_case__				,     offset=snake_case__     )

     if do_normalize:
      SCREAMING_SNAKE_CASE		    =      self.normalize(image=snake_case__				,     mean=snake_case__				,     std=snake_case__     )

     SCREAMING_SNAKE_CASE		    =      to_channel_dimension_format(snake_case__				,     snake_case__     )
     return image




    def UpperCamelCase      (      self			:   int				,     snake_case__			:   ImageInput				,     snake_case__			:   bool = None				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   PILImageResampling = None				,     snake_case__			:   bool = None				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   bool = None				,     snake_case__			:   float = None				,     snake_case__			:   bool = None				,     snake_case__			:   bool = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[Union[str, TensorType]] = None				,     snake_case__			:   ChannelDimension = ChannelDimension.FIRST				,     **snake_case__			:   Dict				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      do_resize if do_resize is not None else self.do_resize
     SCREAMING_SNAKE_CASE		    =      resample if resample is not None else self.resample
     SCREAMING_SNAKE_CASE		    =      do_center_crop if do_center_crop is not None else self.do_center_crop
     SCREAMING_SNAKE_CASE		    =      do_rescale if do_rescale is not None else self.do_rescale
     SCREAMING_SNAKE_CASE		    =      rescale_factor if rescale_factor is not None else self.rescale_factor
     SCREAMING_SNAKE_CASE		    =      offset if offset is not None else self.offset
     SCREAMING_SNAKE_CASE		    =      do_normalize if do_normalize is not None else self.do_normalize
     SCREAMING_SNAKE_CASE		    =      image_mean if image_mean is not None else self.image_mean
     SCREAMING_SNAKE_CASE		    =      image_std if image_std is not None else self.image_std

     SCREAMING_SNAKE_CASE		    =      size if size is not None else self.size
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     default_to_square=snake_case__     )
     SCREAMING_SNAKE_CASE		    =      crop_size if crop_size is not None else self.crop_size
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     param_name='crop_size'     )

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

     SCREAMING_SNAKE_CASE		    =      make_batched(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      [
         [
             self._preprocess_image(
                 image=snake_case__				,     do_resize=snake_case__				,     size=snake_case__				,     resample=snake_case__				,     do_center_crop=snake_case__				,     crop_size=snake_case__				,     do_rescale=snake_case__				,     rescale_factor=snake_case__				,     offset=snake_case__				,     do_normalize=snake_case__				,     image_mean=snake_case__				,     image_std=snake_case__				,     data_format=snake_case__				,     )
             for img in video
         ]
         for video in videos
     ]

     SCREAMING_SNAKE_CASE		    =      {'pixel_values': videos}
     return BatchFeature(data=snake_case__				,     tensor_type=snake_case__     )




import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding




class   UpperCamelCase			(  SCREAMING_SNAKE_CASE ):
    __UpperCamelCase    =["image_processor", "tokenizer"]
    __UpperCamelCase    ="CLIPImageProcessor"
    __UpperCamelCase    =("CLIPTokenizer", "CLIPTokenizerFast")

    def __init__(      self			:   int				,     snake_case__			:   List[Any]=None				,     snake_case__			:   Any=None				,     **snake_case__			:   Any     ):

     """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.'				,     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__(snake_case__				,     snake_case__     )

    def __call__(      self			:   List[Any]				,     snake_case__			:   Tuple=None				,     snake_case__			:   Optional[int]=None				,     snake_case__			:   List[str]=None				,     **snake_case__			:   Dict     ):

     """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(snake_case__				,     return_tensors=snake_case__				,     **snake_case__     )

     if images is not None:
      SCREAMING_SNAKE_CASE		    =      self.image_processor(snake_case__				,     return_tensors=snake_case__				,     **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(**snake_case__     )				,     tensor_type=snake_case__     )

    def UpperCamelCase      (      self			:   Union[str, Any]				,     *snake_case__			:   Optional[Any]				,     **snake_case__			:   Optional[int]     ):

     """simple docstring"""






     return self.tokenizer.batch_decode(*snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   Dict				,     *snake_case__			:   Optional[Any]				,     **snake_case__			:   Dict     ):

     """simple docstring"""






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

    @property
    def UpperCamelCase      (      self			:   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     )     )

    @property
    def UpperCamelCase      (      self			:   int     ):

     """simple docstring"""






     warnings.warn(
         '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'				,     snake_case__				,     )
     return self.image_processor_class




    @property
    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






     warnings.warn(
         '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'				,     snake_case__				,     )
     return self.image_processor

