





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 ViTImageProcessor



class 					__lowerCAmelCase       (			unittest.TestCase     ):






			def __init__(self					,							__magic_name__					,							__magic_name__=13					,							__magic_name__=3					,							__magic_name__=224					,							__magic_name__=30					,							__magic_name__=400					,							__magic_name__=True					,							__magic_name__=None					,							__magic_name__=True					,							__magic_name__=[0.5, 0.5, 0.5]					,							__magic_name__=[0.5, 0.5, 0.5]					,							)     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  size if size is not None else {'''height''': 18, '''width''': 18}
							snake_case_						:    List[str]				=  parent
							snake_case_						:    List[str]				=  batch_size
							snake_case_						:    Optional[Any]				=  num_channels
							snake_case_						:    Union[str, Any]				=  image_size
							snake_case_						:    str				=  min_resolution
							snake_case_						:    Optional[Any]				=  max_resolution
							snake_case_						:    List[str]				=  do_resize
							snake_case_						:    List[str]				=  size
							snake_case_						:    Any				=  do_normalize
							snake_case_						:    List[str]				=  image_mean
							snake_case_						:    Tuple				=  image_std






			def 			lowerCamelCase  (self    )     ->   Tuple:


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




@require_torch
@require_vision
class 					__lowerCAmelCase       (			_a,     unittest.TestCase     ):
			lowerCamelCase_     :     str       			=      ViTImageProcessor if is_vision_available() else None






			def 			lowerCamelCase  (self    )     ->   List[Any]:


							'''simple docstring'''



							snake_case_						:    List[str]				=  EfficientFormerImageProcessorTester(self    )






			@property
			def 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''simple docstring'''



							return self.image_proc_tester.prepare_image_processor_dict()






			def 			lowerCamelCase  (self    )     ->   Any:


							'''simple docstring'''



							snake_case_						:    List[str]				=  self.image_processing_class(**self.image_processor_dict    )
							self.assertTrue(hasattr(__magic_name__					,							'''image_mean'''    )    )
							self.assertTrue(hasattr(__magic_name__					,							'''image_std'''    )    )
							self.assertTrue(hasattr(__magic_name__					,							'''do_normalize'''    )    )
							self.assertTrue(hasattr(__magic_name__					,							'''do_resize'''    )    )
							self.assertTrue(hasattr(__magic_name__					,							'''size'''    )    )






			def 			lowerCamelCase  (self    )     ->   Union[str, Any]:


							'''simple docstring'''



							pass






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    List[str]				=  self.image_processing_class(**self.image_processor_dict    )
							# create random PIL images
							snake_case_						:    str				=  prepare_image_inputs(self.image_proc_tester					,							equal_resolution=__magic_name__    )
							for image in image_inputs:
											self.assertIsInstance(__magic_name__					,							Image.Image    )

							# Test not batched input
							snake_case_						:    List[Any]				=  image_processor(image_inputs[0]					,							return_tensors='''pt'''    ).pixel_values
							self.assertEqual(
							    encoded_images.shape					,							(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['''height'''],
							        self.image_proc_tester.size['''width'''],
							    )					,							)

							# Test batched
							snake_case_						:    Any				=  image_processor(__magic_name__					,							return_tensors='''pt'''    ).pixel_values
							self.assertEqual(
							    encoded_images.shape					,							(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['''height'''],
							        self.image_proc_tester.size['''width'''],
							    )					,							)






			def 			lowerCamelCase  (self    )     ->   Any:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  self.image_processing_class(**self.image_processor_dict    )
							# create random numpy tensors
							snake_case_						:    List[str]				=  prepare_image_inputs(self.image_proc_tester					,							equal_resolution=__magic_name__					,							numpify=__magic_name__    )
							for image in image_inputs:
											self.assertIsInstance(__magic_name__					,							np.ndarray    )

							# Test not batched input
							snake_case_						:    Any				=  image_processor(image_inputs[0]					,							return_tensors='''pt'''    ).pixel_values
							self.assertEqual(
							    encoded_images.shape					,							(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['''height'''],
							        self.image_proc_tester.size['''width'''],
							    )					,							)

							# Test batched
							snake_case_						:    List[str]				=  image_processor(__magic_name__					,							return_tensors='''pt'''    ).pixel_values
							self.assertEqual(
							    encoded_images.shape					,							(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['''height'''],
							        self.image_proc_tester.size['''width'''],
							    )					,							)






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    int				=  self.image_processing_class(**self.image_processor_dict    )
							# create random PyTorch tensors
							snake_case_						:    int				=  prepare_image_inputs(self.image_proc_tester					,							equal_resolution=__magic_name__					,							torchify=__magic_name__    )
							for image in image_inputs:
											self.assertIsInstance(__magic_name__					,							torch.Tensor    )

							# Test not batched input
							snake_case_						:    Optional[int]				=  image_processor(image_inputs[0]					,							return_tensors='''pt'''    ).pixel_values
							self.assertEqual(
							    encoded_images.shape					,							(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['''height'''],
							        self.image_proc_tester.size['''width'''],
							    )					,							)

							# Test batched
							snake_case_						:    int				=  image_processor(__magic_name__					,							return_tensors='''pt'''    ).pixel_values
							self.assertEqual(
							    encoded_images.shape					,							(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['''height'''],
							        self.image_proc_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 					__lowerCAmelCase       (			unittest.TestCase     ):
			lowerCamelCase_     :     Any       			=      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Dict:


							'''simple docstring'''



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






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__    )     ->   Any:


							'''simple docstring'''



							for example in examples:
											snake_case_						:    Union[str, Any]				=  video_classifier(__magic_name__    )

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






			@require_torch
			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Any				=  '''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
							snake_case_						:    str				=  VideoMAEFeatureExtractor(
							    size={'''shortest_edge''': 10}					,							crop_size={'''height''': 10, '''width''': 10}    )
							snake_case_						:    int				=  pipeline(
							    '''video-classification'''					,							model=__magic_name__					,							feature_extractor=__magic_name__					,							frame_sampling_rate=4    )

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

							snake_case_						:    int				=  video_classifier(
							    [
							        video_file_path,
							        video_file_path,
							    ]					,							top_k=2					,							)
							self.assertEqual(
							    nested_simplify(__magic_name__					,							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 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''simple docstring'''



							pass
