






'''simple docstring'''



import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import YolosImageProcessor
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      def __init__( self			:		Optional[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Optional[Any]=7     ,      UpperCAmelCase			:		Optional[Any]=3     ,      UpperCAmelCase			:		Optional[int]=30     ,      UpperCAmelCase			:		Any=400     ,      UpperCAmelCase			:		str=True     ,      UpperCAmelCase			:		Optional[Any]=None     ,      UpperCAmelCase			:		Dict=True     ,      UpperCAmelCase			:		List[Any]=[0.5, 0.5, 0.5]     ,      UpperCAmelCase			:		Any=[0.5, 0.5, 0.5]     ,      UpperCAmelCase			:		str=True     ,      UpperCAmelCase			:		Optional[Any]=1 / 255     ,      UpperCAmelCase			:		int=True     ,      )  ->  str:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333}
       lowercase					:   List[str]				  =parent
       lowercase					:   Union[str, Any]				  =batch_size
       lowercase					:   List[Any]				  =num_channels
       lowercase					:   List[str]				  =min_resolution
       lowercase					:   Union[str, Any]				  =max_resolution
       lowercase					:   Union[str, Any]				  =do_resize
       lowercase					:   Optional[Any]				  =size
       lowercase					:   List[Any]				  =do_normalize
       lowercase					:   Tuple				  =image_mean
       lowercase					:   Any				  =image_std
       lowercase					:   Any				  =do_rescale
       lowercase					:   str				  =rescale_factor
       lowercase					:   Union[str, Any]				  =do_pad






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


       '''simple docstring'''

       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_rescale": self.do_rescale,
           "rescale_factor": self.rescale_factor,
           "do_pad": self.do_pad,
       }






      def 				A__      ( self			:		Dict     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		Tuple=False		)  ->  Tuple:


       '''simple docstring'''

       if not batched:
        lowercase					:   str				  =image_inputs[0]
        if isinstance(UpperCAmelCase     ,      Image.Image		):
         lowercase  ,		lowercase					:   Dict				  =image.size
        else:
         lowercase  ,		lowercase					:   int				  =image.shape[1], image.shape[2]
        if w < h:
         lowercase					:   List[Any]				  =int(self.size['''shortest_edge'''] * h / w		)
         lowercase					:   List[Any]				  =self.size['''shortest_edge''']
        elif w > h:
         lowercase					:   str				  =self.size['''shortest_edge''']
         lowercase					:   str				  =int(self.size['''shortest_edge'''] * w / h		)
        else:
         lowercase					:   Union[str, Any]				  =self.size['''shortest_edge''']
         lowercase					:   List[str]				  =self.size['''shortest_edge''']

       else:
        lowercase					:   Optional[Any]				  =[]
        for image in image_inputs:
         lowercase  ,		lowercase					:   int				  =self.get_expected_values([image]		)
         expected_values.append((expected_height, expected_width)		)
        lowercase					:   Union[str, Any]				  =max(UpperCAmelCase     ,      key=lambda UpperCAmelCase		: item[0]		)[0]
        lowercase					:   Dict				  =max(UpperCAmelCase     ,      key=lambda UpperCAmelCase		: item[1]		)[1]

       return expected_height, expected_width





