


import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
    from PIL import Image
else:


    class     lowercase__  :






      @staticmethod
      def 			_UpperCAmelCase  (					*_lowercase      :					Any		,						**_lowercase      :					Dict ):




             """simple docstring"""







             pass


@is_pipeline_test
@require_vision
@require_torch
class     lowercase__  (					unittest.TestCase		):
  A__=  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






  def 			_UpperCAmelCase  (					self      :					int		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					Dict		,						_lowercase      :					Dict ):




         """simple docstring"""







         UpperCAmelCase__										=     pipeline(
             "zero-shot-object-detection"		,						model="hf-internal-testing/tiny-random-owlvit-object-detection" )

         UpperCAmelCase__										=     [
             {
                 "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                 "candidate_labels": ["cat", "remote", "couch"],
             }
         ]
         return object_detector, examples






  def 			_UpperCAmelCase  (					self      :					Any		,						_lowercase      :					Tuple		,						_lowercase      :					str ):




         """simple docstring"""







         UpperCAmelCase__										=     object_detector(examples[0]		,						threshold=0.0 )

         UpperCAmelCase__										=     len(_lowercase )
         self.assertGreater(_lowercase		,						0 )
         self.assertEqual(
             _lowercase		,						[
                 {
                     "score": ANY(_lowercase ),
                     "label": ANY(_lowercase ),
                     "box": {"xmin": ANY(_lowercase ), "ymin": ANY(_lowercase ), "xmax": ANY(_lowercase ), "ymax": ANY(_lowercase )},
                 }
                 for i in range(_lowercase )
             ]		,						)






  @require_tf
  @unittest.skip("Zero Shot Object Detection not implemented in TF" )
  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         pass






  @require_torch
  def 			_UpperCAmelCase  (					self      :					Union[str, Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     pipeline(
             "zero-shot-object-detection"		,						model="hf-internal-testing/tiny-random-owlvit-object-detection" )

         UpperCAmelCase__										=     object_detector(
             "./tests/fixtures/tests_samples/COCO/000000039769.png"		,						candidate_labels=["cat", "remote", "couch"]		,						threshold=0.6_4		,						)

         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4 )		,						[
                 {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                 {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                 {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                 {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                 {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                 {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                 {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
                 {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}},
                 {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
             ]		,						)

         UpperCAmelCase__										=     object_detector(
             [
                 {
                     "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                     "candidate_labels": ["cat", "remote", "couch"],
                 }
             ]		,						threshold=0.6_4		,						)

         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4 )		,						[
                 [
                     {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                     {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                     {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_04, "ymin": 1_67, "xmax": 2_32, "ymax": 1_90}},
                     {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                     {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                     {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_71, "ymin": 83, "xmax": 5_98, "ymax": 1_03}},
                     {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
                     {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 2_74, "xmax": 93, "ymax": 2_97}},
                     {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_94, "ymin": 1_05, "xmax": 5_21, "ymax": 1_27}},
                 ]
             ]		,						)






  @require_torch
  @slow
  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         UpperCAmelCase__										=     pipeline("zero-shot-object-detection" )

         UpperCAmelCase__										=     object_detector(
             "http://images.cocodataset.org/val2017/000000039769.jpg"		,						candidate_labels=["cat", "remote", "couch"]		,						)
         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4 )		,						[
                 {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                 {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                 {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                 {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                 {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
             ]		,						)

         UpperCAmelCase__										=     object_detector(
             [
                 {
                     "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                     "candidate_labels": ["cat", "remote", "couch"],
                 },
                 {
                     "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                     "candidate_labels": ["cat", "remote", "couch"],
                 },
             ]		,						)
         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4 )		,						[
                 [
                     {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                     {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                     {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                     {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                     {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
                 ],
                 [
                     {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                     {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                     {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
                     {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_35, "ymin": 74, "xmax": 3_71, "ymax": 1_87}},
                     {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_42, "ymax": 4_76}},
                 ],
             ]		,						)






  @require_tf
  @unittest.skip("Zero Shot Object Detection not implemented in TF" )
  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         pass






  @require_torch
  @slow
  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     0.2
         UpperCAmelCase__										=     pipeline("zero-shot-object-detection" )

         UpperCAmelCase__										=     object_detector(
             "http://images.cocodataset.org/val2017/000000039769.jpg"		,						candidate_labels=["cat", "remote", "couch"]		,						threshold=_lowercase		,						)
         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4 )		,						[
                 {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                 {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
                 {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 3_15, "ymax": 4_72}},
             ]		,						)






  @require_torch
  @slow
  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         UpperCAmelCase__										=     2
         UpperCAmelCase__										=     pipeline("zero-shot-object-detection" )

         UpperCAmelCase__										=     object_detector(
             "http://images.cocodataset.org/val2017/000000039769.jpg"		,						candidate_labels=["cat", "remote", "couch"]		,						top_k=_lowercase		,						)
         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4 )		,						[
                 {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_24, "ymin": 20, "xmax": 6_40, "ymax": 3_73}},
                 {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 1_77, "ymax": 1_15}},
             ]		,						)









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

from .haversine_distance import haversine_distance

A      = 6378137.0
A      = 6356752.314245
A      = 637_8137





def __UpperCAmelCase       (		__A ,						__A ,						__A ,						__A			)						->					float:



       '''simple docstring'''





       UpperCAmelCase__										=     (AXIS_A - AXIS_B) / AXIS_A
       # Parametric latitudes
       # https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude
       UpperCAmelCase__										=     atan((1 - flattening) * tan(radians(__A			)			)			)
       UpperCAmelCase__										=     atan((1 - flattening) * tan(radians(__A			)			)			)

       # Compute central angle between two points
       # using haversine theta. sigma =  haversine_distance / equatorial radius
       UpperCAmelCase__										=     haversine_distance(__A ,						__A ,						__A ,						__A			) / EQUATORIAL_RADIUS

       # Intermediate P and Q values
       UpperCAmelCase__										=     (b_lata + b_lata) / 2
       UpperCAmelCase__										=     (b_lata - b_lata) / 2

       # Intermediate X value
       # X = (sigma - sin(sigma)) * sin^2Pcos^2Q / cos^2(sigma/2)
       UpperCAmelCase__										=     (sin(__A			) ** 2) * (cos(__A			) ** 2)
       UpperCAmelCase__										=     cos(sigma / 2			) ** 2
       UpperCAmelCase__										=     (sigma - sin(__A			)) * (x_numerator / x_demonimator)

       # Intermediate Y value
       # Y = (sigma + sin(sigma)) * cos^2Psin^2Q / sin^2(sigma/2)
       UpperCAmelCase__										=     (cos(__A			) ** 2) * (sin(__A			) ** 2)
       UpperCAmelCase__										=     sin(sigma / 2			) ** 2
       UpperCAmelCase__										=     (sigma + sin(__A			)) * (y_numerator / y_denominator)

       return EQUATORIAL_RADIUS * (sigma - ((flattening / 2) * (x_value + y_value)))


if __name__ == "__main__":
    import doctest

    doctest.testmod()





