





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				(  _lowerCamelCase   :	Union[str, Any]      , _lowerCamelCase   :	List[str]=10      , _lowerCamelCase   :	Union[str, Any]=10      , _lowerCamelCase   :	Optional[int]=False      , _lowerCamelCase   :	Union[str, Any]=False  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					List[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
												SCREAMING_SNAKE_CASE__  :					List[Any] =       [Image.fromarray(np.moveaxis(_lowerCamelCase      , 0      , -1  )  ) for frame in video]

						if torchify:
												SCREAMING_SNAKE_CASE__  :					str =       [torch.from_numpy(_lowerCamelCase  ) for frame in video]

						return video







def    UpperCAmelCase				(  _lowerCamelCase   :	Dict      , _lowerCamelCase   :	str=False      , _lowerCamelCase   :	Tuple=False      , _lowerCamelCase   :	List[str]=False  ):





						'''simple docstring'''


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

						SCREAMING_SNAKE_CASE__  :					str =       []
						for i in range(image_processor_tester.batch_size  ):
												if equal_resolution:
																		SCREAMING_SNAKE_CASE__  :					Any =       image_processor_tester.max_resolution
												else:
																		SCREAMING_SNAKE_CASE__  :					Tuple =       np.random.choice(
																		    np.arange(image_processor_tester.min_resolution      , image_processor_tester.max_resolution  )      , 2  )
																		SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       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				):






							"""simple docstring"""

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

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






							def   A_	(   self					:  int     )						->Tuple:
													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  (					lowercase__       ,				unittest.TestCase				):






							"""simple docstring"""

							snake_case_							=    TvltImageProcessor if is_vision_available() else None

							def   A_	(   self					:  Any     )						->List[str]:
													SCREAMING_SNAKE_CASE__  :					str =       TvltImageProcessorTester(self     )

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

							def   A_	(   self					:  Tuple     )						->str:
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       self.image_processing_class(**self.image_processor_dict     )
													self.assertTrue(hasattr(_lowerCAmelCase				,						"image_mean"     )     )
													self.assertTrue(hasattr(_lowerCAmelCase				,						"image_std"     )     )
													self.assertTrue(hasattr(_lowerCAmelCase				,						"do_normalize"     )     )
													self.assertTrue(hasattr(_lowerCAmelCase				,						"do_resize"     )     )
													self.assertTrue(hasattr(_lowerCAmelCase				,						"do_center_crop"     )     )
													self.assertTrue(hasattr(_lowerCAmelCase				,						"size"     )     )

							def   A_	(   self					:  Optional[int]     )						->int:
													# Initialize image_processor
													SCREAMING_SNAKE_CASE__  :					Tuple =       self.image_processing_class(**self.image_processor_dict     )
													# create random PIL videos
													SCREAMING_SNAKE_CASE__  :					Any =       prepare_video_inputs(self.image_processor_tester				,						equal_resolution=_lowerCAmelCase     )
													for video in video_inputs:
																			self.assertIsInstance(_lowerCAmelCase				,						_lowerCAmelCase     )
																			self.assertIsInstance(video[0]				,						Image.Image     )

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

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

													# Test not batched input
													SCREAMING_SNAKE_CASE__  :					List[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
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       image_processor(_lowerCAmelCase				,						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"],
													    )				,						)





import os

from apache_beam.io.filesystems import FileSystems
from apache_beam.pipeline import Pipeline

from .logging import get_logger


_UpperCAmelCase						:    List[str]      =  2 << 20  # 2mb
_UpperCAmelCase						:    Optional[int]      =  get_logger(__name__)




class  __lowerCAmelCase							(				lowerCAmelCase):






       def        SCREAMING_SNAKE_CASE     ( self:       str					):
         lowercase     :str   						=    self._options.get_all_options().get("runner"					)
         return runner in [None, "DirectRunner", "PortableRunner"]







def    UpperCAmelCase__	(  lowerCamelCase,      lowerCamelCase,      lowerCamelCase=False ):
  lowercase     :str   						=    FileSystems
  if fs.exists(lowerCamelCase ):
    if force_upload:
      logger.info(F"Remote path already exist: {remote_file_path}. Overwriting it as force_upload=True." )
    else:
      logger.info(F"Remote path already exist: {remote_file_path}. Skipping it as force_upload=False." )
      return
  with fs.create(lowerCamelCase ) as remote_file:
    with open(lowerCamelCase,      "rb" ) as local_file:
      lowercase     :int   						=    local_file.read(lowerCamelCase )
      while chunk:
        remote_file.write(lowerCamelCase )
        lowercase     :List[Any]   						=    local_file.read(lowerCamelCase )







def    UpperCAmelCase__	(  lowerCamelCase,      lowerCamelCase,      lowerCamelCase=False ):
  lowercase     :Union[str, Any]   						=    FileSystems
  if os.path.exists(lowerCamelCase ):
    if force_download:
      logger.info(F"Local path already exist: {remote_file_path}. Overwriting it as force_upload=True." )
    else:
      logger.info(F"Local path already exist: {remote_file_path}. Skipping it as force_upload=False." )
      return
  with fs.open(lowerCamelCase ) as remote_file:
    with open(lowerCamelCase,      "wb" ) as local_file:
      lowercase     :Optional[Any]   						=    remote_file.read(lowerCamelCase )
      while chunk:
        local_file.write(lowerCamelCase )
        lowercase     :Dict   						=    remote_file.read(lowerCamelCase )



