


'''simple docstring'''







import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
   from transformers import WhisperFeatureExtractor

if is_torch_available():
   import torch

A__:    Optional[int]					    =       random.Random()


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	Dict    ,_UpperCAmelCase	:	List[Any]=1.0    ,_UpperCAmelCase	:	List[str]=None    ,_UpperCAmelCase	:	List[str]=None  )  ->			Optional[int]:
       if rng is None:
              _a    : Optional[int]     					=global_rng

       _a    : str     					=[]
       for batch_idx in range(shape[0]  ):
              values.append([]  )
              for _ in range(shape[1]  ):
                     values[-1].append(rng.random() * scale  )

       return values







@require_torch
@require_torchaudio
class A__       (						unittest.TestCase      ):






      def __init__( self			:str     ,						SCREAMING_SNAKE_CASE			:List[str]     ,						SCREAMING_SNAKE_CASE			:Tuple=7     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]=4_0_0     ,						SCREAMING_SNAKE_CASE			:str=2_0_0_0     ,						SCREAMING_SNAKE_CASE			:str=1_0     ,						SCREAMING_SNAKE_CASE			:Tuple=1_6_0     ,						SCREAMING_SNAKE_CASE			:List[str]=8     ,						SCREAMING_SNAKE_CASE			:Optional[int]=0.0     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]=4_0_0_0     ,						SCREAMING_SNAKE_CASE			:Optional[int]=False     ,						SCREAMING_SNAKE_CASE			:List[str]=True     ,						)       ->					int:




             '''simple docstring'''
             _a    : List[Any]     					=parent
             _a    : Optional[Any]     					=batch_size
             _a    : int     					=min_seq_length
             _a    : Tuple     					=max_seq_length
             _a    : List[str]     					=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
             _a    : List[Any]     					=padding_value
             _a    : int     					=sampling_rate
             _a    : List[str]     					=return_attention_mask
             _a    : Union[str, Any]     					=do_normalize
             _a    : Tuple     					=feature_size
             _a    : str     					=chunk_length
             _a    : Any     					=hop_length






      def __UpperCAmelCase       ( self			:Dict	)       ->					Dict:




             '''simple docstring'''
             return {
                 "feature_size": self.feature_size,
                 "hop_length": self.hop_length,
                 "chunk_length": self.chunk_length,
                 "padding_value": self.padding_value,
                 "sampling_rate": self.sampling_rate,
                 "return_attention_mask": self.return_attention_mask,
                 "do_normalize": self.do_normalize,
             }






      def __UpperCAmelCase       ( self			:Any     ,						SCREAMING_SNAKE_CASE			:List[str]=False     ,						SCREAMING_SNAKE_CASE			:Any=False	)       ->					List[Any]:




             '''simple docstring'''
             def _flatten(SCREAMING_SNAKE_CASE			:Optional[int]	):
                    return list(itertools.chain(*SCREAMING_SNAKE_CASE	)	)

             if equal_length:
                    _a    : Dict     					=[floats_list((self.max_seq_length, self.feature_size)	) for _ in range(self.batch_size	)]
             else:
                    # make sure that inputs increase in size
                    _a    : Optional[Any]     					=[
                        floats_list((x, self.feature_size)	)
                        for x in range(self.min_seq_length     ,						self.max_seq_length     ,						self.seq_length_diff	)
                    ]
             if numpify:
                    _a    : str     					=[np.asarray(SCREAMING_SNAKE_CASE	) for x in speech_inputs]
             return speech_inputs







