
from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       import torch

       from transformers.modeling_outputs import BaseModelOutput

       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

lowerCamelCase	:	Tuple				=   logging.get_logger(__name__)




@add_end_docstrings(A_							)
class 						__lowercase		(A_							):




      """simple docstring"""





      def __init__(				self    ,				**A     )		->    List[Any]:
         super().__init__(**snake_case__     )

         if self.framework == "tf":
            raise ValueError(f"""The {self.__class__} is only available in PyTorch."""     )

         requires_backends(self    ,				"""vision"""     )
         self.check_model_type(snake_case__     )

      def __call__(				self    ,				A    ,				A = None    ,				**A    ,				)		->    Optional[int]:


         if "text_queries" in kwargs:
            snake_case				:    Tuple           =	kwargs.pop("""text_queries"""     )

         if isinstance(snake_case__    ,				(str, Image.Image)     ):
            snake_case				:    Dict           =	{"image": image, "candidate_labels": candidate_labels}
         else:
            snake_case				:    Tuple           =	image
         snake_case				:    Dict           =	super().__call__(snake_case__    ,				**snake_case__     )
         return results

      def      UpperCAmelCase				(				self    ,				**A     )		->    Tuple:
         snake_case				:    Optional[int]           =	{}
         if "threshold" in kwargs:
            snake_case				:    Optional[Any]           =	kwargs["threshold"]
         if "top_k" in kwargs:
            snake_case				:    int           =	kwargs["top_k"]
         return {}, {}, postprocess_params

      def      UpperCAmelCase				(				self    ,				A     )		->    int:
         snake_case				:    int           =	load_image(inputs["""image"""]     )
         snake_case				:    int           =	inputs["candidate_labels"]
         if isinstance(snake_case__    ,				snake_case__     ):
            snake_case				:    int           =	candidate_labels.split(""","""     )

         snake_case				:    List[Any]           =	torch.tensor([[image.height, image.width]]    ,				dtype=torch.intaa     )
         for i, candidate_label in enumerate(snake_case__     ):
            snake_case				:    Any           =	self.tokenizer(snake_case__    ,				return_tensors=self.framework     )
            snake_case				:    str           =	self.image_processor(snake_case__    ,				return_tensors=self.framework     )
            yield {
                "is_last": i == len(snake_case__     ) - 1,
                "target_size": target_size,
                "candidate_label": candidate_label,
                **text_inputs,
                **image_features,
            }

      def      UpperCAmelCase				(				self    ,				A     )		->    int:
         snake_case				:    Any           =	model_inputs.pop("""target_size"""     )
         snake_case				:    str           =	model_inputs.pop("""candidate_label"""     )
         snake_case				:    Dict           =	model_inputs.pop("""is_last"""     )

         snake_case				:    Optional[Any]           =	self.model(**snake_case__     )

         snake_case				:    Tuple           =	{"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs}
         return model_outputs

      def      UpperCAmelCase				(				self    ,				A    ,				A=0.1    ,				A=None     )		->    str:
         snake_case				:    Dict           =	[]
         for model_output in model_outputs:
            snake_case				:    List[str]           =	model_output["candidate_label"]
            snake_case				:    Union[str, Any]           =	BaseModelOutput(snake_case__     )
            snake_case				:    str           =	self.image_processor.post_process_object_detection(
                outputs=snake_case__    ,				threshold=snake_case__    ,				target_sizes=model_output["""target_size"""]     )[0]

            for index in outputs["scores"].nonzero():
               snake_case				:    str           =	outputs["scores"][index].item()
               snake_case				:    Optional[int]           =	self._get_bounding_box(outputs["""boxes"""][index][0]     )

               snake_case				:    Tuple           =	{"score": score, "label": label, "box": box}
               results.append(snake_case__     )

         snake_case				:    int           =	sorted(snake_case__    ,				key=lambda A     : x["score"]    ,				reverse=snake_case__     )
         if top_k:
            snake_case				:    Optional[Any]           =	results[:top_k]

         return results

      def      UpperCAmelCase				(				self    ,				A     )		->    Dict[str, int]:


         if self.framework != "pt":
            raise ValueError("""The ZeroShotObjectDetectionPipeline is only available in PyTorch."""     )
         snake_case				:    int           =	box.int().tolist()
         snake_case				:    Dict           =	{
             "xmin": xmin,
             "ymin": ymin,
             "xmax": xmax,
             "ymax": ymax,
         }
         return bbox











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	:	int				=   logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase__							)
class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      def __init__(				self    ,				*A    ,				**A     )		->    Optional[int]:
         super().__init__(*A    ,				**A     )
         requires_backends(self    ,				"""decord"""     )
         self.check_model_type(A     )

      def      UpperCAmelCase				(				self    ,				A=None    ,				A=None    ,				A=None     )		->    int:
         snake_case				:    Any           =	{}
         if frame_sampling_rate is not None:
            snake_case				:    int           =	frame_sampling_rate
         if num_frames is not None:
            snake_case				:    Union[str, Any]           =	num_frames

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

      def __call__(				self    ,				A    ,				**A     )		->    Dict:
         return super().__call__(A    ,				**A     )

      def      UpperCAmelCase				(				self    ,				A    ,				A=None    ,				A=1     )		->    Tuple:
         if num_frames is None:
            snake_case				:    Tuple           =	self.model.config.num_frames

         if video.startswith("""http://"""     ) or video.startswith("""https://"""     ):
            snake_case				:    Optional[int]           =	BytesIO(requests.get(A     ).content     )

         snake_case				:    Optional[Any]           =	VideoReader(A     )
         videoreader.seek(0     )

         snake_case				:    Optional[Any]           =	0
         snake_case				:    Optional[Any]           =	num_frames * frame_sampling_rate - 1
         snake_case				:    Any           =	np.linspace(A    ,				A    ,				num=A    ,				dtype=np.intaa     )

         snake_case				:    int           =	videoreader.get_batch(A     ).asnumpy()
         snake_case				:    List[Any]           =	list(A     )

         snake_case				:    Optional[Any]           =	self.image_processor(A    ,				return_tensors=self.framework     )
         return model_inputs

      def      UpperCAmelCase				(				self    ,				A     )		->    List[str]:
         snake_case				:    Dict           =	self.model(**A     )
         return model_outputs

      def      UpperCAmelCase				(				self    ,				A    ,				A=5     )		->    int:
         if top_k > self.model.config.num_labels:
            snake_case				:    str           =	self.model.config.num_labels

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

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





