




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						:   Optional[int]		=   logging.get_logger(__name__)
@add_end_docstrings(lowerCAmelCase		)
class 			_UpperCamelCase      (				lowerCAmelCase		):
      def __init__(			self   :int ,      *lowerCamelCase   :Tuple ,      **lowerCamelCase   :Union[str, Any]      )     ->			int:
       super().__init__(*lowerCamelCase ,      **lowerCamelCase      )
       requires_backends(self ,      "decord"      )
       self.check_model_type(lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :List[Any] ,      lowerCamelCase   :Tuple=None ,      lowerCamelCase   :int=None ,      lowerCamelCase   :int=None      )     ->			Dict:
       UpperCAmelCase__							 =						{}
       if frame_sampling_rate is not None:
        UpperCAmelCase__							 =						frame_sampling_rate
       if num_frames is not None:
        UpperCAmelCase__							 =						num_frames

       UpperCAmelCase__							 =						{}
       if top_k is not None:
        UpperCAmelCase__							 =						top_k
       return preprocess_params, {}, postprocess_params
      def __call__(			self   :Dict ,      lowerCamelCase   :Union[str, List[str]] ,      **lowerCamelCase   :Tuple      )     ->			Union[str, Any]:
       return super().__call__(lowerCamelCase ,      **lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Tuple ,      lowerCamelCase   :str ,      lowerCamelCase   :Dict=None ,      lowerCamelCase   :Tuple=1      )     ->			List[str]:
       if num_frames is None:
        UpperCAmelCase__							 =						self.model.config.num_frames

       if video.startswith("http://"      ) or video.startswith("https://"      ):
        UpperCAmelCase__							 =						BytesIO(requests.get(lowerCamelCase      ).content      )

       UpperCAmelCase__							 =						VideoReader(lowerCamelCase      )
       videoreader.seek(0      )

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

       UpperCAmelCase__							 =						videoreader.get_batch(lowerCamelCase      ).asnumpy()
       UpperCAmelCase__							 =						list(lowerCamelCase      )

       UpperCAmelCase__							 =						self.image_processor(lowerCamelCase ,      return_tensors=self.framework      )
       return model_inputs
      def 	UpperCAmelCase_						(			self   :Optional[Any] ,      lowerCamelCase   :Any      )     ->			List[str]:
       UpperCAmelCase__							 =						self.model(**lowerCamelCase      )
       return model_outputs







      def 	UpperCAmelCase_						(			self   :int ,      lowerCamelCase   :Optional[Any] ,      lowerCamelCase   :Union[str, Any]=5      )     ->			List[Any]:
       if top_k > self.model.config.num_labels:
        UpperCAmelCase__							 =						self.model.config.num_labels

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

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











from __future__ import annotations



def 						lowerCAmelCase		(		_lowerCAmelCase							:   int = 4	):




 """simple docstring"""







 UpperCAmelCase__							 =						abs(_lowerCAmelCase	) or 4
 return [[1 + x + y * row_size for x in range(_lowerCAmelCase	)] for y in range(_lowerCAmelCase	)]



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list[list[int]]	):




 """simple docstring"""







 return reverse_row(transpose(_lowerCAmelCase	)	)
 # OR.. transpose(reverse_column(matrix))



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list[list[int]]	):




 """simple docstring"""







 return reverse_row(reverse_column(_lowerCAmelCase	)	)
 # OR.. reverse_column(reverse_row(matrix))



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list[list[int]]	):




 """simple docstring"""







 return reverse_column(transpose(_lowerCAmelCase	)	)
 # OR.. transpose(reverse_row(matrix))



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list[list[int]]	):




 """simple docstring"""







 UpperCAmelCase__							 =						[list(_lowerCAmelCase	) for x in zip(*_lowerCAmelCase	)]
 return matrix



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list[list[int]]	):




 """simple docstring"""







 UpperCAmelCase__							 =						matrix[::-1]
 return matrix



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list[list[int]]	):




 """simple docstring"""







 UpperCAmelCase__							 =						[x[::-1] for x in matrix]
 return matrix



def 						lowerCAmelCase		(		_lowerCAmelCase							:   list[list[int]]	):




 """simple docstring"""







 for i in matrix:
  print(*_lowerCAmelCase	)


if __name__ == "__main__":
  _lowerCAmelCase						:   Optional[int]		=   make_matrix()
  print("\norigin:\n")
  print_matrix(matrix)
  print("\nrotate 90 counterclockwise:\n")
  print_matrix(rotate_aa(matrix))

  _lowerCAmelCase						:   Union[str, Any]		=   make_matrix()
  print("\norigin:\n")
  print_matrix(matrix)
  print("\nrotate 180:\n")
  print_matrix(rotate_aaa(matrix))

  _lowerCAmelCase						:   Optional[int]		=   make_matrix()
  print("\norigin:\n")
  print_matrix(matrix)
  print("\nrotate 270 counterclockwise:\n")
  print_matrix(rotate_aaa(matrix))





