





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'''

     __snake_case   :     Any		     =      ["image_processor"]
     __snake_case   :     Optional[Any]		     =      "SamImageProcessor"






     def __init__(							self						:   List[str]						,lowerCamelCase__						:   List[str]	)					->		List[str]:




           '''simple docstring'''


           super().__init__(lowerCamelCase__	)
           SCREAMING_SNAKE_CASE		=						self.image_processor
           SCREAMING_SNAKE_CASE		=						-10
           SCREAMING_SNAKE_CASE		=						self.image_processor.size["""longest_edge"""]






     def __call__(							self						:   Optional[int]						,lowerCamelCase__						:   Optional[Any]=None						,lowerCamelCase__						:   Tuple=None						,lowerCamelCase__						:   Dict=None						,lowerCamelCase__						:   str=None						,lowerCamelCase__						:   Optional[Union[str, TensorType]] = None						,**lowerCamelCase__						:   Optional[int]						,)					->		BatchEncoding:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.image_processor(
               lowerCamelCase__						,return_tensors=lowerCamelCase__						,**lowerCamelCase__						,)

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

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

           SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self._check_and_preprocess_points(
               input_points=lowerCamelCase__						,input_labels=lowerCamelCase__						,input_boxes=lowerCamelCase__						,)

           SCREAMING_SNAKE_CASE		=						self._normalize_and_convert(
               lowerCamelCase__						,lowerCamelCase__						,input_points=lowerCamelCase__						,input_labels=lowerCamelCase__						,input_boxes=lowerCamelCase__						,return_tensors=lowerCamelCase__						,)

           return encoding_image_processor






     def     SCREAMING_SNAKE_CASE__  (							self						:   int						,lowerCamelCase__						:   List[Any]						,lowerCamelCase__						:   Optional[Any]						,lowerCamelCase__						:   Optional[int]=None						,lowerCamelCase__						:   Dict=None						,lowerCamelCase__						:   int=None						,lowerCamelCase__						:   Dict="pt"						,)					->		Union[str, Any]:




           '''simple docstring'''


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

                 SCREAMING_SNAKE_CASE		=						np.array(lowerCamelCase__	)

           if input_labels is not None:
                 SCREAMING_SNAKE_CASE		=						np.array(lowerCamelCase__	)

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

           if input_boxes is not None:
                 if return_tensors == "pt":
                       SCREAMING_SNAKE_CASE		=						torch.from_numpy(lowerCamelCase__	)
                       # boxes batch size of 1 by default
                       SCREAMING_SNAKE_CASE		=						input_boxes.unsqueeze(1	) if len(input_boxes.shape	) != 3 else input_boxes
                 elif return_tensors == "tf":
                       SCREAMING_SNAKE_CASE		=						tf.convert_to_tensor(lowerCamelCase__	)
                       # boxes batch size of 1 by default
                       SCREAMING_SNAKE_CASE		=						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":
                       SCREAMING_SNAKE_CASE		=						torch.from_numpy(lowerCamelCase__	)
                       # point batch size of 1 by default
                       SCREAMING_SNAKE_CASE		=						input_points.unsqueeze(1	) if len(input_points.shape	) != 4 else input_points
                 elif return_tensors == "tf":
                       SCREAMING_SNAKE_CASE		=						tf.convert_to_tensor(lowerCamelCase__	)
                       # point batch size of 1 by default
                       SCREAMING_SNAKE_CASE		=						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":
                       SCREAMING_SNAKE_CASE		=						torch.from_numpy(lowerCamelCase__	)
                       # point batch size of 1 by default
                       SCREAMING_SNAKE_CASE		=						input_labels.unsqueeze(1	) if len(input_labels.shape	) != 3 else input_labels
                 elif return_tensors == "tf":
                       SCREAMING_SNAKE_CASE		=						tf.convert_to_tensor(lowerCamelCase__	)
                       # point batch size of 1 by default
                       SCREAMING_SNAKE_CASE		=						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     SCREAMING_SNAKE_CASE__  (							self						:   Tuple						,lowerCamelCase__						:   Any						,lowerCamelCase__						:   Dict	)					->		str:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						max([point.shape[0] for point in input_points]	)
           SCREAMING_SNAKE_CASE		=						[]
           for i, point in enumerate(lowerCamelCase__	):
                 if point.shape[0] != expected_nb_points:
                       SCREAMING_SNAKE_CASE		=						np.concatenate(
                           [point, np.zeros((expected_nb_points - point.shape[0], 2)	) + self.point_pad_value]						,axis=0	)
                       SCREAMING_SNAKE_CASE		=						np.append(input_labels[i]						,[self.point_pad_value]	)
                 processed_input_points.append(lowerCamelCase__	)
           SCREAMING_SNAKE_CASE		=						processed_input_points
           return input_points, input_labels






     def     SCREAMING_SNAKE_CASE__  (							self						:   List[str]						,lowerCamelCase__						:   int						,lowerCamelCase__						:   np.ndarray						,lowerCamelCase__						:   str						,lowerCamelCase__						:   str=False	)					->		np.ndarray:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						original_size
           SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self.image_processor._get_preprocess_shape(lowerCamelCase__						,longest_edge=lowerCamelCase__	)
           SCREAMING_SNAKE_CASE		=						deepcopy(lowerCamelCase__	).astype(lowerCamelCase__	)

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

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

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

           return coords






     def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[Any]						,lowerCamelCase__						:   str=None						,lowerCamelCase__						:   Optional[int]=None						,lowerCamelCase__						:   List[Any]=None						,)					->		List[str]:




           '''simple docstring'''


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

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

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

           return input_points, input_labels, input_boxes






     @property
     def     SCREAMING_SNAKE_CASE__  (							self						:   Dict	)					->		Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.image_processor.model_input_names
           return list(dict.fromkeys(lowerCamelCase__	)	)






     def     SCREAMING_SNAKE_CASE__  (							self						:   List[str]						,*lowerCamelCase__						:   str						,**lowerCamelCase__						:   Optional[int]	)					->		int:




           '''simple docstring'''


           return self.image_processor.post_process_masks(*lowerCamelCase__						,**lowerCamelCase__	)









def        __lowercase       (				_SCREAMING_SNAKE_CASE = 1			,						_SCREAMING_SNAKE_CASE = 10_00     )			->       int:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						1
      SCREAMING_SNAKE_CASE		=						0

      for divide_by_number in range(_SCREAMING_SNAKE_CASE			,						digit + 1     ):
            SCREAMING_SNAKE_CASE		=						[]
            SCREAMING_SNAKE_CASE		=						numerator
            for _ in range(1			,						digit + 1     ):
                  if now_divide in has_been_divided:
                        if longest_list_length < len(_SCREAMING_SNAKE_CASE     ):
                              SCREAMING_SNAKE_CASE		=						len(_SCREAMING_SNAKE_CASE     )
                              SCREAMING_SNAKE_CASE		=						divide_by_number
                  else:
                        has_been_divided.append(_SCREAMING_SNAKE_CASE     )
                        SCREAMING_SNAKE_CASE		=						now_divide * 10 % divide_by_number

      return the_digit


# Tests
if __name__ == "__main__":
      import doctest

      doctest.testmod()


