





"""simple docstring"""






from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
		import torch

if is_tf_available():
		import tensorflow as tf




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					['''image_processor''']
					__lowerCAmelCase					=					'''SamImageProcessor'''







					def __init__(  self		,     _UpperCAmelCase       ):
									super().__init__(_UpperCAmelCase       )
									__a	:  List[str]         =					self.image_processor
									__a	:  int         =					-10
									__a	:  Union[str, Any]         =					self.image_processor.size['''longest_edge''']







					def __call__(  self		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase = None		,     **_UpperCAmelCase		,     ):
									__a	:  Union[str, Any]         =					self.image_processor(
									    _UpperCAmelCase		,     return_tensors=_UpperCAmelCase		,     **_UpperCAmelCase		,     )

									# pop arguments that are not used in the foward but used nevertheless
									__a	:  Tuple         =					encoding_image_processor['''original_sizes''']

									if hasattr(_UpperCAmelCase		,     '''numpy'''       ):  # Checks if Torch or TF tensor
													__a	:  List[Any]         =					original_sizes.numpy()

									__a    ,  __a    ,  __a	:  Optional[int]         =					self._check_and_preprocess_points(
									    input_points=_UpperCAmelCase		,     input_labels=_UpperCAmelCase		,     input_boxes=_UpperCAmelCase		,     )

									__a	:  List[str]         =					self._normalize_and_convert(
									    _UpperCAmelCase		,     _UpperCAmelCase		,     input_points=_UpperCAmelCase		,     input_labels=_UpperCAmelCase		,     input_boxes=_UpperCAmelCase		,     return_tensors=_UpperCAmelCase		,     )

									return encoding_image_processor







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase="pt"		,     ):
									if input_points is not None:
													if len(_UpperCAmelCase       ) != len(_UpperCAmelCase       ):
																	__a	:  List[str]         =					[
																	    self._normalize_coordinates(self.target_size		,     _UpperCAmelCase		,     original_sizes[0]       ) for point in input_points
																	]
													else:
																	__a	:  Tuple         =					[
																	    self._normalize_coordinates(self.target_size		,     _UpperCAmelCase		,     _UpperCAmelCase       )
																	    for point, original_size in zip(_UpperCAmelCase		,     _UpperCAmelCase       )
																	]
													# check that all arrays have the same shape
													if not all(point.shape == input_points[0].shape for point in input_points       ):
																	if input_labels is not None:
																					__a    ,  __a	:  List[str]         =					self._pad_points_and_labels(_UpperCAmelCase		,     _UpperCAmelCase       )

													__a	:  Optional[int]         =					np.array(_UpperCAmelCase       )

									if input_labels is not None:
													__a	:  Tuple         =					np.array(_UpperCAmelCase       )

									if input_boxes is not None:
													if len(_UpperCAmelCase       ) != len(_UpperCAmelCase       ):
																	__a	:  Union[str, Any]         =					[
																	    self._normalize_coordinates(self.target_size		,     _UpperCAmelCase		,     original_sizes[0]		,     is_bounding_box=_UpperCAmelCase       )
																	    for box in input_boxes
																	]
													else:
																	__a	:  Tuple         =					[
																	    self._normalize_coordinates(self.target_size		,     _UpperCAmelCase		,     _UpperCAmelCase		,     is_bounding_box=_UpperCAmelCase       )
																	    for box, original_size in zip(_UpperCAmelCase		,     _UpperCAmelCase       )
																	]
													__a	:  Optional[int]         =					np.array(_UpperCAmelCase       )

									if input_boxes is not None:
													if return_tensors == "pt":
																	__a	:  Any         =					torch.from_numpy(_UpperCAmelCase       )
																	# boxes batch size of 1 by default
																	__a	:  Dict         =					input_boxes.unsqueeze(1       ) if len(input_boxes.shape       ) != 3 else input_boxes
													elif return_tensors == "tf":
																	__a	:  List[str]         =					tf.convert_to_tensor(_UpperCAmelCase       )
																	# boxes batch size of 1 by default
																	__a	:  List[Any]         =					tf.expand_dims(_UpperCAmelCase		,     1       ) if len(input_boxes.shape       ) != 3 else input_boxes
													encoding_image_processor.update({'''input_boxes''': input_boxes}       )
									if input_points is not None:
													if return_tensors == "pt":
																	__a	:  Dict         =					torch.from_numpy(_UpperCAmelCase       )
																	# point batch size of 1 by default
																	__a	:  List[Any]         =					input_points.unsqueeze(1       ) if len(input_points.shape       ) != 4 else input_points
													elif return_tensors == "tf":
																	__a	:  Optional[Any]         =					tf.convert_to_tensor(_UpperCAmelCase       )
																	# point batch size of 1 by default
																	__a	:  List[Any]         =					tf.expand_dims(_UpperCAmelCase		,     1       ) if len(input_points.shape       ) != 4 else input_points
													encoding_image_processor.update({'''input_points''': input_points}       )
									if input_labels is not None:
													if return_tensors == "pt":
																	__a	:  Optional[int]         =					torch.from_numpy(_UpperCAmelCase       )
																	# point batch size of 1 by default
																	__a	:  Tuple         =					input_labels.unsqueeze(1       ) if len(input_labels.shape       ) != 3 else input_labels
													elif return_tensors == "tf":
																	__a	:  Optional[int]         =					tf.convert_to_tensor(_UpperCAmelCase       )
																	# point batch size of 1 by default
																	__a	:  List[str]         =					tf.expand_dims(_UpperCAmelCase		,     1       ) if len(input_labels.shape       ) != 3 else input_labels
													encoding_image_processor.update({'''input_labels''': input_labels}       )

									return encoding_image_processor







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Optional[Any]         =					max([point.shape[0] for point in input_points]       )
									__a	:  Any         =					[]
									for i, point in enumerate(_UpperCAmelCase       ):
													if point.shape[0] != expected_nb_points:
																	__a	:  Optional[Any]         =					np.concatenate(
																	    [point, np.zeros((expected_nb_points - point.shape[0], 2)       ) + self.point_pad_value]		,     axis=0       )
																	__a	:  List[Any]         =					np.append(input_labels[i]		,     [self.point_pad_value]       )
													processed_input_points.append(_UpperCAmelCase       )
									__a	:  List[str]         =					processed_input_points
									return input_points, input_labels







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase=False       ):
									__a    ,  __a	:  str         =					original_size
									__a    ,  __a	:  Any         =					self.image_processor._get_preprocess_shape(_UpperCAmelCase		,     longest_edge=_UpperCAmelCase       )
									__a	:  List[Any]         =					deepcopy(_UpperCAmelCase       ).astype(_UpperCAmelCase       )

									if is_bounding_box:
													__a	:  Dict         =					coords.reshape(-1		,     2		,     2       )

									__a	:  Union[str, Any]         =					coords[..., 0] * (new_w / old_w)
									__a	:  int         =					coords[..., 1] * (new_h / old_h)

									if is_bounding_box:
													__a	:  Union[str, Any]         =					coords.reshape(-1		,     4       )

									return coords







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     ):
									if input_points is not None:
													if hasattr(_UpperCAmelCase		,     '''numpy'''       ):  # Checks for TF or Torch tensor
																	__a	:  str         =					input_points.numpy().tolist()

													if not isinstance(_UpperCAmelCase		,     _UpperCAmelCase       ) or not isinstance(input_points[0]		,     _UpperCAmelCase       ):
																	raise ValueError('''Input points must be a list of list of floating points.'''       )
													__a	:  Optional[int]         =					[np.array(_UpperCAmelCase       ) for input_point in input_points]
									else:
													__a	:  Union[str, Any]         =					None

									if input_labels is not None:
													if hasattr(_UpperCAmelCase		,     '''numpy'''       ):
																	__a	:  Tuple         =					input_labels.numpy().tolist()

													if not isinstance(_UpperCAmelCase		,     _UpperCAmelCase       ) or not isinstance(input_labels[0]		,     _UpperCAmelCase       ):
																	raise ValueError('''Input labels must be a list of list integers.'''       )
													__a	:  int         =					[np.array(_UpperCAmelCase       ) for label in input_labels]
									else:
													__a	:  Tuple         =					None

									if input_boxes is not None:
													if hasattr(_UpperCAmelCase		,     '''numpy'''       ):
																	__a	:  int         =					input_boxes.numpy().tolist()

													if (
													    not isinstance(_UpperCAmelCase		,     _UpperCAmelCase       )
													    or not isinstance(input_boxes[0]		,     _UpperCAmelCase       )
													    or not isinstance(input_boxes[0][0]		,     _UpperCAmelCase       )
													):
																	raise ValueError('''Input boxes must be a list of list of list of floating points.'''       )
													__a	:  Dict         =					[np.array(_UpperCAmelCase       ).astype(np.floataa       ) for box in input_boxes]
									else:
													__a	:  Tuple         =					None

									return input_points, input_labels, input_boxes







					@property
					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.image_processor.model_input_names
									return list(dict.fromkeys(_UpperCAmelCase       )       )







					def 	_lowerCamelCase						(  self		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									return self.image_processor.post_process_masks(*_UpperCAmelCase		,     **_UpperCAmelCase       )






"""simple docstring"""






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(
					    '''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.'''
					)
					__lowerCAmelCase					=					'''CIDAS/clipseg-rd64-refined'''
					__lowerCAmelCase					=					'''image_segmenter'''
					__lowerCAmelCase					=					CLIPSegForImageSegmentation

					__lowerCAmelCase					=					['''image''', '''text''']
					__lowerCAmelCase					=					['''image''']







					def __init__(  self		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									requires_backends(self		,     ['''vision''']       )
									super().__init__(*_UpperCAmelCase		,     **_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									return self.pre_processor(text=[label]		,     images=[image]		,     padding=_UpperCAmelCase		,     return_tensors='''pt'''       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									with torch.no_grad():
													__a	:  List[str]         =					self.model(**_UpperCAmelCase       ).logits
									return logits







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									__a	:  str         =					outputs.cpu().detach().numpy()
									__a	:  int         =					0
									__a	:  Optional[int]         =					1
									return Image.fromarray((array * 255).astype(np.uinta       )       )