


from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
				from ..image_utils import load_image


if is_torch_available():
				import torch

				from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING

__UpperCamelCase    :						Optional[int]     =							logging.get_logger(__name__)


__UpperCamelCase    :						Optional[Any]     =							Dict[str, Any]
__UpperCamelCase    :						int     =							List[Prediction]



@add_end_docstrings(snake_case__						)
class 							_UpperCamelCase (						snake_case__						):

			'''simple docstring'''







			def __init__(  self  : Any       ,  *_lowerCamelCase  : Union[str, Any]       ,  **_lowerCamelCase  : str							):






									'''simple docstring'''



									super().__init__(*_A       ,  **_A							)

									if self.framework == "tf":
															raise ValueError(F"""The {self.__class__} is only available in PyTorch."""							)

									requires_backends(self       ,  """vision"""							)
									self.check_model_type(
									    dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items()							)							)

			def   _snake_case    (  self  : Optional[int]       ,  **_lowerCamelCase  : int							):






									'''simple docstring'''



									__lowerCamelCase     :      Tuple       						=						{}
									if "threshold" in kwargs:
															__lowerCamelCase     :      List[Any]       						=						kwargs['threshold']
									return {}, {}, postprocess_kwargs

			def __call__(  self  : Any       ,  *_lowerCamelCase  : str       ,  **_lowerCamelCase  : int							):






									'''simple docstring'''



									return super().__call__(*_A       ,  **_A							)

			def   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : Union[str, Any]							):






									'''simple docstring'''



									__lowerCamelCase     :      List[str]       						=						load_image(_A							)
									__lowerCamelCase     :      Any       						=						torch.IntTensor([[image.height, image.width]]							)
									__lowerCamelCase     :      Optional[int]       						=						self.image_processor(images=[image]       ,  return_tensors="""pt"""							)
									if self.tokenizer is not None:
															__lowerCamelCase     :      str       						=						self.tokenizer(text=inputs["""words"""]       ,  boxes=inputs["""boxes"""]       ,  return_tensors="""pt"""							)
									__lowerCamelCase     :      Any       						=						target_size
									return inputs

			def   _snake_case    (  self  : Dict       ,  _lowerCamelCase  : List[Any]							):






									'''simple docstring'''



									__lowerCamelCase     :      Any       						=						model_inputs.pop("""target_size"""							)
									__lowerCamelCase     :      Tuple       						=						self.model(**_A							)
									__lowerCamelCase     :      Optional[Any]       						=						outputs.__class__({"""target_size""": target_size, **outputs}							)
									if self.tokenizer is not None:
															__lowerCamelCase     :      List[str]       						=						model_inputs['bbox']
									return model_outputs

			def   _snake_case    (  self  : List[str]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : int=0.9							):






									'''simple docstring'''



									__lowerCamelCase     :      str       						=						model_outputs['target_size']
									if self.tokenizer is not None:
															# This is a LayoutLMForTokenClassification variant.
															# The OCR got the boxes and the model classified the words.
															__lowerCamelCase     :      List[Any]       						=						target_size[0].tolist()

															def unnormalize(_lowerCamelCase  : Dict							):
																					return self._get_bounding_box(
																					    torch.Tensor(
																					        [
																					            (width * bbox[0] / 1_0_0_0),
																					            (height * bbox[1] / 1_0_0_0),
																					            (width * bbox[2] / 1_0_0_0),
																					            (height * bbox[3] / 1_0_0_0),
																					        ]							)							)

															__lowerCamelCase     :      Union[str, Any]       						=						model_outputs['logits'].squeeze(0							).softmax(dim=-1							).max(dim=-1							)
															__lowerCamelCase     :      List[str]       						=						[self.model.config.idalabel[prediction] for prediction in classes.tolist()]
															__lowerCamelCase     :      Optional[Any]       						=						[unnormalize(_A							) for bbox in model_outputs['bbox'].squeeze(0							)]
															__lowerCamelCase     :      Union[str, Any]       						=						['score', 'label', 'box']
															__lowerCamelCase     :      str       						=						[dict(zip(_A       ,  _A							)							) for vals in zip(scores.tolist()       ,  _A       ,  _A							) if vals[0] > threshold]
									else:
															# This is a regular ForObjectDetectionModel
															__lowerCamelCase     :      List[Any]       						=						self.image_processor.post_process_object_detection(_A       ,  _A       ,  _A							)
															__lowerCamelCase     :      Tuple       						=						raw_annotations[0]
															__lowerCamelCase     :      int       						=						raw_annotation['scores']
															__lowerCamelCase     :      Optional[int]       						=						raw_annotation['labels']
															__lowerCamelCase     :      Union[str, Any]       						=						raw_annotation['boxes']

															__lowerCamelCase     :      Optional[int]       						=						scores.tolist()
															__lowerCamelCase     :      str       						=						[self.model.config.idalabel[label.item()] for label in labels]
															__lowerCamelCase     :      str       						=						[self._get_bounding_box(_A							) for box in boxes]

															# {"scores": [...], ...} --> [{"score":x, ...}, ...]
															__lowerCamelCase     :      Tuple       						=						['score', 'label', 'box']
															__lowerCamelCase     :      str       						=						[
															    dict(zip(_A       ,  _A							)							)
															    for vals in zip(raw_annotation["""scores"""]       ,  raw_annotation["""labels"""]       ,  raw_annotation["""boxes"""]							)
															]

									return annotation


			def   _snake_case    (  self  : Any       ,  _lowerCamelCase  : Optional[int]							):






									'''simple docstring'''



									if self.framework != "pt":
															raise ValueError("""The ObjectDetectionPipeline is only available in PyTorch."""							)
									__lowerCamelCase     :      Union[str, Any]       						=						box.int().tolist()
									__lowerCamelCase     :      Union[str, Any]       						=						{
									    'xmin': xmin,
									    'ymin': ymin,
									    'xmax': xmax,
									    'ymax': ymax,
									}
									return bbox







