




'''simple docstring'''







from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
      import PIL

lowercase      :   Dict      =       logging.get_logger(__name__)



def        lowerCamelCase__				(      __lowercase				):

     if isinstance(SCREAMING_SNAKE_CASE__     ,   (list, tuple)				) and isinstance(videos[0]     ,   (list, tuple)				) and is_valid_image(videos[0][0]				):
          return videos

     elif isinstance(SCREAMING_SNAKE_CASE__     ,   (list, tuple)				) and is_valid_image(videos[0]				):
          return [videos]

     elif is_valid_image(SCREAMING_SNAKE_CASE__				):
          return [[videos]]

     raise ValueError(F'''Could not make batched video from {videos}'''				)







class 					_a (_UpperCamelCase	):





     '''simple docstring'''



     lowerCAmelCase_  :     Tuple          =					['pixel_values']







     def __init__(				self							,__a = True							,__a = None							,__a = PILImageResampling.BILINEAR							,__a = True							,__a = None							,__a = True							,__a = 1 / 255							,__a = True							,__a = True							,__a = None							,__a = None							,**__a							,)      ->				Optional[int]:
          super().__init__(**__a							)
          snake_case			:      Any        =			size if size is not None else {"shortest_edge": 256}
          snake_case			:      List[str]        =			get_size_dict(__a							,default_to_square=__a							)
          snake_case			:      Dict        =			crop_size if crop_size is not None else {"height": 224, "width": 224}
          snake_case			:      Optional[int]        =			get_size_dict(__a							,param_name="""crop_size"""							)

          snake_case			:      Optional[int]        =			do_resize
          snake_case			:      int        =			size
          snake_case			:      Optional[int]        =			do_center_crop
          snake_case			:      List[str]        =			crop_size
          snake_case			:      int        =			resample
          snake_case			:      Union[str, Any]        =			do_rescale
          snake_case			:      Dict        =			rescale_factor
          snake_case			:      Dict        =			offset
          snake_case			:      str        =			do_normalize
          snake_case			:      Union[str, Any]        =			image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          snake_case			:      Optional[int]        =			image_std if image_std is not None else IMAGENET_STANDARD_STD







     def 			snake_case_      (				self							,__a							,__a							,__a = PILImageResampling.BILINEAR							,__a = None							,**__a							,)      ->				str:
          snake_case			:      int        =			get_size_dict(__a							,default_to_square=__a							)
          if "shortest_edge" in size:
               snake_case			:      Tuple        =			get_resize_output_image_size(__a							,size["""shortest_edge"""]							,default_to_square=__a							)
          elif "height" in size and "width" in size:
               snake_case			:      Dict        =			(size["height"], size["width"])
          else:
               raise ValueError(F'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}'''							)
          return resize(__a							,size=__a							,resample=__a							,data_format=__a							,**__a							)







     def 			snake_case_      (				self							,__a							,__a							,__a = None							,**__a							,)      ->				Optional[Any]:
          snake_case			:      Optional[Any]        =			get_size_dict(__a							)
          if "height" not in size or "width" not in size:
               raise ValueError(F'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}'''							)
          return center_crop(__a							,size=(size["""height"""], size["""width"""])							,data_format=__a							,**__a							)







     def 			snake_case_      (				self							,__a							,__a							,__a = True							,__a = None							,**__a							,)      ->				Any:
          snake_case			:      Union[str, Any]        =			image.astype(np.floataa							)
          if offset:
               snake_case			:      Dict        =			image - (scale / 2)
          return rescale(__a							,scale=__a							,data_format=__a							,**__a							)







     def 			snake_case_      (				self							,__a							,__a							,__a							,__a = None							,**__a							,)      ->				int:
          return normalize(__a							,mean=__a							,std=__a							,data_format=__a							,**__a							)







     def 			snake_case_      (				self							,__a							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = ChannelDimension.FIRST							,)      ->				List[str]:
          if do_resize and size is None or resample is None:
               raise ValueError("""Size and resample must be specified if do_resize is True."""							)

          if do_center_crop and crop_size is None:
               raise ValueError("""Crop size must be specified if do_center_crop is True."""							)

          if do_rescale and rescale_factor is None:
               raise ValueError("""Rescale factor must be specified if do_rescale is True."""							)

          if do_normalize and (image_mean is None or image_std is None):
               raise ValueError("""Image mean and std must be specified if do_normalize is True."""							)

          if offset and not do_rescale:
               raise ValueError("""For offset, do_rescale must also be set to True."""							)

          # All transformations expect numpy arrays.
          snake_case			:      str        =			to_numpy_array(__a							)

          if do_resize:
               snake_case			:      Any        =			self.resize(image=__a							,size=__a							,resample=__a							)

          if do_center_crop:
               snake_case			:      Tuple        =			self.center_crop(__a							,size=__a							)

          if do_rescale:
               snake_case			:      Dict        =			self.rescale(image=__a							,scale=__a							,offset=__a							)

          if do_normalize:
               snake_case			:      List[str]        =			self.normalize(image=__a							,mean=__a							,std=__a							)

          snake_case			:      Tuple        =			to_channel_dimension_format(__a							,__a							)
          return image







     def 			snake_case_      (				self							,__a							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = None							,__a = ChannelDimension.FIRST							,**__a							,)      ->				str:
          snake_case			:      int        =			do_resize if do_resize is not None else self.do_resize
          snake_case			:      Optional[Any]        =			resample if resample is not None else self.resample
          snake_case			:      List[str]        =			do_center_crop if do_center_crop is not None else self.do_center_crop
          snake_case			:      List[str]        =			do_rescale if do_rescale is not None else self.do_rescale
          snake_case			:      str        =			rescale_factor if rescale_factor is not None else self.rescale_factor
          snake_case			:      List[Any]        =			offset if offset is not None else self.offset
          snake_case			:      Union[str, Any]        =			do_normalize if do_normalize is not None else self.do_normalize
          snake_case			:      List[str]        =			image_mean if image_mean is not None else self.image_mean
          snake_case			:      List[Any]        =			image_std if image_std is not None else self.image_std

          snake_case			:      Dict        =			size if size is not None else self.size
          snake_case			:      List[str]        =			get_size_dict(__a							,default_to_square=__a							)
          snake_case			:      int        =			crop_size if crop_size is not None else self.crop_size
          snake_case			:      Dict        =			get_size_dict(__a							,param_name="""crop_size"""							)

          if not valid_images(__a							):
               raise ValueError(
                   """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                   """torch.Tensor, tf.Tensor or jax.ndarray."""							)

          snake_case			:      Optional[int]        =			make_batched(__a							)

          snake_case			:      List[str]        =			[
              [
                  self._preprocess_image(
                      image=__a							,do_resize=__a							,size=__a							,resample=__a							,do_center_crop=__a							,crop_size=__a							,do_rescale=__a							,rescale_factor=__a							,offset=__a							,do_normalize=__a							,image_mean=__a							,image_std=__a							,data_format=__a							,)
                  for img in video
              ]
              for video in videos
          ]

          snake_case			:      Tuple        =			{"pixel_values": videos}
          return BatchFeature(data=__a							,tensor_type=__a							)








"""simple docstring"""




import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:






 class       UpperCAmelCase_    :



       @staticmethod
       def 		snake_case_   (	*A	:      Union[str, Any]	,      **A	:      Dict		):
            pass






@is_pipeline_test
@require_vision
class       UpperCAmelCase_    (							unittest.TestCase		):



      @require_torch
      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Tuple														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
           # python and torch versions.
           self.assertIn(
               nested_simplify(A		)	,      [
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}],
               ]	,      )

           _UpperCAmelCase     :      str														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[str]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      framework="tf"		)
           _UpperCAmelCase     :      int														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}]	,      )

           _UpperCAmelCase     :      Optional[Any]														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @slow
      @require_torch
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      Optional[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      )
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Dict														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      List[Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Tuple														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )



      @slow
      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      framework="tf"		)
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )
