


'''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 					a			(				snake_case__  ):
 '''simple docstring'''




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




 def __init__(    self		,      lowerCamelCase_      )      ->    List[str]:
    super().__init__(lowerCamelCase_      )
    _a					:  List[Any]						=  self.image_processor
    _a					:  Optional[int]						=  -1_0
    _a					:  Optional[Any]						=  self.image_processor.size['longest_edge']




 def __call__(    self		,      lowerCamelCase_=None		,      lowerCamelCase_=None		,      lowerCamelCase_=None		,      lowerCamelCase_=None		,      lowerCamelCase_ = None		,      **lowerCamelCase_		,      )      ->    BatchEncoding:
    _a					:  Optional[Any]						=  self.image_processor(
        lowerCamelCase_		,      return_tensors=lowerCamelCase_		,      **lowerCamelCase_		,      )

    # pop arguments that are not used in the foward but used nevertheless
    _a					:  Union[str, Any]						=  encoding_image_processor['original_sizes']

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

    _a		,						_a		,						_a					:  Optional[Any]						=  self._check_and_preprocess_points(
        input_points=lowerCamelCase_		,      input_labels=lowerCamelCase_		,      input_boxes=lowerCamelCase_		,      )

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

    return encoding_image_processor




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

       _a					:  Dict						=  np.array(lowerCamelCase_      )

    if input_labels is not None:
       _a					:  List[Any]						=  np.array(lowerCamelCase_      )

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

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




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

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

    _a					:  Any						=  coords[..., 0] * (new_w / old_w)
    _a					:  Tuple						=  coords[..., 1] * (new_h / old_h)

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

    return coords




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

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

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

    return input_points, input_labels, input_boxes




 @property
 def   __UpperCamelCase   (    self      )      ->    Optional[int]:
    _a					:  Tuple						=  self.image_processor.model_input_names
    return list(dict.fromkeys(lowerCamelCase_      )      )




 def   __UpperCamelCase   (    self		,      *lowerCamelCase_		,      **lowerCamelCase_      )      ->    Tuple:
    return self.image_processor.post_process_masks(*lowerCamelCase_		,      **lowerCamelCase_      )






'''simple docstring'''




from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class 					a			(				snake_case__  ):
 '''simple docstring'''




 __lowerCAmelCase						: Optional[int]							  =		"""ClapFeatureExtractor"""
 __lowerCAmelCase						: str							  =		("""RobertaTokenizer""", """RobertaTokenizerFast""")




 def __init__(    self		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    Union[str, Any]:
    super().__init__(lowerCamelCase_		,      lowerCamelCase_      )




 def __call__(    self		,      lowerCamelCase_=None		,      lowerCamelCase_=None		,      lowerCamelCase_=None		,      **lowerCamelCase_      )      ->    List[Any]:
    _a					:  Any						=  kwargs.pop('sampling_rate'		,      lowerCamelCase_      )

    if text is None and audios is None:
       raise ValueError('You have to specify either text or audios. Both cannot be none.'      )

    if text is not None:
       _a					:  Union[str, Any]						=  self.tokenizer(lowerCamelCase_		,      return_tensors=lowerCamelCase_		,      **lowerCamelCase_      )

    if audios is not None:
       _a					:  List[str]						=  self.feature_extractor(
           lowerCamelCase_		,      sampling_rate=lowerCamelCase_		,      return_tensors=lowerCamelCase_		,      **lowerCamelCase_      )

    if text is not None and audios is not None:
       _a					:  List[Any]						=  audio_features.input_features
       return encoding
    elif text is not None:
       return encoding
    else:
       return BatchEncoding(data=dict(**lowerCamelCase_      )		,      tensor_type=lowerCamelCase_      )




 def   __UpperCamelCase   (    self		,      *lowerCamelCase_		,      **lowerCamelCase_      )      ->    Tuple:
    return self.tokenizer.batch_decode(*lowerCamelCase_		,      **lowerCamelCase_      )




 def   __UpperCamelCase   (    self		,      *lowerCamelCase_		,      **lowerCamelCase_      )      ->    List[str]:
    return self.tokenizer.decode(*lowerCamelCase_		,      **lowerCamelCase_      )




 @property
 def   __UpperCamelCase   (    self      )      ->    Tuple:
    _a					:  Dict						=  self.tokenizer.model_input_names
    _a					:  Tuple						=  self.feature_extractor.model_input_names
    return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names      )      )