import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
    import torch

if is_tf_available():
    import tensorflow as tf



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      int       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Tuple       ,  **_lowerCamelCase  : Any							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      Optional[int]       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Optional[Any]       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Optional[Any]       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[str]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      List[Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop original_sizes as it is popped in the processor

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


   @require_torch
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						self.get_image_processor()

         __lowerCamelCase     :      Union[str, Any]       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						[torch.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Any       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Any       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Tuple       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  torch.tensor(_lowerCamelCase							)       ,  torch.tensor(_lowerCamelCase							)							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Any       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      List[str]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(_lowerCamelCase							):
               __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)



@require_vision
@require_tf
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      int       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]       ,  **_lowerCamelCase  : List[Any]							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Dict       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Tuple       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      str       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[Any]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      Union[str, Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop reshaped_input_sizes as it is popped in the processor

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


   @require_tf
   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      str       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						[tf.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Optional[Any]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      List[str]       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Optional[int]       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  return_tensors="""tf"""       ,  )
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Tuple       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      str       						=						processor.post_process_masks(
             _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError							):
               __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
                   _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Optional[int]       ,  **_lowerCamelCase  : Tuple							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   @is_pt_tf_cross_test
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						np.random.randint(0       ,  2       ,  size=(1, 3, 5, 5)							).astype(np.floataa							)
         __lowerCamelCase     :      Tuple       						=						[tf.convert_to_tensor(_lowerCamelCase							)]
         __lowerCamelCase     :      List[str]       						=						[torch.tensor(_lowerCamelCase							)]

         __lowerCamelCase     :      Optional[int]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Tuple       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         __lowerCamelCase     :      Any       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""pt"""							)

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)


   @is_pt_tf_cross_test
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor()

         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Any       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      Any       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      Any       						=						processor(images=_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()

         __lowerCamelCase     :      Dict       						=						image_processor(_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      int       						=						processor(images=_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()

         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)



