





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


  def __init__(       self   ,__snake_case   ,__snake_case=7   ,__snake_case=3   ,__snake_case=1_8   ,__snake_case=3_0   ,__snake_case=4_0_0   ,__snake_case=True   ,__snake_case=None   ,__snake_case=True   ,__snake_case=False   ,__snake_case=[0.4814_5466, 0.457_8275, 0.4082_1073]   ,__snake_case=[0.2686_2954, 0.2613_0258, 0.2757_7711]   ,__snake_case=True   ,):



       """simple docstring"""





       A_					      =					size if size is not None else {'''height''': 2_0, '''width''': 2_0}
       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   ):



       """simple docstring"""





       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   ,__snake_case=False   ,__snake_case=False   ,__snake_case=False   ):



       """simple docstring"""





       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(
                         2_5_5   ,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(2_5_5   ,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(__snake_case   ,0   ,-1   )   ) for x in image_inputs]

       if torchify:
            A_					      =					[torch.from_numpy(__snake_case   ) for x in image_inputs]

       return image_inputs





@require_torch
@require_vision
class UpperCAmelCase_							(   _SCREAMING_SNAKE_CASE    ,		unittest.TestCase   ):
  __lowerCAmelCase      :	Tuple  	=      BlipImageProcessor if is_vision_available() else None


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					BlipImageProcessingTester(self   )


  @property
  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       return self.image_processor_tester.prepare_image_processor_dict()


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.image_processing_class(**self.image_processor_dict   )
       self.assertTrue(hasattr(__snake_case   ,'''do_resize'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''size'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''do_normalize'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''image_mean'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''image_std'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''do_convert_rgb'''   )   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       pass


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.image_processing_class(**self.image_processor_dict   )
       # create random PIL images
       A_					      =					self.image_processor_tester.prepare_inputs(equal_resolution=__snake_case   )
       for image in image_inputs:
            self.assertIsInstance(__snake_case   ,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(__snake_case   ,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   ):



       """simple docstring"""





       A_					      =					self.image_processing_class(**self.image_processor_dict   )
       # create random numpy tensors
       A_					      =					self.image_processor_tester.prepare_inputs(equal_resolution=__snake_case   ,numpify=__snake_case   )
       for image in image_inputs:
            self.assertIsInstance(__snake_case   ,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(__snake_case   ,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   ):



       """simple docstring"""





       A_					      =					self.image_processing_class(**self.image_processor_dict   )
       # create random PyTorch tensors
       A_					      =					self.image_processor_tester.prepare_inputs(equal_resolution=__snake_case   ,torchify=__snake_case   )
       for image in image_inputs:
            self.assertIsInstance(__snake_case   ,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(__snake_case   ,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 UpperCAmelCase_							(   _SCREAMING_SNAKE_CASE    ,		unittest.TestCase   ):
  __lowerCAmelCase      :	str  	=      BlipImageProcessor if is_vision_available() else None


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					BlipImageProcessingTester(self   ,num_channels=4   )
       A_					      =					3


  @property
  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       return self.image_processor_tester.prepare_image_processor_dict()


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.image_processing_class(**self.image_processor_dict   )
       self.assertTrue(hasattr(__snake_case   ,'''do_resize'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''size'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''do_normalize'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''image_mean'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''image_std'''   )   )
       self.assertTrue(hasattr(__snake_case   ,'''do_convert_rgb'''   )   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       pass




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.image_processing_class(**self.image_processor_dict   )
       # create random PIL images
       A_					      =					self.image_processor_tester.prepare_inputs(equal_resolution=__snake_case   )
       for image in image_inputs:
            self.assertIsInstance(__snake_case   ,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(__snake_case   ,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'''],
           )   ,)










from __future__ import annotations

def     UpperCAmelCase_						(						_UpperCAmelCase       :list							)  ->							int | float:
     '''simple docstring'''


     A_					      =					sorted(_UpperCAmelCase							)
     A_					      =					len(_UpperCAmelCase							)
     A_					      =					length >> 1
     return (
         (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
         if length % 2 == 0
         else sorted_list[mid_index]
     )

def     UpperCAmelCase_						(						)  ->							Optional[Any]:
     '''simple docstring'''


     import doctest

     doctest.testmod()


if __name__ == "__main__":
      main()



