


'''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  __magic_name__							(		lowerCAmelCase    ):
       UpperCAmelCase										=["image_processor"]
       UpperCAmelCase										="SamImageProcessor"






       def __init__(      self  ,							snake_case)						->  Union[str, Any]:




          '''simple docstring'''
          super().__init__(snake_case)
          _UpperCAmelCase       :       Tuple							   =self.image_processor
          _UpperCAmelCase       :       Tuple							   =-1_0
          _UpperCAmelCase       :       Optional[int]							   =self.image_processor.size['longest_edge']






       def __call__(      self  ,							snake_case=None  ,							snake_case=None  ,							snake_case=None  ,							snake_case=None  ,							snake_case = None  ,							**snake_case  ,							)						->  BatchEncoding:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =self.image_processor(
              snake_case  ,							return_tensors=snake_case  ,							**snake_case  ,							)

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

          if hasattr(snake_case  ,							'numpy'):  # Checks if Torch or TF tensor
             _UpperCAmelCase       :       Any							   =original_sizes.numpy()

          _UpperCAmelCase   ,	_UpperCAmelCase   ,	_UpperCAmelCase       :       int							   =self._check_and_preprocess_points(
              input_points=snake_case  ,							input_labels=snake_case  ,							input_boxes=snake_case  ,							)

          _UpperCAmelCase       :       List[Any]							   =self._normalize_and_convert(
              snake_case  ,							snake_case  ,							input_points=snake_case  ,							input_labels=snake_case  ,							input_boxes=snake_case  ,							return_tensors=snake_case  ,							)

          return encoding_image_processor






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case=None  ,							snake_case=None  ,							snake_case=None  ,							snake_case="pt"  ,							)						->  Dict:




          '''simple docstring'''
          if input_points is not None:
             if len(snake_case) != len(snake_case):
                _UpperCAmelCase       :       int							   =[
                    self._normalize_coordinates(self.target_size  ,							snake_case  ,							original_sizes[0]) for point in input_points
                ]
             else:
                _UpperCAmelCase       :       List[str]							   =[
                    self._normalize_coordinates(self.target_size  ,							snake_case  ,							snake_case)
                    for point, original_size in zip(snake_case  ,							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:
                   _UpperCAmelCase   ,	_UpperCAmelCase       :       str							   =self._pad_points_and_labels(snake_case  ,							snake_case)

             _UpperCAmelCase       :       Union[str, Any]							   =np.array(snake_case)

          if input_labels is not None:
             _UpperCAmelCase       :       Dict							   =np.array(snake_case)

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

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




          '''simple docstring'''
          _UpperCAmelCase       :       Tuple							   =max([point.shape[0] for point in input_points])
          _UpperCAmelCase       :       Any							   =[]
          for i, point in enumerate(snake_case):
             if point.shape[0] != expected_nb_points:
                _UpperCAmelCase       :       str							   =np.concatenate(
                    [point, np.zeros((expected_nb_points - point.shape[0], 2)) + self.point_pad_value]  ,							axis=0)
                _UpperCAmelCase       :       Optional[Any]							   =np.append(input_labels[i]  ,							[self.point_pad_value])
             processed_input_points.append(snake_case)
          _UpperCAmelCase       :       Optional[Any]							   =processed_input_points
          return input_points, input_labels






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case=False)						->  np.ndarray:




          '''simple docstring'''
          _UpperCAmelCase   ,	_UpperCAmelCase       :       int							   =original_size
          _UpperCAmelCase   ,	_UpperCAmelCase       :       Dict							   =self.image_processor._get_preprocess_shape(snake_case  ,							longest_edge=snake_case)
          _UpperCAmelCase       :       Optional[Any]							   =deepcopy(snake_case).astype(snake_case)

          if is_bounding_box:
             _UpperCAmelCase       :       List[str]							   =coords.reshape(-1  ,							2  ,							2)

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

          if is_bounding_box:
             _UpperCAmelCase       :       List[Any]							   =coords.reshape(-1  ,							4)

          return coords






       def       lowerCAmelCase		(      self  ,							snake_case=None  ,							snake_case=None  ,							snake_case=None  ,							)						->  Any:




          '''simple docstring'''
          if input_points is not None:
             if hasattr(snake_case  ,							'numpy'):  # Checks for TF or Torch tensor
                _UpperCAmelCase       :       List[str]							   =input_points.numpy().tolist()

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

          if input_labels is not None:
             if hasattr(snake_case  ,							'numpy'):
                _UpperCAmelCase       :       Any							   =input_labels.numpy().tolist()

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

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

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

          return input_points, input_labels, input_boxes






       @property
       def       lowerCAmelCase		(      self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =self.image_processor.model_input_names
          return list(dict.fromkeys(snake_case))






       def       lowerCAmelCase		(      self  ,							*snake_case  ,							**snake_case)						->  Optional[int]:




          '''simple docstring'''
          return self.image_processor.post_process_masks(*snake_case  ,							**snake_case)





'''simple docstring'''






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
 from transformers import TvltImageProcessor

if is_speech_available():
 from transformers import TvltFeatureExtractor

from transformers import TvltProcessor






@require_torch
class  __magic_name__							(		unittest.TestCase    ):






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   ='ZinengTang/tvlt-base'
          _UpperCAmelCase       :       Dict							   =tempfile.mkdtemp()






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Union[str, Any]:




          '''simple docstring'''
          return TvltImageProcessor.from_pretrained(self.checkpoint  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Dict:




          '''simple docstring'''
          return TvltFeatureExtractor.from_pretrained(self.checkpoint  ,							**snake_case)






       def       lowerCAmelCase		(      self)						->  Any:




          '''simple docstring'''
          shutil.rmtree(self.tmpdirname)






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =self.get_image_processor()
          _UpperCAmelCase       :       Optional[Any]							   =self.get_feature_extractor()

          _UpperCAmelCase       :       str							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)
          processor.save_pretrained(self.tmpdirname)
          _UpperCAmelCase       :       str							   =TvltProcessor.from_pretrained(self.tmpdirname)

          self.assertIsInstance(processor.feature_extractor  ,							snake_case)
          self.assertIsInstance(processor.image_processor  ,							snake_case)






       def       lowerCAmelCase		(      self)						->  Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =self.get_image_processor()
          _UpperCAmelCase       :       List[Any]							   =self.get_feature_extractor()

          _UpperCAmelCase       :       str							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Optional[int]							   =np.ones([1_2_0_0_0])

          _UpperCAmelCase       :       str							   =feature_extractor(snake_case  ,							return_tensors='np')
          _UpperCAmelCase       :       Dict							   =processor(audio=snake_case  ,							return_tensors='np')

          for key in audio_dict.keys():
             self.assertAlmostEqual(audio_dict[key].sum()  ,							input_processor[key].sum()  ,							delta=1E-2)






       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   =self.get_image_processor()
          _UpperCAmelCase       :       int							   =self.get_feature_extractor()

          _UpperCAmelCase       :       int							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Union[str, Any]							   =np.ones([3, 2_2_4, 2_2_4])

          _UpperCAmelCase       :       Optional[Any]							   =image_processor(snake_case  ,							return_tensors='np')
          _UpperCAmelCase       :       List[Any]							   =processor(images=snake_case  ,							return_tensors='np')

          for key in image_dict.keys():
             self.assertAlmostEqual(image_dict[key].sum()  ,							input_processor[key].sum()  ,							delta=1E-2)






       def       lowerCAmelCase		(      self)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       List[str]							   =self.get_image_processor()
          _UpperCAmelCase       :       Dict							   =self.get_feature_extractor()

          _UpperCAmelCase       :       Optional[int]							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Optional[int]							   =np.ones([1_2_0_0_0])
          _UpperCAmelCase       :       str							   =np.ones([3, 2_2_4, 2_2_4])

          _UpperCAmelCase       :       Optional[int]							   =processor(audio=snake_case  ,							images=snake_case)

          self.assertListEqual(list(inputs.keys())  ,							['audio_values', 'audio_mask', 'pixel_values', 'pixel_mask'])

          # test if it raises when no input is passed
          with pytest.raises(snake_case):
             processor()






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =self.get_image_processor()
          _UpperCAmelCase       :       Tuple							   =self.get_feature_extractor()

          _UpperCAmelCase       :       Dict							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          self.assertListEqual(
              processor.model_input_names  ,							image_processor.model_input_names + feature_extractor.model_input_names  ,							msg='`processor` and `image_processor`+`feature_extractor` model input names do not match'  ,							)

