'''simple docstring'''





from typing import Dict, Iterable, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format, to_pil_image
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    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       lowerCAmelCase_     (		lowercase:							Optional[Any]  ,					lowercase:							Optional[Any]  ,					lowercase:							Tuple		)    ->				Optional[Any]:







 '''simple docstring'''
 return [
     int(1_000 * (box[0] / width)		),
     int(1_000 * (box[1] / height)		),
     int(1_000 * (box[2] / width)		),
     int(1_000 * (box[3] / height)		),
 ]




def       lowerCAmelCase_     (		lowercase:							Tuple  ,					lowercase:							List[str]  ,					lowercase:							int		)    ->				List[Any]:







 '''simple docstring'''
 _UpperCamelCase:     int							= to_pil_image(lowercase_		)
 _UpperCamelCase:     List[str]							= pil_image.size
 _UpperCamelCase:     Tuple							= pytesseract.image_to_data(lowercase_  ,					lang=lowercase_  ,					output_type='''dict'''  ,					config=lowercase_		)
 _UpperCamelCase:     str							= data["text"], data["left"], data["top"], data["width"], data["height"]

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

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

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

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

 return words, normalized_boxes





class  __magic_name__  (		_UpperCamelCase    ):





   """simple docstring"""




   lowerCAmelCase      :    Any  			=   ["pixel_values"]



   def __init__(	self					:  Tuple		,  _lowercase					:  bool = True		,  _lowercase					:  Dict[str, int] = None		,  _lowercase					:  PILImageResampling = PILImageResampling.BILINEAR		,  _lowercase					:  bool = True		,  _lowercase					:  float = 1 / 255		,  _lowercase					:  bool = True		,  _lowercase					:  Union[float, Iterable[float]] = None		,  _lowercase					:  Union[float, Iterable[float]] = None		,  _lowercase					:  bool = True		,  _lowercase					:  Optional[str] = None		,  _lowercase					:  Optional[str] = ""		,  **_lowercase					:  Optional[Any]		,  ):



    """simple docstring"""



    super().__init__(**__a							)
    _UpperCamelCase:     str							= size if size is not None else {"height": 224, "width": 224}
    _UpperCamelCase:     Optional[Any]							= get_size_dict(__a							)

    _UpperCamelCase:     int							= do_resize
    _UpperCamelCase:     Union[str, Any]							= size
    _UpperCamelCase:     Optional[int]							= resample
    _UpperCamelCase:     Any							= do_rescale
    _UpperCamelCase:     List[str]							= rescale_value
    _UpperCamelCase:     Tuple							= do_normalize
    _UpperCamelCase:     Optional[Any]							= image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
    _UpperCamelCase:     Tuple							= image_std if image_std is not None else IMAGENET_STANDARD_STD
    _UpperCamelCase:     str							= apply_ocr
    _UpperCamelCase:     List[Any]							= ocr_lang
    _UpperCamelCase:     Dict							= tesseract_config



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



    """simple docstring"""



    _UpperCamelCase:     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()}"""							)
    _UpperCamelCase:     Union[str, Any]							= (size["height"], size["width"])
    return resize(__a		,  size=__a		,  resample=__a		,  data_format=__a		,  **__a							)



   def   lowerCAmelCase						(	self					:  Tuple		,  _lowercase					:  np.ndarray		,  _lowercase					:  Union[int, float]		,  _lowercase					:  Optional[Union[str, ChannelDimension]] = None		,  **_lowercase					:  Optional[Any]		,  ):



    """simple docstring"""



    return rescale(__a		,  scale=__a		,  data_format=__a		,  **__a							)



   def   lowerCAmelCase						(	self					:  str		,  _lowercase					:  np.ndarray		,  _lowercase					:  Union[float, Iterable[float]]		,  _lowercase					:  Union[float, Iterable[float]]		,  _lowercase					:  Optional[Union[str, ChannelDimension]] = None		,  **_lowercase					:  Optional[Any]		,  ):



    """simple docstring"""



    return normalize(__a		,  mean=__a		,  std=__a		,  data_format=__a		,  **__a							)



   def   lowerCAmelCase						(	self					:  str		,  _lowercase					:  ImageInput		,  _lowercase					:  bool = None		,  _lowercase					:  Dict[str, int] = None		,  _lowercase					:  str=None		,  _lowercase					:  bool = None		,  _lowercase					:  float = None		,  _lowercase					:  bool = None		,  _lowercase					:  Union[float, Iterable[float]] = None		,  _lowercase					:  Union[float, Iterable[float]] = None		,  _lowercase					:  bool = None		,  _lowercase					:  Optional[str] = None		,  _lowercase					:  Optional[str] = None		,  _lowercase					:  Optional[Union[str, TensorType]] = None		,  _lowercase					:  ChannelDimension = ChannelDimension.FIRST		,  **_lowercase					:  Optional[int]		,  ):



    """simple docstring"""



    _UpperCamelCase:     str							= do_resize if do_resize is not None else self.do_resize
    _UpperCamelCase:     Optional[int]							= size if size is not None else self.size
    _UpperCamelCase:     Tuple							= get_size_dict(__a							)
    _UpperCamelCase:     Union[str, Any]							= resample if resample is not None else self.resample
    _UpperCamelCase:     Optional[Any]							= do_rescale if do_rescale is not None else self.do_rescale
    _UpperCamelCase:     Tuple							= rescale_factor if rescale_factor is not None else self.rescale_factor
    _UpperCamelCase:     List[str]							= do_normalize if do_normalize is not None else self.do_normalize
    _UpperCamelCase:     Tuple							= image_mean if image_mean is not None else self.image_mean
    _UpperCamelCase:     Any							= image_std if image_std is not None else self.image_std
    _UpperCamelCase:     Any							= apply_ocr if apply_ocr is not None else self.apply_ocr
    _UpperCamelCase:     Union[str, Any]							= ocr_lang if ocr_lang is not None else self.ocr_lang
    _UpperCamelCase:     Dict							= tesseract_config if tesseract_config is not None else self.tesseract_config

    _UpperCamelCase:     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.'''							)

    if do_rescale and rescale_factor is None:
     raise ValueError('''Rescale factor must be specified if do_rescale is True.'''							)

    if do_normalize and (image_mean is None or image_std is None):
     raise ValueError('''If do_normalize is True, image_mean and image_std must be specified.'''							)

    # All transformations expect numpy arrays.
    _UpperCamelCase:     Any							= [to_numpy_array(__a							) for image in images]

    # Tesseract OCR to get words + normalized bounding boxes
    if apply_ocr:
     requires_backends(self		,  '''pytesseract'''							)
     _UpperCamelCase:     List[str]							= []
     _UpperCamelCase:     Any							= []
     for image in images:
      _UpperCamelCase:     int							= apply_tesseract(__a		,  __a		,  __a							)
      words_batch.append(__a							)
      boxes_batch.append(__a							)

    if do_resize:
     _UpperCamelCase:     List[str]							= [self.resize(image=__a		,  size=__a		,  resample=__a							) for image in images]

    if do_rescale:
     _UpperCamelCase:     Tuple							= [self.rescale(image=__a		,  scale=__a							) for image in images]

    if do_normalize:
     _UpperCamelCase:     Optional[int]							= [self.normalize(image=__a		,  mean=__a		,  std=__a							) for image in images]

    _UpperCamelCase:     Dict							= [to_channel_dimension_format(__a		,  __a							) for image in images]

    _UpperCamelCase:     Any							= BatchFeature(data={'''pixel_values''': images}		,  tensor_type=__a							)

    if apply_ocr:
     _UpperCamelCase:     Union[str, Any]							= words_batch
     _UpperCamelCase:     List[str]							= boxes_batch
    return data
# Lint as: python3
# pylint: enable=line-too-long
# pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position

UpperCAmelCase_			    =							'''2.13.1'''

import platform

import pyarrow
from packaging import version


if version.parse(platform.python_version()) < version.parse('''3.7'''):
  raise ImportWarning(
      '''To use `datasets`, Python>=3.7 is required, and the current version of Python doesn\'t match this condition.'''
  )

if version.parse(pyarrow.__version__).major < 8:
  raise ImportWarning(
      '''To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn\'t match this condition.\n'''
      '''If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.'''
  )

del platform
del pyarrow
del version

from .arrow_dataset import Dataset
from .arrow_reader import ReadInstruction
from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder
from .combine import concatenate_datasets, interleave_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .download import *
from .features import *
from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled
from .info import DatasetInfo, MetricInfo
from .inspect import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
    list_datasets,
    list_metrics,
)
from .iterable_dataset import IterableDataset
from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric
from .metric import Metric
from .splits import (
    NamedSplit,
    NamedSplitAll,
    Split,
    SplitBase,
    SplitDict,
    SplitGenerator,
    SplitInfo,
    SubSplitInfo,
    percent,
)
from .tasks import *
from .utils import *
from .utils import logging


# deprecated modules
from datasets import arrow_dataset as _arrow_dataset  # isort:skip
from datasets import utils as _utils  # isort:skip
from datasets.utils import download_manager as _deprecated_download_manager  # isort:skip

UpperCAmelCase_			    =							concatenate_datasets
UpperCAmelCase_			    =							DownloadConfig
UpperCAmelCase_			    =							DownloadManager
UpperCAmelCase_			    =							DownloadMode
UpperCAmelCase_			    =							DownloadConfig
UpperCAmelCase_			    =							DownloadMode
UpperCAmelCase_			    =							DownloadManager

del _arrow_dataset, _utils, _deprecated_download_manager