



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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import VideoMAEImageProcessor


class 	a__		(    unittest.TestCase  ):


   def __init__(				self   :				int,  lowerCAmelCase   :				Dict,  lowerCAmelCase   :				Union[str, Any]=7,  lowerCAmelCase   :				Tuple=3,  lowerCAmelCase   :				int=10,  lowerCAmelCase   :				List[Any]=18,  lowerCAmelCase   :				int=30,  lowerCAmelCase   :				str=400,  lowerCAmelCase   :				Optional[int]=True,  lowerCAmelCase   :				str=None,  lowerCAmelCase   :				List[Any]=True,  lowerCAmelCase   :				str=[0.5, 0.5, 0.5],  lowerCAmelCase   :				Tuple=[0.5, 0.5, 0.5],  lowerCAmelCase   :				Optional[int]=None,  )     ->							Dict:
         lowercase	:   int											= size if size is not None else {'shortest_edge': 18}
         lowercase	:   Dict											= crop_size if crop_size is not None else {'height': 18, 'width': 18}

         lowercase	:   Any											= parent
         lowercase	:   Union[str, Any]											= batch_size
         lowercase	:   Any											= num_channels
         lowercase	:   str											= num_frames
         lowercase	:   Dict											= image_size
         lowercase	:   Any											= min_resolution
         lowercase	:   Union[str, Any]											= max_resolution
         lowercase	:   str											= do_resize
         lowercase	:   Optional[Any]											= size
         lowercase	:   Optional[Any]											= do_normalize
         lowercase	:   List[Any]											= image_mean
         lowercase	:   List[str]											= image_std
         lowercase	:   Optional[Any]											= crop_size






   def 							lowercase					(				self   :				List[Any]      )     ->							Union[str, Any]:
         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
             "crop_size": self.crop_size,
         }




@require_torch
@require_vision
class 	a__		(    SCREAMING_SNAKE_CASE__,	unittest.TestCase  ):
   _lowerCamelCase       				=					VideoMAEImageProcessor if is_vision_available() else None


   def 							lowercase					(				self   :				List[str]      )     ->							List[str]:
         lowercase	:   int											= VideoMAEImageProcessingTester(self      )


   @property
   def 							lowercase					(				self   :				List[str]      )     ->							Optional[int]:
         return self.image_processor_tester.prepare_image_processor_dict()


   def 							lowercase					(				self   :				int      )     ->							List[Any]:
         lowercase	:   List[str]											= self.image_processing_class(**self.image_processor_dict      )
         self.assertTrue(hasattr(lowerCAmelCase,  'image_mean'      )      )
         self.assertTrue(hasattr(lowerCAmelCase,  'image_std'      )      )
         self.assertTrue(hasattr(lowerCAmelCase,  'do_normalize'      )      )
         self.assertTrue(hasattr(lowerCAmelCase,  'do_resize'      )      )
         self.assertTrue(hasattr(lowerCAmelCase,  'do_center_crop'      )      )
         self.assertTrue(hasattr(lowerCAmelCase,  'size'      )      )


   def 							lowercase					(				self   :				Dict      )     ->							str:
         lowercase	:   Tuple											= self.image_processing_class.from_dict(self.image_processor_dict      )
         self.assertEqual(image_processor.size,  {'shortest_edge': 18}      )
         self.assertEqual(image_processor.crop_size,  {'height': 18, 'width': 18}      )

         lowercase	:   str											= 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 							lowercase					(				self   :				str      )     ->							Any:
         pass


   def 							lowercase					(				self   :				Optional[Any]      )     ->							Optional[int]:
         # Initialize image_processing
         lowercase	:   Tuple											= self.image_processing_class(**self.image_processor_dict      )
         # create random PIL videos
         lowercase	:   List[str]											= prepare_video_inputs(self.image_processor_tester,  equal_resolution=lowerCAmelCase      )
         for video in video_inputs:
               self.assertIsInstance(lowerCAmelCase,  lowerCAmelCase      )
               self.assertIsInstance(video[0],  Image.Image      )

         # Test not batched input
         lowercase	:   Optional[int]											= image_processing(video_inputs[0],  return_tensors='pt'      ).pixel_values
         self.assertEqual(
             encoded_videos.shape,  (
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             ),  )

         # Test batched
         lowercase	:   List[Any]											= image_processing(lowerCAmelCase,  return_tensors='pt'      ).pixel_values
         self.assertEqual(
             encoded_videos.shape,  (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             ),  )


   def 							lowercase					(				self   :				Dict      )     ->							int:
         # Initialize image_processing
         lowercase	:   Tuple											= self.image_processing_class(**self.image_processor_dict      )
         # create random numpy tensors
         lowercase	:   Optional[int]											= prepare_video_inputs(self.image_processor_tester,  equal_resolution=lowerCAmelCase,  numpify=lowerCAmelCase      )
         for video in video_inputs:
               self.assertIsInstance(lowerCAmelCase,  lowerCAmelCase      )
               self.assertIsInstance(video[0],  np.ndarray      )

         # Test not batched input
         lowercase	:   int											= image_processing(video_inputs[0],  return_tensors='pt'      ).pixel_values
         self.assertEqual(
             encoded_videos.shape,  (
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             ),  )

         # Test batched
         lowercase	:   str											= image_processing(lowerCAmelCase,  return_tensors='pt'      ).pixel_values
         self.assertEqual(
             encoded_videos.shape,  (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             ),  )






   def 							lowercase					(				self   :				Tuple      )     ->							Tuple:
         # Initialize image_processing
         lowercase	:   Dict											= self.image_processing_class(**self.image_processor_dict      )
         # create random PyTorch tensors
         lowercase	:   Dict											= prepare_video_inputs(self.image_processor_tester,  equal_resolution=lowerCAmelCase,  torchify=lowerCAmelCase      )
         for video in video_inputs:
               self.assertIsInstance(lowerCAmelCase,  lowerCAmelCase      )
               self.assertIsInstance(video[0],  torch.Tensor      )

         # Test not batched input
         lowercase	:   Optional[Any]											= image_processing(video_inputs[0],  return_tensors='pt'      ).pixel_values
         self.assertEqual(
             encoded_videos.shape,  (
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             ),  )

         # Test batched
         lowercase	:   Union[str, Any]											= image_processing(lowerCAmelCase,  return_tensors='pt'      ).pixel_values
         self.assertEqual(
             encoded_videos.shape,  (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             ),  )





"""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 	a__		(    unittest.TestCase,	SCREAMING_SNAKE_CASE__  ):


   def 							lowercase					(				self   :				Dict      )     ->							List[str]:
         lowercase	:   str											= load_tool('image-captioning'      )
         self.tool.setup()
         lowercase	:   List[str]											= load_tool('image-captioning',  remote=lowerCAmelCase      )


   def 							lowercase					(				self   :				str      )     ->							int:
         lowercase	:   Union[str, Any]											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   List[str]											= self.tool(lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )


   def 							lowercase					(				self   :				str      )     ->							List[str]:
         lowercase	:   str											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   Tuple											= self.remote_tool(lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )


   def 							lowercase					(				self   :				Tuple      )     ->							Union[str, Any]:
         lowercase	:   Tuple											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   Union[str, Any]											= self.tool(image=lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )






   def 							lowercase					(				self   :				str      )     ->							Optional[int]:
         lowercase	:   List[str]											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   str											= self.remote_tool(image=lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )
