





'''simple docstring'''


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
class    a_     (       lowerCamelCase__							):
     __A  				=    ['image_processor', 'tokenizer']
     __A  				=    'AutoImageProcessor'
     __A  				=    'AutoTokenizer'





     def __init__(				self	:      str			,   lowercase	:      int=None			,   lowercase	:      str=None			,   **lowercase	:      Any		):





        """simple docstring"""




        lowercase_    :List[str]					    =				None
        if "feature_extractor" in kwargs:
           warnings.warn(
               "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
               " instead."			,   lowercase_			,   )
           lowercase_    :Union[str, Any]					    =				kwargs.pop("feature_extractor"		)

        lowercase_    :str					    =				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__(lowercase_			,   lowercase_		)
        lowercase_    :List[str]					    =				self.image_processor
        lowercase_    :int					    =				False





     def __call__(				self	:      Optional[int]			,   *lowercase	:      int			,   **lowercase	:      Any		):





        """simple docstring"""




        if self._in_target_context_manager:
           return self.current_processor(*lowercase_			,   **lowercase_		)

        lowercase_    :Tuple					    =				kwargs.pop("images"			,   lowercase_		)
        lowercase_    :Any					    =				kwargs.pop("text"			,   lowercase_		)
        if len(lowercase_		) > 0:
           lowercase_    :Union[str, Any]					    =				args[0]
           lowercase_    :Any					    =				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:
           lowercase_    :Union[str, Any]					    =				self.image_processor(lowercase_			,   *lowercase_			,   **lowercase_		)
        if text is not None:
           lowercase_    :Union[str, Any]					    =				self.tokenizer(lowercase_			,   **lowercase_		)

        if text is None:
           return inputs
        elif images is None:
           return encodings
        else:
           lowercase_    :List[Any]					    =				encodings["input_ids"]
           return inputs





     def 							lowercase__   (				self	:      str			,   *lowercase	:      Optional[int]			,   **lowercase	:      List[str]		):





        """simple docstring"""




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





     def 							lowercase__   (				self	:      str			,   *lowercase	:      int			,   **lowercase	:      List[Any]		):





        """simple docstring"""




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





     @contextmanager
     def 							lowercase__   (				self	:      Optional[Any]		):





        """simple docstring"""




        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."		)
        lowercase_    :Union[str, Any]					    =				True
        lowercase_    :Union[str, Any]					    =				self.tokenizer
        yield
        lowercase_    :Optional[int]					    =				self.image_processor
        lowercase_    :Tuple					    =				False





     @property
     def 							lowercase__   (				self	:      List[str]		):





        """simple docstring"""




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







     @property
     def 							lowercase__   (				self	:      str		):





        """simple docstring"""




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









'''simple docstring'''


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import EfficientNetImageProcessor
class    a_     (       unittest.TestCase							):





     def __init__(				self	:      int			,   lowercase	:      Optional[int]			,   lowercase	:      Tuple=13			,   lowercase	:      str=3			,   lowercase	:      List[str]=18			,   lowercase	:      Union[str, Any]=30			,   lowercase	:      Any=400			,   lowercase	:      Tuple=True			,   lowercase	:      Union[str, Any]=None			,   lowercase	:      Optional[Any]=True			,   lowercase	:      Optional[int]=[0.5, 0.5, 0.5]			,   lowercase	:      List[str]=[0.5, 0.5, 0.5]			,   ):





        """simple docstring"""




        lowercase_    :Dict					    =				size if size is not None else {"height": 18, "width": 18}
        lowercase_    :Optional[Any]					    =				parent
        lowercase_    :str					    =				batch_size
        lowercase_    :Optional[int]					    =				num_channels
        lowercase_    :str					    =				image_size
        lowercase_    :Tuple					    =				min_resolution
        lowercase_    :Optional[Any]					    =				max_resolution
        lowercase_    :List[str]					    =				do_resize
        lowercase_    :Optional[Any]					    =				size
        lowercase_    :Optional[int]					    =				do_normalize
        lowercase_    :Union[str, Any]					    =				image_mean
        lowercase_    :Dict					    =				image_std







     def 							lowercase__   (				self	:      Union[str, Any]		):





        """simple docstring"""




        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
        }







