


'''simple docstring'''

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

_UpperCAmelCase				: Dict										=				logging.get_logger(__name__)


@add_end_docstrings(__A      )
class       a__				( __A      ):







				"""simple docstring"""



				def __init__(self       ,			**__lowercase		):
										super().__init__(**__UpperCamelCase		)

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

										requires_backends(self       ,			'''vision'''		)
										self.check_model_type(__UpperCamelCase		)




				def __call__(self       ,			__lowercase       ,			__lowercase = None       ,			**__lowercase       ,			):
										if "text_queries" in kwargs:
																__lowerCAmelCase							       =				kwargs.pop('''text_queries'''		)

										if isinstance(__UpperCamelCase       ,			(str, Image.Image)		):
																__lowerCAmelCase							       =				{'''image''': image, '''candidate_labels''': candidate_labels}
										else:
																__lowerCAmelCase							       =				image
										__lowerCAmelCase							       =				super().__call__(__UpperCamelCase       ,			**__UpperCamelCase		)
										return results




				def 					_snake_case  (self       ,			**__lowercase		):
										__lowerCAmelCase							       =				{}
										if "threshold" in kwargs:
																__lowerCAmelCase							       =				kwargs['''threshold''']
										if "top_k" in kwargs:
																__lowerCAmelCase							       =				kwargs['''top_k''']
										return {}, {}, postprocess_params




				def 					_snake_case  (self       ,			__lowercase		):
										__lowerCAmelCase							       =				load_image(inputs['''image''']		)
										__lowerCAmelCase							       =				inputs['''candidate_labels''']
										if isinstance(__UpperCamelCase       ,			__UpperCamelCase		):
																__lowerCAmelCase							       =				candidate_labels.split(''','''		)

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




				def 					_snake_case  (self       ,			__lowercase		):
										__lowerCAmelCase							       =				model_inputs.pop('''target_size'''		)
										__lowerCAmelCase							       =				model_inputs.pop('''candidate_label'''		)
										__lowerCAmelCase							       =				model_inputs.pop('''is_last'''		)

										__lowerCAmelCase							       =				self.model(**__UpperCamelCase		)

										__lowerCAmelCase							       =				{'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs}
										return model_outputs




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase=0.1       ,			__lowercase=None		):
										__lowerCAmelCase							       =				[]
										for model_output in model_outputs:
																__lowerCAmelCase							       =				model_output['''candidate_label''']
																__lowerCAmelCase							       =				BaseModelOutput(__UpperCamelCase		)
																__lowerCAmelCase							       =				self.image_processor.post_process_object_detection(
																    outputs=__UpperCamelCase       ,			threshold=__UpperCamelCase       ,			target_sizes=model_output['''target_size''']		)[0]

																for index in outputs["scores"].nonzero():
																						__lowerCAmelCase							       =				outputs['''scores'''][index].item()
																						__lowerCAmelCase							       =				self._get_bounding_box(outputs['''boxes'''][index][0]		)

																						__lowerCAmelCase							       =				{'''score''': score, '''label''': label, '''box''': box}
																						results.append(__UpperCamelCase		)

										__lowerCAmelCase							       =				sorted(__UpperCamelCase       ,			key=lambda __lowercase		: x["score"]       ,			reverse=__UpperCamelCase		)
										if top_k:
																__lowerCAmelCase							       =				results[:top_k]

										return results




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





'''simple docstring'''

from math import asin, atan, cos, radians, sin, sqrt, tan

_UpperCAmelCase				: str							    =				6_37_81_37.0
_UpperCAmelCase				: Tuple							    =				6_35_67_52.31_42_45
_UpperCAmelCase				: Optional[Any]							    =				6_3_7_8_1_3_7

def 							__magic_name__(						lowerCamelCase,				lowerCamelCase,				lowerCamelCase,				lowerCamelCase):
						__lowerCAmelCase							       =				(AXIS_A - AXIS_B) / AXIS_A
						__lowerCAmelCase							       =				atan((1 - flattening) * tan(radians(lowerCamelCase)))
						__lowerCAmelCase							       =				atan((1 - flattening) * tan(radians(lowerCamelCase)))
						__lowerCAmelCase							       =				radians(lowerCamelCase)
						__lowerCAmelCase							       =				radians(lowerCamelCase)
						# Equation
						__lowerCAmelCase							       =				sin((phi_a - phi_a) / 2)
						__lowerCAmelCase							       =				sin((lambda_a - lambda_a) / 2)
						# Square both values
						sin_sq_phi *= sin_sq_phi
						sin_sq_lambda *= sin_sq_lambda
						__lowerCAmelCase							       =				sqrt(sin_sq_phi + (cos(lowerCamelCase) * cos(lowerCamelCase) * sin_sq_lambda))
						return 2 * RADIUS * asin(lowerCamelCase)


if __name__ == "__main__":
	import doctest

	doctest.testmod()

