


"""simple docstring"""


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


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import ChineseCLIPImageProcessor



class 			lowerCamelCase__						(   unittest.TestCase			):







						def __init__(							self   :    Any			,			_lowercase   :    int			,			_lowercase   :    Optional[Any]=7			,			_lowercase   :    Optional[int]=3			,			_lowercase   :    Union[str, Any]=18			,			_lowercase   :    Optional[int]=30			,			_lowercase   :    List[Any]=400			,			_lowercase   :    Union[str, Any]=True			,			_lowercase   :    Any=None			,			_lowercase   :    Optional[Any]=True			,			_lowercase   :    int=None			,			_lowercase   :    Union[str, Any]=True			,			_lowercase   :    int=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]			,			_lowercase   :    Optional[Any]=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]			,			_lowercase   :    Union[str, Any]=True			,			):
								A					       = size if size is not None else {'height': 224, 'width': 224}
								A					       = crop_size if crop_size is not None else {'height': 18, 'width': 18}
								A					       = parent
								A					       = batch_size
								A					       = num_channels
								A					       = image_size
								A					       = min_resolution
								A					       = max_resolution
								A					       = do_resize
								A					       = size
								A					       = do_center_crop
								A					       = crop_size
								A					       = do_normalize
								A					       = image_mean
								A					       = image_std
								A					       = do_convert_rgb







						def __a						(							self   :    Optional[Any]		):
								return {
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "do_center_crop": self.do_center_crop,
								    "crop_size": self.crop_size,
								    "do_normalize": self.do_normalize,
								    "image_mean": self.image_mean,
								    "image_std": self.image_std,
								    "do_convert_rgb": self.do_convert_rgb,
								}







						def __a						(							self   :    Optional[int]			,			_lowercase   :    str=False			,			_lowercase   :    Optional[Any]=False			,			_lowercase   :    List[Any]=False		):
								assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

								if equal_resolution:
										A					       = []
										for i in range(self.batch_size		):
												image_inputs.append(
												    np.random.randint(
												        255			,			size=(self.num_channels, self.max_resolution, self.max_resolution)			,			dtype=np.uinta		)		)
								else:
										A					       = []
										for i in range(self.batch_size		):
												A       , A					       = np.random.choice(np.arange(self.min_resolution			,			self.max_resolution		)			,			2		)
												image_inputs.append(np.random.randint(255			,			size=(self.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(_lowercase			,			0			,			-1		)		) for x in image_inputs]

								if torchify:
										A					       = [torch.from_numpy(_lowercase		) for x in image_inputs]

								return image_inputs



@require_torch
@require_vision
class 			lowerCamelCase__						(   UpperCAmelCase_							,    unittest.TestCase			):
						lowerCAmelCase       =     ChineseCLIPImageProcessor if is_vision_available() else None







						def __a						(							self   :    Tuple		):
								A					       = ChineseCLIPImageProcessingTester(self			,			do_center_crop=_lowercase		)







						@property
						def __a						(							self   :    Tuple		):
								return self.image_processor_tester.prepare_image_processor_dict()







						def __a						(							self   :    int		):
								A					       = self.image_processing_class(**self.image_processor_dict		)
								self.assertTrue(hasattr(_lowercase			,			'do_resize'		)		)
								self.assertTrue(hasattr(_lowercase			,			'size'		)		)
								self.assertTrue(hasattr(_lowercase			,			'do_center_crop'		)		)
								self.assertTrue(hasattr(_lowercase			,			'center_crop'		)		)
								self.assertTrue(hasattr(_lowercase			,			'do_normalize'		)		)
								self.assertTrue(hasattr(_lowercase			,			'image_mean'		)		)
								self.assertTrue(hasattr(_lowercase			,			'image_std'		)		)
								self.assertTrue(hasattr(_lowercase			,			'do_convert_rgb'		)		)







						def __a						(							self   :    Any		):
								A					       = self.image_processing_class.from_dict(self.image_processor_dict		)
								self.assertEqual(image_processor.size			,			{'height': 224, 'width': 224}		)
								self.assertEqual(image_processor.crop_size			,			{'height': 18, 'width': 18}		)

								A					       = 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 __a						(							self   :    Any		):
								pass







						def __a						(							self   :    int		):
								# Initialize image_processing
								A					       = self.image_processing_class(**self.image_processor_dict		)
								# create random PIL images
								A					       = self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase		)
								for image in image_inputs:
										self.assertIsInstance(_lowercase			,			Image.Image		)

								# Test not batched input
								A					       = 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
								A					       = image_processing(_lowercase			,			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 __a						(							self   :    Tuple		):
								# Initialize image_processing
								A					       = self.image_processing_class(**self.image_processor_dict		)
								# create random numpy tensors
								A					       = self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase			,			numpify=_lowercase		)
								for image in image_inputs:
										self.assertIsInstance(_lowercase			,			np.ndarray		)

								# Test not batched input
								A					       = 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
								A					       = image_processing(_lowercase			,			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 __a						(							self   :    Dict		):
								# Initialize image_processing
								A					       = self.image_processing_class(**self.image_processor_dict		)
								# create random PyTorch tensors
								A					       = self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase			,			torchify=_lowercase		)
								for image in image_inputs:
										self.assertIsInstance(_lowercase			,			torch.Tensor		)

								# Test not batched input
								A					       = 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
								A					       = image_processing(_lowercase			,			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'],
								    )			,			)