@require_torch
@require_torchaudio
class A__       (						UpperCAmelCase__ ,     unittest.TestCase      ):
      __UpperCamelCase       :  Union[str, Any]	       =							WhisperFeatureExtractor if is_speech_available() else None






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




             '''simple docstring'''
             _a    : Optional[int]     					=WhisperFeatureExtractionTester(self	)






      def __UpperCAmelCase       ( self			:Optional[Any]	)       ->					Dict:




             '''simple docstring'''
             _a    : int     					=self.feature_extraction_class(**self.feat_extract_dict	)

             with tempfile.TemporaryDirectory() as tmpdirname:
                    _a    : List[Any]     					=feat_extract_first.save_pretrained(SCREAMING_SNAKE_CASE	)[0]
                    check_json_file_has_correct_format(SCREAMING_SNAKE_CASE	)
                    _a    : Optional[Any]     					=self.feature_extraction_class.from_pretrained(SCREAMING_SNAKE_CASE	)

             _a    : Optional[Any]     					=feat_extract_first.to_dict()
             _a    : Optional[int]     					=feat_extract_second.to_dict()
             _a    : str     					=feat_extract_first.mel_filters
             _a    : List[str]     					=feat_extract_second.mel_filters
             self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)	)
             self.assertEqual(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Dict	)       ->					Optional[int]:




             '''simple docstring'''
             _a    : Any     					=self.feature_extraction_class(**self.feat_extract_dict	)

             with tempfile.TemporaryDirectory() as tmpdirname:
                    _a    : List[Any]     					=os.path.join(SCREAMING_SNAKE_CASE     ,						"""feat_extract.json"""	)
                    feat_extract_first.to_json_file(SCREAMING_SNAKE_CASE	)
                    _a    : Dict     					=self.feature_extraction_class.from_json_file(SCREAMING_SNAKE_CASE	)

             _a    : int     					=feat_extract_first.to_dict()
             _a    : Any     					=feat_extract_second.to_dict()
             _a    : List[str]     					=feat_extract_first.mel_filters
             _a    : List[Any]     					=feat_extract_second.mel_filters
             self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)	)
             self.assertEqual(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)






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




             '''simple docstring'''
             # Tests that all call wrap to encode_plus and batch_encode_plus
             _a    : str     					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
             # create three inputs of length 800, 1000, and 1200
             _a    : List[str]     					=[floats_list((1, x)	)[0] for x in range(8_0_0     ,						1_4_0_0     ,						2_0_0	)]
             _a    : str     					=[np.asarray(SCREAMING_SNAKE_CASE	) for speech_input in speech_inputs]

             # Test feature size
             _a    : Any     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						padding="""max_length"""     ,						return_tensors="""np"""	).input_features
             self.assertTrue(input_features.ndim == 3	)
             self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames	)
             self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size	)

             # Test not batched input
             _a    : Dict     					=feature_extractor(speech_inputs[0]     ,						return_tensors="""np"""	).input_features
             _a    : Optional[Any]     					=feature_extractor(np_speech_inputs[0]     ,						return_tensors="""np"""	).input_features
             self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						atol=1e-3	)	)

             # Test batched
             _a    : Optional[int]     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						return_tensors="""np"""	).input_features
             _a    : Optional[int]     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						return_tensors="""np"""	).input_features
             for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	):
                    self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						atol=1e-3	)	)

             # Test 2-D numpy arrays are batched.
             _a    : int     					=[floats_list((1, x)	)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
             _a    : List[str]     					=np.asarray(SCREAMING_SNAKE_CASE	)
             _a    : Optional[Any]     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						return_tensors="""np"""	).input_features
             _a    : Optional[Any]     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						return_tensors="""np"""	).input_features
             for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	):
                    self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						atol=1e-3	)	)

             # Test truncation required
             _a    : Dict     					=[floats_list((1, x)	)[0] for x in range(2_0_0     ,						(feature_extractor.n_samples + 5_0_0)     ,						2_0_0	)]
             _a    : Dict     					=[np.asarray(SCREAMING_SNAKE_CASE	) for speech_input in speech_inputs]

             _a    : Union[str, Any]     					=[x[: feature_extractor.n_samples] for x in speech_inputs]
             _a    : Dict     					=[np.asarray(SCREAMING_SNAKE_CASE	) for speech_input in speech_inputs_truncated]

             _a    : Dict     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						return_tensors="""np"""	).input_features
             _a    : Optional[int]     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						return_tensors="""np"""	).input_features
             for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	):
                    self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						atol=1e-3	)	)






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




             '''simple docstring'''
             import torch

             _a    : List[Any]     					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
             _a    : int     					=np.random.rand(1_0_0     ,						3_2	).astype(np.floataa	)
             _a    : Optional[int]     					=np_speech_inputs.tolist()

             for inputs in [py_speech_inputs, np_speech_inputs]:
                    _a    : str     					=feature_extractor.pad([{"""input_features""": inputs}]     ,						return_tensors="""np"""	)
                    self.assertTrue(np_processed.input_features.dtype == np.floataa	)
                    _a    : Union[str, Any]     					=feature_extractor.pad([{"""input_features""": inputs}]     ,						return_tensors="""pt"""	)
                    self.assertTrue(pt_processed.input_features.dtype == torch.floataa	)






      def __UpperCAmelCase       ( self			:List[Any]     ,						SCREAMING_SNAKE_CASE			:Tuple	)       ->					Union[str, Any]:




             '''simple docstring'''
             _a    : Optional[Any]     					=load_dataset("""hf-internal-testing/librispeech_asr_dummy"""     ,						"""clean"""     ,						split="""validation"""	)
             # automatic decoding with librispeech
             _a    : Optional[Any]     					=ds.sort("""id"""	).select(range(SCREAMING_SNAKE_CASE	)	)[:num_samples]["""audio"""]

             return [x["array"] for x in speech_samples]






      def __UpperCAmelCase       ( self			:str	)       ->					Optional[int]:




             '''simple docstring'''
             # fmt: off
             _a    : Any     					=torch.tensor(
                 [
                     0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
                     0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
                     0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
                     -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
                 ]	)
             # fmt: on

             _a    : Any     					=self._load_datasamples(1	)
             _a    : int     					=WhisperFeatureExtractor()
             _a    : str     					=feature_extractor(SCREAMING_SNAKE_CASE     ,						return_tensors="""pt"""	).input_features
             self.assertEqual(input_features.shape     ,						(1, 8_0, 3_0_0_0)	)
             self.assertTrue(torch.allclose(input_features[0, 0, :3_0]     ,						SCREAMING_SNAKE_CASE     ,						atol=1e-4	)	)






      def __UpperCAmelCase       ( self			:Any	)       ->					Any:




             '''simple docstring'''
             _a    : Dict     					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()	)
             _a    : str     					=self._load_datasamples(1	)[0]
             _a    : List[Any]     					=((audio - audio.min()) / (audio.max() - audio.min())) * 6_5_5_3_5  # Rescale to [0, 65535] to show issue
             _a    : Union[str, Any]     					=feat_extract.zero_mean_unit_var_norm([audio]     ,						attention_mask=SCREAMING_SNAKE_CASE	)[0]

             self.assertTrue(np.all(np.mean(SCREAMING_SNAKE_CASE	) < 1e-3	)	)
             self.assertTrue(np.all(np.abs(np.var(SCREAMING_SNAKE_CASE	) - 1	) < 1e-3	)	)






