



'''simple docstring'''
import os
from typing import List, Optional, Union

from ...image_processing_utils import BatchFeature
from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
from ..auto import AutoTokenizer






class 				lowercase__      (				lowercase  ):
	lowercase__    							=     ["""image_processor""", """tokenizer"""]
	lowercase__    							=     """BlipImageProcessor"""
	lowercase__    							=     """AutoTokenizer"""





	def __init__(					self   :							int					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Any					):







							'''simple docstring'''
							super().__init__(lowerCamelCase__					,lowerCamelCase__					)

							# add QFormer tokenizer
							_UpperCamelCase   :						Tuple												=  qformer_tokenizer





	def __call__(					self   :							Any					,lowerCamelCase__   :							ImageInput = None					,lowerCamelCase__   :							Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None					,lowerCamelCase__   :							bool = True					,lowerCamelCase__   :							Union[bool, str, PaddingStrategy] = False					,lowerCamelCase__   :							Union[bool, str, TruncationStrategy] = None					,lowerCamelCase__   :							Optional[int] = None					,lowerCamelCase__   :							int = 0					,lowerCamelCase__   :							Optional[int] = None					,lowerCamelCase__   :							Optional[bool] = None					,lowerCamelCase__   :							bool = False					,lowerCamelCase__   :							bool = False					,lowerCamelCase__   :							bool = False					,lowerCamelCase__   :							bool = False					,lowerCamelCase__   :							bool = False					,lowerCamelCase__   :							bool = True					,lowerCamelCase__   :							Optional[Union[str, TensorType]] = None					,**lowerCamelCase__   :							str					,):







							'''simple docstring'''
							if images is None and text is None:
													raise ValueError('You have to specify at least images or text.'					)

							_UpperCamelCase   :						Optional[Any]												=  BatchFeature()

							if text is not None:
													_UpperCamelCase   :						Optional[int]												=  self.tokenizer(
													    text=lowerCamelCase__					,add_special_tokens=lowerCamelCase__					,padding=lowerCamelCase__					,truncation=lowerCamelCase__					,max_length=lowerCamelCase__					,stride=lowerCamelCase__					,pad_to_multiple_of=lowerCamelCase__					,return_attention_mask=lowerCamelCase__					,return_overflowing_tokens=lowerCamelCase__					,return_special_tokens_mask=lowerCamelCase__					,return_offsets_mapping=lowerCamelCase__					,return_token_type_ids=lowerCamelCase__					,return_length=lowerCamelCase__					,verbose=lowerCamelCase__					,return_tensors=lowerCamelCase__					,**lowerCamelCase__					,)
													encoding.update(lowerCamelCase__					)
													_UpperCamelCase   :						List[str]												=  self.qformer_tokenizer(
													    text=lowerCamelCase__					,add_special_tokens=lowerCamelCase__					,padding=lowerCamelCase__					,truncation=lowerCamelCase__					,max_length=lowerCamelCase__					,stride=lowerCamelCase__					,pad_to_multiple_of=lowerCamelCase__					,return_attention_mask=lowerCamelCase__					,return_overflowing_tokens=lowerCamelCase__					,return_special_tokens_mask=lowerCamelCase__					,return_offsets_mapping=lowerCamelCase__					,return_token_type_ids=lowerCamelCase__					,return_length=lowerCamelCase__					,verbose=lowerCamelCase__					,return_tensors=lowerCamelCase__					,**lowerCamelCase__					,)
													_UpperCamelCase   :						str												=  qformer_text_encoding.pop('input_ids'					)
													_UpperCamelCase   :						Dict												=  qformer_text_encoding.pop('attention_mask'					)

							if images is not None:
													_UpperCamelCase   :						Union[str, Any]												=  self.image_processor(lowerCamelCase__					,return_tensors=lowerCamelCase__					)
													encoding.update(lowerCamelCase__					)

							return encoding





	def       UpperCamelCase_    (					self   :							List[str]					,*lowerCamelCase__   :							List[str]					,**lowerCamelCase__   :							List[Any]					):







							'''simple docstring'''
							return self.tokenizer.batch_decode(*lowerCamelCase__					,**lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					,*lowerCamelCase__   :							Dict					,**lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							return self.tokenizer.decode(*lowerCamelCase__					,**lowerCamelCase__					)





	@property
	# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  self.tokenizer.model_input_names
							_UpperCamelCase   :						Optional[Any]												=  self.image_processor.model_input_names
							return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names					)					)





	def       UpperCamelCase_    (					self   :							List[str]					,lowerCamelCase__   :							Union[str, Any]					,**lowerCamelCase__   :							Dict					):







							'''simple docstring'''
							if os.path.isfile(lowerCamelCase__					):
													raise ValueError(F'Provided path ({save_directory}) should be a directory, not a file'					)
							os.makedirs(lowerCamelCase__					,exist_ok=lowerCamelCase__					)
							_UpperCamelCase   :						int												=  os.path.join(lowerCamelCase__					,'qformer_tokenizer'					)
							self.qformer_tokenizer.save_pretrained(lowerCamelCase__					)
							return super().save_pretrained(lowerCamelCase__					,**lowerCamelCase__					)





	@classmethod
	def       UpperCamelCase_    (					cls   :							Union[str, Any]					,lowerCamelCase__   :							Optional[int]					,**lowerCamelCase__   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  AutoTokenizer.from_pretrained(lowerCamelCase__					,subfolder='qformer_tokenizer'					)
							_UpperCamelCase   :						Dict												=  cls._get_arguments_from_pretrained(lowerCamelCase__					,**lowerCamelCase__					)
							args.append(lowerCamelCase__					)
							return cls(*lowerCamelCase__					)







'''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 ViTImageProcessor






class 				lowercase__      (				unittest.TestCase  ):





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







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





	def       UpperCamelCase_    (					self   :							List[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 				lowercase__      (				lowercase		,   unittest.TestCase  ):
	lowercase__    							=     ViTImageProcessor if is_vision_available() else None





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  EfficientFormerImageProcessorTester(self					)





	@property
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							return self.image_proc_tester.prepare_image_processor_dict()





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  self.image_processing_class(**self.image_processor_dict					)
							self.assertTrue(hasattr(lowerCamelCase__					,'image_mean'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'image_std'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'do_normalize'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'do_resize'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'size'					)					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							# Initialize image_processor
							_UpperCamelCase   :						str												=  self.image_processing_class(**self.image_processor_dict					)
							# create random PIL images
							_UpperCamelCase   :						Optional[Any]												=  prepare_image_inputs(self.image_proc_tester					,equal_resolution=lowerCamelCase__					)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__					,Image.Image					)

							# Test not batched input
							_UpperCamelCase   :						int												=  image_processor(image_inputs[0]					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)

							# Test batched
							_UpperCamelCase   :						int												=  image_processor(lowerCamelCase__					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							# Initialize image_processor
							_UpperCamelCase   :						Any												=  self.image_processing_class(**self.image_processor_dict					)
							# create random numpy tensors
							_UpperCamelCase   :						Optional[int]												=  prepare_image_inputs(self.image_proc_tester					,equal_resolution=lowerCamelCase__					,numpify=lowerCamelCase__					)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__					,np.ndarray					)

							# Test not batched input
							_UpperCamelCase   :						Union[str, Any]												=  image_processor(image_inputs[0]					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)

							# Test batched
							_UpperCamelCase   :						Optional[Any]												=  image_processor(lowerCamelCase__					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							# Initialize image_processor
							_UpperCamelCase   :						Any												=  self.image_processing_class(**self.image_processor_dict					)
							# create random PyTorch tensors
							_UpperCamelCase   :						Any												=  prepare_image_inputs(self.image_proc_tester					,equal_resolution=lowerCamelCase__					,torchify=lowerCamelCase__					)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__					,torch.Tensor					)

							# Test not batched input
							_UpperCamelCase   :						Dict												=  image_processor(image_inputs[0]					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)

							# Test batched
							_UpperCamelCase   :						Any												=  image_processor(lowerCamelCase__					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)