@require_torch
@require_vision
class UpperCAmelCase_					(    __A				,       unittest.TestCase				):




      """simple docstring"""




      UpperCamelCase_								=			YolosImageProcessor if is_vision_available() else None






      def 				A__      ( self			:		Optional[Any]		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   Dict				  =YolosImageProcessingTester(self		)






      @property
      def 				A__      ( self			:		List[str]		)  ->  Optional[Any]:


       '''simple docstring'''

       return self.image_processor_tester.prepare_image_processor_dict()






      def 				A__      ( self			:		List[Any]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   str				  =self.image_processing_class(**self.image_processor_dict		)
       self.assertTrue(hasattr(UpperCAmelCase     ,      '''image_mean'''		)		)
       self.assertTrue(hasattr(UpperCAmelCase     ,      '''image_std'''		)		)
       self.assertTrue(hasattr(UpperCAmelCase     ,      '''do_normalize'''		)		)
       self.assertTrue(hasattr(UpperCAmelCase     ,      '''do_resize'''		)		)
       self.assertTrue(hasattr(UpperCAmelCase     ,      '''size'''		)		)






      def 				A__      ( self			:		Tuple		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   int				  =self.image_processing_class.from_dict(self.image_processor_dict		)
       self.assertEqual(image_processor.size     ,      {'''shortest_edge''': 18, '''longest_edge''': 1333}		)
       self.assertEqual(image_processor.do_pad     ,      UpperCAmelCase		)

       lowercase					:   List[Any]				  =self.image_processing_class.from_dict(
           self.image_processor_dict     ,      size=42     ,      max_size=84     ,      pad_and_return_pixel_mask=UpperCAmelCase		)
       self.assertEqual(image_processor.size     ,      {'''shortest_edge''': 42, '''longest_edge''': 84}		)
       self.assertEqual(image_processor.do_pad     ,      UpperCAmelCase		)






      def 				A__      ( self			:		List[Any]		)  ->  List[str]:


       '''simple docstring'''

       pass






      def 				A__      ( self			:		Tuple		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =self.image_processing_class(**self.image_processor_dict		)
       # create random PIL images
       lowercase					:   Tuple				  =prepare_image_inputs(self.image_processor_tester     ,      equal_resolution=UpperCAmelCase		)
       for image in image_inputs:
        self.assertIsInstance(UpperCAmelCase     ,      Image.Image		)

       # Test not batched input
       lowercase					:   int				  =image_processing(image_inputs[0]     ,      return_tensors='''pt'''		).pixel_values

       lowercase  ,		lowercase					:   Union[str, Any]				  =self.image_processor_tester.get_expected_values(UpperCAmelCase		)

       self.assertEqual(
           encoded_images.shape     ,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,      )

       # Test batched
       lowercase  ,		lowercase					:   Any				  =self.image_processor_tester.get_expected_values(UpperCAmelCase     ,      batched=UpperCAmelCase		)

       lowercase					:   int				  =image_processing(UpperCAmelCase     ,      return_tensors='''pt'''		).pixel_values
       self.assertEqual(
           encoded_images.shape     ,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )     ,      )






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


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =self.image_processing_class(**self.image_processor_dict		)
       # create random numpy tensors
       lowercase					:   int				  =prepare_image_inputs(self.image_processor_tester     ,      equal_resolution=UpperCAmelCase     ,      numpify=UpperCAmelCase		)
       for image in image_inputs:
        self.assertIsInstance(UpperCAmelCase     ,      np.ndarray		)

       # Test not batched input
       lowercase					:   int				  =image_processing(image_inputs[0]     ,      return_tensors='''pt'''		).pixel_values

       lowercase  ,		lowercase					:   str				  =self.image_processor_tester.get_expected_values(UpperCAmelCase		)

       self.assertEqual(
           encoded_images.shape     ,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,      )

       # Test batched
       lowercase					:   int				  =image_processing(UpperCAmelCase     ,      return_tensors='''pt'''		).pixel_values

       lowercase  ,		lowercase					:   int				  =self.image_processor_tester.get_expected_values(UpperCAmelCase     ,      batched=UpperCAmelCase		)

       self.assertEqual(
           encoded_images.shape     ,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )     ,      )






      def 				A__      ( self			:		Any		)  ->  int:


       '''simple docstring'''

       lowercase					:   Any				  =self.image_processing_class(**self.image_processor_dict		)
       # create random PyTorch tensors
       lowercase					:   str				  =prepare_image_inputs(self.image_processor_tester     ,      equal_resolution=UpperCAmelCase     ,      torchify=UpperCAmelCase		)
       for image in image_inputs:
        self.assertIsInstance(UpperCAmelCase     ,      torch.Tensor		)

       # Test not batched input
       lowercase					:   List[Any]				  =image_processing(image_inputs[0]     ,      return_tensors='''pt'''		).pixel_values

       lowercase  ,		lowercase					:   Optional[int]				  =self.image_processor_tester.get_expected_values(UpperCAmelCase		)

       self.assertEqual(
           encoded_images.shape     ,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,      )

       # Test batched
       lowercase					:   List[Any]				  =image_processing(UpperCAmelCase     ,      return_tensors='''pt'''		).pixel_values

       lowercase  ,		lowercase					:   Union[str, Any]				  =self.image_processor_tester.get_expected_values(UpperCAmelCase     ,      batched=UpperCAmelCase		)

       self.assertEqual(
           encoded_images.shape     ,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )     ,      )






      def 				A__      ( self			:		Optional[Any]		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =self.image_processing_class(**self.image_processor_dict		)
       lowercase					:   Tuple				  =self.image_processing_class(do_resize=UpperCAmelCase     ,      do_normalize=UpperCAmelCase     ,      do_rescale=UpperCAmelCase		)
       # create random PyTorch tensors
       lowercase					:   str				  =prepare_image_inputs(self.image_processor_tester     ,      equal_resolution=UpperCAmelCase     ,      torchify=UpperCAmelCase		)
       for image in image_inputs:
        self.assertIsInstance(UpperCAmelCase     ,      torch.Tensor		)

       # Test whether the method "pad" and calling the image processor return the same tensors
       lowercase					:   Any				  =image_processing_a.pad(UpperCAmelCase     ,      return_tensors='''pt'''		)
       lowercase					:   Any				  =image_processing_a(UpperCAmelCase     ,      return_tensors='''pt'''		)

       self.assertTrue(
           torch.allclose(encoded_images_with_method['''pixel_values''']     ,      encoded_images['''pixel_values''']     ,      atol=1e-4		)		)






      @slow
      def 				A__      ( self			:		Dict		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt'''     ,      '''r'''		) as f:
        lowercase					:   Union[str, Any]				  =json.loads(f.read()		)

       lowercase					:   List[Any]				  ={'''image_id''': 3_9769, '''annotations''': target}

       # encode them
       lowercase					:   Any				  =YolosImageProcessor.from_pretrained('''hustvl/yolos-small'''		)
       lowercase					:   Optional[int]				  =image_processing(images=UpperCAmelCase     ,      annotations=UpperCAmelCase     ,      return_tensors='''pt'''		)

       # verify pixel values
       lowercase					:   Optional[int]				  =torch.Size([1, 3, 800, 1066]		)
       self.assertEqual(encoding['''pixel_values'''].shape     ,      UpperCAmelCase		)

       lowercase					:   Union[str, Any]				  =torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]		)
       self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]     ,      UpperCAmelCase     ,      atol=1e-4		)		)

       # verify area
       lowercase					:   Optional[Any]				  =torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']     ,      UpperCAmelCase		)		)
       # verify boxes
       lowercase					:   Any				  =torch.Size([6, 4]		)
       self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape     ,      UpperCAmelCase		)
       lowercase					:   Optional[Any]				  =torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]     ,      UpperCAmelCase     ,      atol=1e-3		)		)
       # verify image_id
       lowercase					:   str				  =torch.tensor([3_9769]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']     ,      UpperCAmelCase		)		)
       # verify is_crowd
       lowercase					:   Dict				  =torch.tensor([0, 0, 0, 0, 0, 0]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']     ,      UpperCAmelCase		)		)
       # verify class_labels
       lowercase					:   Optional[int]				  =torch.tensor([75, 75, 63, 65, 17, 17]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']     ,      UpperCAmelCase		)		)
       # verify orig_size
       lowercase					:   str				  =torch.tensor([480, 640]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']     ,      UpperCAmelCase		)		)
       # verify size
       lowercase					:   Optional[Any]				  =torch.tensor([800, 1066]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']     ,      UpperCAmelCase		)		)






      @slow
      def 				A__      ( self			:		Optional[int]		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   int				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'''     ,      '''r'''		) as f:
        lowercase					:   Dict				  =json.loads(f.read()		)

       lowercase					:   Any				  ={'''file_name''': '''000000039769.png''', '''image_id''': 3_9769, '''segments_info''': target}

       lowercase					:   Optional[Any]				  =pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''		)

       # encode them
       lowercase					:   Optional[Any]				  =YolosImageProcessor(format='''coco_panoptic'''		)
       lowercase					:   Optional[int]				  =image_processing(images=UpperCAmelCase     ,      annotations=UpperCAmelCase     ,      masks_path=UpperCAmelCase     ,      return_tensors='''pt'''		)

       # verify pixel values
       lowercase					:   Optional[int]				  =torch.Size([1, 3, 800, 1066]		)
       self.assertEqual(encoding['''pixel_values'''].shape     ,      UpperCAmelCase		)

       lowercase					:   Optional[int]				  =torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]		)
       self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]     ,      UpperCAmelCase     ,      atol=1e-4		)		)

       # verify area
       lowercase					:   Dict				  =torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']     ,      UpperCAmelCase		)		)
       # verify boxes
       lowercase					:   Optional[int]				  =torch.Size([6, 4]		)
       self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape     ,      UpperCAmelCase		)
       lowercase					:   Tuple				  =torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]     ,      UpperCAmelCase     ,      atol=1e-3		)		)
       # verify image_id
       lowercase					:   List[str]				  =torch.tensor([3_9769]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']     ,      UpperCAmelCase		)		)
       # verify is_crowd
       lowercase					:   Any				  =torch.tensor([0, 0, 0, 0, 0, 0]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']     ,      UpperCAmelCase		)		)
       # verify class_labels
       lowercase					:   Dict				  =torch.tensor([17, 17, 63, 75, 75, 93]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']     ,      UpperCAmelCase		)		)
       # verify masks
       lowercase					:   Optional[int]				  =82_2873
       self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()     ,      UpperCAmelCase		)
       # verify orig_size
       lowercase					:   List[Any]				  =torch.tensor([480, 640]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']     ,      UpperCAmelCase		)		)
       # verify size
       lowercase					:   Union[str, Any]				  =torch.tensor([800, 1066]		)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']     ,      UpperCAmelCase		)		)











'''simple docstring'''



import re


def 							lowercase_				(    __A :       str			)       ->      bool:



 """simple docstring"""




 lowercase					:   Any				  =re.compile(R'''^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$'''			)
 if match := re.search(__A					,      __A			):
  return match.string == phone
 return False


if __name__ == "__main__":
      print(indian_phone_validator('+918827897895'))



