
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    _a (						lowerCamelCase,     lowerCamelCase=10,     lowerCamelCase=10,     lowerCamelCase=False,     lowerCamelCase=False							):
 lowerCamelCase		:     str          =				[]
 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
  lowerCamelCase		:     str          =				[Image.fromarray(np.moveaxis(lowerCamelCase,     0,     -1							)							) for frame in video]

 if torchify:
  lowerCamelCase		:     Union[str, Any]          =				[torch.from_numpy(lowerCamelCase							) for frame in video]

 return video





def    _a (						lowerCamelCase,     lowerCamelCase=False,     lowerCamelCase=False,     lowerCamelCase=False							):
 assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

 return video_inputs





class 							A__		(     unittest.TestCase):
      def __init__(				self   ,  __magic_name__   ,  __magic_name__=7   ,  __magic_name__=3   ,  __magic_name__=4   ,  __magic_name__=1_8   ,  __magic_name__=3_0   ,  __magic_name__=4_0_0   ,  __magic_name__=True   ,  __magic_name__=None   ,  __magic_name__=True   ,  __magic_name__=[0.5, 0.5, 0.5]   ,  __magic_name__=[0.5, 0.5, 0.5]   ,  __magic_name__=True   ,  __magic_name__=None   ,  ):
       lowerCamelCase		:     Union[str, Any]          =				size if size is not None else {"""shortest_edge""": 1_8}
       lowerCamelCase		:     Any          =				crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}

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






      def 						UpperCamelCase__							(				self				):
       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 							A__		(     __SCREAMING_SNAKE_CASE  ,       unittest.TestCase):
      _UpperCAmelCase    :   List[Any]   	=      TvltImageProcessor if is_vision_available() else None
      def 						UpperCamelCase__							(				self				):
       lowerCamelCase		:     Optional[int]          =				TvltImageProcessorTester(self				)
      @property
      def 						UpperCamelCase__							(				self				):
       return self.image_processor_tester.prepare_image_processor_dict()
      def 						UpperCamelCase__							(				self				):
       lowerCamelCase		:     int          =				self.image_processing_class(**self.image_processor_dict				)
       self.assertTrue(hasattr(__magic_name__   ,  """image_mean"""				)				)
       self.assertTrue(hasattr(__magic_name__   ,  """image_std"""				)				)
       self.assertTrue(hasattr(__magic_name__   ,  """do_normalize"""				)				)
       self.assertTrue(hasattr(__magic_name__   ,  """do_resize"""				)				)
       self.assertTrue(hasattr(__magic_name__   ,  """do_center_crop"""				)				)
       self.assertTrue(hasattr(__magic_name__   ,  """size"""				)				)
      def 						UpperCamelCase__							(				self				):
       # Initialize image_processor
       lowerCamelCase		:     Tuple          =				self.image_processing_class(**self.image_processor_dict				)
       # create random PIL videos
       lowerCamelCase		:     Any          =				prepare_video_inputs(self.image_processor_tester   ,  equal_resolution=__magic_name__				)
       for video in video_inputs:
        self.assertIsInstance(__magic_name__   ,  __magic_name__				)
        self.assertIsInstance(video[0]   ,  Image.Image				)

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

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

       # Test not batched input
       lowerCamelCase		:     List[Any]          =				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
       lowerCamelCase		:     List[str]          =				image_processor(__magic_name__   ,  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"""],
           )   ,  )






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
    is_vision_available,
)


_lowerCamelCase        ={
    """configuration_tvlt""": ["""TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TvltConfig"""],
    """feature_extraction_tvlt""": ["""TvltFeatureExtractor"""],
    """processing_tvlt""": ["""TvltProcessor"""],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _lowerCamelCase        =[
          """TVLT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """TvltModel""",
          """TvltForPreTraining""",
          """TvltForAudioVisualClassification""",
          """TvltPreTrainedModel""",
      ]

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _lowerCamelCase        =["""TvltImageProcessor"""]


if TYPE_CHECKING:
      from .configuration_tvlt import TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP, TvltConfig
      from .processing_tvlt import TvltProcessor
      from .feature_extraction_tvlt import TvltFeatureExtractor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tvlt import (
                TVLT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TvltForAudioVisualClassification,
                TvltForPreTraining,
                TvltModel,
                TvltPreTrainedModel,
            )

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .image_processing_tvlt import TvltImageProcessor


else:
      import sys

      _lowerCamelCase        =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




