





"""simple docstring"""


import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


lowerCamelCase__						= False
try:
  lowerCamelCase__						= _is_package_available("google.colab")
except ModuleNotFoundError:
  pass



@input.register
class   __SCREAMING_SNAKE_CASE     :







  '''simple docstring'''


  def __init__(		self :							Union[str, Any]       , __a :							str = None       , __a :							list = []						)		->					Union[str, Any]:
        _UpperCamelCase   :						Any    =							0
        _UpperCamelCase   :						Any    =							choices
        _UpperCamelCase   :						Tuple    =							prompt
        if sys.platform == "win32":
              _UpperCamelCase   :						str    =							"*"
        else:
              _UpperCamelCase   :						Dict    =							"➔ "

  def  __SCREAMING_SNAKE_CASE					(		self :							int       , __a :							Tuple       , __a :							str = ""						)		->					Dict:
        if sys.platform != "win32":
              writeColor(self.choices[index]       , 32       , __a						)
        else:
              forceWrite(self.choices[index]       , __a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple       , __a :							int						)		->					Dict:
        if index == self.position:
              forceWrite(F''' {self.arrow_char} '''						)
              self.write_choice(__a						)
        else:
              forceWrite(F'''    {self.choices[index]}'''						)
        reset_cursor()

  def  __SCREAMING_SNAKE_CASE					(		self :							List[str]       , __a :							Direction       , __a :							int = 1						)		->					List[str]:
        _UpperCamelCase   :						Tuple    =							self.position
        if direction == Direction.DOWN:
              if self.position + 1 >= len(self.choices						):
                    return
              self.position += num_spaces
        else:
              if self.position - 1 < 0:
                    return
              self.position -= num_spaces
        clear_line()
        self.print_choice(__a						)
        move_cursor(__a       , direction.name						)
        self.print_choice(self.position						)

  @input.mark(KEYMAP["up"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					Any:
        self.move_direction(Direction.UP						)

  @input.mark(KEYMAP["down"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					List[Any]:
        self.move_direction(Direction.DOWN						)

  @input.mark(KEYMAP["newline"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					str:
        move_cursor(len(self.choices						) - self.position       , "DOWN"						)
        return self.position

  @input.mark(KEYMAP["interrupt"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					Tuple:
        move_cursor(len(self.choices						) - self.position       , "DOWN"						)
        raise KeyboardInterrupt

  @input.mark_multiple(*[KEYMAP[str(__a						)] for number in range(10						)]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]						)		->					List[str]:
        _UpperCamelCase   :						Union[str, Any]    =							int(chr(self.current_selection						)						)
        _UpperCamelCase   :						List[str]    =							index - self.position
        if index == self.position:
              return
        if index < len(self.choices						):
              if self.position > index:
                    self.move_direction(Direction.UP       , -movement						)
              elif self.position < index:
                    self.move_direction(Direction.DOWN       , __a						)
              else:
                    return
        else:
              return


  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]       , __a :							int = 0						)		->					List[Any]:
        if self.prompt:
              linebreak()
              forceWrite(self.prompt       , "\n"						)
              if in_colab:
                    forceWrite("Please input a choice index (starting from 0), and press enter"       , "\n"						)
              else:
                    forceWrite("Please select a choice using the arrow or number keys, and selecting with enter"       , "\n"						)
        _UpperCamelCase   :						int    =							default_choice
        for i in range(len(self.choices						)						):
              self.print_choice(__a						)
              forceWrite("\n"						)
        move_cursor(len(self.choices						) - self.position       , "UP"						)
        with cursor.hide():
              while True:
                    if in_colab:
                          try:
                                _UpperCamelCase   :						Union[str, Any]    =							int(builtins.input()						)
                          except ValueError:
                                _UpperCamelCase   :						int    =							default_choice
                    else:
                          _UpperCamelCase   :						Any    =							self.handle_input()
                    if choice is not None:
                          reset_cursor()
                          for _ in range(len(self.choices						) + 1						):
                                move_cursor(1       , "UP"						)
                                clear_line()
                          self.write_choice(__a       , "\n"						)
                          return choice







"""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 (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


lowerCamelCase__						= logging.get_logger(__name__)


if is_vision_available():
  import PIL



class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:Optional[int]						  =       ["pixel_values"]

  def __init__(		self :							Tuple       , __a :							bool = True       , __a :							Dict[str, int] = None       , __a :							PILImageResampling = PILImageResampling.BICUBIC       , __a :							bool = True       , __a :							Dict[str, int] = None       , __a :							bool = True       , __a :							Union[int, float] = 1 / 255       , __a :							bool = True       , __a :							Optional[Union[float, List[float]]] = None       , __a :							Optional[Union[float, List[float]]] = None       , __a :							bool = True       , **__a :							List[Any]       , )		->					None:
        super().__init__(**__a						)
        _UpperCamelCase   :						Optional[int]    =							size if size is not None else {"shortest_edge": 224}
        _UpperCamelCase   :						Optional[int]    =							get_size_dict(__a       , default_to_square=__a						)
        _UpperCamelCase   :						Union[str, Any]    =							crop_size if crop_size is not None else {"height": 224, "width": 224}
        _UpperCamelCase   :						Tuple    =							get_size_dict(__a       , default_to_square=__a       , param_name="crop_size"						)

        _UpperCamelCase   :						Optional[Any]    =							do_resize
        _UpperCamelCase   :						Dict    =							size
        _UpperCamelCase   :						Any    =							resample
        _UpperCamelCase   :						Tuple    =							do_center_crop
        _UpperCamelCase   :						str    =							crop_size
        _UpperCamelCase   :						Any    =							do_rescale
        _UpperCamelCase   :						Dict    =							rescale_factor
        _UpperCamelCase   :						int    =							do_normalize
        _UpperCamelCase   :						Dict    =							image_mean if image_mean is not None else OPENAI_CLIP_MEAN
        _UpperCamelCase   :						Tuple    =							image_std if image_std is not None else OPENAI_CLIP_STD
        _UpperCamelCase   :						List[Any]    =							do_convert_rgb

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]       , __a :							np.ndarray       , __a :							Dict[str, int]       , __a :							PILImageResampling = PILImageResampling.BICUBIC       , __a :							Optional[Union[str, ChannelDimension]] = None       , **__a :							str       , )		->					np.ndarray:
        _UpperCamelCase   :						Any    =							get_size_dict(__a       , default_to_square=__a						)
        if "shortest_edge" not in size:
              raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''						)
        _UpperCamelCase   :						Dict    =							get_resize_output_image_size(__a       , size=size["shortest_edge"]       , default_to_square=__a						)
        return resize(__a       , size=__a       , resample=__a       , data_format=__a       , **__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]       , __a :							np.ndarray       , __a :							Dict[str, int]       , __a :							Optional[Union[str, ChannelDimension]] = None       , **__a :							Tuple       , )		->					np.ndarray:
        _UpperCamelCase   :						List[str]    =							get_size_dict(__a						)
        if "height" not in size or "width" not in size:
              raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''						)
        return center_crop(__a       , size=(size["height"], size["width"])       , data_format=__a       , **__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]       , __a :							np.ndarray       , __a :							Union[int, float]       , __a :							Optional[Union[str, ChannelDimension]] = None       , **__a :							Optional[Any]       , )		->					List[str]:
        return rescale(__a       , scale=__a       , data_format=__a       , **__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple       , __a :							np.ndarray       , __a :							Union[float, List[float]]       , __a :							Union[float, List[float]]       , __a :							Optional[Union[str, ChannelDimension]] = None       , **__a :							Tuple       , )		->					np.ndarray:
        return normalize(__a       , mean=__a       , std=__a       , data_format=__a       , **__a						)


  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]       , __a :							ImageInput       , __a :							bool = None       , __a :							Dict[str, int] = None       , __a :							PILImageResampling = None       , __a :							bool = None       , __a :							int = None       , __a :							bool = None       , __a :							float = None       , __a :							bool = None       , __a :							Optional[Union[float, List[float]]] = None       , __a :							Optional[Union[float, List[float]]] = None       , __a :							bool = None       , __a :							Optional[Union[str, TensorType]] = None       , __a :							Optional[ChannelDimension] = ChannelDimension.FIRST       , **__a :							Union[str, Any]       , )		->					PIL.Image.Image:
        _UpperCamelCase   :						Tuple    =							do_resize if do_resize is not None else self.do_resize
        _UpperCamelCase   :						int    =							size if size is not None else self.size
        _UpperCamelCase   :						int    =							get_size_dict(__a       , param_name="size"       , default_to_square=__a						)
        _UpperCamelCase   :						List[Any]    =							resample if resample is not None else self.resample
        _UpperCamelCase   :						List[Any]    =							do_center_crop if do_center_crop is not None else self.do_center_crop
        _UpperCamelCase   :						Union[str, Any]    =							crop_size if crop_size is not None else self.crop_size
        _UpperCamelCase   :						List[Any]    =							get_size_dict(__a       , param_name="crop_size"       , default_to_square=__a						)
        _UpperCamelCase   :						Dict    =							do_rescale if do_rescale is not None else self.do_rescale
        _UpperCamelCase   :						Union[str, Any]    =							rescale_factor if rescale_factor is not None else self.rescale_factor
        _UpperCamelCase   :						Union[str, Any]    =							do_normalize if do_normalize is not None else self.do_normalize
        _UpperCamelCase   :						List[Any]    =							image_mean if image_mean is not None else self.image_mean
        _UpperCamelCase   :						List[Any]    =							image_std if image_std is not None else self.image_std
        _UpperCamelCase   :						List[str]    =							do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

        _UpperCamelCase   :						Any    =							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_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."						)

        # PIL RGBA images are converted to RGB
        if do_convert_rgb:
              _UpperCamelCase   :						int    =							[convert_to_rgb(__a						) for image in images]

        # All transformations expect numpy arrays.
        _UpperCamelCase   :						List[Any]    =							[to_numpy_array(__a						) for image in images]

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

        if do_center_crop:
              _UpperCamelCase   :						Optional[Any]    =							[self.center_crop(image=__a       , size=__a						) for image in images]

        if do_rescale:
              _UpperCamelCase   :						Dict    =							[self.rescale(image=__a       , scale=__a						) for image in images]

        if do_normalize:
              _UpperCamelCase   :						List[Any]    =							[self.normalize(image=__a       , mean=__a       , std=__a						) for image in images]

        _UpperCamelCase   :						List[Any]    =							[to_channel_dimension_format(__a       , __a						) for image in images]

        _UpperCamelCase   :						str    =							{"pixel_values": images}
        return BatchFeature(data=__a       , tensor_type=__a						)