'''simple docstring'''







from typing import Dict, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends


if is_vision_available():
   import PIL

# soft dependency
if is_pytesseract_available():
   import pytesseract

A__:    Union[str, Any]					    =       logging.get_logger(__name__)


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	str    ,_UpperCAmelCase	:	Tuple    ,_UpperCAmelCase	:	List[str]  )  ->			int:
       return [
           int(1000 * (box[0] / width)  ),
           int(1000 * (box[1] / height)  ),
           int(1000 * (box[2] / width)  ),
           int(1000 * (box[3] / height)  ),
       ]


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	np.ndarray    ,_UpperCAmelCase	:	Optional[str]    ,_UpperCAmelCase	:	Optional[str] = None  )  ->			Optional[int]:
       _a    : Any     					=tesseract_config if tesseract_config is not None else """"""

       # apply OCR
       _a    : Optional[Any]     					=to_pil_image(_UpperCAmelCase  )
       _a					,    _a    : List[Any]     					=pil_image.size
       _a    : List[str]     					=pytesseract.image_to_data(_UpperCAmelCase    ,lang=_UpperCAmelCase    ,output_type="""dict"""    ,config=_UpperCAmelCase  )
       _a					,    _a					,    _a					,    _a					,    _a    : str     					=data["""text"""], data["""left"""], data["""top"""], data["""width"""], data["""height"""]

       # filter empty words and corresponding coordinates
       _a    : Tuple     					=[idx for idx, word in enumerate(_UpperCAmelCase  ) if not word.strip()]
       _a    : List[Any]     					=[word for idx, word in enumerate(_UpperCAmelCase  ) if idx not in irrelevant_indices]
       _a    : Dict     					=[coord for idx, coord in enumerate(_UpperCAmelCase  ) if idx not in irrelevant_indices]
       _a    : List[str]     					=[coord for idx, coord in enumerate(_UpperCAmelCase  ) if idx not in irrelevant_indices]
       _a    : Union[str, Any]     					=[coord for idx, coord in enumerate(_UpperCAmelCase  ) if idx not in irrelevant_indices]
       _a    : Union[str, Any]     					=[coord for idx, coord in enumerate(_UpperCAmelCase  ) if idx not in irrelevant_indices]

       # turn coordinates into (left, top, left+width, top+height) format
       _a    : List[str]     					=[]
       for x, y, w, h in zip(_UpperCAmelCase    ,_UpperCAmelCase    ,_UpperCAmelCase    ,_UpperCAmelCase  ):
              _a    : int     					=[x, y, x + w, y + h]
              actual_boxes.append(_UpperCAmelCase  )

       # finally, normalize the bounding boxes
       _a    : str     					=[]
       for box in actual_boxes:
              normalized_boxes.append(normalize_box(_UpperCAmelCase    ,_UpperCAmelCase    ,_UpperCAmelCase  )  )

       assert len(_UpperCAmelCase  ) == len(_UpperCAmelCase  ), "Not as many words as there are bounding boxes"

       return words, normalized_boxes







