





"""simple docstring"""


import numpy as np
import torch

from ..models.clipseg import CLIPSegForImageSegmentation
from ..utils import is_vision_available, requires_backends
from .base import PipelineTool


if is_vision_available():
  from PIL import Image



class      __SCREAMING_SNAKE_CASE    (						_UpperCamelCase   ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:Tuple	     =					(
      "This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image."
      "It takes two arguments named `image` which should be the original image, and `label` which should be a text "
      "describing the elements what should be identified in the segmentation mask. The tool returns the mask."
  )
  SCREAMING_SNAKE_CASE__							:Tuple	     =					"CIDAS/clipseg-rd64-refined"
  SCREAMING_SNAKE_CASE__							:Tuple	     =					"image_segmenter"
  SCREAMING_SNAKE_CASE__							:List[Any]	     =					CLIPSegForImageSegmentation

  SCREAMING_SNAKE_CASE__							:List[Any]	     =					["image", "text"]
  SCREAMING_SNAKE_CASE__							:Optional[Any]	     =					["image"]

  def __init__(		self   :       List[str]					,   *__a   :       str					,   **__a   :       Optional[Any]	)		->				List[str]:
        requires_backends(self					,   ["vision"]	)
        super().__init__(*_SCREAMING_SNAKE_CASE					,   **_SCREAMING_SNAKE_CASE	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]					,   __a   :       Any					,   __a   :       Any	)		->				str:
        return self.pre_processor(text=[label]					,   images=[image]					,   padding=_SCREAMING_SNAKE_CASE					,   return_tensors="pt"	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[Any]					,   __a   :       Any	)		->				Tuple:






        with torch.no_grad():
              _UpperCamelCase      :	Any						=       self.model(**_SCREAMING_SNAKE_CASE	).logits
        return logits


  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple					,   __a   :       Optional[int]	)		->				Union[str, Any]:
        _UpperCamelCase      :	Dict						=       outputs.cpu().detach().numpy()
        _UpperCamelCase      :	Tuple						=       0
        _UpperCamelCase      :	str						=       1
        return Image.fromarray((array * 255).astype(np.uinta	)	)












"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


lowerCamelCase__				  =   {
    "configuration_owlvit": [
        "OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "OwlViTConfig",
        "OwlViTOnnxConfig",
        "OwlViTTextConfig",
        "OwlViTVisionConfig",
    ],
    "processing_owlvit": ["OwlViTProcessor"],
}


try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowerCamelCase__				  =   ["OwlViTFeatureExtractor"]
  lowerCamelCase__				  =   ["OwlViTImageProcessor"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowerCamelCase__				  =   [
      "OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "OwlViTModel",
      "OwlViTPreTrainedModel",
      "OwlViTTextModel",
      "OwlViTVisionModel",
      "OwlViTForObjectDetection",
  ]

if TYPE_CHECKING:
  from .configuration_owlvit import (
      OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
      OwlViTConfig,
      OwlViTOnnxConfig,
      OwlViTTextConfig,
      OwlViTVisionConfig,
  )
  from .processing_owlvit import OwlViTProcessor

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_owlvit import OwlViTFeatureExtractor
    from .image_processing_owlvit import OwlViTImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_owlvit import (
        OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
        OwlViTForObjectDetection,
        OwlViTModel,
        OwlViTPreTrainedModel,
        OwlViTTextModel,
        OwlViTVisionModel,
    )

else:
  import sys

  lowerCamelCase__				  =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
