


'''simple docstring'''



from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

A__		:					int												=  TypeVar("""KEY""")
A__		:					int												=  TypeVar("""VAL""")





@dataclass(frozen=_UpperCAmelCase					,		slots=_UpperCAmelCase  )
class    UpperCAmelCase_  (Generic[KEY, VAL]  ):





    """simple docstring"""






    lowerCamelCase  :					KEY
    lowerCamelCase  :					VAL







class    UpperCAmelCase_  (_Item  ):





    """simple docstring"""






    def __init__(							self	)				->     None:
         super().__init__(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)







    def __bool__(							self	)				->     bool:
         return False







A__		:					Union[str, Any]												=  _DeletedItem()





class    UpperCAmelCase_  (MutableMapping[KEY, VAL]  ):





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_ = 8			,     SCREAMING_SNAKE_CASE_ = 0.7_5	)				->     None:
         __lowerCamelCase   :	Dict								=		initial_block_size
         __lowerCamelCase   :	list[_Item | None]								=		[None] * initial_block_size
         assert 0.0 < capacity_factor < 1.0
         __lowerCamelCase   :	Any								=		capacity_factor
         __lowerCamelCase   :	Tuple								=		0







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     int:
         return hash(SCREAMING_SNAKE_CASE_	) % len(self._buckets	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     int:
         return (ind + 1) % len(self._buckets	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     bool:
         __lowerCamelCase   :	str								=		self._buckets[ind]
         if not stored:
              __lowerCamelCase   :	Any								=		_Item(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
              self._len += 1
              return True
         elif stored.key == key:
              __lowerCamelCase   :	Dict								=		_Item(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
              return True
         else:
              return False







    def       lowercase_				(							self	)				->     bool:
         __lowerCamelCase   :	Optional[Any]								=		len(self._buckets	) * self._capacity_factor
         return len(self	) >= int(SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     bool:
         if len(self._buckets	) <= self._initial_block_size:
              return False
         __lowerCamelCase   :	List[Any]								=		len(self._buckets	) * self._capacity_factor / 2
         return len(self	) < limit







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     None:
         __lowerCamelCase   :	Union[str, Any]								=		self._buckets
         __lowerCamelCase   :	str								=		[None] * new_size
         __lowerCamelCase   :	List[Any]								=		0
         for item in old_buckets:
              if item:
                   self._add_item(item.key			,     item.val	)







    def       lowercase_				(							self	)				->     None:
         self._resize(len(self._buckets	) * 2	)







    def       lowercase_				(							self	)				->     None:
         self._resize(len(self._buckets	) // 2	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     Iterator[int]:
         __lowerCamelCase   :	Optional[int]								=		self._get_bucket_index(SCREAMING_SNAKE_CASE_	)
         for _ in range(len(self._buckets	)	):
              yield ind
              __lowerCamelCase   :	int								=		self._get_next_ind(SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     None:
         for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE_	):
              if self._try_set(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	):
                   break







    def __setitem__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     None:
         if self._is_full():
              self._size_up()

         self._add_item(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)







    def __delitem__(							self			,     SCREAMING_SNAKE_CASE_	)				->     None:
         for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE_	):
              __lowerCamelCase   :	int								=		self._buckets[ind]
              if item is None:
                   raise KeyError(SCREAMING_SNAKE_CASE_	)
              if item is _deleted:
                   continue
              if item.key == key:
                   __lowerCamelCase   :	Optional[Any]								=		_deleted
                   self._len -= 1
                   break
         if self._is_sparse():
              self._size_down()







    def __getitem__(							self			,     SCREAMING_SNAKE_CASE_	)				->     VAL:
         for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE_	):
              __lowerCamelCase   :	Optional[int]								=		self._buckets[ind]
              if item is None:
                   break
              if item is _deleted:
                   continue
              if item.key == key:
                   return item.val
         raise KeyError(SCREAMING_SNAKE_CASE_	)







    def __len__(							self	)				->     int:
         return self._len







    def __iter__(							self	)				->     Iterator[KEY]:
         yield from (item.key for item in self._buckets if item)







    def __repr__(							self	)				->     str:
         __lowerCamelCase   :	List[str]								=		' ,'.join(
             f'{item.key}: {item.val}' for item in self._buckets if item	)
         return f'HashMap({val_string})'










'''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_  (_UpperCAmelCase  ):





    """simple docstring"""






    lowerCamelCase  :					Dict							     =			['image_processor']
    lowerCamelCase  :					str							     =			'SamImageProcessor'







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







    def __call__(							self			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_ = None			,     **SCREAMING_SNAKE_CASE_			,     )				->     BatchEncoding:
         __lowerCamelCase   :	Union[str, Any]								=		self.image_processor(
             SCREAMING_SNAKE_CASE_			,     return_tensors=SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_			,     )

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

         if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):  # Checks if Torch or TF tensor
              __lowerCamelCase   :	Dict								=		original_sizes.numpy()

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

         __lowerCamelCase   :	Tuple								=		self._normalize_and_convert(
             SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     input_points=SCREAMING_SNAKE_CASE_			,     input_labels=SCREAMING_SNAKE_CASE_			,     input_boxes=SCREAMING_SNAKE_CASE_			,     return_tensors=SCREAMING_SNAKE_CASE_			,     )

         return encoding_image_processor







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

              __lowerCamelCase   :	Optional[int]								=		np.array(SCREAMING_SNAKE_CASE_	)

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

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

         if input_boxes is not None:
              if return_tensors == "pt":
                   __lowerCamelCase   :	Any								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # boxes batch size of 1 by default
                   __lowerCamelCase   :	Union[str, Any]								=		input_boxes.unsqueeze(1	) if len(input_boxes.shape	) != 3 else input_boxes
              elif return_tensors == "tf":
                   __lowerCamelCase   :	List[str]								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # boxes batch size of 1 by default
                   __lowerCamelCase   :	str								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     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   :	int								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		input_points.unsqueeze(1	) if len(input_points.shape	) != 4 else input_points
              elif return_tensors == "tf":
                   __lowerCamelCase   :	Dict								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Tuple								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     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   :	List[Any]								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		input_labels.unsqueeze(1	) if len(input_labels.shape	) != 3 else input_labels
              elif return_tensors == "tf":
                   __lowerCamelCase   :	str								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     1	) if len(input_labels.shape	) != 3 else input_labels
              encoding_image_processor.update({'input_labels': input_labels}	)

         return encoding_image_processor







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     Tuple:
         __lowerCamelCase   :	List[str]								=		max([point.shape[0] for point in input_points]	)
         __lowerCamelCase   :	Union[str, Any]								=		[]
         for i, point in enumerate(SCREAMING_SNAKE_CASE_	):
              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   :	List[Any]								=		np.append(input_labels[i]			,     [self.point_pad_value]	)
              processed_input_points.append(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	List[Any]								=		processed_input_points
         return input_points, input_labels







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=False	)				->     np.ndarray:
         __lowerCamelCase				,       __lowerCamelCase   :	Tuple								=		original_size
         __lowerCamelCase				,       __lowerCamelCase   :	Optional[Any]								=		self.image_processor._get_preprocess_shape(SCREAMING_SNAKE_CASE_			,     longest_edge=SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Dict								=		deepcopy(SCREAMING_SNAKE_CASE_	).astype(SCREAMING_SNAKE_CASE_	)

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

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

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

         return coords







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     )				->     Optional[Any]:
         if input_points is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):  # Checks for TF or Torch tensor
                   __lowerCamelCase   :	List[str]								=		input_points.numpy().tolist()

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

         if input_labels is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):
                   __lowerCamelCase   :	Union[str, Any]								=		input_labels.numpy().tolist()

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

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

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

         return input_points, input_labels, input_boxes







    @property
    def       lowercase_				(							self	)				->     Dict:
         __lowerCamelCase   :	Any								=		self.image_processor.model_input_names
         return list(dict.fromkeys(SCREAMING_SNAKE_CASE_	)	)







    def       lowercase_				(							self			,     *SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)				->     Optional[Any]:
         return self.image_processor.post_process_masks(*SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)






