




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 SwinaSRImageProcessor
     from transformers.image_transforms import get_image_size




class  __lowerCAmelCase							(				unittest.TestCase):






       def __init__( self:       Optional[Any]							,       _lowerCAmelCase:       Optional[Any]							,       _lowerCAmelCase:       Dict=7							,       _lowerCAmelCase:       Tuple=3							,       _lowerCAmelCase:       int=18							,       _lowerCAmelCase:       Optional[int]=30							,       _lowerCAmelCase:       Dict=4_00							,       _lowerCAmelCase:       List[Any]=True							,       _lowerCAmelCase:       Optional[Any]=1 / 2_55							,       _lowerCAmelCase:       List[Any]=True							,       _lowerCAmelCase:       Any=8							,       ):
         lowercase     :int   						=    parent
         lowercase     :Dict   						=    batch_size
         lowercase     :Any   						=    num_channels
         lowercase     :Tuple   						=    image_size
         lowercase     :Tuple   						=    min_resolution
         lowercase     :Optional[Any]   						=    max_resolution
         lowercase     :int   						=    do_rescale
         lowercase     :List[Any]   						=    rescale_factor
         lowercase     :Optional[int]   						=    do_pad
         lowercase     :Any   						=    pad_size






       def        SCREAMING_SNAKE_CASE     ( self:       str					):
         return {
             "do_rescale": self.do_rescale,
             "rescale_factor": self.rescale_factor,
             "do_pad": self.do_pad,
             "pad_size": self.pad_size,
         }






       def        SCREAMING_SNAKE_CASE     ( self:       int							,       _lowerCAmelCase:       Optional[int]=False							,       _lowerCAmelCase:       Optional[Any]=False							,       _lowerCAmelCase:       Optional[Any]=False					):
         assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

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

         if torchify:
           lowercase     :int   						=    [torch.from_numpy(_lowerCAmelCase					) for x in image_inputs]

         return image_inputs







@require_torch
@require_vision
class  __lowerCAmelCase							(				lowerCAmelCase   ,			unittest.TestCase):
       _a			       =    SwinaSRImageProcessor if is_vision_available() else None






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase     :int   						=    SwinaSRImageProcessingTester(self					)






       @property
       def        SCREAMING_SNAKE_CASE     ( self:       str					):
         return self.image_processor_tester.prepare_image_processor_dict()






       def        SCREAMING_SNAKE_CASE     ( self:       Any					):
         lowercase     :List[Any]   						=    self.image_processing_class(**self.image_processor_dict					)
         self.assertTrue(hasattr(_lowerCAmelCase							,       "do_rescale"					)					)
         self.assertTrue(hasattr(_lowerCAmelCase							,       "rescale_factor"					)					)
         self.assertTrue(hasattr(_lowerCAmelCase							,       "do_pad"					)					)
         self.assertTrue(hasattr(_lowerCAmelCase							,       "pad_size"					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[Any]					):
         pass






       def        SCREAMING_SNAKE_CASE     ( self:       List[str]							,       _lowerCAmelCase:       List[str]					):
         lowercase						,     lowercase     :Optional[Any]   						=    get_image_size(_lowerCAmelCase					)
         lowercase     :int   						=    self.image_processor_tester.pad_size

         lowercase     :Optional[int]   						=    (old_height // size + 1) * size - old_height
         lowercase     :str   						=    (old_width // size + 1) * size - old_width
         return old_height + pad_height, old_width + pad_width






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

         # Test not batched input
         lowercase     :Any   						=    image_processor(image_inputs[0]							,       return_tensors="pt"					).pixel_values
         lowercase						,     lowercase     :int   						=    self.calculate_expected_size(np.array(image_inputs[0]					)					)
         self.assertEqual(
             encoded_images.shape							,       (
                 1,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_width,
             )							,       )






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]					):
         # Initialize image_processor
         lowercase     :int   						=    self.image_processing_class(**self.image_processor_dict					)
         # create random numpy tensors
         lowercase     :Dict   						=    self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase							,       numpify=_lowerCAmelCase					)
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase							,       np.ndarray					)

         # Test not batched input
         lowercase     :List[str]   						=    image_processor(image_inputs[0]							,       return_tensors="pt"					).pixel_values
         lowercase						,     lowercase     :Optional[Any]   						=    self.calculate_expected_size(image_inputs[0]					)
         self.assertEqual(
             encoded_images.shape							,       (
                 1,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_width,
             )							,       )






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         # Initialize image_processor
         lowercase     :List[Any]   						=    self.image_processing_class(**self.image_processor_dict					)
         # create random PyTorch tensors
         lowercase     :Union[str, Any]   						=    self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase							,       torchify=_lowerCAmelCase					)
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase							,       torch.Tensor					)

         # Test not batched input
         lowercase     :Optional[Any]   						=    image_processor(image_inputs[0]							,       return_tensors="pt"					).pixel_values
         lowercase						,     lowercase     :List[Any]   						=    self.calculate_expected_size(image_inputs[0]					)
         self.assertEqual(
             encoded_images.shape							,       (
                 1,
                 self.image_processor_tester.num_channels,
                 expected_height,
                 expected_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  __lowerCAmelCase							(				unittest.TestCase   ,			lowerCAmelCase):






       def        SCREAMING_SNAKE_CASE     ( self:       Dict					):
         lowercase     :Any   						=    load_tool("image-segmentation"					)
         self.tool.setup()
         lowercase     :int   						=    load_tool("image-segmentation"							,       remote=_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       str					):
         lowercase     :Any   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :Dict   						=    self.tool(_lowerCAmelCase							,       "cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]					):
         lowercase     :Dict   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :List[Any]   						=    self.remote_tool(_lowerCAmelCase							,       "cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]					):
         lowercase     :Optional[Any]   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :Dict   						=    self.tool(image=_lowerCAmelCase							,       label="cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase     :int   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :List[str]   						=    self.remote_tool(image=_lowerCAmelCase							,       label="cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)



