


"""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  (   lowerCAmelCase__		):
							'''simple docstring'''







							__UpperCAmelCase						:    List[str]				      =["""image_processor"""]
							__UpperCAmelCase						:    str				      ="""SamImageProcessor"""

							def __init__(     self    ,   __a	):
										super().__init__(__a	)
										__lowerCAmelCase				    =			self.image_processor
										__lowerCAmelCase				    =			-10
										__lowerCAmelCase				    =			self.image_processor.size["longest_edge"]

							def __call__(     self    ,   __a=None    ,   __a=None    ,   __a=None    ,   __a=None    ,   __a = None    ,   **__a    ,   ):
										__lowerCAmelCase				    =			self.image_processor(
										    __a    ,   return_tensors=__a    ,   **__a    ,   )

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

										if hasattr(__a    ,   "numpy"	):  # Checks if Torch or TF tensor
													__lowerCAmelCase				    =			original_sizes.numpy()

										__lowerCAmelCase    ,	__lowerCAmelCase    ,	__lowerCAmelCase				    =			self._check_and_preprocess_points(
										    input_points=__a    ,   input_labels=__a    ,   input_boxes=__a    ,   )

										__lowerCAmelCase				    =			self._normalize_and_convert(
										    __a    ,   __a    ,   input_points=__a    ,   input_labels=__a    ,   input_boxes=__a    ,   return_tensors=__a    ,   )

										return encoding_image_processor

							def 			snake_case				(     self    ,   __a    ,   __a    ,   __a=None    ,   __a=None    ,   __a=None    ,   __a="pt"    ,   ):
										if input_points is not None:
													if len(__a	) != len(__a	):
																__lowerCAmelCase				    =			[
																    self._normalize_coordinates(self.target_size    ,   __a    ,   original_sizes[0]	) for point in input_points
																]
													else:
																__lowerCAmelCase				    =			[
																    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:
																			__lowerCAmelCase    ,	__lowerCAmelCase				    =			self._pad_points_and_labels(__a    ,   __a	)

													__lowerCAmelCase				    =			np.array(__a	)

										if input_labels is not None:
													__lowerCAmelCase				    =			np.array(__a	)

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

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

							def 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a=False	):
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			original_size
										__lowerCAmelCase    ,	__lowerCAmelCase				    =			self.image_processor._get_preprocess_shape(__a    ,   longest_edge=__a	)
										__lowerCAmelCase				    =			deepcopy(__a	).astype(__a	)

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

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

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

										return coords

							def 			snake_case				(     self    ,   __a=None    ,   __a=None    ,   __a=None    ,   ):
										if input_points is not None:
													if hasattr(__a    ,   "numpy"	):  # Checks for TF or Torch tensor
																__lowerCAmelCase				    =			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."	)
													__lowerCAmelCase				    =			[np.array(__a	) for input_point in input_points]
										else:
													__lowerCAmelCase				    =			None

										if input_labels is not None:
													if hasattr(__a    ,   "numpy"	):
																__lowerCAmelCase				    =			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."	)
													__lowerCAmelCase				    =			[np.array(__a	) for label in input_labels]
										else:
													__lowerCAmelCase				    =			None

										if input_boxes is not None:
													if hasattr(__a    ,   "numpy"	):
																__lowerCAmelCase				    =			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."	)
													__lowerCAmelCase				    =			[np.array(__a	).astype(np.floataa	) for box in input_boxes]
										else:
													__lowerCAmelCase				    =			None

										return input_points, input_labels, input_boxes

							@property
							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.image_processor.model_input_names
										return list(dict.fromkeys(__a	)	)



							def 			snake_case				(     self    ,   *__a    ,   **__a	):
										return self.image_processor.post_process_masks(*__a    ,   **__a	)








"""simple docstring"""


from diffusers.utils.testing_utils import require_onnxruntime
@require_onnxruntime
class   _UpperCamelCase  :
							'''simple docstring'''







							pass




