


"""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, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor


class       SCREAMING_SNAKE_CASE__				(		unittest.TestCase				):
       """simple docstring"""


       def __init__(						self		,     snake_case__		,     snake_case__=13		,     snake_case__=3		,     snake_case__=224		,     snake_case__=30		,     snake_case__=400		,     snake_case__=True		,     snake_case__=None		,     snake_case__=True		,     snake_case__=[0.5, 0.5, 0.5]		,     snake_case__=[0.5, 0.5, 0.5]		,     ):






         """simple docstring"""





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







       def        lowercase__							(						self						):






         """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       SCREAMING_SNAKE_CASE__				(		lowercase   ,    unittest.TestCase				):
       """simple docstring"""


       a  :				str   =ViTImageProcessor if is_vision_available() else None

       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     Union[str, Any]								=							EfficientFormerImageProcessorTester(self						)

       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         return self.image_proc_tester.prepare_image_processor_dict()

       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     Optional[Any]								=							self.image_processing_class(**self.image_processor_dict						)
         self.assertTrue(hasattr(snake_case__		,     "image_mean"						)						)
         self.assertTrue(hasattr(snake_case__		,     "image_std"						)						)
         self.assertTrue(hasattr(snake_case__		,     "do_normalize"						)						)
         self.assertTrue(hasattr(snake_case__		,     "do_resize"						)						)
         self.assertTrue(hasattr(snake_case__		,     "size"						)						)

       def        lowercase__							(						self						):






         """simple docstring"""





         pass

       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     int								=							self.image_processing_class(**self.image_processor_dict						)
         # create random PIL images
         lowerCAmelCase     :     Optional[Any]								=							prepare_image_inputs(self.image_proc_tester		,     equal_resolution=snake_case__						)
         for image in image_inputs:
           self.assertIsInstance(snake_case__		,     Image.Image						)

         # Test not batched input
         lowerCAmelCase     :     Optional[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
         lowerCAmelCase     :     Any								=							image_processor(snake_case__		,     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        lowercase__							(						self						):






         """simple docstring"""





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

         # Test not batched input
         lowerCAmelCase     :     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
         lowerCAmelCase     :     Optional[int]								=							image_processor(snake_case__		,     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        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     Dict								=							self.image_processing_class(**self.image_processor_dict						)
         # create random PyTorch tensors
         lowerCAmelCase     :     List[Any]								=							prepare_image_inputs(self.image_proc_tester		,     equal_resolution=snake_case__		,     torchify=snake_case__						)
         for image in image_inputs:
           self.assertIsInstance(snake_case__		,     torch.Tensor						)

         # Test not batched input
         lowerCAmelCase     :     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
         lowerCAmelCase     :     str								=							image_processor(snake_case__		,     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"],
             )		,     )









"""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       SCREAMING_SNAKE_CASE__				(		unittest.TestCase				):
       """simple docstring"""


       a  :				int   =MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING

       def        lowercase__							(						self		,     snake_case__		,     snake_case__		,     snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     Tuple								=							hf_hub_download(
             repo_id="nateraw/video-demo"		,     filename="archery.mp4"		,     repo_type="dataset"						)
         lowerCAmelCase     :     Dict								=							VideoClassificationPipeline(model=snake_case__		,     image_processor=snake_case__		,     top_k=2						)
         lowerCAmelCase     :     Any								=							[
             example_video_filepath,
             "https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4",
         ]
         return video_classifier, examples

       def        lowercase__							(						self		,     snake_case__		,     snake_case__						):






         """simple docstring"""





         for example in examples:
           lowerCAmelCase     :     str								=							video_classifier(snake_case__						)

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

       @require_torch
       def        lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase     :     Union[str, Any]								=							"hf-internal-testing/tiny-random-VideoMAEForVideoClassification"
         lowerCAmelCase     :     str								=							VideoMAEFeatureExtractor(
             size={"shortest_edge": 10}		,     crop_size={"height": 10, "width": 10}						)
         lowerCAmelCase     :     int								=							pipeline(
             "video-classification"		,     model=snake_case__		,     feature_extractor=snake_case__		,     frame_sampling_rate=4						)

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

         lowerCAmelCase     :     Tuple								=							video_classifier(
             [
                 video_file_path,
                 video_file_path,
             ]		,     top_k=2		,     )
         self.assertEqual(
             nested_simplify(snake_case__		,     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        lowercase__							(						self						):






         """simple docstring"""





         pass





