
'''simple docstring'''





import os

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

from .logging import get_logger


SCREAMING_SNAKE_CASE    =					2 << 2_0  # 2mb
SCREAMING_SNAKE_CASE    =					get_logger(__name__)






class        UpperCAmelCase_       (						A							):

 '''simple docstring'''







 def        UpperCamelCase     (				self		:     Optional[Any]   ):



   '''simple docstring'''




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







def snake_case_    (				lowercase__      ,					lowercase__      ,					lowercase__=False		):
  UpperCAmelCase__			:				Union[str, Any]												=       FileSystems
  if fs.exists(lowercase__		):
    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(lowercase__		) as remote_file:
    with open(lowercase__      ,					"rb"		) as local_file:
      UpperCAmelCase__			:				List[str]												=       local_file.read(lowercase__		)
      while chunk:
        remote_file.write(lowercase__		)
        UpperCAmelCase__			:				str												=       local_file.read(lowercase__		)







def snake_case_    (				lowercase__      ,					lowercase__      ,					lowercase__=False		):
  UpperCAmelCase__			:				List[str]												=       FileSystems
  if os.path.exists(lowercase__		):
    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(lowercase__		) as remote_file:
    with open(lowercase__      ,					"wb"		) as local_file:
      UpperCAmelCase__			:				Tuple												=       remote_file.read(lowercase__		)
      while chunk:
        local_file.write(lowercase__		)
        UpperCAmelCase__			:				Tuple												=       remote_file.read(lowercase__		)





'''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, prepare_video_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import VideoMAEImageProcessor






class        UpperCAmelCase_       (						unittest.TestCase							):

 '''simple docstring'''







 def __init__(				self		:     str ,							snake_case__		:     Optional[int] ,							snake_case__		:     Union[str, Any]=7 ,							snake_case__		:     Any=3 ,							snake_case__		:     Tuple=10 ,							snake_case__		:     Dict=18 ,							snake_case__		:     Dict=30 ,							snake_case__		:     Tuple=4_00 ,							snake_case__		:     str=True ,							snake_case__		:     Optional[Any]=None ,							snake_case__		:     Union[str, Any]=True ,							snake_case__		:     List[Any]=[0.5, 0.5, 0.5] ,							snake_case__		:     Dict=[0.5, 0.5, 0.5] ,							snake_case__		:     Tuple=None ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       size if size is not None else {"shortest_edge": 18}
   UpperCAmelCase__			:				Optional[int]												=       crop_size if crop_size is not None else {"height": 18, "width": 18}

   UpperCAmelCase__			:				List[Any]												=       parent
   UpperCAmelCase__			:				Optional[Any]												=       batch_size
   UpperCAmelCase__			:				int												=       num_channels
   UpperCAmelCase__			:				str												=       num_frames
   UpperCAmelCase__			:				List[Any]												=       image_size
   UpperCAmelCase__			:				Optional[int]												=       min_resolution
   UpperCAmelCase__			:				int												=       max_resolution
   UpperCAmelCase__			:				Optional[int]												=       do_resize
   UpperCAmelCase__			:				List[str]												=       size
   UpperCAmelCase__			:				Optional[int]												=       do_normalize
   UpperCAmelCase__			:				int												=       image_mean
   UpperCAmelCase__			:				Any												=       image_std
   UpperCAmelCase__			:				Optional[int]												=       crop_size







 def        UpperCamelCase     (				self		:     Union[str, 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,
       "crop_size": self.crop_size,
   }






@require_torch
@require_vision
class        UpperCAmelCase_       (						A				,							unittest.TestCase							):

 '''simple docstring'''
 lowercase_				:						Any				=       VideoMAEImageProcessor if is_vision_available() else None







 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[int]												=       VideoMAEImageProcessingTester(self   )







 @property
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   return self.image_processor_tester.prepare_image_processor_dict()







 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				str												=       self.image_processing_class(**self.image_processor_dict   )
   self.assertTrue(hasattr(snake_case__ ,							"image_mean"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"image_std"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_normalize"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_resize"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_center_crop"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"size"   )   )







 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       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}   )

   UpperCAmelCase__			:				Union[str, Any]												=       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		:     Optional[int]   ):



   '''simple docstring'''




   pass







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




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

   # Test not batched input
   UpperCAmelCase__			:				Dict												=       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
   UpperCAmelCase__			:				Tuple												=       image_processing(snake_case__ ,							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		:     Union[str, Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Dict												=       self.image_processing_class(**self.image_processor_dict   )
   # create random numpy tensors
   UpperCAmelCase__			:				List[Any]												=       prepare_video_inputs(self.image_processor_tester ,							equal_resolution=snake_case__ ,							numpify=snake_case__   )
   for video in video_inputs:
     self.assertIsInstance(snake_case__ ,							snake_case__   )
     self.assertIsInstance(video[0] ,							np.ndarray   )

   # Test not batched input
   UpperCAmelCase__			:				Dict												=       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
   UpperCAmelCase__			:				Optional[int]												=       image_processing(snake_case__ ,							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		:     Dict   ):



   '''simple docstring'''




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

   # Test not batched input
   UpperCAmelCase__			:				int												=       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
   UpperCAmelCase__			:				Optional[Any]												=       image_processing(snake_case__ ,							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"],
       ) ,							)



