





'''simple docstring'''


import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import DonutImageProcessor

class    lowerCAmelCase_		(		unittest.TestCase		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase=7			,     _lowerCAmelCase=3			,     _lowerCAmelCase=18			,     _lowerCAmelCase=30			,     _lowerCAmelCase=400			,     _lowerCAmelCase=True			,     _lowerCAmelCase=None			,     _lowerCAmelCase=True			,     _lowerCAmelCase=False			,     _lowerCAmelCase=True			,     _lowerCAmelCase=True			,     _lowerCAmelCase=[0.5, 0.5, 0.5]			,     _lowerCAmelCase=[0.5, 0.5, 0.5]			,     )    ->							Optional[int]:
     _lowerCAmelCase				=      parent
     _lowerCAmelCase				=      batch_size
     _lowerCAmelCase				=      num_channels
     _lowerCAmelCase				=      image_size
     _lowerCAmelCase				=      min_resolution
     _lowerCAmelCase				=      max_resolution
     _lowerCAmelCase				=      do_resize
     _lowerCAmelCase				=      size if size is not None else {"height": 18, "width": 20}
     _lowerCAmelCase				=      do_thumbnail
     _lowerCAmelCase				=      do_align_axis
     _lowerCAmelCase				=      do_pad
     _lowerCAmelCase				=      do_normalize
     _lowerCAmelCase				=      image_mean
     _lowerCAmelCase				=      image_std





 def 							_snake_case   (	self		)    ->							Tuple:
     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_thumbnail": self.do_thumbnail,
         "do_align_long_axis": self.do_align_axis,
         "do_pad": self.do_pad,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
     }







@require_torch
@require_vision
class    lowerCAmelCase_		(		__magic_name__						,unittest.TestCase		):
 __lowerCamelCase						:							Optional[Any]         =					DonutImageProcessor if is_vision_available() else None




 def 							_snake_case   (	self		)    ->							List[Any]:
     _lowerCAmelCase				=      DonutImageProcessingTester(self		)




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




 def 							_snake_case   (	self		)    ->							int:
     _lowerCAmelCase				=      self.image_processing_class(**self.image_processor_dict		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "do_resize"		)		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "size"		)		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "do_thumbnail"		)		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "do_align_long_axis"		)		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "do_pad"		)		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "do_normalize"		)		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "image_mean"		)		)
     self.assertTrue(hasattr(_lowerCAmelCase			,     "image_std"		)		)




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      self.image_processing_class.from_dict(self.image_processor_dict		)
     self.assertEqual(image_processor.size			,     {"height": 18, "width": 20}		)

     _lowerCAmelCase				=      self.image_processing_class.from_dict(self.image_processor_dict			,     size=42		)
     self.assertEqual(image_processor.size			,     {"height": 42, "width": 42}		)

     # Previous config had dimensions in (width, height) order
     _lowerCAmelCase				=      self.image_processing_class.from_dict(self.image_processor_dict			,     size=(42, 84)		)
     self.assertEqual(image_processor.size			,     {"height": 84, "width": 42}		)




 def 							_snake_case   (	self		)    ->							Dict:
     pass




 @is_flaky()
 def 							_snake_case   (	self		)    ->							Optional[int]:
     _lowerCAmelCase				=      self.image_processing_class(**self.image_processor_dict		)
     # create random PIL images
     _lowerCAmelCase				=      prepare_image_inputs(self.image_processor_tester			,     equal_resolution=_lowerCAmelCase		)
     for image in image_inputs:
         self.assertIsInstance(_lowerCAmelCase			,     Image.Image		)

     # Test not batched input
     _lowerCAmelCase				=      image_processing(image_inputs[0]			,     return_tensors="pt"		).pixel_values
     self.assertEqual(
         encoded_images.shape			,     (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )			,     )

     # Test batched
     _lowerCAmelCase				=      image_processing(_lowerCAmelCase			,     return_tensors="pt"		).pixel_values
     self.assertEqual(
         encoded_images.shape			,     (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )			,     )




 @is_flaky()
 def 							_snake_case   (	self		)    ->							Tuple:
     _lowerCAmelCase				=      self.image_processing_class(**self.image_processor_dict		)
     # create random numpy tensors
     _lowerCAmelCase				=      prepare_image_inputs(self.image_processor_tester			,     equal_resolution=_lowerCAmelCase			,     numpify=_lowerCAmelCase		)
     for image in image_inputs:
         self.assertIsInstance(_lowerCAmelCase			,     np.ndarray		)

     # Test not batched input
     _lowerCAmelCase				=      image_processing(image_inputs[0]			,     return_tensors="pt"		).pixel_values
     self.assertEqual(
         encoded_images.shape			,     (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )			,     )

     # Test batched
     _lowerCAmelCase				=      image_processing(_lowerCAmelCase			,     return_tensors="pt"		).pixel_values
     self.assertEqual(
         encoded_images.shape			,     (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )			,     )





 @is_flaky()
 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      self.image_processing_class(**self.image_processor_dict		)
     # create random PyTorch tensors
     _lowerCAmelCase				=      prepare_image_inputs(self.image_processor_tester			,     equal_resolution=_lowerCAmelCase			,     torchify=_lowerCAmelCase		)
     for image in image_inputs:
         self.assertIsInstance(_lowerCAmelCase			,     torch.Tensor		)

     # Test not batched input
     _lowerCAmelCase				=      image_processing(image_inputs[0]			,     return_tensors="pt"		).pixel_values
     self.assertEqual(
         encoded_images.shape			,     (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )			,     )

     # Test batched
     _lowerCAmelCase				=      image_processing(_lowerCAmelCase			,     return_tensors="pt"		).pixel_values
     self.assertEqual(
         encoded_images.shape			,     (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )			,     )









'''simple docstring'''


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
       from transformers import TvltImageProcessor

if is_speech_available():
       from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class    lowerCAmelCase_		(		unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      "ZinengTang/tvlt-base"
     _lowerCAmelCase				=      tempfile.mkdtemp()




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Any:
     return TvltImageProcessor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							str:
     return TvltFeatureExtractor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     shutil.rmtree(self.tmpdirname		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)
     processor.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      TvltProcessor.from_pretrained(self.tmpdirname		)

     self.assertIsInstance(processor.feature_extractor			,     _lowerCAmelCase		)
     self.assertIsInstance(processor.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)

     _lowerCAmelCase				=      feature_extractor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     return_tensors="np"		)

     for key in audio_dict.keys():
         self.assertAlmostEqual(audio_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      image_processor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     return_tensors="np"		)

     for key in image_dict.keys():
         self.assertAlmostEqual(image_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)
     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     images=_lowerCAmelCase		)

     self.assertListEqual(list(inputs.keys()		)			,     ["audio_values", "audio_mask", "pixel_values", "pixel_mask"]		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()





 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     self.assertListEqual(
         processor.model_input_names			,     image_processor.model_input_names + feature_extractor.model_input_names			,     msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"			,     )


