


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__	( A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Tuple              =  ["image_processor"]
    UpperCAmelCase__						:		Optional[Any]              =  "SamImageProcessor"






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






    def __call__(      self ,       A_=None ,       A_=None ,       A_=None ,       A_=None ,       A_ = None ,       **A_ ,       )  ->	BatchEncoding:
         __UpperCamelCase   							=self.image_processor(
             A_ ,       return_tensors=A_ ,       **A_ ,       )

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

         if hasattr(A_ ,       'numpy'   ):  # Checks if Torch or TF tensor
              __UpperCamelCase   							=original_sizes.numpy()

         __UpperCamelCase							,				__UpperCamelCase							,				__UpperCamelCase   							=self._check_and_preprocess_points(
             input_points=A_ ,       input_labels=A_ ,       input_boxes=A_ ,       )

         __UpperCamelCase   							=self._normalize_and_convert(
             A_ ,       A_ ,       input_points=A_ ,       input_labels=A_ ,       input_boxes=A_ ,       return_tensors=A_ ,       )

         return encoding_image_processor






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

              __UpperCamelCase   							=np.array(A_   )

         if input_labels is not None:
              __UpperCamelCase   							=np.array(A_   )

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

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






    def _a (      self ,       A_ ,       A_ ,       A_ ,       A_=False   )  ->	np.ndarray:
         __UpperCamelCase							,				__UpperCamelCase   							=original_size
         __UpperCamelCase							,				__UpperCamelCase   							=self.image_processor._get_preprocess_shape(A_ ,       longest_edge=A_   )
         __UpperCamelCase   							=deepcopy(A_   ).astype(A_   )

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

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

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

         return coords






    def _a (      self ,       A_=None ,       A_=None ,       A_=None ,       )  ->	List[str]:
         if input_points is not None:
              if hasattr(A_ ,       'numpy'   ):  # Checks for TF or Torch tensor
                   __UpperCamelCase   							=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.'   )
              __UpperCamelCase   							=[np.array(A_   ) for input_point in input_points]
         else:
              __UpperCamelCase   							=None

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

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

         return input_points, input_labels, input_boxes






    @property
    def _a (      self   )  ->	int:
         __UpperCamelCase   							=self.image_processor.model_input_names
         return list(dict.fromkeys(A_   )   )






    def _a (      self ,       *A_ ,       **A_   )  ->	Optional[int]:
         return self.image_processor.post_process_masks(*A_ ,       **A_   )









from ..utils import DummyObject, requires_backends






class    UpperCAmelCase__	( metaclass=A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Any              =  ["speech"]






    def __init__(      self ,       *A_ ,       **A_   )  ->	Any:
         requires_backends(self ,       ['speech']   )






class    UpperCAmelCase__	( metaclass=A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Union[str, Any]              =  ["speech"]






    def __init__(      self ,       *A_ ,       **A_   )  ->	Union[str, Any]:
         requires_backends(self ,       ['speech']   )





