

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

__UpperCAmelCase    				=     random.Random()



def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE=1.0		,							SCREAMING_SNAKE_CASE=None		,							SCREAMING_SNAKE_CASE=None				)       ->      Any:

     """simple docstring"""







     if rng is None:
          _lowerCAmelCase   :					str        =    global_rng

     _lowerCAmelCase   :					Optional[int]        =    []
     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 ):



 """simple docstring"""







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






      '''simple docstring'''




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

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






      '''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    __magic_name__       (		self :	Tuple     ,						A_ :	Optional[Any]=False     ,						A_ :	Optional[int]=False       ):






      '''simple docstring'''




      def _flatten(A_ :	Tuple       ):
           return list(itertools.chain(*A_       )       )

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





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



 """simple docstring"""







 _lowercase     :					Any      =						WhisperFeatureExtractor if is_speech_available() else None

 def    __magic_name__       (		self :	List[str]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    WhisperFeatureExtractionTester(self       )

 def    __magic_name__       (		self :	Dict       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Any        =    self.feature_extraction_class(**self.feat_extract_dict       )

      with tempfile.TemporaryDirectory() as tmpdirname:
           _lowerCAmelCase   :					Union[str, Any]        =    feat_extract_first.save_pretrained(A_       )[0]
           check_json_file_has_correct_format(A_       )
           _lowerCAmelCase   :					str        =    self.feature_extraction_class.from_pretrained(A_       )

      _lowerCAmelCase   :					Optional[int]        =    feat_extract_first.to_dict()
      _lowerCAmelCase   :					Optional[Any]        =    feat_extract_second.to_dict()
      _lowerCAmelCase   :					int        =    feat_extract_first.mel_filters
      _lowerCAmelCase   :					Dict        =    feat_extract_second.mel_filters
      self.assertTrue(np.allclose(A_     ,						A_       )       )
      self.assertEqual(A_     ,						A_       )

 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Dict        =    self.feature_extraction_class(**self.feat_extract_dict       )

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

      _lowerCAmelCase   :					Tuple        =    feat_extract_first.to_dict()
      _lowerCAmelCase   :					List[Any]        =    feat_extract_second.to_dict()
      _lowerCAmelCase   :					Optional[Any]        =    feat_extract_first.mel_filters
      _lowerCAmelCase   :					Tuple        =    feat_extract_second.mel_filters
      self.assertTrue(np.allclose(A_     ,						A_       )       )
      self.assertEqual(A_     ,						A_       )

 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
      # create three inputs of length 800, 1000, and 1200
      _lowerCAmelCase   :					Dict        =    [floats_list((1, x)       )[0] for x in range(8_0_0     ,						1_4_0_0     ,						2_0_0       )]
      _lowerCAmelCase   :					Union[str, Any]        =    [np.asarray(A_       ) for speech_input in speech_inputs]

      # Test feature size
      _lowerCAmelCase   :					List[Any]        =    feature_extractor(A_     ,						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
      _lowerCAmelCase   :					Optional[Any]        =    feature_extractor(speech_inputs[0]     ,						return_tensors="np"       ).input_features
      _lowerCAmelCase   :					List[Any]        =    feature_extractor(np_speech_inputs[0]     ,						return_tensors="np"       ).input_features
      self.assertTrue(np.allclose(A_     ,						A_     ,						atol=1E-3       )       )

      # Test batched
      _lowerCAmelCase   :					Any        =    feature_extractor(A_     ,						return_tensors="np"       ).input_features
      _lowerCAmelCase   :					str        =    feature_extractor(A_     ,						return_tensors="np"       ).input_features
      for enc_seq_a, enc_seq_a in zip(A_     ,						A_       ):
           self.assertTrue(np.allclose(A_     ,						A_     ,						atol=1E-3       )       )

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

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

      _lowerCAmelCase   :					List[Any]        =    [x[: feature_extractor.n_samples] for x in speech_inputs]
      _lowerCAmelCase   :					str        =    [np.asarray(A_       ) for speech_input in speech_inputs_truncated]

      _lowerCAmelCase   :					List[str]        =    feature_extractor(A_     ,						return_tensors="np"       ).input_features
      _lowerCAmelCase   :					str        =    feature_extractor(A_     ,						return_tensors="np"       ).input_features
      for enc_seq_a, enc_seq_a in zip(A_     ,						A_       ):
           self.assertTrue(np.allclose(A_     ,						A_     ,						atol=1E-3       )       )

 def    __magic_name__       (		self :	Dict       ):






      '''simple docstring'''




      import torch

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

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

 def    __magic_name__       (		self :	str     ,						A_ :	int       ):






      '''simple docstring'''




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

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

 def    __magic_name__       (		self :	Dict       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Any        =    torch.tensor(
          [
              0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951,
              0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678,
              0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554,
              -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854
          ]       )
      # fmt: on

      _lowerCAmelCase   :					Tuple        =    self._load_datasamples(1       )
      _lowerCAmelCase   :					List[Any]        =    WhisperFeatureExtractor()
      _lowerCAmelCase   :					int        =    feature_extractor(A_     ,						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]     ,						A_     ,						atol=1E-4       )       )


 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
      _lowerCAmelCase   :					Union[str, Any]        =    self._load_datasamples(1       )[0]
      _lowerCAmelCase   :					str        =    ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5_5_3_5  # Rescale to [0, 65535] to show issue
      _lowerCAmelCase   :					int        =    feat_extract.zero_mean_unit_var_norm([audio]     ,						attention_mask=A_       )[0]

      self.assertTrue(np.all(np.mean(A_       ) < 1E-3       )       )
      self.assertTrue(np.all(np.abs(np.var(A_       ) - 1       ) < 1E-3       )       )









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

__UpperCAmelCase    				=     logging.get_logger(__name__)







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      Union[str, Any]:

     """simple docstring"""







     return [
         int(1000 * (box[0] / width)				),
         int(1000 * (box[1] / height)				),
         int(1000 * (box[2] / width)				),
         int(1000 * (box[3] / height)				),
     ]







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE = None				)       ->      str:

     """simple docstring"""







     _lowerCAmelCase   :					str        =    tesseract_config if tesseract_config is not None else ""

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

     # filter empty words and corresponding coordinates
     _lowerCAmelCase   :					Dict        =    [idx for idx, word in enumerate(SCREAMING_SNAKE_CASE				) if not word.strip()]
     _lowerCAmelCase   :					Any        =    [word for idx, word in enumerate(SCREAMING_SNAKE_CASE				) if idx not in irrelevant_indices]
     _lowerCAmelCase   :					Dict        =    [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE				) if idx not in irrelevant_indices]
     _lowerCAmelCase   :					Optional[Any]        =    [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE				) if idx not in irrelevant_indices]
     _lowerCAmelCase   :					Optional[Any]        =    [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE				) if idx not in irrelevant_indices]
     _lowerCAmelCase   :					Optional[int]        =    [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE				) if idx not in irrelevant_indices]

     # turn coordinates into (left, top, left+width, top+height) format
     _lowerCAmelCase   :					Union[str, Any]        =    []
     for x, y, w, h in zip(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				):
          _lowerCAmelCase   :					Tuple        =    [x, y, x + w, y + h]
          actual_boxes.append(SCREAMING_SNAKE_CASE				)

     # finally, normalize the bounding boxes
     _lowerCAmelCase   :					Dict        =    []
     for box in actual_boxes:
          normalized_boxes.append(normalize_box(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)				)

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

     return words, normalized_boxes





class A__    ( A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''pixel_values''']

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






      '''simple docstring'''




      super().__init__(**A_       )
      _lowerCAmelCase   :					int        =    size if size is not None else {"height": 2_2_4, "width": 2_2_4}
      _lowerCAmelCase   :					Any        =    get_size_dict(A_       )

      _lowerCAmelCase   :					Any        =    do_resize
      _lowerCAmelCase   :					Any        =    size
      _lowerCAmelCase   :					Dict        =    resample
      _lowerCAmelCase   :					Optional[int]        =    apply_ocr
      _lowerCAmelCase   :					Dict        =    ocr_lang
      _lowerCAmelCase   :					Optional[int]        =    tesseract_config

 def    __magic_name__       (		self :	Dict     ,						A_ :	np.ndarray     ,						A_ :	Dict[str, int]     ,						A_ :	PILImageResampling = PILImageResampling.BILINEAR     ,						A_ :	Optional[Union[str, ChannelDimension]] = None     ,						**A_ :	List[str]     ,						):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    get_size_dict(A_       )
      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()}'''       )
      _lowerCAmelCase   :					Dict        =    (size["height"], size["width"])
      return resize(A_     ,						size=A_     ,						resample=A_     ,						data_format=A_     ,						**A_       )


 def    __magic_name__       (		self :	Dict     ,						A_ :	ImageInput     ,						A_ :	bool = None     ,						A_ :	Dict[str, int] = None     ,						A_ :	PILImageResampling = None     ,						A_ :	bool = None     ,						A_ :	Optional[str] = None     ,						A_ :	Optional[str] = None     ,						A_ :	Optional[Union[str, TensorType]] = None     ,						A_ :	ChannelDimension = ChannelDimension.FIRST     ,						**A_ :	Dict     ,						):






      '''simple docstring'''




      _lowerCAmelCase   :					List[Any]        =    do_resize if do_resize is not None else self.do_resize
      _lowerCAmelCase   :					str        =    size if size is not None else self.size
      _lowerCAmelCase   :					List[Any]        =    get_size_dict(A_       )
      _lowerCAmelCase   :					Tuple        =    resample if resample is not None else self.resample
      _lowerCAmelCase   :					Union[str, Any]        =    apply_ocr if apply_ocr is not None else self.apply_ocr
      _lowerCAmelCase   :					Tuple        =    ocr_lang if ocr_lang is not None else self.ocr_lang
      _lowerCAmelCase   :					str        =    tesseract_config if tesseract_config is not None else self.tesseract_config

      _lowerCAmelCase   :					Union[str, Any]        =    make_list_of_images(A_       )

      if not valid_images(A_       ):
           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.
      _lowerCAmelCase   :					Dict        =    [to_numpy_array(A_       ) for image in images]

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

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

      # flip color channels from RGB to BGR (as Detectron2 requires this)
      _lowerCAmelCase   :					Optional[int]        =    [flip_channel_order(A_       ) for image in images]
      _lowerCAmelCase   :					List[Any]        =    [to_channel_dimension_format(A_     ,						A_       ) for image in images]

      _lowerCAmelCase   :					Tuple        =    BatchFeature(data={"pixel_values": images}     ,						tensor_type=A_       )

      if apply_ocr:
           _lowerCAmelCase   :					Tuple        =    words_batch
           _lowerCAmelCase   :					Any        =    boxes_batch
      return data






