





"""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       a							(   unittest.TestCase  ):



 def __init__(     self				,       _snake_case				,       _snake_case=7				,       _snake_case=3				,       _snake_case=18				,       _snake_case=30				,       _snake_case=4_00				,       _snake_case=True				,       _snake_case=None				,       _snake_case=True				,       _snake_case=False				,       _snake_case=True				,       _snake_case=True				,       _snake_case=[0.5, 0.5, 0.5]				,       _snake_case=[0.5, 0.5, 0.5]				,       ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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       a							(   _a   , unittest.TestCase  ):
 snake_case__             =       DonutImageProcessor if is_vision_available() else None



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= DonutImageProcessingTester(self     )



 @property
 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  return self.image_processor_tester.prepare_image_processor_dict()



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  pass



 @is_flaky()
 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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'],
      )				,       )












import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY

@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class 							__SCREAMING_SNAKE_CASE       (  unittest.TestCase							):
							snake_case  :   List[str]      =							MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														UpperCamelCase__					  =      hf_hub_download(
														    repo_id="""nateraw/video-demo"""	,			filename="""archery.mp4"""	,			repo_type="""dataset"""	)
														UpperCamelCase__					  =      VideoClassificationPipeline(model=__lowerCAmelCase	,			image_processor=__lowerCAmelCase	,			top_k=2	)
														UpperCamelCase__					  =      [
														    example_video_filepath,
														    """https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4""",
														]
														return video_classifier, examples






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														for example in examples:
																					UpperCamelCase__					  =      video_classifier(__lowerCAmelCase	)

																					self.assertEqual(
																					    __lowerCAmelCase	,			[
																					        {"""score""": ANY(__lowerCAmelCase	), """label""": ANY(__lowerCAmelCase	)},
																					        {"""score""": ANY(__lowerCAmelCase	), """label""": ANY(__lowerCAmelCase	)},
																					    ]	,			)






							@require_torch
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      """hf-internal-testing/tiny-random-VideoMAEForVideoClassification"""
														UpperCamelCase__					  =      VideoMAEFeatureExtractor(
														    size={"""shortest_edge""": 10}	,			crop_size={"""height""": 10, """width""": 10}	)
														UpperCamelCase__					  =      pipeline(
														    """video-classification"""	,			model=__lowerCAmelCase	,			feature_extractor=__lowerCAmelCase	,			frame_sampling_rate=4	)

														UpperCamelCase__					  =      hf_hub_download(repo_id="""nateraw/video-demo"""	,			filename="""archery.mp4"""	,			repo_type="""dataset"""	)
														UpperCamelCase__					  =      video_classifier(__lowerCAmelCase	,			top_k=2	)
														self.assertEqual(
														    nested_simplify(__lowerCAmelCase	,			decimals=4	)	,			[{"""score""": 0.5199, """label""": """LABEL_0"""}, {"""score""": 0.4801, """label""": """LABEL_1"""}]	,			)

														UpperCamelCase__					  =      video_classifier(
														    [
														        video_file_path,
														        video_file_path,
														    ]	,			top_k=2	,			)
														self.assertEqual(
														    nested_simplify(__lowerCAmelCase	,			decimals=4	)	,			[
														        [{"""score""": 0.5199, """label""": """LABEL_0"""}, {"""score""": 0.4801, """label""": """LABEL_1"""}],
														        [{"""score""": 0.5199, """label""": """LABEL_0"""}, {"""score""": 0.4801, """label""": """LABEL_1"""}],
														    ]	,			)







							@require_tf
							def 					_lowerCamelCase		(    self	):
														pass




