
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


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import CLIPImageProcessor







class 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





	def __init__(      self					,							UpperCamelCase__					,							UpperCamelCase__=7					,							UpperCamelCase__=3					,							UpperCamelCase__=18					,							UpperCamelCase__=30					,							UpperCamelCase__=400					,							UpperCamelCase__=True					,							UpperCamelCase__=None					,							UpperCamelCase__=True					,							UpperCamelCase__=None					,							UpperCamelCase__=True					,							UpperCamelCase__=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]					,							UpperCamelCase__=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]					,							UpperCamelCase__=True					,							):
					A__	:						Any								=				size if size is not None else {'''shortest_edge''': 20}
					A__	:						List[Any]								=				crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
					A__	:						Dict								=				parent
					A__	:						List[Any]								=				batch_size
					A__	:						int								=				num_channels
					A__	:						List[Any]								=				image_size
					A__	:						Union[str, Any]								=				min_resolution
					A__	:						List[Any]								=				max_resolution
					A__	:						List[Any]								=				do_resize
					A__	:						int								=				size
					A__	:						List[Any]								=				do_center_crop
					A__	:						Tuple								=				crop_size
					A__	:						str								=				do_normalize
					A__	:						Optional[int]								=				image_mean
					A__	:						int								=				image_std
					A__	:						Tuple								=				do_convert_rgb





	def 	__snake_case					(      self		):
					return {
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "do_center_crop": self.do_center_crop,
					    "crop_size": self.crop_size,
					    "do_normalize": self.do_normalize,
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					    "do_convert_rgb": self.do_convert_rgb,
					}





	def 	__snake_case					(      self					,							UpperCamelCase__=False					,							UpperCamelCase__=False					,							UpperCamelCase__=False		):
					assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

					if equal_resolution:
									A__	:						Tuple								=				[]
									for i in range(self.batch_size		):
													image_inputs.append(
													    np.random.randint(
													        255					,							size=(self.num_channels, self.max_resolution, self.max_resolution)					,							dtype=np.uinta		)		)
					else:
									A__	:						Optional[int]								=				[]
									for i in range(self.batch_size		):
													A__							,				A__	:						str								=				np.random.choice(np.arange(self.min_resolution					,							self.max_resolution		)					,							2		)
													image_inputs.append(np.random.randint(255					,							size=(self.num_channels, width, height)					,							dtype=np.uinta		)		)

					if not numpify and not torchify:
									# PIL expects the channel dimension as last dimension
									A__	:						Dict								=				[Image.fromarray(np.moveaxis(UpperCamelCase__					,							0					,							-1		)		) for x in image_inputs]

					if torchify:
									A__	:						Optional[int]								=				[torch.from_numpy(UpperCamelCase__		) for x in image_inputs]

					return image_inputs







@require_torch
@require_vision
class 			UpperCamelCase__		(     SCREAMING_SNAKE_CASE_,      unittest.TestCase ):







	'''simple docstring'''
	_lowerCAmelCase											=					CLIPImageProcessor if is_vision_available() else None





	def 	__snake_case					(      self		):
					A__	:						int								=				CLIPImageProcessingTester(self		)





	@property
	def 	__snake_case					(      self		):
					return self.image_processor_tester.prepare_image_processor_dict()





	def 	__snake_case					(      self		):
					A__	:						Optional[Any]								=				self.image_processing_class(**self.image_processor_dict		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_resize'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''size'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_center_crop'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''center_crop'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_normalize'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''image_mean'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''image_std'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_convert_rgb'''		)		)





	def 	__snake_case					(      self		):
					A__	:						List[str]								=				self.image_processing_class.from_dict(self.image_processor_dict		)
					self.assertEqual(image_processor.size					,							{'''shortest_edge''': 20}		)
					self.assertEqual(image_processor.crop_size					,							{'''height''': 18, '''width''': 18}		)

					A__	:						Union[str, Any]								=				self.image_processing_class.from_dict(self.image_processor_dict					,							size=42					,							crop_size=84		)
					self.assertEqual(image_processor.size					,							{'''shortest_edge''': 42}		)
					self.assertEqual(image_processor.crop_size					,							{'''height''': 84, '''width''': 84}		)





	def 	__snake_case					(      self		):
					pass





	def 	__snake_case					(      self		):
					# Initialize image_processing
					A__	:						int								=				self.image_processing_class(**self.image_processor_dict		)
					# create random PIL images
					A__	:						Optional[int]								=				self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__		)
					for image in image_inputs:
									self.assertIsInstance(UpperCamelCase__					,							Image.Image		)

					# Test not batched input
					A__	:						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.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

					# Test batched
					A__	:						str								=				image_processing(UpperCamelCase__					,							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.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)





	def 	__snake_case					(      self		):
					# Initialize image_processing
					A__	:						Optional[Any]								=				self.image_processing_class(**self.image_processor_dict		)
					# create random numpy tensors
					A__	:						List[str]								=				self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__					,							numpify=UpperCamelCase__		)
					for image in image_inputs:
									self.assertIsInstance(UpperCamelCase__					,							np.ndarray		)

					# Test not batched input
					A__	:						List[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.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

					# Test batched
					A__	:						str								=				image_processing(UpperCamelCase__					,							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.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)





	def 	__snake_case					(      self		):
					# Initialize image_processing
					A__	:						Tuple								=				self.image_processing_class(**self.image_processor_dict		)
					# create random PyTorch tensors
					A__	:						Optional[Any]								=				self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__					,							torchify=UpperCamelCase__		)
					for image in image_inputs:
									self.assertIsInstance(UpperCamelCase__					,							torch.Tensor		)

					# Test not batched input
					A__	:						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.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

					# Test batched
					A__	:						Dict								=				image_processing(UpperCamelCase__					,							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.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)







@require_torch
@require_vision
class 			UpperCamelCase__		(     SCREAMING_SNAKE_CASE_,      unittest.TestCase ):







	'''simple docstring'''
	_lowerCAmelCase											=					CLIPImageProcessor if is_vision_available() else None





	def 	__snake_case					(      self		):
					A__	:						str								=				CLIPImageProcessingTester(self					,							num_channels=4		)
					A__	:						List[Any]								=				3





	@property
	def 	__snake_case					(      self		):
					return self.image_processor_tester.prepare_image_processor_dict()





	def 	__snake_case					(      self		):
					A__	:						int								=				self.image_processing_class(**self.image_processor_dict		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_resize'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''size'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_center_crop'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''center_crop'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_normalize'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''image_mean'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''image_std'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_convert_rgb'''		)		)





	def 	__snake_case					(      self		):
					pass





	def 	__snake_case					(      self		):
					# Initialize image_processing
					A__	:						Union[str, Any]								=				self.image_processing_class(**self.image_processor_dict		)
					# create random PIL images
					A__	:						List[Any]								=				self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__		)
					for image in image_inputs:
									self.assertIsInstance(UpperCamelCase__					,							Image.Image		)

					# Test not batched input
					A__	:						Optional[Any]								=				image_processing(image_inputs[0]					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_images.shape					,							(
					        1,
					        self.expected_encoded_image_num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

					# Test batched
					A__	:						List[Any]								=				image_processing(UpperCamelCase__					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_images.shape					,							(
					        self.image_processor_tester.batch_size,
					        self.expected_encoded_image_num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
					from PIL import Image







class 			UpperCamelCase__		(     unittest.TestCase,      SCREAMING_SNAKE_CASE_ ):







	'''simple docstring'''





	def 	__snake_case					(      self		):
					A__	:						Dict								=				load_tool('''image-question-answering'''		)
					self.tool.setup()
					A__	:						List[Any]								=				load_tool('''image-question-answering'''					,							remote=UpperCamelCase__		)





	def 	__snake_case					(      self		):
					A__	:						str								=				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''		)		) / '''000000039769.png'''		)
					A__	:						List[str]								=				self.tool(UpperCamelCase__					,							'''How many cats are sleeping on the couch?'''		)
					self.assertEqual(UpperCamelCase__					,							'''2'''		)





	def 	__snake_case					(      self		):
					A__	:						Tuple								=				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''		)		) / '''000000039769.png'''		)
					A__	:						Dict								=				self.remote_tool(UpperCamelCase__					,							'''How many cats are sleeping on the couch?'''		)
					self.assertEqual(UpperCamelCase__					,							'''2'''		)





	def 	__snake_case					(      self		):
					A__	:						Dict								=				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''		)		) / '''000000039769.png'''		)
					A__	:						int								=				self.tool(image=UpperCamelCase__					,							question='''How many cats are sleeping on the couch?'''		)
					self.assertEqual(UpperCamelCase__					,							'''2'''		)





	def 	__snake_case					(      self		):
					A__	:						Tuple								=				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''		)		) / '''000000039769.png'''		)
					A__	:						str								=				self.remote_tool(image=UpperCamelCase__					,							question='''How many cats are sleeping on the couch?'''		)
					self.assertEqual(UpperCamelCase__					,							'''2'''		)