


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_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import LevitImageProcessor







class lowerCAmelCase_		(				unittest.TestCase ):

 '''simple docstring'''

 def __init__(			self	,   _UpperCAmelCase	,   _UpperCAmelCase=7	,   _UpperCAmelCase=3	,   _UpperCAmelCase=18	,   _UpperCAmelCase=30	,   _UpperCAmelCase=4_00	,   _UpperCAmelCase=True	,   _UpperCAmelCase=None	,   _UpperCAmelCase=True	,   _UpperCAmelCase=None	,   _UpperCAmelCase=True	,   _UpperCAmelCase=[0.5, 0.5, 0.5]	,   _UpperCAmelCase=[0.5, 0.5, 0.5]	,   ):
       snake_case_ =   size if size is not None else {'''shortest_edge''': 18}
       snake_case_ =   crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
       snake_case_ =   parent
       snake_case_ =   batch_size
       snake_case_ =   num_channels
       snake_case_ =   image_size
       snake_case_ =   min_resolution
       snake_case_ =   max_resolution
       snake_case_ =   do_resize
       snake_case_ =   size
       snake_case_ =   do_center_crop
       snake_case_ =   crop_size
       snake_case_ =   do_normalize
       snake_case_ =   image_mean
       snake_case_ =   image_std





 def 		UpperCamelCase__  (			self      ):
       return {
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_normalize": self.do_normalize,
           "do_resize": self.do_resize,
           "do_center_crop": self.do_center_crop,
           "size": self.size,
           "crop_size": self.crop_size,
       }







@require_torch
@require_vision
class lowerCAmelCase_		(				lowerCamelCase__      ,		unittest.TestCase ):

 '''simple docstring'''

 __snake_case				=						LevitImageProcessor if is_vision_available() else None



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   LevitImageProcessingTester(self      )



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.image_processor_tester.prepare_image_processor_dict()



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.image_processing_class(**self.image_processor_dict      )
       self.assertTrue(hasattr(_UpperCAmelCase	,   '''image_mean'''      )      )
       self.assertTrue(hasattr(_UpperCAmelCase	,   '''image_std'''      )      )
       self.assertTrue(hasattr(_UpperCAmelCase	,   '''do_normalize'''      )      )
       self.assertTrue(hasattr(_UpperCAmelCase	,   '''do_resize'''      )      )
       self.assertTrue(hasattr(_UpperCAmelCase	,   '''do_center_crop'''      )      )
       self.assertTrue(hasattr(_UpperCAmelCase	,   '''size'''      )      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   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}      )

       snake_case_ =   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      ):
       pass



 def 		UpperCamelCase__  (			self      ):
       # Initialize image_processing
       snake_case_ =   self.image_processing_class(**self.image_processor_dict      )
       # create random PIL images
       snake_case_ =   prepare_image_inputs(self.image_processor_tester	,   equal_resolution=_UpperCAmelCase      )
       for image in image_inputs:
             self.assertIsInstance(_UpperCAmelCase	,   Image.Image      )

       # Test not batched input
       snake_case_ =   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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )	,   )

       # Test batched
       snake_case_ =   image_processing(_UpperCAmelCase	,   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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )	,   )



 def 		UpperCamelCase__  (			self      ):
       # Initialize image_processing
       snake_case_ =   self.image_processing_class(**self.image_processor_dict      )
       # create random numpy tensors
       snake_case_ =   prepare_image_inputs(self.image_processor_tester	,   equal_resolution=_UpperCAmelCase	,   numpify=_UpperCAmelCase      )
       for image in image_inputs:
             self.assertIsInstance(_UpperCAmelCase	,   np.ndarray      )

       # Test not batched input
       snake_case_ =   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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )	,   )

       # Test batched
       snake_case_ =   image_processing(_UpperCAmelCase	,   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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )	,   )





 def 		UpperCamelCase__  (			self      ):
       # Initialize image_processing
       snake_case_ =   self.image_processing_class(**self.image_processor_dict      )
       # create random PyTorch tensors
       snake_case_ =   prepare_image_inputs(self.image_processor_tester	,   equal_resolution=_UpperCAmelCase	,   torchify=_UpperCAmelCase      )
       for image in image_inputs:
             self.assertIsInstance(_UpperCAmelCase	,   torch.Tensor      )

       # Test not batched input
       snake_case_ =   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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )	,   )

       # Test batched
       snake_case_ =   image_processing(_UpperCAmelCase	,   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.crop_size['''height'''],
               self.image_processor_tester.crop_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 lowerCAmelCase_		(				unittest.TestCase ):

 '''simple docstring'''

 __snake_case				=						MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



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



 def 		UpperCamelCase__  (			self	,   _UpperCAmelCase	,   _UpperCAmelCase      ):
       for example in examples:
             snake_case_ =   video_classifier(_UpperCAmelCase      )

             self.assertEqual(
                 _UpperCAmelCase	,   [
                     {'''score''': ANY(_UpperCAmelCase      ), '''label''': ANY(_UpperCAmelCase      )},
                     {'''score''': ANY(_UpperCAmelCase      ), '''label''': ANY(_UpperCAmelCase      )},
                 ]	,   )



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

       snake_case_ =   hf_hub_download(repo_id='''nateraw/video-demo'''	,   filename='''archery.mp4'''	,   repo_type='''dataset'''      )
       snake_case_ =   video_classifier(_UpperCAmelCase	,   top_k=2      )
       self.assertEqual(
           nested_simplify(_UpperCAmelCase	,   decimals=4      )	,   [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}]	,   )

       snake_case_ =   video_classifier(
           [
               video_file_path,
               video_file_path,
           ]	,   top_k=2	,   )
       self.assertEqual(
           nested_simplify(_UpperCAmelCase	,   decimals=4      )	,   [
               [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
               [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
           ]	,   )





 @require_tf
 def 		UpperCamelCase__  (			self      ):
       pass