
"""simple docstring"""

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        _lowercase   (				unittest.TestCase				,       __UpperCAmelCase		):




	def 			_UpperCamelCase    ( self					)		->							Tuple:
			lowerCamelCase     :			Union[str, Any]		=    load_tool('image-captioning'					)
			self.tool.setup()
			lowerCamelCase     :			Dict		=    load_tool('image-captioning'		, remote=UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							List[str]:
			lowerCamelCase     :			Dict		=    Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'					)
			lowerCamelCase     :			str		=    self.tool(UpperCAmelCase_					)
			self.assertEqual(UpperCAmelCase_		, 'two cats sleeping on a couch'					)




	def 			_UpperCamelCase    ( self					)		->							Tuple:
			lowerCamelCase     :			str		=    Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'					)
			lowerCamelCase     :			Tuple		=    self.remote_tool(UpperCAmelCase_					)
			self.assertEqual(UpperCAmelCase_		, 'two cats sleeping on a couch'					)




	def 			_UpperCamelCase    ( self					)		->							List[str]:
			lowerCamelCase     :			List[str]		=    Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'					)
			lowerCamelCase     :			List[str]		=    self.tool(image=UpperCAmelCase_					)
			self.assertEqual(UpperCAmelCase_		, 'two cats sleeping on a couch'					)




	def 			_UpperCamelCase    ( self					)		->							Any:
			lowerCamelCase     :			Tuple		=    Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'					)
			lowerCamelCase     :			Dict		=    self.remote_tool(image=UpperCAmelCase_					)
			self.assertEqual(UpperCAmelCase_		, 'two cats sleeping on a couch'					)









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     lowercase						(       unittest.TestCase					):



 '''simple docstring'''


 def __init__(self       ,       __a       ,       __a=7       ,       __a=3       ,       __a=18       ,       __a=30       ,       __a=400       ,       __a=True       ,       __a=None       ,       __a=True       ,       __a=None       ,       __a=True       ,       __a=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]       ,       __a=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]       ,       __a=True       ,       ) ->					Optional[int]:






  """simple docstring"""





  UpperCAmelCase__							=				size if size is not None else {'shortest_edge': 20}
  UpperCAmelCase__							=				crop_size if crop_size is not None else {'height': 18, 'width': 18}
  UpperCAmelCase__							=				parent
  UpperCAmelCase__							=				batch_size
  UpperCAmelCase__							=				num_channels
  UpperCAmelCase__							=				image_size
  UpperCAmelCase__							=				min_resolution
  UpperCAmelCase__							=				max_resolution
  UpperCAmelCase__							=				do_resize
  UpperCAmelCase__							=				size
  UpperCAmelCase__							=				do_center_crop
  UpperCAmelCase__							=				crop_size
  UpperCAmelCase__							=				do_normalize
  UpperCAmelCase__							=				image_mean
  UpperCAmelCase__							=				image_std
  UpperCAmelCase__							=				do_convert_rgb




 def   UpperCamelCase__							(self ) ->					List[str]:






  """simple docstring"""





  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   UpperCamelCase__							(self       ,       __a=False       ,       __a=False       ,       __a=False ) ->					Union[str, Any]:






  """simple docstring"""





  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  if equal_resolution:
   UpperCAmelCase__							=				[]
   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:
   UpperCAmelCase__							=				[]
   for i in range(self.batch_size ):
    UpperCAmelCase__				,    UpperCAmelCase__							=				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
   UpperCAmelCase__							=				[Image.fromarray(np.moveaxis(__a       ,       0       ,       -1 ) ) for x in image_inputs]

  if torchify:
   UpperCAmelCase__							=				[torch.from_numpy(__a ) for x in image_inputs]

  return image_inputs







@require_torch
@require_vision
class     lowercase						(       _UpperCamelCase ,				unittest.TestCase					):



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE												=				CLIPImageProcessor if is_vision_available() else None




 def   UpperCamelCase__							(self ) ->					Union[str, Any]:






  """simple docstring"""





  UpperCAmelCase__							=				CLIPImageProcessingTester(self )




 @property
 def   UpperCamelCase__							(self ) ->					List[str]:






  """simple docstring"""





  return self.image_processor_tester.prepare_image_processor_dict()




 def   UpperCamelCase__							(self ) ->					Any:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  self.assertTrue(hasattr(__a       ,       'do_resize' ) )
  self.assertTrue(hasattr(__a       ,       'size' ) )
  self.assertTrue(hasattr(__a       ,       'do_center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'do_normalize' ) )
  self.assertTrue(hasattr(__a       ,       'image_mean' ) )
  self.assertTrue(hasattr(__a       ,       'image_std' ) )
  self.assertTrue(hasattr(__a       ,       'do_convert_rgb' ) )




 def   UpperCamelCase__							(self ) ->					str:






  """simple docstring"""





  UpperCAmelCase__							=				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} )

  UpperCAmelCase__							=				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   UpperCamelCase__							(self ) ->					List[Any]:






  """simple docstring"""





  pass




 def   UpperCamelCase__							(self ) ->					Dict:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random PIL images
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       Image.Image )

  # Test not batched input
  UpperCAmelCase__							=				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
  UpperCAmelCase__							=				image_processing(__a       ,       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   UpperCamelCase__							(self ) ->					Optional[Any]:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random numpy tensors
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a       ,       numpify=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       np.ndarray )

  # Test not batched input
  UpperCAmelCase__							=				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
  UpperCAmelCase__							=				image_processing(__a       ,       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   UpperCamelCase__							(self ) ->					Tuple:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random PyTorch tensors
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a       ,       torchify=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       torch.Tensor )

  # Test not batched input
  UpperCAmelCase__							=				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
  UpperCAmelCase__							=				image_processing(__a       ,       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     lowercase						(       _UpperCamelCase ,				unittest.TestCase					):



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE												=				CLIPImageProcessor if is_vision_available() else None




 def   UpperCamelCase__							(self ) ->					Optional[int]:






  """simple docstring"""





  UpperCAmelCase__							=				CLIPImageProcessingTester(self       ,       num_channels=4 )
  UpperCAmelCase__							=				3




 @property
 def   UpperCamelCase__							(self ) ->					Optional[int]:






  """simple docstring"""





  return self.image_processor_tester.prepare_image_processor_dict()




 def   UpperCamelCase__							(self ) ->					List[str]:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  self.assertTrue(hasattr(__a       ,       'do_resize' ) )
  self.assertTrue(hasattr(__a       ,       'size' ) )
  self.assertTrue(hasattr(__a       ,       'do_center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'center_crop' ) )
  self.assertTrue(hasattr(__a       ,       'do_normalize' ) )
  self.assertTrue(hasattr(__a       ,       'image_mean' ) )
  self.assertTrue(hasattr(__a       ,       'image_std' ) )
  self.assertTrue(hasattr(__a       ,       'do_convert_rgb' ) )




 def   UpperCamelCase__							(self ) ->					Tuple:






  """simple docstring"""





  pass




 def   UpperCamelCase__							(self ) ->					Optional[int]:






  """simple docstring"""





  UpperCAmelCase__							=				self.image_processing_class(**self.image_processor_dict )
  # create random PIL images
  UpperCAmelCase__							=				self.image_processor_tester.prepare_inputs(equal_resolution=__a )
  for image in image_inputs:
   self.assertIsInstance(__a       ,       Image.Image )

  # Test not batched input
  UpperCAmelCase__							=				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
  UpperCAmelCase__							=				image_processing(__a       ,       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'],
      )       ,       )



