





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

a						: int     	=			logging.get_logger(__name__)






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    Optional[Any]					) ->List[List[ImageInput]]:






	'''simple docstring'''






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

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

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

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







class        __UpperCamelCase				(						UpperCAmelCase__						):
							lowerCamelCase   :					List[str]      					=["""pixel_values"""]






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

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






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = PILImageResampling.BILINEAR      ,					lowerCAmelCase__ = None      ,					**lowerCAmelCase__      ,					)	-> np.ndarray:
								a      :		Union[str, Any]	=				get_size_dict(__lowercase      ,					default_to_square=__lowercase      )
								if "shortest_edge" in size:
									a      :		str	=				get_resize_output_image_size(__lowercase      ,					size["shortest_edge"]      ,					default_to_square=__lowercase      )
								elif "height" in size and "width" in size:
									a      :		Tuple	=				(size['''height'''], size['''width'''])
								else:
									raise ValueError(f"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"""      )
								return resize(__lowercase      ,					size=__lowercase      ,					resample=__lowercase      ,					data_format=__lowercase      ,					**__lowercase      )






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = None      ,					**lowerCAmelCase__      ,					)	-> np.ndarray:
								a      :		Tuple	=				get_size_dict(__lowercase      )
								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(__lowercase      ,					size=(size["height"], size["width"])      ,					data_format=__lowercase      ,					**__lowercase      )






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






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = None      ,					**lowerCAmelCase__      ,					)	-> np.ndarray:
								return normalize(__lowercase      ,					mean=__lowercase      ,					std=__lowercase      ,					data_format=__lowercase      ,					**__lowercase      )






							def     __a   (   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = ChannelDimension.FIRST      ,					)	-> np.ndarray:
								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.
								a      :		int	=				to_numpy_array(__lowercase      )

								if do_resize:
									a      :		Dict	=				self.resize(image=__lowercase      ,					size=__lowercase      ,					resample=__lowercase      )

								if do_center_crop:
									a      :		int	=				self.center_crop(__lowercase      ,					size=__lowercase      )

								if do_rescale:
									a      :		Dict	=				self.rescale(image=__lowercase      ,					scale=__lowercase      ,					offset=__lowercase      )

								if do_normalize:
									a      :		Union[str, Any]	=				self.normalize(image=__lowercase      ,					mean=__lowercase      ,					std=__lowercase      )

								a      :		Optional[int]	=				to_channel_dimension_format(__lowercase      ,					__lowercase      )
								return image






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

								a      :		Dict	=				size if size is not None else self.size
								a      :		Dict	=				get_size_dict(__lowercase      ,					default_to_square=__lowercase      )
								a      :		Optional[Any]	=				crop_size if crop_size is not None else self.crop_size
								a      :		List[Any]	=				get_size_dict(__lowercase      ,					param_name="crop_size"      )

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

								a      :		List[Any]	=				make_batched(__lowercase      )

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

								a      :		Any	=				{'''pixel_values''': videos}
								return BatchFeature(data=__lowercase      ,					tensor_type=__lowercase      )






import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, 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  snake_case__:







												'''simple docstring'''




												@staticmethod
												def   lowercase_       ( *__lowercase      ,  **__lowercase							)    ->			Union[str, Any]:
																pass







@is_pipeline_test
@require_vision
@require_torch
class  snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__					:     Union[str, Any]    =							MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING




						def   lowercase_       ( self      ,  __lowercase      ,  __lowercase      ,  __lowercase							)    ->			List[str]:
										lowerCAmelCase_       :					Union[str, Any]			 = pipeline(
										    '''zero-shot-object-detection'''      ,  model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

										lowerCAmelCase_       :					str			 = [
										    {
										        '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
										        '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										    }
										]
										return object_detector, examples




						def   lowercase_       ( self      ,  __lowercase      ,  __lowercase							)    ->			str:
										lowerCAmelCase_       :					Tuple			 = object_detector(examples[0]      ,  threshold=0.0							)

										lowerCAmelCase_       :					Dict			 = len(__lowercase							)
										self.assertGreater(__lowercase      ,  0							)
										self.assertEqual(
										    __lowercase      ,  [
										        {
										            '''score''': ANY(__lowercase							),
										            '''label''': ANY(__lowercase							),
										            '''box''': {'''xmin''': ANY(__lowercase							), '''ymin''': ANY(__lowercase							), '''xmax''': ANY(__lowercase							), '''ymax''': ANY(__lowercase							)},
										        }
										        for i in range(__lowercase							)
										    ]      ,  )




						@require_tf
						@unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
						def   lowercase_       ( self							)    ->			List[str]:
										pass




						@require_torch
						def   lowercase_       ( self							)    ->			int:
										lowerCAmelCase_       :					Union[str, Any]			 = pipeline(
										    '''zero-shot-object-detection'''      ,  model='''hf-internal-testing/tiny-random-owlvit-object-detection'''							)

										lowerCAmelCase_       :					Union[str, Any]			 = object_detector(
										    '''./tests/fixtures/tests_samples/COCO/000000039769.png'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  threshold=0.64      ,  )

										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										        {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										        {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										        {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										        {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										        {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										        {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										        {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
										        {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										    ]      ,  )

										lowerCAmelCase_       :					Union[str, Any]			 = object_detector(
										    [
										        {
										            '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
										            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										        }
										    ]      ,  threshold=0.64      ,  )

										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        [
										            {'''score''': 0.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										            {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										            {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
										            {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										            {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										            {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
										            {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										            {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
										            {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
										        ]
										    ]      ,  )




						@require_torch
						@slow
						def   lowercase_       ( self							)    ->			Union[str, Any]:
										lowerCAmelCase_       :					Any			 = pipeline('''zero-shot-object-detection'''							)

										lowerCAmelCase_       :					Dict			 = object_detector(
										    '''http://images.cocodataset.org/val2017/000000039769.jpg'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										        {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										        {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										        {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
										        {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
										    ]      ,  )

										lowerCAmelCase_       :					Tuple			 = object_detector(
										    [
										        {
										            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
										            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										        },
										        {
										            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
										            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
										        },
										    ]      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        [
										            {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										            {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										            {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										            {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
										            {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
										        ],
										        [
										            {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										            {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										            {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										            {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
										            {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
										        ],
										    ]      ,  )




						@require_tf
						@unittest.skip('''Zero Shot Object Detection not implemented in TF'''							)
						def   lowercase_       ( self							)    ->			List[str]:
										pass




						@require_torch
						@slow
						def   lowercase_       ( self							)    ->			Optional[int]:
										lowerCAmelCase_       :					Any			 = 0.2
										lowerCAmelCase_       :					List[Any]			 = pipeline('''zero-shot-object-detection'''							)

										lowerCAmelCase_       :					Optional[Any]			 = object_detector(
										    '''http://images.cocodataset.org/val2017/000000039769.jpg'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  threshold=__lowercase      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										        {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										        {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
										    ]      ,  )




						@require_torch
						@slow
						def   lowercase_       ( self							)    ->			Optional[int]:
										lowerCAmelCase_       :					Dict			 = 2
										lowerCAmelCase_       :					Union[str, Any]			 = pipeline('''zero-shot-object-detection'''							)

										lowerCAmelCase_       :					Optional[Any]			 = object_detector(
										    '''http://images.cocodataset.org/val2017/000000039769.jpg'''      ,  candidate_labels=['''cat''', '''remote''', '''couch''']      ,  top_k=__lowercase      ,  )
										self.assertEqual(
										    nested_simplify(__lowercase      ,  decimals=4							)      ,  [
										        {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
										        {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
										    ]      ,  )