'''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 TvltImageProcessor



def 						__snake_case (       lowerCAmelCase       :     Optional[int]      ,  lowerCAmelCase       :     Dict=10      ,  lowerCAmelCase       :     Tuple=10      ,  lowerCAmelCase       :     Optional[int]=False      ,  lowerCAmelCase       :     List[str]=False			)     ->   List[Any]:
 __UpperCAmelCase         =					[]
 for i in range(image_processor_tester.num_frames			):
  video.append(np.random.randint(255      ,  size=(image_processor_tester.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(lowerCAmelCase      ,  0      ,  -1			)			) for frame in video]

 if torchify:
  __UpperCAmelCase         =					[torch.from_numpy(lowerCAmelCase			) for frame in video]

 return video



def 						__snake_case (       lowerCAmelCase       :     int      ,  lowerCAmelCase       :     Union[str, Any]=False      ,  lowerCAmelCase       :     Union[str, Any]=False      ,  lowerCAmelCase       :     List[Any]=False			)     ->   List[Any]:
 assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

 __UpperCAmelCase         =					[]
 for i in range(image_processor_tester.batch_size			):
  if equal_resolution:
   __UpperCAmelCase         =					__UpperCAmelCase         =					image_processor_tester.max_resolution
  else:
   __UpperCAmelCase  ,			__UpperCAmelCase         =					np.random.choice(
       np.arange(image_processor_tester.min_resolution      ,  image_processor_tester.max_resolution			)      ,  2			)
   __UpperCAmelCase         =					prepare_video(
       image_processor_tester=lowerCAmelCase      ,  width=lowerCAmelCase      ,  height=lowerCAmelCase      ,  numpify=lowerCAmelCase      ,  torchify=lowerCAmelCase      ,  )
  video_inputs.append(lowerCAmelCase			)

 return video_inputs

class   _lowercase(     unittest.TestCase				):



   """simple docstring"""





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

    __UpperCAmelCase         =					parent
    __UpperCAmelCase         =					batch_size
    __UpperCAmelCase         =					num_channels
    __UpperCAmelCase         =					num_frames
    __UpperCAmelCase         =					image_size
    __UpperCAmelCase         =					min_resolution
    __UpperCAmelCase         =					max_resolution
    __UpperCAmelCase         =					do_resize
    __UpperCAmelCase         =					size
    __UpperCAmelCase         =					do_normalize
    __UpperCAmelCase         =					image_mean
    __UpperCAmelCase         =					image_std
    __UpperCAmelCase         =					do_center_crop
    __UpperCAmelCase         =					crop_size





   def 			snake_case	(							self:    Optional[int]	):
    return {
        "image_mean": self.image_mean,
        "image_std": self.image_std,
        "do_normalize": self.do_normalize,
        "do_resize": self.do_resize,
        "size": self.size,
        "do_center_crop": self.do_center_crop,
        "crop_size": self.crop_size,
    }




@require_torch
@require_vision
class   _lowercase(     _lowerCamelCase   ,unittest.TestCase				):



   """simple docstring"""





   __lowerCamelCase    =  TvltImageProcessor if is_vision_available() else None





   def 			snake_case	(							self:    Dict	):
    __UpperCAmelCase         =					TvltImageProcessorTester(self	)





   @property
   def 			snake_case	(							self:    Dict	):
    return self.image_processor_tester.prepare_image_processor_dict()





   def 			snake_case	(							self:    Tuple	):
    __UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict	)
    self.assertTrue(hasattr(a			,'image_mean'	)	)
    self.assertTrue(hasattr(a			,'image_std'	)	)
    self.assertTrue(hasattr(a			,'do_normalize'	)	)
    self.assertTrue(hasattr(a			,'do_resize'	)	)
    self.assertTrue(hasattr(a			,'do_center_crop'	)	)
    self.assertTrue(hasattr(a			,'size'	)	)





   def 			snake_case	(							self:    Any	):
    # Initialize image_processor
    __UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict	)
    # create random PIL videos
    __UpperCAmelCase         =					prepare_video_inputs(self.image_processor_tester			,equal_resolution=a	)
    for video in video_inputs:
     self.assertIsInstance(a			,a	)
     self.assertIsInstance(video[0]			,Image.Image	)

    # Test not batched input
    __UpperCAmelCase         =					image_processor(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
    __UpperCAmelCase         =					image_processor(a			,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 			snake_case	(							self:    Any	):
    # Initialize image_processor
    __UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict	)
    # create random numpy tensors
    __UpperCAmelCase         =					prepare_video_inputs(self.image_processor_tester			,equal_resolution=a			,numpify=a	)
    for video in video_inputs:
     self.assertIsInstance(a			,a	)
     self.assertIsInstance(video[0]			,np.ndarray	)

    # Test not batched input
    __UpperCAmelCase         =					image_processor(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
    __UpperCAmelCase         =					image_processor(a			,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 			snake_case	(							self:    Any	):
    # Initialize image_processor
    __UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict	)
    # create random PyTorch tensors
    __UpperCAmelCase         =					prepare_video_inputs(self.image_processor_tester			,equal_resolution=a			,torchify=a	)
    for video in video_inputs:
     self.assertIsInstance(a			,a	)
     self.assertIsInstance(video[0]			,torch.Tensor	)

    # Test not batched input
    __UpperCAmelCase         =					image_processor(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
    __UpperCAmelCase         =					image_processor(a			,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   _lowercase(     unittest.TestCase   ,_lowerCamelCase				):



   """simple docstring"""





   def 			snake_case	(							self:    Optional[int]	):
    __UpperCAmelCase         =					load_tool('image-captioning'	)
    self.tool.setup()
    __UpperCAmelCase         =					load_tool('image-captioning'			,remote=a	)





   def 			snake_case	(							self:    List[Any]	):
    __UpperCAmelCase         =					Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'	)	) / '000000039769.png'	)
    __UpperCAmelCase         =					self.tool(a	)
    self.assertEqual(a			,'two cats sleeping on a couch'	)





   def 			snake_case	(							self:    List[Any]	):
    __UpperCAmelCase         =					Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'	)	) / '000000039769.png'	)
    __UpperCAmelCase         =					self.remote_tool(a	)
    self.assertEqual(a			,'two cats sleeping on a couch'	)





   def 			snake_case	(							self:    Optional[Any]	):
    __UpperCAmelCase         =					Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'	)	) / '000000039769.png'	)
    __UpperCAmelCase         =					self.tool(image=a	)
    self.assertEqual(a			,'two cats sleeping on a couch'	)





   def 			snake_case	(							self:    Tuple	):
    __UpperCAmelCase         =					Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'	)	) / '000000039769.png'	)
    __UpperCAmelCase         =					self.remote_tool(image=a	)
    self.assertEqual(a			,'two cats sleeping on a couch'	)



