

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   lowercase_   (  unittest.TestCase					):



      def 						UpperCamelCase_		(      self				:    int		)   ->					List[str]:
        _snake_case						=							tempfile.mkdtemp()
        _snake_case						=							SamImageProcessor()
        _snake_case						=							SamProcessor(A__		)
        processor.save_pretrained(self.tmpdirname		)



      def 						UpperCamelCase_		(      self				:    Tuple      , **A__				:    int		)   ->					List[Any]:
        return AutoProcessor.from_pretrained(self.tmpdirname      , **A__		).image_processor



      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					Any:
        shutil.rmtree(self.tmpdirname		)



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Any:
        _snake_case						=							[np.random.randint(255      , size=(3, 30, 400)      , dtype=np.uinta		)]

        _snake_case						=							[Image.fromarray(np.moveaxis(A__      , 0      , -1		)		) for x in image_inputs]

        return image_inputs



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					List[str]:
        _snake_case						=							SamProcessor(image_processor=self.get_image_processor()		)
        processor.save_pretrained(self.tmpdirname		)

        _snake_case						=							self.get_image_processor(do_normalize=A__      , padding_value=1.0		)

        _snake_case						=							SamProcessor.from_pretrained(self.tmpdirname      , do_normalize=A__      , padding_value=1.0		)

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



      def 						UpperCamelCase_		(      self				:    Any		)   ->					Union[str, Any]:
        _snake_case						=							self.get_image_processor()

        _snake_case						=							SamProcessor(image_processor=A__		)

        _snake_case						=							self.prepare_image_inputs()

        _snake_case						=							image_processor(A__      , return_tensors='''np'''		)
        _snake_case						=							processor(images=A__      , 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 						UpperCamelCase_		(      self				:    str		)   ->					Dict:
        _snake_case						=							self.get_image_processor()

        _snake_case						=							SamProcessor(image_processor=A__		)
        _snake_case						=							[torch.ones((1, 3, 5, 5)		)]

        _snake_case						=							[[1764, 2646]]

        _snake_case						=							[[683, 1024]]
        _snake_case						=							processor.post_process_masks(A__      , A__      , A__		)
        self.assertEqual(masks[0].shape      , (1, 3, 1764, 2646)		)

        _snake_case						=							processor.post_process_masks(
            A__      , torch.tensor(A__		)      , torch.tensor(A__		)		)
        self.assertEqual(masks[0].shape      , (1, 3, 1764, 2646)		)

        # should also work with np
        _snake_case						=							[np.ones((1, 3, 5, 5)		)]
        _snake_case						=							processor.post_process_masks(A__      , np.array(A__		)      , np.array(A__		)		)

        self.assertEqual(masks[0].shape      , (1, 3, 1764, 2646)		)

        _snake_case						=							[[1, 0], [0, 1]]
        with self.assertRaises(A__		):
          _snake_case						=							processor.post_process_masks(A__      , np.array(A__		)      , np.array(A__		)		)



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



      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					Dict:
        _snake_case						=							tempfile.mkdtemp()
        _snake_case						=							SamImageProcessor()
        _snake_case						=							SamProcessor(A__		)
        processor.save_pretrained(self.tmpdirname		)



      def 						UpperCamelCase_		(      self				:    Optional[int]      , **A__				:    Union[str, Any]		)   ->					Dict:
        return AutoProcessor.from_pretrained(self.tmpdirname      , **A__		).image_processor



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					List[str]:
        shutil.rmtree(self.tmpdirname		)



      def 						UpperCamelCase_		(      self				:    Any		)   ->					Any:
        _snake_case						=							[np.random.randint(255      , size=(3, 30, 400)      , dtype=np.uinta		)]

        _snake_case						=							[Image.fromarray(np.moveaxis(A__      , 0      , -1		)		) for x in image_inputs]

        return image_inputs



      def 						UpperCamelCase_		(      self				:    int		)   ->					Optional[Any]:
        _snake_case						=							SamProcessor(image_processor=self.get_image_processor()		)
        processor.save_pretrained(self.tmpdirname		)

        _snake_case						=							self.get_image_processor(do_normalize=A__      , padding_value=1.0		)

        _snake_case						=							SamProcessor.from_pretrained(self.tmpdirname      , do_normalize=A__      , padding_value=1.0		)

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



      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					List[str]:
        _snake_case						=							self.get_image_processor()

        _snake_case						=							SamProcessor(image_processor=A__		)

        _snake_case						=							self.prepare_image_inputs()

        _snake_case						=							image_processor(A__      , return_tensors='''np'''		)
        _snake_case						=							processor(images=A__      , 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 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					Union[str, Any]:
        _snake_case						=							self.get_image_processor()

        _snake_case						=							SamProcessor(image_processor=A__		)
        _snake_case						=							[tf.ones((1, 3, 5, 5)		)]

        _snake_case						=							[[1764, 2646]]

        _snake_case						=							[[683, 1024]]
        _snake_case						=							processor.post_process_masks(A__      , A__      , A__      , return_tensors='''tf'''		)
        self.assertEqual(masks[0].shape      , (1, 3, 1764, 2646)		)

        _snake_case						=							processor.post_process_masks(
            A__      , tf.convert_to_tensor(A__		)      , tf.convert_to_tensor(A__		)      , return_tensors='''tf'''      , )
        self.assertEqual(masks[0].shape      , (1, 3, 1764, 2646)		)

        # should also work with np
        _snake_case						=							[np.ones((1, 3, 5, 5)		)]
        _snake_case						=							processor.post_process_masks(
            A__      , np.array(A__		)      , np.array(A__		)      , return_tensors='''tf'''		)

        self.assertEqual(masks[0].shape      , (1, 3, 1764, 2646)		)

        _snake_case						=							[[1, 0], [0, 1]]
        with self.assertRaises(tf.errors.InvalidArgumentError		):
          _snake_case						=							processor.post_process_masks(
              A__      , np.array(A__		)      , np.array(A__		)      , return_tensors='''tf'''		)



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



      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					str:
        _snake_case						=							tempfile.mkdtemp()
        _snake_case						=							SamImageProcessor()
        _snake_case						=							SamProcessor(A__		)
        processor.save_pretrained(self.tmpdirname		)



      def 						UpperCamelCase_		(      self				:    Union[str, Any]      , **A__				:    int		)   ->					Union[str, Any]:
        return AutoProcessor.from_pretrained(self.tmpdirname      , **A__		).image_processor



      def 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					Any:
        shutil.rmtree(self.tmpdirname		)



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					str:
        _snake_case						=							[np.random.randint(255      , size=(3, 30, 400)      , dtype=np.uinta		)]

        _snake_case						=							[Image.fromarray(np.moveaxis(A__      , 0      , -1		)		) for x in image_inputs]

        return image_inputs



      @is_pt_tf_cross_test
      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					Tuple:
        _snake_case						=							self.get_image_processor()

        _snake_case						=							SamProcessor(image_processor=A__		)
        _snake_case						=							np.random.randint(0      , 2      , size=(1, 3, 5, 5)		).astype(np.floataa		)
        _snake_case						=							[tf.convert_to_tensor(A__		)]
        _snake_case						=							[torch.tensor(A__		)]

        _snake_case						=							[[1764, 2646]]

        _snake_case						=							[[683, 1024]]
        _snake_case						=							processor.post_process_masks(
            A__      , A__      , A__      , return_tensors='''tf'''		)
        _snake_case						=							processor.post_process_masks(
            A__      , A__      , A__      , return_tensors='''pt'''		)

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







      @is_pt_tf_cross_test
      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					Any:
        _snake_case						=							self.get_image_processor()

        _snake_case						=							SamProcessor(image_processor=A__		)

        _snake_case						=							self.prepare_image_inputs()

        _snake_case						=							image_processor(A__      , return_tensors='''pt'''		)['''pixel_values'''].numpy()
        _snake_case						=							processor(images=A__      , return_tensors='''pt'''		)['''pixel_values'''].numpy()

        _snake_case						=							image_processor(A__      , return_tensors='''tf'''		)['''pixel_values'''].numpy()
        _snake_case						=							processor(images=A__      , return_tensors='''tf'''		)['''pixel_values'''].numpy()

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




import warnings

from ...utils import logging
from .image_processing_deformable_detr import DeformableDetrImageProcessor


__A      		=     logging.get_logger(__name__)



class   lowercase_   (  __lowercase					):



      def __init__(      self				:    Optional[Any]      , *A__				:    List[Any]      , **A__				:    int		)   ->					None:
        warnings.warn(
            '''The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
            ''' Please use DeformableDetrImageProcessor instead.'''      , A__      , )
        super().__init__(*A__      , **A__		)

