




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

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

  return video



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

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






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

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






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







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






       def        SCREAMING_SNAKE_CASE     ( self:       Any					):
         lowercase     :str   						=    TvltImageProcessorTester(self					)






       @property
       def        SCREAMING_SNAKE_CASE     ( self:       Optional[int]					):
         return self.image_processor_tester.prepare_image_processor_dict()






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase     :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        SCREAMING_SNAKE_CASE     ( self:       Optional[int]					):
         # Initialize image_processor
         lowercase     :Tuple   						=    self.image_processing_class(**self.image_processor_dict					)
         # create random PIL videos
         lowercase     :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
         lowercase     :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
         lowercase     :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        SCREAMING_SNAKE_CASE     ( self:       List[Any]					):
         # Initialize image_processor
         lowercase     :Dict   						=    self.image_processing_class(**self.image_processor_dict					)
         # create random numpy tensors
         lowercase     :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
         lowercase     :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
         lowercase     :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        SCREAMING_SNAKE_CASE     ( self:       List[str]					):
         # Initialize image_processor
         lowercase     :Optional[int]   						=    self.image_processing_class(**self.image_processor_dict					)
         # create random PyTorch tensors
         lowercase     :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
         lowercase     :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
         lowercase     :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 )



