





"""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

__lowerCamelCase											=  logging.get_logger(__name__)






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""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   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							to_pil_image(lowercase_				)
							A__	     =							pil_image.size
							A__	     =							pytesseract.image_to_data(lowercase_       ,    lang=lowercase_       ,    output_type='dict'       ,    config=lowercase_				)
							A__	     =							data["""text"""], data["""left"""], data["""top"""], data["""width"""], data["""height"""]

							# filter empty words and corresponding coordinates
							A__	     =							[idx for idx, word in enumerate(lowercase_				) if not word.strip()]
							A__	     =							[word for idx, word in enumerate(lowercase_				) if idx not in irrelevant_indices]
							A__	     =							[coord for idx, coord in enumerate(lowercase_				) if idx not in irrelevant_indices]
							A__	     =							[coord for idx, coord in enumerate(lowercase_				) if idx not in irrelevant_indices]
							A__	     =							[coord for idx, coord in enumerate(lowercase_				) if idx not in irrelevant_indices]
							A__	     =							[coord for idx, coord in enumerate(lowercase_				) if idx not in irrelevant_indices]

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

							# finally, normalize the bounding boxes
							A__	     =							[]
							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     UpperCamelCase__(			__A ):
						lowerCAmelCase__  :						List[Any]													=		['''pixel_values''']





						def __init__(       self		,__UpperCAmelCase = True		,__UpperCAmelCase = None		,__UpperCAmelCase = PILImageResampling.BILINEAR		,__UpperCAmelCase = True		,__UpperCAmelCase = 1 / 2_55		,__UpperCAmelCase = True		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = True		,__UpperCAmelCase = None		,__UpperCAmelCase = ""		,**__UpperCAmelCase		,)					->				int:
													super().__init__(**A__     )
													A__	     =							size if size is not None else {"""height""": 2_24, """width""": 2_24}
													A__	     =							get_size_dict(A__     )

													A__	     =							do_resize
													A__	     =							size
													A__	     =							resample
													A__	     =							do_rescale
													A__	     =							rescale_value
													A__	     =							do_normalize
													A__	     =							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
													A__	     =							image_std if image_std is not None else IMAGENET_STANDARD_STD
													A__	     =							apply_ocr
													A__	     =							ocr_lang
													A__	     =							tesseract_config





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase = PILImageResampling.BILINEAR		,__UpperCAmelCase = None		,**__UpperCAmelCase		,)					->				str:
													A__	     =							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()}'''     )
													A__	     =							(size["""height"""], size["""width"""])
													return resize(A__		,size=A__		,resample=A__		,data_format=A__		,**A__     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase = None		,**__UpperCAmelCase		,)					->				Any:
													return rescale(A__		,scale=A__		,data_format=A__		,**A__     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase = None		,**__UpperCAmelCase		,)					->				str:
													return normalize(A__		,mean=A__		,std=A__		,data_format=A__		,**A__     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase=None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = None		,__UpperCAmelCase = ChannelDimension.FIRST		,**__UpperCAmelCase		,)					->				List[Any]:
													A__	     =							do_resize if do_resize is not None else self.do_resize
													A__	     =							size if size is not None else self.size
													A__	     =							get_size_dict(A__     )
													A__	     =							resample if resample is not None else self.resample
													A__	     =							do_rescale if do_rescale is not None else self.do_rescale
													A__	     =							rescale_factor if rescale_factor is not None else self.rescale_factor
													A__	     =							do_normalize if do_normalize is not None else self.do_normalize
													A__	     =							image_mean if image_mean is not None else self.image_mean
													A__	     =							image_std if image_std is not None else self.image_std
													A__	     =							apply_ocr if apply_ocr is not None else self.apply_ocr
													A__	     =							ocr_lang if ocr_lang is not None else self.ocr_lang
													A__	     =							tesseract_config if tesseract_config is not None else self.tesseract_config

													A__	     =							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.
													A__	     =							[to_numpy_array(A__     ) for image in images]

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

													if do_resize:
																				A__	     =							[self.resize(image=A__		,size=A__		,resample=A__     ) for image in images]

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

													if do_normalize:
																				A__	     =							[self.normalize(image=A__		,mean=A__		,std=A__     ) for image in images]

													A__	     =							[to_channel_dimension_format(A__		,A__     ) for image in images]

													A__	     =							BatchFeature(data={'pixel_values': images}		,tensor_type=A__     )

													if apply_ocr:
																				A__	     =							words_batch
																				A__	     =							boxes_batch
													return data






"""simple docstring"""




from io import BytesIO
from typing import List, Union

import requests

from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_decord_available():
				import numpy as np
				from decord import VideoReader


if is_torch_available():
				from ..models.auto.modeling_auto import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING

__lowerCamelCase											=  logging.get_logger(__name__)
@add_end_docstrings(__A )
class     UpperCamelCase__(			__A ):





						def __init__(       self		,*__UpperCAmelCase		,**__UpperCAmelCase     )					->				List[Any]:
													super().__init__(*__UpperCAmelCase		,**__UpperCAmelCase     )
													requires_backends(self		,'decord'     )
													self.check_model_type(__UpperCAmelCase     )





						def 				snake_case__	(       self		,__UpperCAmelCase=None		,__UpperCAmelCase=None		,__UpperCAmelCase=None     )					->				Any:
													A__	     =							{}
													if frame_sampling_rate is not None:
																				A__	     =							frame_sampling_rate
													if num_frames is not None:
																				A__	     =							num_frames

													A__	     =							{}
													if top_k is not None:
																				A__	     =							top_k
													return preprocess_params, {}, postprocess_params





						def __call__(       self		,__UpperCAmelCase		,**__UpperCAmelCase     )					->				Optional[int]:
													return super().__call__(__UpperCAmelCase		,**__UpperCAmelCase     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase=None		,__UpperCAmelCase=1     )					->				List[Any]:
													if num_frames is None:
																				A__	     =							self.model.config.num_frames

													if video.startswith('http://'     ) or video.startswith('https://'     ):
																				A__	     =							BytesIO(requests.get(__UpperCAmelCase     ).content     )

													A__	     =							VideoReader(__UpperCAmelCase     )
													videoreader.seek(0     )

													A__	     =							0
													A__	     =							num_frames * frame_sampling_rate - 1
													A__	     =							np.linspace(__UpperCAmelCase		,__UpperCAmelCase		,num=__UpperCAmelCase		,dtype=np.intaa     )

													A__	     =							videoreader.get_batch(__UpperCAmelCase     ).asnumpy()
													A__	     =							list(__UpperCAmelCase     )

													A__	     =							self.image_processor(__UpperCAmelCase		,return_tensors=self.framework     )
													return model_inputs





						def 				snake_case__	(       self		,__UpperCAmelCase     )					->				Optional[int]:
													A__	     =							self.model(**__UpperCAmelCase     )
													return model_outputs





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase=5     )					->				int:
													if top_k > self.model.config.num_labels:
																				A__	     =							self.model.config.num_labels

													if self.framework == "pt":
																				A__	     =							model_outputs.logits.softmax(-1     )[0]
																				A__     ,  A__	     =							probs.topk(__UpperCAmelCase     )
													else:
																				raise ValueError(f'''Unsupported framework: {self.framework}'''     )

													A__	     =							scores.tolist()
													A__	     =							ids.tolist()
													return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(__UpperCAmelCase		,__UpperCAmelCase     )]