@require_torch
@require_vision
class    a_     (       _lowerCAmelCase			,		unittest.TestCase							):
     __A  				=    EfficientNetImageProcessor if is_vision_available() else None





     def 							lowercase__   (				self	:      Optional[int]		):





        """simple docstring"""




        lowercase_    :List[Any]					    =				EfficientNetImageProcessorTester(self		)





     @property
     def 							lowercase__   (				self	:      Optional[Any]		):





        """simple docstring"""




        return self.image_processor_tester.prepare_image_processor_dict()





     def 							lowercase__   (				self	:      Union[str, Any]		):





        """simple docstring"""




        lowercase_    :Optional[Any]					    =				self.image_processing_class(**self.image_processor_dict		)
        self.assertTrue(hasattr(lowercase			,   "image_mean"		)		)
        self.assertTrue(hasattr(lowercase			,   "image_std"		)		)
        self.assertTrue(hasattr(lowercase			,   "do_normalize"		)		)
        self.assertTrue(hasattr(lowercase			,   "do_resize"		)		)
        self.assertTrue(hasattr(lowercase			,   "size"		)		)





     def 							lowercase__   (				self	:      str		):





        """simple docstring"""




        lowercase_    :Tuple					    =				self.image_processing_class.from_dict(self.image_processor_dict		)
        self.assertEqual(image_processor.size			,   {"height": 18, "width": 18}		)

        lowercase_    :List[str]					    =				self.image_processing_class.from_dict(self.image_processor_dict			,   size=42		)
        self.assertEqual(image_processor.size			,   {"height": 42, "width": 42}		)





     def 							lowercase__   (				self	:      Optional[int]		):





        """simple docstring"""




        lowercase_    :List[Any]					    =				self.image_processing_class(**self.image_processor_dict		)
        # create random PIL images
        lowercase_    :Tuple					    =				prepare_image_inputs(self.image_processor_tester			,   equal_resolution=lowercase		)
        for image in image_inputs:
           self.assertIsInstance(lowercase			,   Image.Image		)

        # Test not batched input
        lowercase_    :Optional[Any]					    =				image_processing(image_inputs[0]			,   return_tensors="pt"		).pixel_values
        self.assertEqual(
            encoded_images.shape			,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
            )			,   )

        # Test batched
        lowercase_    :Optional[Any]					    =				image_processing(lowercase			,   return_tensors="pt"		).pixel_values
        self.assertEqual(
            encoded_images.shape			,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
            )			,   )





     def 							lowercase__   (				self	:      Union[str, Any]		):





        """simple docstring"""




        lowercase_    :int					    =				self.image_processing_class(**self.image_processor_dict		)
        # create random numpy tensors
        lowercase_    :List[Any]					    =				prepare_image_inputs(self.image_processor_tester			,   equal_resolution=lowercase			,   numpify=lowercase		)
        for image in image_inputs:
           self.assertIsInstance(lowercase			,   np.ndarray		)

        # Test not batched input
        lowercase_    :Optional[Any]					    =				image_processing(image_inputs[0]			,   return_tensors="pt"		).pixel_values
        self.assertEqual(
            encoded_images.shape			,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
            )			,   )

        # Test batched
        lowercase_    :Any					    =				image_processing(lowercase			,   return_tensors="pt"		).pixel_values
        self.assertEqual(
            encoded_images.shape			,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
            )			,   )







     def 							lowercase__   (				self	:      Union[str, Any]		):





        """simple docstring"""




        lowercase_    :str					    =				self.image_processing_class(**self.image_processor_dict		)
        # create random PyTorch tensors
        lowercase_    :Tuple					    =				prepare_image_inputs(self.image_processor_tester			,   equal_resolution=lowercase			,   torchify=lowercase		)
        for image in image_inputs:
           self.assertIsInstance(lowercase			,   torch.Tensor		)

        # Test not batched input
        lowercase_    :str					    =				image_processing(image_inputs[0]			,   return_tensors="pt"		).pixel_values
        self.assertEqual(
            encoded_images.shape			,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
            )			,   )

        # Test batched
        lowercase_    :Optional[int]					    =				image_processing(lowercase			,   return_tensors="pt"		).pixel_values
        self.assertEqual(
            encoded_images.shape			,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
            )			,   )


