


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        UpperCAmelCase_      (							__snake_case  ,		__snake_case=10  ,		__snake_case=10  ,		__snake_case=False  ,		__snake_case=False		)  ->    Optional[int]:







 """simple docstring"""


 _lowercase							   =[]
 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
  _lowercase							   =[Image.fromarray(np.moveaxis(__snake_case  ,		0  ,		-1		)		) for frame in video]

 if torchify:
  _lowercase							   =[torch.from_numpy(__snake_case		) for frame in video]

 return video
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case=False  ,		__snake_case=False  ,		__snake_case=False		)  ->    Tuple:







 """simple docstring"""


 assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

 return video_inputs







class    lowerCamelCase__     ( unittest.TestCase):





     def __init__(self	,  UpperCAmelCase	,  UpperCAmelCase=7	,  UpperCAmelCase=3	,  UpperCAmelCase=4	,  UpperCAmelCase=1_8	,  UpperCAmelCase=3_0	,  UpperCAmelCase=4_0_0	,  UpperCAmelCase=True	,  UpperCAmelCase=None	,  UpperCAmelCase=True	,  UpperCAmelCase=[0.5, 0.5, 0.5]	,  UpperCAmelCase=[0.5, 0.5, 0.5]	,  UpperCAmelCase=True	,  UpperCAmelCase=None	,  )  ->     Optional[Any]:
      _lowercase							   =size if size is not None else {'''shortest_edge''': 1_8}
      _lowercase							   =crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}

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





     def   __A      (self )  ->     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,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
      }







@require_torch
@require_vision
class    lowerCamelCase__     ( lowerCAmelCase     ,							unittest.TestCase):
     SCREAMING_SNAKE_CASE__       =    TvltImageProcessor if is_vision_available() else None





     def   __A      (self )  ->     Any:
      _lowercase							   =TvltImageProcessorTester(self )





     @property
     def   __A      (self )  ->     str:
      return self.image_processor_tester.prepare_image_processor_dict()





     def   __A      (self )  ->     Union[str, Any]:
      _lowercase							   =self.image_processing_class(**self.image_processor_dict )
      self.assertTrue(hasattr(UpperCAmelCase	,  '''image_mean''' ) )
      self.assertTrue(hasattr(UpperCAmelCase	,  '''image_std''' ) )
      self.assertTrue(hasattr(UpperCAmelCase	,  '''do_normalize''' ) )
      self.assertTrue(hasattr(UpperCAmelCase	,  '''do_resize''' ) )
      self.assertTrue(hasattr(UpperCAmelCase	,  '''do_center_crop''' ) )
      self.assertTrue(hasattr(UpperCAmelCase	,  '''size''' ) )





     def   __A      (self )  ->     Dict:
      # Initialize image_processor
      _lowercase							   =self.image_processing_class(**self.image_processor_dict )
      # create random PIL videos
      _lowercase							   =prepare_video_inputs(self.image_processor_tester	,  equal_resolution=UpperCAmelCase )
      for video in video_inputs:
       self.assertIsInstance(UpperCAmelCase	,  UpperCAmelCase )
       self.assertIsInstance(video[0]	,  Image.Image )

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

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

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





from ..utils import DummyObject, requires_backends







class    lowerCamelCase__     ( metaclass=lowerCAmelCase):
     SCREAMING_SNAKE_CASE__       =    ['''tensorflow_text''']





     def __init__(self	,  *UpperCAmelCase	,  **UpperCAmelCase )  ->     Any:
      requires_backends(self	,  ['''tensorflow_text'''] )

