


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


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import BlipImageProcessor


class      __lowerCamelCase	(       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=False     ,					__UpperCAmelCase=[0.48145466, 0.4578275, 0.40821073]     ,					__UpperCAmelCase=[0.26862954, 0.26130258, 0.27577711]     ,					__UpperCAmelCase=True     ,					)							->				List[str]:
          _a     						=					size if size is not None else {'''height''': 20, '''width''': 20}
          _a     						=					parent
          _a     						=					batch_size
          _a     						=					num_channels
          _a     						=					image_size
          _a     						=					min_resolution
          _a     						=					max_resolution
          _a     						=					do_resize
          _a     						=					size
          _a     						=					do_normalize
          _a     						=					image_mean
          _a     						=					image_std
          _a     						=					do_pad
          _a     						=					do_convert_rgb


     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          return {
              "do_resize": self.do_resize,
              "size": self.size,
              "do_normalize": self.do_normalize,
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_convert_rgb": self.do_convert_rgb,
              "do_pad": self.do_pad,
          }





     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=False				)							->				List[str]:
          assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

          if torchify:
               _a     						=					[torch.from_numpy(lowercase__				) for x in image_inputs]

          return image_inputs







@require_torch
@require_vision
class      __lowerCamelCase	(       a__	,       unittest.TestCase					):

     '''simple docstring'''
     A_							:						Optional[int]     =		BlipImageProcessor if is_vision_available() else None


     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          _a     						=					BlipImageProcessingTester(self				)


     @property
     def 				_UpperCAmelCase   (   self				)							->				List[Any]:
          return self.image_processor_tester.prepare_image_processor_dict()


     def 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          self.assertTrue(hasattr(lowercase__     ,					'''do_resize'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''size'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''do_normalize'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''image_mean'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''image_std'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''do_convert_rgb'''				)				)


     def 				_UpperCAmelCase   (   self				)							->				Optional[Any]:
          pass


     def 				_UpperCAmelCase   (   self				)							->				Any:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          # create random PIL images
          _a     						=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__				)
          for image in image_inputs:
               self.assertIsInstance(lowercase__     ,					Image.Image				)

          # Test not batched input
          _a     						=					image_processor(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
          _a     						=					image_processor(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 				_UpperCAmelCase   (   self				)							->				Any:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          # create random numpy tensors
          _a     						=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__     ,					numpify=lowercase__				)
          for image in image_inputs:
               self.assertIsInstance(lowercase__     ,					np.ndarray				)

          # Test not batched input
          _a     						=					image_processor(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
          _a     						=					image_processor(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 				_UpperCAmelCase   (   self				)							->				Dict:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          # create random PyTorch tensors
          _a     						=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__     ,					torchify=lowercase__				)
          for image in image_inputs:
               self.assertIsInstance(lowercase__     ,					torch.Tensor				)

          # Test not batched input
          _a     						=					image_processor(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
          _a     						=					image_processor(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'''],
              )     ,					)







@require_torch
@require_vision
class      __lowerCamelCase	(       a__	,       unittest.TestCase					):

     '''simple docstring'''
     A_							:						Any     =		BlipImageProcessor if is_vision_available() else None


     def 				_UpperCAmelCase   (   self				)							->				Optional[Any]:
          _a     						=					BlipImageProcessingTester(self     ,					num_channels=4				)
          _a     						=					3


     @property
     def 				_UpperCAmelCase   (   self				)							->				Dict:
          return self.image_processor_tester.prepare_image_processor_dict()


     def 				_UpperCAmelCase   (   self				)							->				Optional[int]:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          self.assertTrue(hasattr(lowercase__     ,					'''do_resize'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''size'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''do_normalize'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''image_mean'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''image_std'''				)				)
          self.assertTrue(hasattr(lowercase__     ,					'''do_convert_rgb'''				)				)


     def 				_UpperCAmelCase   (   self				)							->				Dict:
          pass





     def 				_UpperCAmelCase   (   self				)							->				Optional[Any]:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          # create random PIL images
          _a     						=					self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__				)
          for image in image_inputs:
               self.assertIsInstance(lowercase__     ,					Image.Image				)

          # Test not batched input
          _a     						=					image_processor(image_inputs[0]     ,					return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape     ,					(
                  1,
                  self.expected_encoded_image_num_channels,
                  self.image_processor_tester.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)

          # Test batched
          _a     						=					image_processor(lowercase__     ,					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.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)
def  __lowerCamelCase						(		lowerCAmelCase__      ,    lowerCAmelCase__      ,    lowerCAmelCase__      ):
       if hue < 0 or hue > 3_6_0:
              raise Exception('hue should be between 0 and 360'      )

       if saturation < 0 or saturation > 1:
              raise Exception('saturation should be between 0 and 1'      )

       if value < 0 or value > 1:
              raise Exception('value should be between 0 and 1'      )

       lowerCAmelCase__	      =   value * saturation
       lowerCAmelCase__	      =   hue / 6_0
       lowerCAmelCase__	      =   chroma * (1 - abs(hue_section % 2 - 1      ))
       lowerCAmelCase__	      =   value - chroma

       if hue_section >= 0 and hue_section <= 1:
              lowerCAmelCase__	      =   round(2_5_5 * (chroma + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (second_largest_component + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (match_value)      )
       elif hue_section > 1 and hue_section <= 2:
              lowerCAmelCase__	      =   round(2_5_5 * (second_largest_component + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (chroma + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (match_value)      )
       elif hue_section > 2 and hue_section <= 3:
              lowerCAmelCase__	      =   round(2_5_5 * (match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (chroma + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (second_largest_component + match_value)      )
       elif hue_section > 3 and hue_section <= 4:
              lowerCAmelCase__	      =   round(2_5_5 * (match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (second_largest_component + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (chroma + match_value)      )
       elif hue_section > 4 and hue_section <= 5:
              lowerCAmelCase__	      =   round(2_5_5 * (second_largest_component + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (chroma + match_value)      )
       else:
              lowerCAmelCase__	      =   round(2_5_5 * (chroma + match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (match_value)      )
              lowerCAmelCase__	      =   round(2_5_5 * (second_largest_component + match_value)      )

       return [red, green, blue]






def  __lowerCamelCase						(		lowerCAmelCase__      ,    lowerCAmelCase__      ,    lowerCAmelCase__      ):
       if red < 0 or red > 2_5_5:
              raise Exception('red should be between 0 and 255'      )

       if green < 0 or green > 2_5_5:
              raise Exception('green should be between 0 and 255'      )

       if blue < 0 or blue > 2_5_5:
              raise Exception('blue should be between 0 and 255'      )

       lowerCAmelCase__	      =   red / 2_5_5
       lowerCAmelCase__	      =   green / 2_5_5
       lowerCAmelCase__	      =   blue / 2_5_5
       lowerCAmelCase__	      =   max(lowerCAmelCase__      ,    lowerCAmelCase__      ,    lowerCAmelCase__      )
       lowerCAmelCase__	      =   value - min(lowerCAmelCase__      ,    lowerCAmelCase__      ,    lowerCAmelCase__      )
       lowerCAmelCase__	      =   0 if value == 0 else chroma / value

       if chroma == 0:
              lowerCAmelCase__	      =   0.0
       elif value == float_red:
              lowerCAmelCase__	      =   6_0 * (0 + (float_green - float_blue) / chroma)
       elif value == float_green:
              lowerCAmelCase__	      =   6_0 * (2 + (float_blue - float_red) / chroma)
       else:
              lowerCAmelCase__	      =   6_0 * (4 + (float_red - float_green) / chroma)

       lowerCAmelCase__	      =   (hue + 3_6_0) % 3_6_0

       return [hue, saturation, value]






def  __lowerCamelCase						(		lowerCAmelCase__      ,    lowerCAmelCase__      ):
       lowerCAmelCase__	      =   abs(hsv_a[0] - hsv_a[0]      ) < 0.2
       lowerCAmelCase__	      =   abs(hsv_a[1] - hsv_a[1]      ) < 0.0_0_2
       lowerCAmelCase__	      =   abs(hsv_a[2] - hsv_a[2]      ) < 0.0_0_2

       return check_hue and check_saturation and check_value
