

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 VivitImageProcessor



class   lowercase_   (  unittest.TestCase					):



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

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







      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					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,
            "crop_size": self.crop_size,
        }



@require_torch
@require_vision
class   lowercase_   (  _a       ,				unittest.TestCase					):
      UpperCamelCase_      :		Union[str, Any]										=    VivitImageProcessor if is_vision_available() else None



      def 						UpperCamelCase_		(      self				:    int		)   ->					Union[str, Any]:
        _snake_case						=							VivitImageProcessingTester(self		)



      @property
      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					Optional[Any]:
        return self.image_processor_tester.prepare_image_processor_dict()



      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					List[Any]:
        _snake_case						=							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 						UpperCamelCase_		(      self				:    List[str]		)   ->					int:
        _snake_case						=							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}		)

        _snake_case						=							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 						UpperCamelCase_		(      self				:    Tuple		)   ->					Any:
        # Initialize image_processing
        _snake_case						=							self.image_processing_class(**self.image_processor_dict		)
        # create random PIL videos
        _snake_case						=							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
        _snake_case						=							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
        _snake_case						=							image_processing(_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 						UpperCamelCase_		(      self				:    List[Any]		)   ->					List[str]:
        # Initialize image_processing
        _snake_case						=							self.image_processing_class(**self.image_processor_dict		)
        # create random numpy tensors
        _snake_case						=							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
        _snake_case						=							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
        _snake_case						=							image_processing(_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 						UpperCamelCase_		(      self				:    List[Any]		)   ->					Optional[Any]:
        # Initialize image_processing
        _snake_case						=							self.image_processing_class(**self.image_processor_dict		)
        # create random PyTorch tensors
        _snake_case						=							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
        _snake_case						=							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
        _snake_case						=							image_processing(_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'''],
            )      , )




__A      		=     {
    '''Pillow''': '''Pillow<10.0.0''',
    '''accelerate''': '''accelerate>=0.20.3''',
    '''av''': '''av==9.2.0''',
    '''beautifulsoup4''': '''beautifulsoup4''',
    '''black''': '''black~=23.1''',
    '''codecarbon''': '''codecarbon==1.2.0''',
    '''cookiecutter''': '''cookiecutter==1.7.3''',
    '''dataclasses''': '''dataclasses''',
    '''datasets''': '''datasets!=2.5.0''',
    '''decord''': '''decord==0.6.0''',
    '''deepspeed''': '''deepspeed>=0.9.3''',
    '''diffusers''': '''diffusers''',
    '''dill''': '''dill<0.3.5''',
    '''evaluate''': '''evaluate>=0.2.0''',
    '''fairscale''': '''fairscale>0.3''',
    '''faiss-cpu''': '''faiss-cpu''',
    '''fastapi''': '''fastapi''',
    '''filelock''': '''filelock''',
    '''flax''': '''flax>=0.4.1,<=0.7.0''',
    '''ftfy''': '''ftfy''',
    '''fugashi''': '''fugashi>=1.0''',
    '''GitPython''': '''GitPython<3.1.19''',
    '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''',
    '''huggingface-hub''': '''huggingface-hub>=0.14.1,<1.0''',
    '''importlib_metadata''': '''importlib_metadata''',
    '''ipadic''': '''ipadic>=1.0.0,<2.0''',
    '''isort''': '''isort>=5.5.4''',
    '''jax''': '''jax>=0.2.8,!=0.3.2,<=0.4.13''',
    '''jaxlib''': '''jaxlib>=0.1.65,<=0.4.13''',
    '''jieba''': '''jieba''',
    '''kenlm''': '''kenlm''',
    '''keras-nlp''': '''keras-nlp>=0.3.1''',
    '''librosa''': '''librosa''',
    '''nltk''': '''nltk''',
    '''natten''': '''natten>=0.14.6''',
    '''numpy''': '''numpy>=1.17''',
    '''onnxconverter-common''': '''onnxconverter-common''',
    '''onnxruntime-tools''': '''onnxruntime-tools>=1.4.2''',
    '''onnxruntime''': '''onnxruntime>=1.4.0''',
    '''opencv-python''': '''opencv-python''',
    '''optuna''': '''optuna''',
    '''optax''': '''optax>=0.0.8,<=0.1.4''',
    '''packaging''': '''packaging>=20.0''',
    '''parameterized''': '''parameterized''',
    '''phonemizer''': '''phonemizer''',
    '''protobuf''': '''protobuf''',
    '''psutil''': '''psutil''',
    '''pyyaml''': '''pyyaml>=5.1''',
    '''pydantic''': '''pydantic<2''',
    '''pytest''': '''pytest>=7.2.0''',
    '''pytest-timeout''': '''pytest-timeout''',
    '''pytest-xdist''': '''pytest-xdist''',
    '''python''': '''python>=3.8.0''',
    '''ray[tune]''': '''ray[tune]''',
    '''regex''': '''regex!=2019.12.17''',
    '''requests''': '''requests''',
    '''rhoknp''': '''rhoknp>=1.1.0,<1.3.1''',
    '''rjieba''': '''rjieba''',
    '''rouge-score''': '''rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1''',
    '''ruff''': '''ruff>=0.0.241,<=0.0.259''',
    '''sacrebleu''': '''sacrebleu>=1.4.12,<2.0.0''',
    '''sacremoses''': '''sacremoses''',
    '''safetensors''': '''safetensors>=0.3.1''',
    '''sagemaker''': '''sagemaker>=2.31.0''',
    '''scikit-learn''': '''scikit-learn''',
    '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''',
    '''sigopt''': '''sigopt''',
    '''starlette''': '''starlette''',
    '''sudachipy''': '''sudachipy>=0.6.6''',
    '''sudachidict_core''': '''sudachidict_core>=20220729''',
    '''tensorflow-cpu''': '''tensorflow-cpu>=2.6,<2.14''',
    '''tensorflow''': '''tensorflow>=2.6,<2.14''',
    '''tensorflow-text''': '''tensorflow-text<2.14''',
    '''tf2onnx''': '''tf2onnx''',
    '''timeout-decorator''': '''timeout-decorator''',
    '''timm''': '''timm''',
    '''tokenizers''': '''tokenizers>=0.11.1,!=0.11.3,<0.14''',
    '''torch''': '''torch>=1.9,!=1.12.0''',
    '''torchaudio''': '''torchaudio''',
    '''torchvision''': '''torchvision''',
    '''pyctcdecode''': '''pyctcdecode>=0.4.0''',
    '''tqdm''': '''tqdm>=4.27''',
    '''unidic''': '''unidic>=1.0.2''',
    '''unidic_lite''': '''unidic_lite>=1.0.7''',
    '''urllib3''': '''urllib3<2.0.0''',
    '''uvicorn''': '''uvicorn''',
}

