



'''simple docstring'''



from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
		import torch

if is_tf_available():
		import tensorflow as tf





class _UpperCamelCase		(     A		):





				'''simple docstring'''





				lowerCAmelCase__					    =    ["""image_processor"""]
				lowerCAmelCase__					    =    """SamImageProcessor"""

				def __init__(							self  :  List[Any]  ,       _lowerCAmelCase  :  List[str]):







											'''simple docstring'''

											super().__init__(_lowerCAmelCase)
											__lowercase       	=self.image_processor
											__lowercase       	=-1_0
											__lowercase       	=self.image_processor.size['longest_edge']

				def __call__(							self  :  Any  ,       _lowerCAmelCase  :  Optional[Any]=None  ,       _lowerCAmelCase  :  Any=None  ,       _lowerCAmelCase  :  Any=None  ,       _lowerCAmelCase  :  List[Any]=None  ,       _lowerCAmelCase  :  Optional[Union[str, TensorType]] = None  ,       **_lowerCAmelCase  :  Optional[Any]  ,       ):







											'''simple docstring'''

											__lowercase       	=self.image_processor(
											    _lowerCAmelCase  ,       return_tensors=_lowerCAmelCase  ,       **_lowerCAmelCase  ,       )

											# pop arguments that are not used in the foward but used nevertheless
											__lowercase       	=encoding_image_processor['original_sizes']

											if hasattr(_lowerCAmelCase  ,       'numpy'):  # Checks if Torch or TF tensor
																		__lowercase       	=original_sizes.numpy()

											__lowercase	,				__lowercase	,				__lowercase       	=self._check_and_preprocess_points(
											    input_points=_lowerCAmelCase  ,       input_labels=_lowerCAmelCase  ,       input_boxes=_lowerCAmelCase  ,       )

											__lowercase       	=self._normalize_and_convert(
											    _lowerCAmelCase  ,       _lowerCAmelCase  ,       input_points=_lowerCAmelCase  ,       input_labels=_lowerCAmelCase  ,       input_boxes=_lowerCAmelCase  ,       return_tensors=_lowerCAmelCase  ,       )

											return encoding_image_processor

				def 				__lowerCamelCase     (							self  :  int  ,       _lowerCAmelCase  :  Any  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  Optional[Any]=None  ,       _lowerCAmelCase  :  List[Any]=None  ,       _lowerCAmelCase  :  Optional[Any]=None  ,       _lowerCAmelCase  :  Optional[int]="pt"  ,       ):







											'''simple docstring'''

											if input_points is not None:
																		if len(_lowerCAmelCase) != len(_lowerCAmelCase):
																									__lowercase       	=[
																									    self._normalize_coordinates(self.target_size  ,       _lowerCAmelCase  ,       original_sizes[0]) for point in input_points
																									]
																		else:
																									__lowercase       	=[
																									    self._normalize_coordinates(self.target_size  ,       _lowerCAmelCase  ,       _lowerCAmelCase)
																									    for point, original_size in zip(_lowerCAmelCase  ,       _lowerCAmelCase)
																									]
																		# check that all arrays have the same shape
																		if not all(point.shape == input_points[0].shape for point in input_points):
																									if input_labels is not None:
																																__lowercase	,				__lowercase       	=self._pad_points_and_labels(_lowerCAmelCase  ,       _lowerCAmelCase)

																		__lowercase       	=np.array(_lowerCAmelCase)

											if input_labels is not None:
																		__lowercase       	=np.array(_lowerCAmelCase)

											if input_boxes is not None:
																		if len(_lowerCAmelCase) != len(_lowerCAmelCase):
																									__lowercase       	=[
																									    self._normalize_coordinates(self.target_size  ,       _lowerCAmelCase  ,       original_sizes[0]  ,       is_bounding_box=_lowerCAmelCase)
																									    for box in input_boxes
																									]
																		else:
																									__lowercase       	=[
																									    self._normalize_coordinates(self.target_size  ,       _lowerCAmelCase  ,       _lowerCAmelCase  ,       is_bounding_box=_lowerCAmelCase)
																									    for box, original_size in zip(_lowerCAmelCase  ,       _lowerCAmelCase)
																									]
																		__lowercase       	=np.array(_lowerCAmelCase)

											if input_boxes is not None:
																		if return_tensors == "pt":
																									__lowercase       	=torch.from_numpy(_lowerCAmelCase)
																									# boxes batch size of 1 by default
																									__lowercase       	=input_boxes.unsqueeze(1) if len(input_boxes.shape) != 3 else input_boxes
																		elif return_tensors == "tf":
																									__lowercase       	=tf.convert_to_tensor(_lowerCAmelCase)
																									# boxes batch size of 1 by default
																									__lowercase       	=tf.expand_dims(_lowerCAmelCase  ,       1) if len(input_boxes.shape) != 3 else input_boxes
																		encoding_image_processor.update({'input_boxes': input_boxes})
											if input_points is not None:
																		if return_tensors == "pt":
																									__lowercase       	=torch.from_numpy(_lowerCAmelCase)
																									# point batch size of 1 by default
																									__lowercase       	=input_points.unsqueeze(1) if len(input_points.shape) != 4 else input_points
																		elif return_tensors == "tf":
																									__lowercase       	=tf.convert_to_tensor(_lowerCAmelCase)
																									# point batch size of 1 by default
																									__lowercase       	=tf.expand_dims(_lowerCAmelCase  ,       1) if len(input_points.shape) != 4 else input_points
																		encoding_image_processor.update({'input_points': input_points})
											if input_labels is not None:
																		if return_tensors == "pt":
																									__lowercase       	=torch.from_numpy(_lowerCAmelCase)
																									# point batch size of 1 by default
																									__lowercase       	=input_labels.unsqueeze(1) if len(input_labels.shape) != 3 else input_labels
																		elif return_tensors == "tf":
																									__lowercase       	=tf.convert_to_tensor(_lowerCAmelCase)
																									# point batch size of 1 by default
																									__lowercase       	=tf.expand_dims(_lowerCAmelCase  ,       1) if len(input_labels.shape) != 3 else input_labels
																		encoding_image_processor.update({'input_labels': input_labels})

											return encoding_image_processor

				def 				__lowerCamelCase     (							self  :  List[str]  ,       _lowerCAmelCase  :  str  ,       _lowerCAmelCase  :  List[str]):







											'''simple docstring'''

											__lowercase       	=max([point.shape[0] for point in input_points])
											__lowercase       	=[]
											for i, point in enumerate(_lowerCAmelCase):
																		if point.shape[0] != expected_nb_points:
																									__lowercase       	=np.concatenate(
																									    [point, np.zeros((expected_nb_points - point.shape[0], 2)) + self.point_pad_value]  ,       axis=0)
																									__lowercase       	=np.append(input_labels[i]  ,       [self.point_pad_value])
																		processed_input_points.append(_lowerCAmelCase)
											__lowercase       	=processed_input_points
											return input_points, input_labels

				def 				__lowerCamelCase     (							self  :  Union[str, Any]  ,       _lowerCAmelCase  :  int  ,       _lowerCAmelCase  :  np.ndarray  ,       _lowerCAmelCase  :  Any  ,       _lowerCAmelCase  :  Union[str, Any]=False):







											'''simple docstring'''

											__lowercase	,				__lowercase       	=original_size
											__lowercase	,				__lowercase       	=self.image_processor._get_preprocess_shape(_lowerCAmelCase  ,       longest_edge=_lowerCAmelCase)
											__lowercase       	=deepcopy(_lowerCAmelCase).astype(_lowerCAmelCase)

											if is_bounding_box:
																		__lowercase       	=coords.reshape(-1  ,       2  ,       2)

											__lowercase       	=coords[..., 0] * (new_w / old_w)
											__lowercase       	=coords[..., 1] * (new_h / old_h)

											if is_bounding_box:
																		__lowercase       	=coords.reshape(-1  ,       4)

											return coords

				def 				__lowerCamelCase     (							self  :  Any  ,       _lowerCAmelCase  :  Optional[int]=None  ,       _lowerCAmelCase  :  Dict=None  ,       _lowerCAmelCase  :  Dict=None  ,       ):







											'''simple docstring'''

											if input_points is not None:
																		if hasattr(_lowerCAmelCase  ,       'numpy'):  # Checks for TF or Torch tensor
																									__lowercase       	=input_points.numpy().tolist()

																		if not isinstance(_lowerCAmelCase  ,       _lowerCAmelCase) or not isinstance(input_points[0]  ,       _lowerCAmelCase):
																									raise ValueError('Input points must be a list of list of floating points.')
																		__lowercase       	=[np.array(_lowerCAmelCase) for input_point in input_points]
											else:
																		__lowercase       	=None

											if input_labels is not None:
																		if hasattr(_lowerCAmelCase  ,       'numpy'):
																									__lowercase       	=input_labels.numpy().tolist()

																		if not isinstance(_lowerCAmelCase  ,       _lowerCAmelCase) or not isinstance(input_labels[0]  ,       _lowerCAmelCase):
																									raise ValueError('Input labels must be a list of list integers.')
																		__lowercase       	=[np.array(_lowerCAmelCase) for label in input_labels]
											else:
																		__lowercase       	=None

											if input_boxes is not None:
																		if hasattr(_lowerCAmelCase  ,       'numpy'):
																									__lowercase       	=input_boxes.numpy().tolist()

																		if (
																		    not isinstance(_lowerCAmelCase  ,       _lowerCAmelCase)
																		    or not isinstance(input_boxes[0]  ,       _lowerCAmelCase)
																		    or not isinstance(input_boxes[0][0]  ,       _lowerCAmelCase)
																		):
																									raise ValueError('Input boxes must be a list of list of list of floating points.')
																		__lowercase       	=[np.array(_lowerCAmelCase).astype(np.floataa) for box in input_boxes]
											else:
																		__lowercase       	=None

											return input_points, input_labels, input_boxes

				@property
				def 				__lowerCamelCase     (							self  :  List[Any]):







											'''simple docstring'''

											__lowercase       	=self.image_processor.model_input_names
											return list(dict.fromkeys(_lowerCAmelCase))

				def 				__lowerCamelCase     (							self  :  List[str]  ,       *_lowerCAmelCase  :  Optional[int]  ,       **_lowerCAmelCase  :  Any):







											'''simple docstring'''

											return self.image_processor.post_process_masks(*_lowerCAmelCase  ,       **_lowerCAmelCase)








'''simple docstring'''



import warnings

from ...utils import logging
from .image_processing_deit import DeiTImageProcessor


lowerCamelCase	 =       logging.get_logger(__name__)





class _UpperCamelCase		(     A		):





				'''simple docstring'''





				def __init__(							self  :  Optional[int]  ,       *_lowerCAmelCase  :  Union[str, Any]  ,       **_lowerCAmelCase  :  Union[str, Any]):







											'''simple docstring'''

											warnings.warn(
											    'The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
											    ' use DeiTImageProcessor instead.'  ,       _lowerCAmelCase  ,       )
											super().__init__(*_lowerCAmelCase  ,       **_lowerCAmelCase)