class A__       (						UpperCAmelCase__      ):
      __UpperCamelCase       :  List[Any]	       =							["pixel_values"]






      def __init__( self			:Tuple     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						SCREAMING_SNAKE_CASE			:Dict[str, int] = None     ,						SCREAMING_SNAKE_CASE			:PILImageResampling = PILImageResampling.BILINEAR     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						SCREAMING_SNAKE_CASE			:Optional[str] = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = ""     ,						**SCREAMING_SNAKE_CASE			:Tuple     ,						)       ->					None:




             '''simple docstring'''
             super().__init__(**SCREAMING_SNAKE_CASE	)
             _a    : List[Any]     					=size if size is not None else {"""height""": 2_2_4, """width""": 2_2_4}
             _a    : Tuple     					=get_size_dict(SCREAMING_SNAKE_CASE	)

             _a    : Dict     					=do_resize
             _a    : Tuple     					=size
             _a    : str     					=resample
             _a    : Dict     					=apply_ocr
             _a    : Union[str, Any]     					=ocr_lang
             _a    : Dict     					=tesseract_config






      def __UpperCAmelCase       ( self			:List[str]     ,						SCREAMING_SNAKE_CASE			:np.ndarray     ,						SCREAMING_SNAKE_CASE			:Dict[str, int]     ,						SCREAMING_SNAKE_CASE			:PILImageResampling = PILImageResampling.BILINEAR     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, ChannelDimension]] = None     ,						**SCREAMING_SNAKE_CASE			:Dict     ,						)       ->					np.ndarray:




             '''simple docstring'''
             _a    : int     					=get_size_dict(SCREAMING_SNAKE_CASE	)
             if "height" not in size or "width" not in size:
                    raise ValueError(f"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}"	)
             _a    : Any     					=(size["""height"""], size["""width"""])
             return resize(SCREAMING_SNAKE_CASE     ,						size=SCREAMING_SNAKE_CASE     ,						resample=SCREAMING_SNAKE_CASE     ,						data_format=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Dict     ,						SCREAMING_SNAKE_CASE			:ImageInput     ,						SCREAMING_SNAKE_CASE			:bool = None     ,						SCREAMING_SNAKE_CASE			:Dict[str, int] = None     ,						SCREAMING_SNAKE_CASE			:PILImageResampling = None     ,						SCREAMING_SNAKE_CASE			:bool = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = None     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, TensorType]] = None     ,						SCREAMING_SNAKE_CASE			:ChannelDimension = ChannelDimension.FIRST     ,						**SCREAMING_SNAKE_CASE			:Optional[Any]     ,						)       ->					PIL.Image.Image:




             '''simple docstring'''
             _a    : Optional[int]     					=do_resize if do_resize is not None else self.do_resize
             _a    : Optional[int]     					=size if size is not None else self.size
             _a    : str     					=get_size_dict(SCREAMING_SNAKE_CASE	)
             _a    : List[str]     					=resample if resample is not None else self.resample
             _a    : int     					=apply_ocr if apply_ocr is not None else self.apply_ocr
             _a    : str     					=ocr_lang if ocr_lang is not None else self.ocr_lang
             _a    : Union[str, Any]     					=tesseract_config if tesseract_config is not None else self.tesseract_config

             _a    : List[str]     					=make_list_of_images(SCREAMING_SNAKE_CASE	)

             if not valid_images(SCREAMING_SNAKE_CASE	):
                    raise ValueError(
                        """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                        """torch.Tensor, tf.Tensor or jax.ndarray."""	)

             if do_resize and size is None:
                    raise ValueError("""Size must be specified if do_resize is True."""	)

             # All transformations expect numpy arrays.
             _a    : List[Any]     					=[to_numpy_array(SCREAMING_SNAKE_CASE	) for image in images]

             if apply_ocr:
                    requires_backends(self     ,						"""pytesseract"""	)
                    _a    : Any     					=[]
                    _a    : Any     					=[]
                    for image in images:
                           _a					,    _a    : int     					=apply_tesseract(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)
                           words_batch.append(SCREAMING_SNAKE_CASE	)
                           boxes_batch.append(SCREAMING_SNAKE_CASE	)

             if do_resize:
                    _a    : Union[str, Any]     					=[self.resize(image=SCREAMING_SNAKE_CASE     ,						size=SCREAMING_SNAKE_CASE     ,						resample=SCREAMING_SNAKE_CASE	) for image in images]

             # flip color channels from RGB to BGR (as Detectron2 requires this)
             _a    : Dict     					=[flip_channel_order(SCREAMING_SNAKE_CASE	) for image in images]
             _a    : str     					=[to_channel_dimension_format(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	) for image in images]

             _a    : str     					=BatchFeature(data={"""pixel_values""": images}     ,						tensor_type=SCREAMING_SNAKE_CASE	)

             if apply_ocr:
                    _a    : List[Any]     					=words_batch
                    _a    : Dict     					=boxes_batch
             return data


