






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

__UpperCAmelCase								=						logging.get_logger(__name__)



@add_end_docstrings(_snake_case			)
class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



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



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase=None				,		_UpperCamelCase=None				,		_UpperCamelCase=None   )	->							Dict:
							UpperCAmelCase_    :      Optional[Any]            =						{}
							if frame_sampling_rate is not None:
												UpperCAmelCase_    :      Optional[Any]            =						frame_sampling_rate
							if num_frames is not None:
												UpperCAmelCase_    :      Optional[Any]            =						num_frames

							UpperCAmelCase_    :      Optional[Any]            =						{}
							if top_k is not None:
												UpperCAmelCase_    :      List[Any]            =						top_k
							return preprocess_params, {}, postprocess_params



		def __call__(							self				,		_UpperCamelCase				,		**_UpperCamelCase   )	->							Union[str, Any]:
							return super().__call__(_UpperCamelCase				,		**_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=None				,		_UpperCamelCase=1   )	->							Optional[int]:
							if num_frames is None:
												UpperCAmelCase_    :      Optional[Any]            =						self.model.config.num_frames

							if video.startswith('http://'   ) or video.startswith('https://'   ):
												UpperCAmelCase_    :      List[str]            =						BytesIO(requests.get(_UpperCamelCase   ).content   )

							UpperCAmelCase_    :      Optional[Any]            =						VideoReader(_UpperCamelCase   )
							videoreader.seek(0   )

							UpperCAmelCase_    :      List[Any]            =						0
							UpperCAmelCase_    :      Union[str, Any]            =						num_frames * frame_sampling_rate - 1
							UpperCAmelCase_    :      Optional[int]            =						np.linspace(_UpperCamelCase				,		_UpperCamelCase				,		num=_UpperCamelCase				,		dtype=np.intaa   )

							UpperCAmelCase_    :      List[Any]            =						videoreader.get_batch(_UpperCamelCase   ).asnumpy()
							UpperCAmelCase_    :      Union[str, Any]            =						list(_UpperCamelCase   )

							UpperCAmelCase_    :      List[Any]            =						self.image_processor(_UpperCamelCase				,		return_tensors=self.framework   )
							return model_inputs



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase   )	->							List[Any]:
							UpperCAmelCase_    :      Dict            =						self.model(**_UpperCamelCase   )
							return model_outputs



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=5   )	->							List[str]:
							if top_k > self.model.config.num_labels:
												UpperCAmelCase_    :      List[Any]            =						self.model.config.num_labels

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

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










from typing import TYPE_CHECKING

from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available
from ...utils import OptionalDependencyNotAvailable


lowerCAmelCase       =							{'configuration_dpt': ['DPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DPTConfig']}

try:
			if not is_vision_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase       =							['DPTFeatureExtractor']
			lowerCAmelCase       =							['DPTImageProcessor']

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase       =							[
			    'DPT_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'DPTForDepthEstimation',
			    'DPTForSemanticSegmentation',
			    'DPTModel',
			    'DPTPreTrainedModel',
			]


if TYPE_CHECKING:
			from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig

			try:
						if not is_vision_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .feature_extraction_dpt import DPTFeatureExtractor
						from .image_processing_dpt import DPTImageProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_dpt import (
						    DPT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    DPTForDepthEstimation,
						    DPTForSemanticSegmentation,
						    DPTModel,
						    DPTPreTrainedModel,
						)


else:
			import sys

			lowerCAmelCase       =							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


