





import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


UpperCAmelCase_           =	threading.Lock()
UpperCAmelCase_           =	None

UpperCAmelCase_           =	{
    "debug": logging.DEBUG,
    "info": logging.INFO,
    "warning": logging.WARNING,
    "error": logging.ERROR,
    "critical": logging.CRITICAL,
}

UpperCAmelCase_           =	logging.WARNING

UpperCAmelCase_           =	True
def      A__       (    )  ->  Tuple:






   """simple docstring"""







   _UpperCAmelCase           =   os.getenv('''TRANSFORMERS_VERBOSITY'''				,	SCREAMING_SNAKE_CASE_						)
   if env_level_str:
      if env_level_str in log_levels:
         return log_levels[env_level_str]
      else:
         logging.getLogger().warning(
             F'''Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, '''
             F'''has to be one of: { ', '.join(log_levels.keys()						) }'''						)
   return _default_log_level
def      A__       (    )  ->  str:






   """simple docstring"""







   return __name__.split('''.'''						)[0]
def      A__       (    )  ->  logging.Logger:






   """simple docstring"""







   return logging.getLogger(_get_library_name()						)
def      A__       (    )  ->  None:






   """simple docstring"""







   global _default_handler

   with _lock:
      if _default_handler:
         # This library has already configured the library root logger.
         return
      _UpperCAmelCase           =   logging.StreamHandler()  # Set sys.stderr as stream.
      _UpperCAmelCase           =   sys.stderr.flush

      # Apply our default configuration to the library root logger.
      _UpperCAmelCase           =   _get_library_root_logger()
      library_root_logger.addHandler(_default_handler						)
      library_root_logger.setLevel(_get_default_logging_level()						)
      _UpperCAmelCase           =   False
def      A__       (    )  ->  None:






   """simple docstring"""







   global _default_handler

   with _lock:
      if not _default_handler:
         return

      _UpperCAmelCase           =   _get_library_root_logger()
      library_root_logger.removeHandler(_default_handler						)
      library_root_logger.setLevel(logging.NOTSET						)
      _UpperCAmelCase           =   None
def      A__       (    )  ->  List[Any]:






   """simple docstring"""







   return log_levels
