






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 							__a							(  __A						):





		'''simple docstring'''



		UpperCAmelCase__      :				str				    =						["""image_processor"""]
		UpperCAmelCase__      :				str				    =						"""SamImageProcessor"""


		def __init__(						self ,    UpperCamelCase__       ):
						super().__init__(UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	List[str]													=   self.image_processor
						SCREAMING_SNAKE_CASE_   :	Tuple													=   -10
						SCREAMING_SNAKE_CASE_   :	Any													=   self.image_processor.size['longest_edge']


		def __call__(						self ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    UpperCamelCase__ = None ,    **UpperCamelCase__ ,    ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.image_processor(
						    UpperCamelCase__ ,    return_tensors=UpperCamelCase__ ,    **UpperCamelCase__ ,    )

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

						if hasattr(UpperCamelCase__ ,    'numpy'       ):  # Checks if Torch or TF tensor
										SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   original_sizes.numpy()

						SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	str													=   self._check_and_preprocess_points(
						    input_points=UpperCamelCase__ ,    input_labels=UpperCamelCase__ ,    input_boxes=UpperCamelCase__ ,    )

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

						return encoding_image_processor


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    UpperCamelCase__="pt" ,    ):
						if input_points is not None:
										if len(UpperCamelCase__       ) != len(UpperCamelCase__       ):
														SCREAMING_SNAKE_CASE_   :	Any													=   [
														    self._normalize_coordinates(self.target_size ,    UpperCamelCase__ ,    original_sizes[0]       ) for point in input_points
														]
										else:
														SCREAMING_SNAKE_CASE_   :	Tuple													=   [
														    self._normalize_coordinates(self.target_size ,    UpperCamelCase__ ,    UpperCamelCase__       )
														    for point, original_size in zip(UpperCamelCase__ ,    UpperCamelCase__       )
														]
										# 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:
																		SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   self._pad_points_and_labels(UpperCamelCase__ ,    UpperCamelCase__       )

										SCREAMING_SNAKE_CASE_   :	str													=   np.array(UpperCamelCase__       )

						if input_labels is not None:
										SCREAMING_SNAKE_CASE_   :	str													=   np.array(UpperCamelCase__       )

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

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


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__=False       ):
						SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	Optional[int]													=   original_size
						SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.image_processor._get_preprocess_shape(UpperCamelCase__ ,    longest_edge=UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	int													=   deepcopy(UpperCamelCase__       ).astype(UpperCamelCase__       )

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

						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   coords[..., 0] * (new_w / old_w)
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   coords[..., 1] * (new_h / old_h)

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

						return coords


		def   __snake_case			(						self ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    ):
						if input_points is not None:
										if hasattr(UpperCamelCase__ ,    'numpy'       ):  # Checks for TF or Torch tensor
														SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   input_points.numpy().tolist()

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

						if input_labels is not None:
										if hasattr(UpperCamelCase__ ,    'numpy'       ):
														SCREAMING_SNAKE_CASE_   :	List[Any]													=   input_labels.numpy().tolist()

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

						if input_boxes is not None:
										if hasattr(UpperCamelCase__ ,    'numpy'       ):
														SCREAMING_SNAKE_CASE_   :	str													=   input_boxes.numpy().tolist()

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

						return input_points, input_labels, input_boxes


		@property
		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	str													=   self.image_processor.model_input_names
						return list(dict.fromkeys(UpperCamelCase__       )       )






		def   __snake_case			(						self ,    *UpperCamelCase__ ,    **UpperCamelCase__       ):
						return self.image_processor.post_process_masks(*UpperCamelCase__ ,    **UpperCamelCase__       )







from timeit import timeit



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				if number < 0:
								raise ValueError('the value of input must not be negative'      )
				SCREAMING_SNAKE_CASE_   :	Tuple													=   0
				while number:
								number &= number - 1
								result += 1
				return result



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				if number < 0:
								raise ValueError('the value of input must not be negative'      )
				SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   0
				while number:
								if number % 2 == 1:
												result += 1
								number >>= 1
				return result



def 					_lowerCamelCase(			):





				'''simple docstring'''







				def do_benchmark(lowerCAmelCase__				:		int      ) -> None:
								SCREAMING_SNAKE_CASE_   :	int													=   'import __main__ as z'
								print(F'''Benchmark when {number = }:'''      )
								print(F'''{get_set_bits_count_using_modulo_operator(lowerCAmelCase__      ) = }'''      )
								SCREAMING_SNAKE_CASE_   :	int													=   timeit('z.get_set_bits_count_using_modulo_operator(25)'    ,    setup=lowerCAmelCase__      )
								print(F'''timeit() runs in {timing} seconds'''      )
								print(F'''{get_set_bits_count_using_brian_kernighans_algorithm(lowerCAmelCase__      ) = }'''      )
								SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   timeit(
								    'z.get_set_bits_count_using_brian_kernighans_algorithm(25)'    ,    setup=lowerCAmelCase__    ,    )
								print(F'''timeit() runs in {timing} seconds'''      )

				for number in (25, 37, 58, 0):
								do_benchmark(lowerCAmelCase__      )
								print()


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				benchmark()