





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

_lowercase		:  Dict			       =logging.get_logger(__name__)






@add_end_docstrings(snake_case__							)
class 						UpperCamelCase_  (  snake_case__							):



					def __init__(		self     :     Any       ,      *lowerCamelCase     :     Optional[Any]       ,      **lowerCamelCase     :     List[Any]						):
							super().__init__(*lowerCamelCase       ,      **lowerCamelCase						)
							requires_backends(self       ,      'decord'						)
							self.check_model_type(lowerCamelCase						)



					def   __a     (		self     :     List[Any]       ,      lowerCamelCase     :     Dict=None       ,      lowerCamelCase     :     str=None       ,      lowerCamelCase     :     int=None						):
							lowerCamelCase_	:     List[str] 						=      {}
							if frame_sampling_rate is not None:
									lowerCamelCase_	:     int 						=      frame_sampling_rate
							if num_frames is not None:
									lowerCamelCase_	:     Optional[Any] 						=      num_frames

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



					def __call__(		self     :     Dict       ,      lowerCamelCase     :     Union[str, List[str]]       ,      **lowerCamelCase     :     Optional[Any]						):
							return super().__call__(lowerCamelCase       ,      **lowerCamelCase						)



					def   __a     (		self     :     str       ,      lowerCamelCase     :     Any       ,      lowerCamelCase     :     str=None       ,      lowerCamelCase     :     str=1						):
							if num_frames is None:
									lowerCamelCase_	:     Any 						=      self.model.config.num_frames

							if video.startswith('http://'						) or video.startswith('https://'						):
									lowerCamelCase_	:     Any 						=      BytesIO(requests.get(lowerCamelCase						).content						)

							lowerCamelCase_	:     Optional[Any] 						=      VideoReader(lowerCamelCase						)
							videoreader.seek(0						)

							lowerCamelCase_	:     Dict 						=      0
							lowerCamelCase_	:     List[Any] 						=      num_frames * frame_sampling_rate - 1
							lowerCamelCase_	:     List[str] 						=      np.linspace(lowerCamelCase       ,      lowerCamelCase       ,      num=lowerCamelCase       ,      dtype=np.intaa						)

							lowerCamelCase_	:     Any 						=      videoreader.get_batch(lowerCamelCase						).asnumpy()
							lowerCamelCase_	:     Any 						=      list(lowerCamelCase						)

							lowerCamelCase_	:     Any 						=      self.image_processor(lowerCamelCase       ,      return_tensors=self.framework						)
							return model_inputs



					def   __a     (		self     :     Any       ,      lowerCamelCase     :     Any						):
							lowerCamelCase_	:     List[str] 						=      self.model(**lowerCamelCase						)
							return model_outputs





					def   __a     (		self     :     List[str]       ,      lowerCamelCase     :     List[Any]       ,      lowerCamelCase     :     Tuple=5						):
							if top_k > self.model.config.num_labels:
									lowerCamelCase_	:     List[Any] 						=      self.model.config.num_labels

							if self.framework == "pt":
									lowerCamelCase_	:     Dict 						=      model_outputs.logits.softmax(-1						)[0]
									lowerCamelCase_						,      lowerCamelCase_	:     str 						=      probs.topk(lowerCamelCase						)
							else:
									raise ValueError(F"Unsupported framework: {self.framework}"						)

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







import warnings

from ...utils import logging
from .image_processing_yolos import YolosImageProcessor


_lowercase		:  int			       =logging.get_logger(__name__)






class 						UpperCamelCase_  (  snake_case__							):



					def __init__(		self     :     Tuple       ,      *lowerCamelCase     :     List[Any]       ,      **lowerCamelCase     :     Union[str, Any]						):
							warnings.warn(
							    'The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
							    ' use YolosImageProcessor instead.'       ,      lowerCamelCase       ,      )
							super().__init__(*lowerCamelCase       ,      **lowerCamelCase						)