def      A__       (    SCREAMING_SNAKE_CASE_   :      Optional[str] = None						)  ->  logging.Logger:






   """simple docstring"""







   if name is None:
      _UpperCAmelCase           =   _get_library_name()

   _configure_library_root_logger()
   return logging.getLogger(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  int:






   """simple docstring"""







   _configure_library_root_logger()
   return _get_library_root_logger().getEffectiveLevel()
def      A__       (    SCREAMING_SNAKE_CASE_   :      int						)  ->  None:






   """simple docstring"""







   _configure_library_root_logger()
   _get_library_root_logger().setLevel(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  Optional[Any]:






   """simple docstring"""







   return set_verbosity(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  int:






   """simple docstring"""







   return set_verbosity(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  List[str]:






   """simple docstring"""







   return set_verbosity(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  Optional[Any]:






   """simple docstring"""







   return set_verbosity(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  None:






   """simple docstring"""







   _configure_library_root_logger()

   assert _default_handler is not None
   _get_library_root_logger().removeHandler(_default_handler						)
def      A__       (    )  ->  None:






   """simple docstring"""







   _configure_library_root_logger()

   assert _default_handler is not None
   _get_library_root_logger().addHandler(_default_handler						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      logging.Handler						)  ->  None:






   """simple docstring"""







   _configure_library_root_logger()

   assert handler is not None
   _get_library_root_logger().addHandler(SCREAMING_SNAKE_CASE_						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      logging.Handler						)  ->  None:






   """simple docstring"""







   _configure_library_root_logger()

   assert handler is not None and handler not in _get_library_root_logger().handlers
   _get_library_root_logger().removeHandler(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  None:






   """simple docstring"""







   _configure_library_root_logger()
   _UpperCAmelCase           =   False
def      A__       (    )  ->  None:






   """simple docstring"""







   _configure_library_root_logger()
   _UpperCAmelCase           =   True
def      A__       (    )  ->  None:






   """simple docstring"""







   _UpperCAmelCase           =   _get_library_root_logger().handlers

   for handler in handlers:
      _UpperCAmelCase           =   logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'''						)
      handler.setFormatter(SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  None:






   """simple docstring"""







   _UpperCAmelCase           =   _get_library_root_logger().handlers

   for handler in handlers:
      handler.setFormatter(SCREAMING_SNAKE_CASE_						)
def      A__       (    self   :      Optional[int]				,	*SCREAMING_SNAKE_CASE_   :      Any				,	**SCREAMING_SNAKE_CASE_   :      Union[str, Any]						)  ->  str:






   """simple docstring"""







   _UpperCAmelCase           =   os.getenv('''TRANSFORMERS_NO_ADVISORY_WARNINGS'''				,	SCREAMING_SNAKE_CASE_						)
   if no_advisory_warnings:
      return
   self.warning(*SCREAMING_SNAKE_CASE_				,	**SCREAMING_SNAKE_CASE_						)


UpperCAmelCase_           =	warning_advice
@functools.lru_cache(SCREAMING_SNAKE_CASE_						)
def      A__       (    self   :      Dict				,	*SCREAMING_SNAKE_CASE_   :      List[Any]				,	**SCREAMING_SNAKE_CASE_   :      Dict						)  ->  Optional[int]:






   """simple docstring"""







   self.warning(*SCREAMING_SNAKE_CASE_				,	**SCREAMING_SNAKE_CASE_						)


UpperCAmelCase_           =	warning_once




class 			__UpperCamelCase    :





     def __init__(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):  # pylint: disable=unused-argument
        _UpperCAmelCase           =   args[0] if args else None





     def __iter__(     self					):
        return iter(self._iterator					)





     def __getattr__(     self				,							_UpperCamelCase					):

        def empty_fn(*_UpperCamelCase				,							**_UpperCamelCase					):  # pylint: disable=unused-argument
           return

        return empty_fn





     def __enter__(     self					):
        return self





     def __exit__(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					):
        return




class 			__UpperCamelCase    :





     def __call__(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        if _tqdm_active:
           return tqdm_lib.tqdm(*_UpperCamelCase				,							**_UpperCamelCase					)
        else:
           return EmptyTqdm(*_UpperCamelCase				,							**_UpperCamelCase					)





     def 		UpperCamelCase(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        _UpperCAmelCase           =   None
        if _tqdm_active:
           return tqdm_lib.tqdm.set_lock(*_UpperCamelCase				,							**_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        if _tqdm_active:
           return tqdm_lib.tqdm.get_lock()




UpperCAmelCase_           =	_tqdm_cls()
def      A__       (    )  ->  bool:






   """simple docstring"""







   global _tqdm_active
   return bool(_tqdm_active						)
def      A__       (    )  ->  str:






   """simple docstring"""







   global _tqdm_active
   _UpperCAmelCase           =   True
   hf_hub_utils.enable_progress_bars()
def      A__       (    )  ->  List[Any]:






   """simple docstring"""







   global _tqdm_active
   _UpperCAmelCase           =   False
   hf_hub_utils.disable_progress_bars()






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,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCAmelCase_           =	logging.get_logger(__name__)




class 			__UpperCamelCase    (							A__    ):
     __A	:  int							  =   ["""pixel_values"""]





     def __init__(     self				,							_UpperCamelCase = True				,							_UpperCamelCase = None				,							_UpperCamelCase = PILImageResampling.BICUBIC				,							_UpperCamelCase = True				,							_UpperCamelCase = True				,							_UpperCamelCase = 1 / 255				,							_UpperCamelCase = None				,							_UpperCamelCase = True				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							**_UpperCamelCase				,							):
        super().__init__(**_UpperCamelCase					)
        _UpperCAmelCase           =   size if size is not None else {'''height''': 224, '''width''': 224}
        _UpperCAmelCase           =   get_size_dict(_UpperCamelCase					)
        _UpperCAmelCase           =   crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
        _UpperCAmelCase           =   get_size_dict(_UpperCamelCase				,							default_to_square=_UpperCamelCase				,							param_name='''crop_size'''					)

        _UpperCAmelCase           =   do_resize
        _UpperCAmelCase           =   do_rescale
        _UpperCAmelCase           =   do_normalize
        _UpperCAmelCase           =   do_center_crop
        _UpperCAmelCase           =   crop_size
        _UpperCAmelCase           =   size
        _UpperCAmelCase           =   resample
        _UpperCAmelCase           =   rescale_factor
        _UpperCAmelCase           =   image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
        _UpperCAmelCase           =   image_std if image_std is not None else IMAGENET_DEFAULT_STD





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase = PILImageResampling.BILINEAR				,							_UpperCamelCase = None				,							**_UpperCamelCase				,							):
        _UpperCAmelCase           =   get_size_dict(_UpperCamelCase					)

        if "shortest_edge" in size:
           _UpperCAmelCase           =   get_resize_output_image_size(_UpperCamelCase				,							size=size['''shortest_edge''']				,							default_to_square=_UpperCamelCase					)
           # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
        elif "height" in size and "width" in size:
           _UpperCAmelCase           =   (size['''height'''], size['''width'''])
        else:
           raise ValueError(f'''Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}'''					)
        return resize(_UpperCamelCase				,							size=_UpperCamelCase				,							resample=_UpperCamelCase				,							data_format=_UpperCamelCase				,							**_UpperCamelCase					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase = None				,							**_UpperCamelCase				,							):
        _UpperCAmelCase           =   get_size_dict(_UpperCamelCase					)
        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(_UpperCamelCase				,							size=(size['''height'''], size['''width'''])				,							data_format=_UpperCamelCase				,							**_UpperCamelCase					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase = None				,							**_UpperCamelCase					):
        return rescale(_UpperCamelCase				,							scale=_UpperCamelCase				,							data_format=_UpperCamelCase				,							**_UpperCamelCase					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase = None				,							**_UpperCamelCase				,							):
        return normalize(_UpperCamelCase				,							mean=_UpperCamelCase				,							std=_UpperCamelCase				,							data_format=_UpperCamelCase				,							**_UpperCamelCase					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = ChannelDimension.FIRST				,							**_UpperCamelCase				,							):
        _UpperCAmelCase           =   do_resize if do_resize is not None else self.do_resize
        _UpperCAmelCase           =   do_rescale if do_rescale is not None else self.do_rescale
        _UpperCAmelCase           =   do_normalize if do_normalize is not None else self.do_normalize
        _UpperCAmelCase           =   do_center_crop if do_center_crop is not None else self.do_center_crop
        _UpperCAmelCase           =   crop_size if crop_size is not None else self.crop_size
        _UpperCAmelCase           =   get_size_dict(_UpperCamelCase				,							param_name='''crop_size'''				,							default_to_square=_UpperCamelCase					)
        _UpperCAmelCase           =   resample if resample is not None else self.resample
        _UpperCAmelCase           =   rescale_factor if rescale_factor is not None else self.rescale_factor
        _UpperCAmelCase           =   image_mean if image_mean is not None else self.image_mean
        _UpperCAmelCase           =   image_std if image_std is not None else self.image_std

        _UpperCAmelCase           =   size if size is not None else self.size
        _UpperCAmelCase           =   get_size_dict(_UpperCamelCase					)

        if not is_batched(_UpperCamelCase					):
           _UpperCAmelCase           =   [images]

        if not valid_images(_UpperCamelCase					):
           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.'''					)

        # All transformations expect numpy arrays.
        _UpperCAmelCase           =   [to_numpy_array(_UpperCamelCase					) for image in images]

        if do_resize:
           _UpperCAmelCase           =   [self.resize(image=_UpperCamelCase				,							size=_UpperCamelCase				,							resample=_UpperCamelCase					) for image in images]

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

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

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

        _UpperCAmelCase           =   [to_channel_dimension_format(_UpperCamelCase				,							_UpperCamelCase					) for image in images]

        _UpperCAmelCase           =   {'''pixel_values''': images}
        return BatchFeature(data=_UpperCamelCase				,							tensor_type=_UpperCamelCase					)