
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 					lowerCamelCase__				(							__lowercase):




	'''simple docstring'''




	_A								=	['image_processor']
	_A								=	'SamImageProcessor'







	def __init__(   self						:Any	,  a						:List[str]		)						->					Tuple:
								super().__init__(a		)
								__UpperCamelCase      :  List[str]       =   self.image_processor
								__UpperCamelCase      :  Tuple       =   -1_0
								__UpperCamelCase      :  List[str]       =   self.image_processor.size["longest_edge"]







	def __call__(   self						:Optional[int]	,  a						:Tuple=None	,  a						:Any=None	,  a						:List[Any]=None	,  a						:Optional[Any]=None	,  a						:Optional[Union[str, TensorType]] = None	,  **a						:List[Any]	,  )						->					BatchEncoding:
								__UpperCamelCase      :  Union[str, Any]       =   self.image_processor(
								    a	,  return_tensors=a	,  **a	,  )

								# pop arguments that are not used in the foward but used nevertheless
								__UpperCamelCase      :  Any       =   encoding_image_processor["original_sizes"]

								if hasattr(a	,  "numpy"		):  # Checks if Torch or TF tensor
															__UpperCamelCase      :  Any       =   original_sizes.numpy()

								__UpperCamelCase  ,		__UpperCamelCase  ,		__UpperCamelCase      :  Dict       =   self._check_and_preprocess_points(
								    input_points=a	,  input_labels=a	,  input_boxes=a	,  )

								__UpperCamelCase      :  Dict       =   self._normalize_and_convert(
								    a	,  a	,  input_points=a	,  input_labels=a	,  input_boxes=a	,  return_tensors=a	,  )

								return encoding_image_processor







	def   _lowerCamelCase							(   self						:Any	,  a						:List[Any]	,  a						:Any	,  a						:str=None	,  a						:Dict=None	,  a						:Tuple=None	,  a						:Tuple="pt"	,  )						->					List[str]:
								if input_points is not None:
															if len(a		) != len(a		):
																						__UpperCamelCase      :  Tuple       =   [
																						    self._normalize_coordinates(self.target_size	,  a	,  original_sizes[0]		) for point in input_points
																						]
															else:
																						__UpperCamelCase      :  List[Any]       =   [
																						    self._normalize_coordinates(self.target_size	,  a	,  a		)
																						    for point, original_size in zip(a	,  a		)
																						]
															# 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:
																													__UpperCamelCase  ,		__UpperCamelCase      :  Union[str, Any]       =   self._pad_points_and_labels(a	,  a		)

															__UpperCamelCase      :  Optional[int]       =   np.array(a		)

								if input_labels is not None:
															__UpperCamelCase      :  Optional[Any]       =   np.array(a		)

								if input_boxes is not None:
															if len(a		) != len(a		):
																						__UpperCamelCase      :  Tuple       =   [
																						    self._normalize_coordinates(self.target_size	,  a	,  original_sizes[0]	,  is_bounding_box=a		)
																						    for box in input_boxes
																						]
															else:
																						__UpperCamelCase      :  Optional[Any]       =   [
																						    self._normalize_coordinates(self.target_size	,  a	,  a	,  is_bounding_box=a		)
																						    for box, original_size in zip(a	,  a		)
																						]
															__UpperCamelCase      :  Union[str, Any]       =   np.array(a		)

								if input_boxes is not None:
															if return_tensors == "pt":
																						__UpperCamelCase      :  Dict       =   torch.from_numpy(a		)
																						# boxes batch size of 1 by default
																						__UpperCamelCase      :  Optional[int]       =   input_boxes.unsqueeze(1		) if len(input_boxes.shape		) != 3 else input_boxes
															elif return_tensors == "tf":
																						__UpperCamelCase      :  List[Any]       =   tf.convert_to_tensor(a		)
																						# boxes batch size of 1 by default
																						__UpperCamelCase      :  str       =   tf.expand_dims(a	,  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":
																						__UpperCamelCase      :  Optional[int]       =   torch.from_numpy(a		)
																						# point batch size of 1 by default
																						__UpperCamelCase      :  Any       =   input_points.unsqueeze(1		) if len(input_points.shape		) != 4 else input_points
															elif return_tensors == "tf":
																						__UpperCamelCase      :  Union[str, Any]       =   tf.convert_to_tensor(a		)
																						# point batch size of 1 by default
																						__UpperCamelCase      :  List[str]       =   tf.expand_dims(a	,  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":
																						__UpperCamelCase      :  List[str]       =   torch.from_numpy(a		)
																						# point batch size of 1 by default
																						__UpperCamelCase      :  str       =   input_labels.unsqueeze(1		) if len(input_labels.shape		) != 3 else input_labels
															elif return_tensors == "tf":
																						__UpperCamelCase      :  List[Any]       =   tf.convert_to_tensor(a		)
																						# point batch size of 1 by default
																						__UpperCamelCase      :  Union[str, Any]       =   tf.expand_dims(a	,  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						:Any	,  a						:Dict	,  a						:List[Any]		)						->					Optional[int]:
								__UpperCamelCase      :  Optional[int]       =   max([point.shape[0] for point in input_points]		)
								__UpperCamelCase      :  List[str]       =   []
								for i, point in enumerate(a		):
															if point.shape[0] != expected_nb_points:
																						__UpperCamelCase      :  Optional[int]       =   np.concatenate(
																						    [point, np.zeros((expected_nb_points - point.shape[0], 2)		) + self.point_pad_value]	,  axis=0		)
																						__UpperCamelCase      :  List[str]       =   np.append(input_labels[i]	,  [self.point_pad_value]		)
															processed_input_points.append(a		)
								__UpperCamelCase      :  Dict       =   processed_input_points
								return input_points, input_labels







	def   _lowerCamelCase							(   self						:Union[str, Any]	,  a						:int	,  a						:np.ndarray	,  a						:Optional[int]	,  a						:Optional[Any]=False		)						->					np.ndarray:
								__UpperCamelCase  ,		__UpperCamelCase      :  Any       =   original_size
								__UpperCamelCase  ,		__UpperCamelCase      :  Dict       =   self.image_processor._get_preprocess_shape(a	,  longest_edge=a		)
								__UpperCamelCase      :  Tuple       =   deepcopy(a		).astype(a		)

								if is_bounding_box:
															__UpperCamelCase      :  List[str]       =   coords.reshape(-1	,  2	,  2		)

								__UpperCamelCase      :  Any       =   coords[..., 0] * (new_w / old_w)
								__UpperCamelCase      :  List[Any]       =   coords[..., 1] * (new_h / old_h)

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

								return coords







	def   _lowerCamelCase							(   self						:Dict	,  a						:Optional[Any]=None	,  a						:Dict=None	,  a						:Optional[Any]=None	,  )						->					Dict:
								if input_points is not None:
															if hasattr(a	,  "numpy"		):  # Checks for TF or Torch tensor
																						__UpperCamelCase      :  Dict       =   input_points.numpy().tolist()

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

								if input_labels is not None:
															if hasattr(a	,  "numpy"		):
																						__UpperCamelCase      :  Any       =   input_labels.numpy().tolist()

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

								if input_boxes is not None:
															if hasattr(a	,  "numpy"		):
																						__UpperCamelCase      :  Tuple       =   input_boxes.numpy().tolist()

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

								return input_points, input_labels, input_boxes







	@property
	def   _lowerCamelCase							(   self						:Union[str, Any]		)						->					List[Any]:
								__UpperCamelCase      :  Tuple       =   self.image_processor.model_input_names
								return list(dict.fromkeys(a		)		)







	def   _lowerCamelCase							(   self						:List[str]	,  *a						:str	,  **a						:Any		)						->					int:
								return self.image_processor.post_process_masks(*a	,  **a		)

import warnings

from ...utils import logging
from .image_processing_perceiver import PerceiverImageProcessor


lowercase    :   List[str]						=			logging.get_logger(__name__)




class 					lowerCamelCase__				(							__lowercase):




	'''simple docstring'''




	def __init__(   self						:Dict	,  *a						:Tuple	,  **a						:List[Any]		)						->					None:
								warnings.warn(
								    "The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers."
								    " Please use PerceiverImageProcessor instead."	,  a	,  )
								super().__init__(*a	,  **a		)