code: 
  
  
  
  import json
  import os
  import tempfile
  
  from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_vision
  from transformers.utils import is_torch_available, is_vision_available
  
  
  if is_torch_available():
      import numpy as np
      import torch
  
  if is_vision_available():
      from PIL import Image
  
  
  
  
  def     UpperCAmelCase__ (							__snake_case						,				__snake_case=False						,				__snake_case=False						,				__snake_case=False							)   -> Optional[Any]:
    assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"
  
    _A           =    []
    for i in range(image_processor_tester.batch_size							):
      if equal_resolution:
        _A           =    _A           =    image_processor_tester.max_resolution
      else:
        # To avoid getting image width/height 0
        _A           =    image_processor_tester.min_resolution
        if getattr(__snake_case						,				'''size_divisor'''						,				__snake_case							):
          # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
          _A           =    max(image_processor_tester.size_divisor						,				__snake_case							)
        _A    ,   _A           =    np.random.choice(np.arange(__snake_case						,				image_processor_tester.max_resolution							)						,				2							)
      image_inputs.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
      _A           =    [Image.fromarray(np.moveaxis(__snake_case						,				0						,				-1							)							) for image in image_inputs]
  
    if torchify:
      _A           =    [torch.from_numpy(__snake_case							) for image in image_inputs]
  
    return image_inputs
  
  
  
  
  def     UpperCAmelCase__ (							__snake_case						,				__snake_case=10						,				__snake_case=10						,				__snake_case=False						,				__snake_case=False							)   -> List[str]:
    _A           =    []
    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
      _A           =    [Image.fromarray(np.moveaxis(__snake_case						,				0						,				-1							)							) for frame in video]
  
    if torchify:
      _A           =    [torch.from_numpy(__snake_case							) for frame in video]
  
    return video
  
  
  
  
  def     UpperCAmelCase__ (							__snake_case						,				__snake_case=False						,				__snake_case=False						,				__snake_case=False							)   -> Tuple:
    assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"
  
    _A           =    []
    for i in range(image_processor_tester.batch_size							):
      if equal_resolution:
        _A           =    _A           =    image_processor_tester.max_resolution
      else:
        _A    ,   _A           =    np.random.choice(
            np.arange(image_processor_tester.min_resolution						,				image_processor_tester.max_resolution							)						,				2							)
        _A           =    prepare_video(
            image_processor_tester=__snake_case						,				width=__snake_case						,				height=__snake_case						,				numpify=__snake_case						,				torchify=__snake_case						,				)
      video_inputs.append(__snake_case							)
  
    return video_inputs
  
  
  
  class 		_snake_case      :
  
  
  
  
  
   """simple docstring"""
  
  
  
   lowerCamelCase_          =			None
   def lowercase_		(						self					)	->							int:
  
  
  
  
     """simple docstring"""
  
  
  
  
  
  
  
     _A           =    self.image_processing_class(**self.image_processor_dict					)
     _A           =    json.loads(image_processor.to_json_string()					)
     for key, value in self.image_processor_dict.items():
       self.assertEqual(obj[key]	,    a					)
   def lowercase_		(						self					)	->							List[Any]:
  
  
  
  
     """simple docstring"""
  
  
  
  
  
  
  
     _A           =    self.image_processing_class(**self.image_processor_dict					)
  
     with tempfile.TemporaryDirectory() as tmpdirname:
       _A           =    os.path.join(a	,    '''image_processor.json'''					)
       image_processor_first.to_json_file(a					)
       _A           =    self.image_processing_class.from_json_file(a					)
  
     self.assertEqual(image_processor_second.to_dict()	,    image_processor_first.to_dict()					)
   def lowercase_		(						self					)	->							Union[str, Any]:
  
  
  
  
     """simple docstring"""
  
  
  
  
  
  
  
     _A           =    self.image_processing_class(**self.image_processor_dict					)
  
     with tempfile.TemporaryDirectory() as tmpdirname:
       _A           =    image_processor_first.save_pretrained(a					)[0]
       check_json_file_has_correct_format(a					)
       _A           =    self.image_processing_class.from_pretrained(a					)
  
     self.assertEqual(image_processor_second.to_dict()	,    image_processor_first.to_dict()					)
   def lowercase_		(						self					)	->							Tuple:
  
  
  
  
     """simple docstring"""
  
  
  
  
  
  
  
     _A           =    self.image_processing_class()
     self.assertIsNotNone(a					)
  
  
  
   @require_torch
   @require_vision
   def lowercase_		(						self					)	->							Optional[Any]:
  
  
  
  
     """simple docstring"""
  
  
  
  
  
  
  
     if self.test_cast_dtype is not None:
       # Initialize image_processor
       _A           =    self.image_processing_class(**self.image_processor_dict					)
  
       # create random PyTorch tensors
       _A           =    prepare_image_inputs(self.image_processor_tester	,    equal_resolution=a	,    torchify=a					)
  
       _A           =    image_processor(a	,    return_tensors='''pt'''					)
       # for layoutLM compatiblity
       self.assertEqual(encoding.pixel_values.device	,    torch.device('''cpu'''					)					)
       self.assertEqual(encoding.pixel_values.dtype	,    torch.floataa					)
  
       _A           =    image_processor(a	,    return_tensors='''pt'''					).to(torch.floataa					)
       self.assertEqual(encoding.pixel_values.device	,    torch.device('''cpu'''					)					)
       self.assertEqual(encoding.pixel_values.dtype	,    torch.floataa					)
  
       _A           =    image_processor(a	,    return_tensors='''pt'''					).to('''cpu'''	,    torch.bfloataa					)
       self.assertEqual(encoding.pixel_values.device	,    torch.device('''cpu'''					)					)
       self.assertEqual(encoding.pixel_values.dtype	,    torch.bfloataa					)
  
       with self.assertRaises(a					):
         _A           =    image_processor(a	,    return_tensors='''pt'''					).to(torch.bfloataa	,    '''cpu'''					)
  
       # Try with text + image feature
       _A           =    image_processor(a	,    return_tensors='''pt'''					)
       encoding.update({'''input_ids''': torch.LongTensor([[1, 2, 3], [4, 5, 6]]					)}					)
       _A           =    encoding.to(torch.floataa					)
  
       self.assertEqual(encoding.pixel_values.device	,    torch.device('''cpu'''					)					)
       self.assertEqual(encoding.pixel_values.dtype	,    torch.floataa					)
       self.assertEqual(encoding.input_ids.dtype	,    torch.long					)
style_context: 
  
  
  
  from ..utils import requires_backends
  
  
  __lowerCamelCase  				=					None
  
  
  
  class 		_snake_case      :
  
  
  
  
  
   """simple docstring"""
  
  
  
   def __init__(						self	,    *a	,    **a					)	->							Tuple:
  
  
  
  
     """simple docstring"""
  
  
  
  
  
  
  
     requires_backends(self	,    ['''detectron2''']					)
  
  
  
   @classmethod
   def lowercase_		(						cls	,    *a	,    **a					)	->							Optional[int]:
  
  
  
  
     """simple docstring"""
  
  
  
  
  
  
  
     requires_backends(cls	,    ['''detectron2''']					)