



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   :							int							 =threading.Lock()
UpperCAmelCase   :							Optional[logging.Handler]							 =None

UpperCAmelCase   :							List[Any]							 ={
    """debug""": logging.DEBUG,
    """info""": logging.INFO,
    """warning""": logging.WARNING,
    """error""": logging.ERROR,
    """critical""": logging.CRITICAL,
}

UpperCAmelCase   :							Any							 =logging.WARNING

UpperCAmelCase   :							List[Any]							 =True



def    _lowerCAmelCase    ():
					UpperCamelCase_		  =   os.getenv("TRANSFORMERS_VERBOSITY"   ,	_lowerCAmelCase)
					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    _lowerCAmelCase    ():
					return __name__.split(".")[0]



def    _lowerCAmelCase    ():
					return logging.getLogger(_get_library_name())



def    _lowerCAmelCase    ():
					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    _lowerCAmelCase    ():
					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    _lowerCAmelCase    ():
					return log_levels



def    _lowerCAmelCase    (_lowerCAmelCase = None):

					if name is None:
										UpperCamelCase_		  =   _get_library_name()

					_configure_library_root_logger()
					return logging.getLogger(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					_configure_library_root_logger()
					return _get_library_root_logger().getEffectiveLevel()



def    _lowerCAmelCase    (_lowerCAmelCase):
					_configure_library_root_logger()
					_get_library_root_logger().setLevel(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					return set_verbosity(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					return set_verbosity(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					return set_verbosity(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					return set_verbosity(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					_configure_library_root_logger()

					assert _default_handler is not None
					_get_library_root_logger().removeHandler(_default_handler)



def    _lowerCAmelCase    ():
					_configure_library_root_logger()

					assert _default_handler is not None
					_get_library_root_logger().addHandler(_default_handler)



def    _lowerCAmelCase    (_lowerCAmelCase):
					_configure_library_root_logger()

					assert handler is not None
					_get_library_root_logger().addHandler(_lowerCAmelCase)



def    _lowerCAmelCase    (_lowerCAmelCase):
					_configure_library_root_logger()

					assert handler is not None and handler not in _get_library_root_logger().handlers
					_get_library_root_logger().removeHandler(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					_configure_library_root_logger()
					UpperCamelCase_		  =   False



def    _lowerCAmelCase    ():
					_configure_library_root_logger()
					UpperCamelCase_		  =   True



def    _lowerCAmelCase    ():
					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(_lowerCAmelCase)



def    _lowerCAmelCase    ():
					UpperCamelCase_		  =   _get_library_root_logger().handlers

					for handler in handlers:
										handler.setFormatter(_lowerCAmelCase)



def    _lowerCAmelCase    (self   ,	*_lowerCAmelCase   ,	**_lowerCAmelCase):
					UpperCamelCase_		  =   os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS"   ,	_lowerCAmelCase)
					if no_advisory_warnings:
										return
					self.warning(*_lowerCAmelCase   ,	**_lowerCAmelCase)


UpperCAmelCase   :							Dict							 =warning_advice



@functools.lru_cache(_lowerCAmelCase)
def    _lowerCAmelCase    (self   ,	*_lowerCAmelCase   ,	**_lowerCAmelCase):
					self.warning(*_lowerCAmelCase   ,	**_lowerCAmelCase)


UpperCAmelCase   :							Optional[Any]							 =warning_once


class       _lowercase   :
				'''simple docstring'''







				def __init__( self     , *snake_case__     , **snake_case__    ):  # pylint: disable=unused-argument

									'''simple docstring'''


									UpperCamelCase_		  =   args[0] if args else None

				def __iter__( self    ):

									'''simple docstring'''


									return iter(self._iterator    )

				def __getattr__( self     , snake_case__    ):

									'''simple docstring'''


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

									return empty_fn

				def __enter__( self    ):

									'''simple docstring'''


									return self

				def __exit__( self     , snake_case__     , snake_case__     , snake_case__    ):

									'''simple docstring'''


									return




class       _lowercase   :
				'''simple docstring'''







				def __call__( self     , *snake_case__     , **snake_case__    ):

									'''simple docstring'''


									if _tqdm_active:
														return tqdm_lib.tqdm(*snake_case__     , **snake_case__    )
									else:
														return EmptyTqdm(*snake_case__     , **snake_case__    )

				def       _lowerCamelCase					( self     , *snake_case__     , **snake_case__    ):

									'''simple docstring'''


									UpperCamelCase_		  =   None
									if _tqdm_active:
														return tqdm_lib.tqdm.set_lock(*snake_case__     , **snake_case__    )

				def       _lowerCamelCase					( self    ):

									'''simple docstring'''


									if _tqdm_active:
														return tqdm_lib.tqdm.get_lock()




UpperCAmelCase   :							Tuple							 =_tqdm_cls()



def    _lowerCAmelCase    ():
					global _tqdm_active
					return bool(_tqdm_active)



def    _lowerCAmelCase    ():
					global _tqdm_active
					UpperCamelCase_		  =   True
					hf_hub_utils.enable_progress_bars()






def    _lowerCAmelCase    ():
					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_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCAmelCase   :							Dict							 =logging.get_logger(__name__)


class       _lowercase   (a_      ):
				'''simple docstring'''







				lowercase__   						=						["""pixel_values"""]

				def __init__( self     , snake_case__ = True     , snake_case__ = None     , snake_case__ = PILImageResampling.BILINEAR     , snake_case__ = True     , snake_case__ = None     , snake_case__ = True     , snake_case__ = 1 / 255     , snake_case__ = True     , snake_case__ = None     , snake_case__ = None     , **snake_case__     , ):

									'''simple docstring'''


									super().__init__(**snake_case__    )
									UpperCamelCase_		  =   size if size is not None else {"shortest_edge": 256}
									UpperCamelCase_		  =   get_size_dict(snake_case__     , default_to_square=snake_case__    )
									UpperCamelCase_		  =   crop_size if crop_size is not None else {"height": 224, "width": 224}
									UpperCamelCase_		  =   get_size_dict(snake_case__    )
									UpperCamelCase_		  =   do_resize
									UpperCamelCase_		  =   size
									UpperCamelCase_		  =   resample
									UpperCamelCase_		  =   do_center_crop
									UpperCamelCase_		  =   crop_size
									UpperCamelCase_		  =   do_rescale
									UpperCamelCase_		  =   rescale_factor
									UpperCamelCase_		  =   do_normalize
									UpperCamelCase_		  =   image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
									UpperCamelCase_		  =   image_std if image_std is not None else IMAGENET_STANDARD_STD

				def       _lowerCamelCase					( self     , snake_case__     , snake_case__     , snake_case__ = PILImageResampling.BICUBIC     , snake_case__ = None     , **snake_case__     , ):

									'''simple docstring'''


									UpperCamelCase_		  =   get_size_dict(snake_case__     , default_to_square=snake_case__    )
									if "shortest_edge" not in size:
														raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""    )
									UpperCamelCase_		  =   get_resize_output_image_size(snake_case__     , size=size["shortest_edge"]     , default_to_square=snake_case__    )
									return resize(snake_case__     , size=snake_case__     , resample=snake_case__     , data_format=snake_case__     , **snake_case__    )

				def       _lowerCamelCase					( self     , snake_case__     , snake_case__     , snake_case__ = None     , **snake_case__     , ):

									'''simple docstring'''


									UpperCamelCase_		  =   get_size_dict(snake_case__    )
									return center_crop(snake_case__     , size=(size["height"], size["width"])     , data_format=snake_case__     , **snake_case__    )

				def       _lowerCamelCase					( self     , snake_case__     , snake_case__     , snake_case__ = None     , **snake_case__    ):

									'''simple docstring'''


									return rescale(snake_case__     , scale=snake_case__     , data_format=snake_case__     , **snake_case__    )

				def       _lowerCamelCase					( self     , snake_case__     , snake_case__     , snake_case__     , snake_case__ = None     , **snake_case__     , ):

									'''simple docstring'''


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

				def       _lowerCamelCase					( self     , snake_case__     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = None     , snake_case__ = ChannelDimension.FIRST     , **snake_case__     , ):

									'''simple docstring'''


									UpperCamelCase_		  =   do_resize if do_resize is not None else self.do_resize
									UpperCamelCase_		  =   size if size is not None else self.size
									UpperCamelCase_		  =   get_size_dict(snake_case__     , default_to_square=snake_case__    )
									UpperCamelCase_		  =   resample if resample is not None else self.resample
									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(snake_case__    )
									UpperCamelCase_		  =   do_rescale if do_rescale is not None else self.do_rescale
									UpperCamelCase_		  =   rescale_factor if rescale_factor is not None else self.rescale_factor
									UpperCamelCase_		  =   do_normalize if do_normalize is not None else self.do_normalize
									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_		  =   make_list_of_images(snake_case__    )

									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."    )

									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."    )

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

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

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

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

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

									UpperCamelCase_		  =   [to_channel_dimension_format(snake_case__     , snake_case__    ) for image in images]

									UpperCamelCase_		  =   {"pixel_values": images}
									return BatchFeature(data=snake_case__     , tensor_type=snake_case__    )