@require_torch
@require_vision
class 			lowerCamelCase__						(   UpperCAmelCase_							,    unittest.TestCase			):
						lowerCAmelCase       =     ChineseCLIPImageProcessor if is_vision_available() else None







						def __a						(							self   :    Optional[Any]		):
								A					       = ChineseCLIPImageProcessingTester(self			,			num_channels=4			,			do_center_crop=_lowercase		)
								A					       = 3







						@property
						def __a						(							self   :    Optional[Any]		):
								return self.image_processor_tester.prepare_image_processor_dict()







						def __a						(							self   :    Dict		):
								A					       = self.image_processing_class(**self.image_processor_dict		)
								self.assertTrue(hasattr(_lowercase			,			'do_resize'		)		)
								self.assertTrue(hasattr(_lowercase			,			'size'		)		)
								self.assertTrue(hasattr(_lowercase			,			'do_center_crop'		)		)
								self.assertTrue(hasattr(_lowercase			,			'center_crop'		)		)
								self.assertTrue(hasattr(_lowercase			,			'do_normalize'		)		)
								self.assertTrue(hasattr(_lowercase			,			'image_mean'		)		)
								self.assertTrue(hasattr(_lowercase			,			'image_std'		)		)
								self.assertTrue(hasattr(_lowercase			,			'do_convert_rgb'		)		)







						def __a						(							self   :    Union[str, Any]		):
								pass







						def __a						(							self   :    Any		):
								# Initialize image_processing
								A					       = self.image_processing_class(**self.image_processor_dict		)
								# create random PIL images
								A					       = self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase		)
								for image in image_inputs:
										self.assertIsInstance(_lowercase			,			Image.Image		)

								# Test not batched input
								A					       = image_processing(image_inputs[0]			,			return_tensors='pt'		).pixel_values
								self.assertEqual(
								    encoded_images.shape			,			(
								        1,
								        self.expected_encoded_image_num_channels,
								        self.image_processor_tester.crop_size['height'],
								        self.image_processor_tester.crop_size['width'],
								    )			,			)

								# Test batched
								A					       = image_processing(_lowercase			,			return_tensors='pt'		).pixel_values
								self.assertEqual(
								    encoded_images.shape			,			(
								        self.image_processor_tester.batch_size,
								        self.expected_encoded_image_num_channels,
								        self.image_processor_tester.crop_size['height'],
								        self.image_processor_tester.crop_size['width'],
								    )			,			)









"""simple docstring"""


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       =     MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING







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







						def __a						(							self   :    Tuple			,			_lowercase   :    Union[str, Any]			,			_lowercase   :    List[Any]		):
								for example in examples:
										A					       = video_classifier(_lowercase		)

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







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

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

								A					       = video_classifier(
								    [
								        video_file_path,
								        video_file_path,
								    ]			,			top_k=2			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        [{'score': 0.5_1_9_9, 'label': 'LABEL_0'}, {'score': 0.4_8_0_1, 'label': 'LABEL_1'}],
								        [{'score': 0.5_1_9_9, 'label': 'LABEL_0'}, {'score': 0.4_8_0_1, 'label': 'LABEL_1'}],
								    ]			,			)







						@require_tf
						def __a						(							self   :    Dict		):
								pass





