



"""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 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							['image_processor']
					_a             =							'SamImageProcessor'



					def __init__(	self							:  Dict,  lowerCamelCase							:  Optional[int]					)-> Tuple:
								super().__init__(lowerCamelCase					)
								lowerCamelCase__		:		Union[str, Any]		     =self.image_processor
								lowerCamelCase__		:		Any		     =-10
								lowerCamelCase__		:		Union[str, Any]		     =self.image_processor.size['''longest_edge''']



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

								# pop arguments that are not used in the foward but used nevertheless
								lowerCamelCase__		:		List[Any]		     =encoding_image_processor['''original_sizes''']

								if hasattr(lowerCamelCase,  '''numpy'''					):  # Checks if Torch or TF tensor
											lowerCamelCase__		:		str		     =original_sizes.numpy()

								lowerCamelCase__     ,       lowerCamelCase__     ,       lowerCamelCase__		:		Tuple		     =self._check_and_preprocess_points(
								    input_points=lowerCamelCase,  input_labels=lowerCamelCase,  input_boxes=lowerCamelCase,  )

								lowerCamelCase__		:		Union[str, Any]		     =self._normalize_and_convert(
								    lowerCamelCase,  lowerCamelCase,  input_points=lowerCamelCase,  input_labels=lowerCamelCase,  input_boxes=lowerCamelCase,  return_tensors=lowerCamelCase,  )

								return encoding_image_processor



					def 		snake_case	(	self							:  List[Any],  lowerCamelCase							:  List[Any],  lowerCamelCase							:  str,  lowerCamelCase							:  List[Any]=None,  lowerCamelCase							:  int=None,  lowerCamelCase							:  Optional[Any]=None,  lowerCamelCase							:  List[str]="pt",  )-> Dict:
								if input_points is not None:
											if len(lowerCamelCase					) != len(lowerCamelCase					):
														lowerCamelCase__		:		Optional[int]		     =[
														    self._normalize_coordinates(self.target_size,  lowerCamelCase,  original_sizes[0]					) for point in input_points
														]
											else:
														lowerCamelCase__		:		str		     =[
														    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:
																	lowerCamelCase__     ,       lowerCamelCase__		:		Dict		     =self._pad_points_and_labels(lowerCamelCase,  lowerCamelCase					)

											lowerCamelCase__		:		str		     =np.array(lowerCamelCase					)

								if input_labels is not None:
											lowerCamelCase__		:		Optional[int]		     =np.array(lowerCamelCase					)

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

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



					def 		snake_case	(	self							:  List[Any],  lowerCamelCase							:  int,  lowerCamelCase							:  np.ndarray,  lowerCamelCase							:  Dict,  lowerCamelCase							:  Any=False					)-> np.ndarray:
								lowerCamelCase__     ,       lowerCamelCase__		:		str		     =original_size
								lowerCamelCase__     ,       lowerCamelCase__		:		str		     =self.image_processor._get_preprocess_shape(lowerCamelCase,  longest_edge=lowerCamelCase					)
								lowerCamelCase__		:		Tuple		     =deepcopy(lowerCamelCase					).astype(lowerCamelCase					)

								if is_bounding_box:
											lowerCamelCase__		:		Optional[Any]		     =coords.reshape(-1,  2,  2					)

								lowerCamelCase__		:		List[str]		     =coords[..., 0] * (new_w / old_w)
								lowerCamelCase__		:		Dict		     =coords[..., 1] * (new_h / old_h)

								if is_bounding_box:
											lowerCamelCase__		:		List[Any]		     =coords.reshape(-1,  4					)

								return coords



					def 		snake_case	(	self							:  Any,  lowerCamelCase							:  str=None,  lowerCamelCase							:  Tuple=None,  lowerCamelCase							:  str=None,  )-> Tuple:
								if input_points is not None:
											if hasattr(lowerCamelCase,  '''numpy'''					):  # Checks for TF or Torch tensor
														lowerCamelCase__		:		int		     =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.'''					)
											lowerCamelCase__		:		str		     =[np.array(lowerCamelCase					) for input_point in input_points]
								else:
											lowerCamelCase__		:		Optional[Any]		     =None

								if input_labels is not None:
											if hasattr(lowerCamelCase,  '''numpy'''					):
														lowerCamelCase__		:		Any		     =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.'''					)
											lowerCamelCase__		:		Any		     =[np.array(lowerCamelCase					) for label in input_labels]
								else:
											lowerCamelCase__		:		Optional[Any]		     =None

								if input_boxes is not None:
											if hasattr(lowerCamelCase,  '''numpy'''					):
														lowerCamelCase__		:		Optional[int]		     =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.'''					)
											lowerCamelCase__		:		List[Any]		     =[np.array(lowerCamelCase					).astype(np.floataa					) for box in input_boxes]
								else:
											lowerCamelCase__		:		Tuple		     =None

								return input_points, input_labels, input_boxes



					@property
					def 		snake_case	(	self							:  Optional[int]					)-> Dict:
								lowerCamelCase__		:		Union[str, Any]		     =self.image_processor.model_input_names
								return list(dict.fromkeys(lowerCamelCase					)					)




					def 		snake_case	(	self							:  Optional[Any],  *lowerCamelCase							:  Any,  **lowerCamelCase							:  Optional[int]					)-> Optional[int]:
								return self.image_processor.post_process_masks(*lowerCamelCase,  **lowerCamelCase					)









"""simple docstring"""






from ..utils import DummyObject, requires_backends

class 	__SCREAMING_SNAKE_CASE     (				metaclass=lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							['torch', 'torchsde']



					def __init__(	self							:  Union[str, Any],  *lowerCamelCase							:  str,  **lowerCamelCase							:  int					)-> Tuple:
								requires_backends(self,  ['''torch''', '''torchsde''']					)



					@classmethod
					def 		snake_case	(	cls							:  List[str],  *lowerCamelCase							:  Optional[Any],  **lowerCamelCase							:  Dict					)-> str:
								requires_backends(cls,  ['''torch''', '''torchsde''']					)




					@classmethod
					def 		snake_case	(	cls							:  Tuple,  *lowerCamelCase							:  Dict,  **lowerCamelCase							:  Tuple					)-> List[str]:
								requires_backends(cls,  ['''torch''', '''torchsde''']					)




