


'''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 A_					(				__SCREAMING_SNAKE_CASE :					Tuple	,   __SCREAMING_SNAKE_CASE :					Optional[int]=10	,   __SCREAMING_SNAKE_CASE :					Optional[Any]=10	,   __SCREAMING_SNAKE_CASE :					Optional[Any]=False	,   __SCREAMING_SNAKE_CASE :					Any=False       )     ->  Tuple:
     __SCREAMING_SNAKE_CASE					:							List[Any]			   = []
     for i in range(image_processor_tester.num_frames       ):
          video.append(np.random.randint(2_55	,   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
          __SCREAMING_SNAKE_CASE					:							Tuple			   = [Image.fromarray(np.moveaxis(lowercase_	,   0	,   -1       )       ) for frame in video]

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

     return video


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

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

     return video_inputs






class      SCREAMING_SNAKE_CASE_	( unittest.TestCase		):




   def __init__(      self					,      lowercase					,      lowercase=7					,      lowercase=3					,      lowercase=4					,      lowercase=1_8					,      lowercase=3_0					,      lowercase=4_0_0					,      lowercase=True					,      lowercase=None					,      lowercase=True					,      lowercase=[0.5, 0.5, 0.5]					,      lowercase=[0.5, 0.5, 0.5]					,      lowercase=True					,      lowercase=None					,      )			->						Union[str, Any]:


        '''simple docstring'''






        __SCREAMING_SNAKE_CASE					:							Optional[int]			   = size if size is not None else {'shortest_edge': 1_8}
        __SCREAMING_SNAKE_CASE					:							Dict			   = crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}

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




   def 			_snake_case   (      self       )			->						Optional[Any]:


        '''simple docstring'''






        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      SCREAMING_SNAKE_CASE_	( a_					,					unittest.TestCase		):
   __a					:      Any					=  TvltImageProcessor if is_vision_available() else None




   def 			_snake_case   (      self       )			->						Optional[int]:


        '''simple docstring'''






        __SCREAMING_SNAKE_CASE					:							Optional[Any]			   = TvltImageProcessorTester(self       )




   @property
   def 			_snake_case   (      self       )			->						Any:


        '''simple docstring'''






        return self.image_processor_tester.prepare_image_processor_dict()




   def 			_snake_case   (      self       )			->						str:


        '''simple docstring'''






        __SCREAMING_SNAKE_CASE					:							Tuple			   = self.image_processing_class(**self.image_processor_dict       )
        self.assertTrue(hasattr(lowercase					,      '''image_mean'''       )       )
        self.assertTrue(hasattr(lowercase					,      '''image_std'''       )       )
        self.assertTrue(hasattr(lowercase					,      '''do_normalize'''       )       )
        self.assertTrue(hasattr(lowercase					,      '''do_resize'''       )       )
        self.assertTrue(hasattr(lowercase					,      '''do_center_crop'''       )       )
        self.assertTrue(hasattr(lowercase					,      '''size'''       )       )




   def 			_snake_case   (      self       )			->						Optional[int]:


        '''simple docstring'''






        __SCREAMING_SNAKE_CASE					:							str			   = self.image_processing_class(**self.image_processor_dict       )
        # create random PIL videos
        __SCREAMING_SNAKE_CASE					:							int			   = prepare_video_inputs(self.image_processor_tester					,      equal_resolution=lowercase       )
        for video in video_inputs:
             self.assertIsInstance(lowercase					,      lowercase       )
             self.assertIsInstance(video[0]					,      Image.Image       )

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


        '''simple docstring'''






        __SCREAMING_SNAKE_CASE					:							Optional[int]			   = self.image_processing_class(**self.image_processor_dict       )
        # create random numpy tensors
        __SCREAMING_SNAKE_CASE					:							int			   = prepare_video_inputs(self.image_processor_tester					,      equal_resolution=lowercase					,      numpify=lowercase       )
        for video in video_inputs:
             self.assertIsInstance(lowercase					,      lowercase       )
             self.assertIsInstance(video[0]					,      np.ndarray       )

        # Test not batched input
        __SCREAMING_SNAKE_CASE					:							Optional[int]			   = 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
        __SCREAMING_SNAKE_CASE					:							int			   = image_processor(lowercase					,      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:


        '''simple docstring'''






        __SCREAMING_SNAKE_CASE					:							int			   = self.image_processing_class(**self.image_processor_dict       )
        # create random PyTorch tensors
        __SCREAMING_SNAKE_CASE					:							Optional[Any]			   = prepare_video_inputs(self.image_processor_tester					,      equal_resolution=lowercase					,      torchify=lowercase       )
        for video in video_inputs:
             self.assertIsInstance(lowercase					,      lowercase       )
             self.assertIsInstance(video[0]					,      torch.Tensor       )

        # Test not batched input
        __SCREAMING_SNAKE_CASE					:							Dict			   = 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
        __SCREAMING_SNAKE_CASE					:							Optional[int]			   = image_processor(lowercase					,      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 abc
import copy
import dataclasses
from dataclasses import dataclass
from typing import ClassVar, Dict, Type, TypeVar

from ..features import Features


_A      =     TypeVar("""T""", bound="""TaskTemplate""")






@dataclass(frozen=snake_case		)
class      SCREAMING_SNAKE_CASE_	( abc.ABC		):
			# `task` is not a ClassVar since we want it to be part of the `asdict` output for JSON serialization
			__a					:      str
			__a					:      ClassVar[Features]
			__a					:      ClassVar[Features]




			def 			_snake_case   (      self					,      lowercase       )			->						T:


								'''simple docstring'''






								return copy.deepcopy(self       )




			@property
			def 			_snake_case   (      self       )			->						Features:


								'''simple docstring'''






								return Features(**self.input_schema					,      **self.label_schema       )




			@property
			@abc.abstractmethod
			def 			_snake_case   (      self       )			->						Dict[str, str]:


								'''simple docstring'''






								raise NotImplementedError




			@classmethod
			def 			_snake_case   (      cls					,      lowercase       )			->						T:


								'''simple docstring'''






								__SCREAMING_SNAKE_CASE					:							Union[str, Any]			   = {f.name for f in dataclasses.fields(cls       )}
								return cls(**{k: v for k, v in template_dict.items() if k in field_names}       )


