
    ^j8                      d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZ d dlZd dlm	Z d dlmZmZ d dlmZmZ d d	lmZ d d
lmZmZmZ d dlmZmZ d dlm Z m!Z!m"Z" d dl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d dl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB d dlCmDZD d dlEmFZFmGZG d dlHmIZImJZJ e G d d             ZK	 	 	 	 d"dZLd#dZM edd      	 	 	 	 	 	 d$d       ZN edd      de,j                  f	 	 	 	 	 	 	 d%d       ZP edd      	 	 	 	 d#d       ZQ	 	 	 	 	 	 d&dZR eeRdd       	 	 	 	 	 	 d&d!       ZSy)'    )annotations)Iterator)	dataclassfield)reduce)AnycastN)
deprecatedvoid)CLASS_NAME_DATA_FIELDORIENTED_BOX_COORDINATES)CompactMask)%process_transformers_detection_result+process_transformers_v4_segmentation_result+process_transformers_v5_segmentation_result)obb_polygon_areaxyxyxyxy_to_xyxy)mask_to_xyxypolygon_to_maskxywh_to_xyxy)extract_ultralytics_masksget_data_itemis_data_equalis_metadata_equal
merge_datamerge_metadataprocess_roboflow_result)	OverlapMetricbox_iou_batchbox_non_max_mergebox_non_max_suppressionmask_iou_batchmask_non_max_mergemask_non_max_suppressionoriented_box_non_max_merge oriented_box_non_max_suppression)calculate_masks_centroids)LMMVLM_validate_vlm_parametersfrom_deepseek_vl_2from_florence_2from_google_gemini_2_0from_google_gemini_2_5from_moondreamfrom_paligemmafrom_qwen_2_5_vlfrom_qwen_3_vl)Position)get_instance_variableswarn_deprecated)_validate_detections_fields_validate_resolutionc                     e Zd ZU dZded<   dZded<   dZded<   dZded	<   dZded
<    e	e
      Zded<    e	e
      Zded<   d2dZd3dZ	 	 d4dZd5dZed6d       Zed7d       Zed8d       Ze	 	 	 	 	 	 d9d       Zed:d       Zed;d       Ze	 d<	 	 	 	 	 d=d       Zed>d       Zed?d       Zed@d       Ze	 	 	 	 	 	 dAd       Ze	 d<	 	 	 	 	 dBd       ZedCd        Ze	 	 	 	 	 	 	 	 dDd!       Ze	 	 	 	 	 	 	 	 dEd"       Z edFd#       Z!edGd$       Z"edHd%       Z#dId&Z$edJd'       Z%dKd(Z&	 	 	 	 dLd)Z'dMd*Z(e)dNd+       Z*e)dNd,       Z+e)dNd-       Z,d.d/e-j\                  f	 	 	 	 	 	 	 dOd0Z/d.d/e-j\                  f	 	 	 	 	 	 	 dOd1Z0y)P
Detectionsa"  
    The `sv.Detections` class in the Supervision library standardizes results from
    various object detection and segmentation models into a consistent format. This
    class simplifies data manipulation and filtering, providing a uniform API for
    integration with Supervision [trackers](/trackers/), [annotators](/latest/detection/annotators/), and [tools](/detection/tools/line_zone/).

    === "Inference"

        Use [`sv.Detections.from_inference`](/detection/core/#supervision.detection.core.Detections.from_inference)
        method, which accepts model results from both detection and segmentation models.

        ```python
        import cv2
        import supervision as sv
        from inference import get_model

        model = get_model(model_id="yolov8n-640")
        image = cv2.imread("<SOURCE_IMAGE_PATH>")
        results = model.infer(image)[0]
        detections = sv.Detections.from_inference(results)
        ```

    === "Ultralytics"

        Use [`sv.Detections.from_ultralytics`](/detection/core/#supervision.detection.core.Detections.from_ultralytics)
        method, which accepts model results from both detection and segmentation models.

        ```python
        import cv2
        import supervision as sv
        from ultralytics import YOLO

        model = YOLO("yolov8n.pt")
        image = cv2.imread("<SOURCE_IMAGE_PATH>")
        results = model(image)[0]
        detections = sv.Detections.from_ultralytics(results)
        ```

    === "Transformers"

        Use [`sv.Detections.from_transformers`](/detection/core/#supervision.detection.core.Detections.from_transformers)
        method, which accepts model results from both detection and segmentation models.

        ```python
        import torch
        import supervision as sv
        from PIL import Image
        from transformers import DetrImageProcessor, DetrForObjectDetection

        processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
        model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")

        image = Image.open("<SOURCE_IMAGE_PATH>")
        inputs = processor(images=image, return_tensors="pt")

        with torch.no_grad():
            outputs = model(**inputs)

        width, height = image.size
        target_size = torch.tensor([[height, width]])
        results = processor.post_process_object_detection(
            outputs=outputs, target_sizes=target_size)[0]
        detections = sv.Detections.from_transformers(
            transformers_results=results,
            id2label=model.config.id2label)
        ```

    Attributes:
        xyxy: An array of shape `(n, 4)` containing
            the bounding boxes coordinates in format `[x1, y1, x2, y2]`
        mask: An array of shape `(n, H, W)` containing the segmentation masks
            (`bool` data type), or `None` when masks are not available, or as
            :class:`~supervision.detection.compact_mask.CompactMask`.
        confidence: An array of shape `(n,)` containing the confidence scores
            of the detections, or `None` when confidence values are not available.
        class_id: An array of shape `(n,)` containing the class ids of the
            detections, or `None` when class ids are not available.
        tracker_id: An array of shape `(n,)` containing the tracker ids of the
            detections, or `None` when tracker ids are not available.
        data: A dictionary containing additional
            data where each key is a string representing the data type, and the value
            is either a NumPy array or a list of corresponding data.
        metadata: A dictionary containing collection-level metadata
            that applies to the entire set of detections. This may include information such
            as the video name, camera parameters, timestamp, or other global metadata.
    npt.NDArray[np.generic]xyxyN,npt.NDArray[np.generic] | CompactMask | Nonemaskznpt.NDArray[np.generic] | None
confidenceclass_id
tracker_id)default_factoryz.dict[str, npt.NDArray[np.generic] | list[Any]]datadict[str, Any]metadatac                    t        | j                  | j                  | j                  | j                  | j
                  | j                         y )Nr;   r=   r>   r?   r@   rB   )r6   r;   r=   r>   r?   r@   rB   selfs    e/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/detection/core.py__post_init__zDetections.__post_init__   s4    #]]	
    c                ,    t        | j                        S )zL
        Returns the number of detections in the Detections object.
        lenr;   rG   s    rI   __len__zDetections.__len__   s     499~rK   c           
   #    K   t        t        | j                              D ]  }| j                  |   | j                  | j                  |   nd| j                  | j                  |   nd| j
                  | j
                  |   nd| j                  | j                  |   ndt        | j                  |      f  yw)z
        Iterates over the Detections object and yield a tuple of
        `(xyxy, mask, confidence, class_id, tracker_id, data)` for each detection.
        N)	rangerN   r;   r=   r>   r?   r@   r   rB   )rH   is     rI   __iter__zDetections.__iter__   s       s499~& 	A		! $		 5		!4&*oo&A"t$(MM$=a 4&*oo&A"tdii+ 	s   B<B>c                Z   t        |t              st        S t        t	        j
                  | j                  |j                        t	        j
                  | j                  |j                        t	        j
                  | j                  |j                        t	        j
                  | j                  |j                        t	        j
                  | j                  |j                        t        | j                  |j                        t        | j                  |j                        g      S N)
isinstancer9   NotImplementedallnparray_equalr;   r=   r?   r>   r@   r   rB   r   rD   )rH   others     rI   __eq__zDetections.__eq__   s    %,!!tyy%**5tyy%**5t}}enn=t0@0@At0@0@Adii4!$--@

 
	
rK   c                    |j                   d   j                         j                         j                         } | |ddddf   |dddf   |dddf   j                  t                    S )as  
        Creates a Detections instance from a
        [YOLOv5](https://github.com/ultralytics/yolov5) inference result.

        Args:
            yolov5_results: The output Detections instance from YOLOv5.

        Returns:
            A new Detections object.

        Example:
            ```python
            import cv2
            import torch
            import supervision as sv

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
            result = model(image)
            detections = sv.Detections.from_yolov5(result)
            ```
        r   N      r;   r>   r?   )predcpunumpyastypeint)clsyolov5_resultsyolov5_detections_predictionss      rI   from_yolov5zDetections.from_yolov5   sp    0 )7(;(;A(>(B(B(D(H(H(J(P(P(R%.q"1"u54QT:21a48??D
 	
rK   c           
        t        |d      r|j                  v|j                  j                  j                         j	                         j                  t              }t        j                  |D cg c]  }|j                  |    c}      }|j                  j                  j                         j	                         } | |j                  j                  j                         j	                         |j                  j                  j                         j	                         ||j                  j                  @|j                  j                  j                         j                         j	                         ndt        |t        |i      S t        |d      rG|j                   ;t#        |      } | t%        |      |t        j&                  t)        |                  S t        |d      rU|j                   H|j                   j                  j                         j	                         j                  t              }t        j                  |D cg c]  }|j                  |    c}      } | |j                   j                  j                         j	                         |j                   j                  j                         j	                         |t#        |      |j                   j                  @|j                   j                  j                         j                         j	                         ndt        |i      S | j+                         }t        t        j*                  dt,              i|_        |S c c}w c c}w )	a  
        Creates a `sv.Detections` instance from a
        [YOLOv8](https://github.com/ultralytics/ultralytics) inference result.

        !!! Note

            `from_ultralytics` is compatible with
            [detection](https://docs.ultralytics.com/tasks/detect/),
            [segmentation](https://docs.ultralytics.com/tasks/segment/), and
            [OBB](https://docs.ultralytics.com/tasks/obb/) models.

        Args:
            ultralytics_results: The output Results instance from Ultralytics.

        Returns:
            A new Detections object.

        Example:
            ```python
            import cv2
            import supervision as sv
            from ultralytics import YOLO

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = YOLO('yolov8s.pt')
            results = model(image)[0]
            detections = sv.Detections.from_ultralytics(results)
            ```
        obbN)r;   r>   r?   r@   rB   boxes)r;   r=   r?   r;   r>   r?   r=   r@   rB   r   dtype)hasattrrk   rf   rb   rc   rd   re   rY   arraynamesxyxyxyxyr;   confidr   r   rl   r   r   arangerN   emptystrrB   )rf   ultralytics_resultsr?   rR   class_namesoriented_box_coordinatesmasksrw   s           rI   from_ultralyticszDetections.from_ultralytics   s   @ &.3F3J3J3V*..22668>>@GGLH(((#SQ$7$=$=a$@#STK':'>'>'G'G'K'K'M'S'S'U$(,,11557==?.2277;;=CCE! +..11= (++..22488:@@B -.F);  &05H5N5N5V-.ABE!%(3':#;<  '1#))5*004488:@@BII#NH(((#SQ$7$=$=a$@#STK(..33779??A.4499==?EEG!./BC +0033? (--00446::<BBD+[9  		+RXXas-CD
] $T< $Ts   3M>Nc                H   t        j                  |j                  j                        j                  d   dk(  r| j                         S  | |j                  j                  |j                  j                  |j                  j                  j                  t                    S )aZ  
        Creates a Detections instance from a
        [YOLO-NAS](https://github.com/Deci-AI/super-gradients/blob/master/YOLONAS.md)
        inference result.

        Args:
            yolo_nas_results: The output Results instance from YOLO-NAS.
                ImageDetectionPrediction is coming from
                'super_gradients.training.models.prediction_results'.

        Returns:
            A new Detections object.

        Example:
            ```python
            import cv2
            from super_gradients.training import models
            import supervision as sv

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = models.get('yolo_nas_l', pretrained_weights="coco")

            result = list(model.predict(image, conf=0.35))[0]
            detections = sv.Detections.from_yolo_nas(result)
            ```
        r   r`   )
rY   asarray
predictionbboxes_xyxyshaperw   r>   labelsrd   re   )rf   yolo_nas_resultss     rI   from_yolo_naszDetections.from_yolo_nasO  s~    8 ::&11==>DDQG1L99;!,,88'22==%0077>>sC
 	
rK   c                (   |d   d   j                         }|ddddgfxx   |d   z  cc<   |ddddgfxx   |d   z  cc<   |ddg df   } | ||d   d   j                         |d	   d   j                         j                  t              
      S )a7  
        Creates a Detections instance from a
        [Tensorflow Hub](https://www.tensorflow.org/hub/tutorials/tf2_object_detection)
        inference result.

        Args:
            tensorflow_results: The output results from Tensorflow Hub.
            resolution_wh: The input image resolution as `(width, height)`.
                Bounding boxes from Tensorflow are normalized and are scaled
                to absolute coordinates using this resolution.

        Returns:
            A new Detections object.

        Example:
            ```python
            import tensorflow as tf
            import tensorflow_hub as hub
            import numpy as np
            import cv2

            module_handle = "https://tfhub.dev/tensorflow/centernet/hourglass_512x512_kpts/1"
            model = hub.load(module_handle)
            img = np.array(cv2.imread(SOURCE_IMAGE_PATH))
            result = model(img)
            detections = sv.Detections.from_tensorflow(
                result, resolution_wh=(img.shape[1], img.shape[0])
            )
            ```
        detection_boxesr   N         )r   r   r   r   detection_scoresdetection_classesr`   )rc   rd   re   )rf   tensorflow_resultsresolution_whrl   s       rI   from_tensorflowzDetections.from_tensorflowt  s    F ##45a8>>@a!QiM!,,a!QiM!,,ao&)*<=a@FFH'(;<Q?EEGNNsS
 	
rK   c                   t        j                  |j                  d         j                  d   dk(  r| j	                         S  | t        j
                  |j                  d         t        j
                  |j                  d         t        j
                  |j                  d         j                  t              j                  t                    S )a  
        Creates a Detections instance from a
        [DeepSparse](https://github.com/neuralmagic/deepsparse)
        inference result.

        Args:
            deepsparse_results: The output Results instance from DeepSparse.

        Returns:
            A new Detections object.

        Example:
            ```python
            import supervision as sv
            from deepsparse import Pipeline

            yolo_pipeline = Pipeline.create(
                task="yolo",
                model_path = "zoo:cv/detection/yolov5-l/pytorch/ultralytics/coco/pruned80_quant-none"
             )
            result = yolo_pipeline(<SOURCE IMAGE PATH>)
            detections = sv.Detections.from_deepsparse(result)
            ```
        r   r`   )rY   r   rl   r   rw   rq   scoresr   rd   floatre   )rf   deepsparse_resultss     rI   from_deepsparsezDetections.from_deepsparse  s    6 ::(..q1288;q@99;,22156xx 2 9 9! <=XX077:;BB5IPPQTU
 	
rK   c                    | |j                   j                  j                         j                         |j                   j                  j                         j                         |j                   j
                  j                         j                         j                  t              d|j                   v r7|j                   j                  j                         j                               S d      S )a  
        Creates a Detections instance from a
        [mmdetection](https://github.com/open-mmlab/mmdetection) and
        [mmyolo](https://github.com/open-mmlab/mmyolo) inference result.

        Args:
            mmdet_results: The output Results instance from MMDetection.

        Returns:
            A new Detections object.

        Example:
            ```python
            import cv2
            import supervision as sv
            from mmdet.apis import init_detector, inference_detector

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = init_detector("<CONFIG_PATH>", "<WEIGHTS_PATH>", device="<DEVICE>")

            result = inference_detector(model, image)
            detections = sv.Detections.from_mmdetection(result)
            ```
        r|   N)r;   r>   r?   r=   )	pred_instancesbboxesrb   rc   r   r   rd   re   r|   )rf   mmdet_resultss     rI   from_mmdetectionzDetections.from_mmdetection  s    6 --4488:@@B$33::>>@FFH"1188<<>DDFMMcR m::: ,,22668>>@	
 		
 	
 		
rK   c                    |j                   j                  dk(  sd|v r | di t        ||      S d|v sd|v r | di t        ||      S d|v r | di t	        ||      S t        d      )aA  
        Creates a Detections instance from object detection or panoptic, semantic
        and instance segmentation
        [Transformer](https://github.com/huggingface/transformers) inference result.

        Args:
            transformers_results: Inference results from your Transformers model.
                This can be either a dictionary containing valuable outputs like
                `scores`, `labels`, `boxes`, `masks`, `segments_info`, and
                `segmentation`, or a `torch.Tensor` holding a segmentation map
                where values represent class IDs.
            id2label: A dictionary mapping class IDs to labels, typically part of
                the `transformers` model configuration. If provided, the resulting
                dictionary will include class names.

        Returns:
            A new Detections object.

        Example:
            ```python
            import torch
            import supervision as sv
            from PIL import Image
            from transformers import DetrImageProcessor, DetrForObjectDetection

            processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
            model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")

            image = Image.open("<SOURCE_IMAGE_PATH>")
            inputs = processor(images=image, return_tensors="pt")

            with torch.no_grad():
                outputs = model(**inputs)

            width, height = image.size
            target_size = torch.tensor([[height, width]])
            results = processor.post_process_object_detection(
                outputs=outputs, target_sizes=target_size)[0]

            detections = sv.Detections.from_transformers(
                transformers_results=results,
                id2label=model.config.id2label
            )
            ```
        Tensorsegmentationr|   
png_stringrl   zThe provided Transformers results do not contain any valid fields. Expected fields are 'boxes', 'masks', 'segments_info' or 'segmentation'. )	__class____name__r   r   r   
ValueError)rf   transformers_resultsid2labels      rI   from_transformerszDetections.from_transformers  s    j !**33x?!55 =((  **l>R.R =((  ** 78LhW 
 # rK   c                    | |d   j                   j                  j                         j                         |d   j                  j                         j                         t        |d   d      r+|d   j                  j                         j                         nd|d   j                  j                         j                         j                  t                    S )a  
        Create a Detections object from the
        [Detectron2](https://github.com/facebookresearch/detectron2) inference result.

        Args:
            detectron2_results: The output of a
                Detectron2 model containing instances with prediction data.

        Returns:
            A Detections object containing the bounding boxes,
                class IDs, and confidences of the predictions.

        Example:
            ```python
            import cv2
            import supervision as sv
            from detectron2.engine import DefaultPredictor
            from detectron2.config import get_cfg


            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            cfg = get_cfg()
            cfg.merge_from_file("<CONFIG_PATH>")
            cfg.MODEL.WEIGHTS = "<WEIGHTS_PATH>"
            predictor = DefaultPredictor(cfg)

            result = predictor(image)
            detections = sv.Detections.from_detectron2(result)
            ```
        	instances
pred_masksN)r;   r>   r=   r?   )

pred_boxestensorrb   rc   r   rp   r   pred_classesrd   re   )rf   detectron2_resultss     rI   from_detectron2zDetections.from_detectron2<  s    B #K0;;BBFFHNNP)+6==AACIIK -k:LI #;/::>>@FFH'4\##%UWVC[
 	
rK   c                8   t        |d      r|j                  dd      }nt        |d      r|j                         }t        |      \  }}}}}}t	        j
                  |      j                  d   dk(  r| j                         }||_        |S  | ||||||      S )a  
        Create a `sv.Detections` object from the [Roboflow](https://roboflow.com/)
        API inference result or the [Inference](https://inference.roboflow.com/)
        package results. This method extracts bounding boxes, class IDs,
        confidences, and class names from the Roboflow API result and encapsulates
        them into a Detections object.

        Args:
            roboflow_result: The result from the
                Roboflow API or Inference package containing predictions.

        Returns:
            A Detections object containing the bounding boxes, class IDs,
                and confidences of the predictions.
                `detections.data["class_name"]` is always present as a
                string-dtype NumPy array aligned with the detections; it is
                empty (shape `(0,)`, dtype str) when `predictions` is empty
                or absent.

        Example:
            ```python
            import cv2
            import supervision as sv
            from inference import get_model

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = get_model(model_id="yolov8s-640")

            result = model.infer(image)[0]
            detections = sv.Detections.from_inference(result)
            ```
        dictT)exclude_noneby_aliasjson)roboflow_resultr   rm   )	rp   r   r   r   rY   r   r   rw   rB   )	rf   r   r;   r>   r?   r|   trackersrB   empty_detections	            rI   from_inferencezDetections.from_inferencek  s    D ?F+-22t2TO_f--224O<S+=
9j(E8T ::d!!!$)!iikO#'O ""!
 	
rK   c                b   t        |d d      }t        j                  |D cg c]  }|d   	 c}      }t        j                  |D cg c]  }|d   	 c}      }t        j                  |      j                  d   dk(  r| j                         S t        |      } | ||      S c c}w c c}w )	aS  
        Creates a Detections instance from
        [Segment Anything Model](https://github.com/facebookresearch/segment-anything)
        inference result.

        Args:
            sam_result: The output Results instance from SAM.

        Returns:
            A new Detections object.

        Example:
            ```python
            import supervision as sv
            from segment_anything import (
                sam_model_registry,
                SamAutomaticMaskGenerator
             )

            sam_model_reg = sam_model_registry[MODEL_TYPE]
            sam = sam_model_reg(checkpoint=CHECKPOINT_PATH).to(device=DEVICE)
            mask_generator = SamAutomaticMaskGenerator(sam)
            sam_result = mask_generator.generate(IMAGE)
            detections = sv.Detections.from_sam(sam_result=sam_result)
            ```
        c                    | d   S )Narear   )xs    rI   <lambda>z%Detections.from_sam.<locals>.<lambda>  s
    ai rK   T)keyreversebboxr   r   )xywh)r;   r=   )sortedrY   rq   r   r   rw   r   )rf   
sam_resultsorted_generated_masksr=   r   r;   s         rI   from_samzDetections.from_sam  s    : "(/"
 xx2HI$fIJxx:PQ$n-QR::d!!!$)99;&4(( JQs   B'	B,c           	     d   t        |      \  }}g }g }g }t        |t              r"|j                  dg       }|s9d|v r5|d   ddg}n+t	        |dg       }|st        |d      rt	        |d      ddg}t        |      D ]  \  }	}
t        |
t              r%|
j                  dg       }|
j                  d|	      }nt	        |
dg       }t	        |
d|	      }|D ]=  }t        |t              r>|j                  d      }|r|dk7  r-|j                  dg       }|j                  d	d
      }n/t	        |dd      }|r|dk7  rgt	        |dg       }t	        |d	d
      }|st        j                  ||ft              }|D ]e  }t        j                  |t        j                        }t        |||f      }|j                  t        d      }t        j                  |||       g |j                  |       |j                  |       |j                  |       @  |s| j!                         S t        j"                  |d      }t%        |      } | |j                  t        j&                        |t        j                  |t        j&                        t        j                  |t(                    S )aX  
        Creates a Detections instance from
        [SAM 3](https://github.com/facebookresearch/sam3) inference result.
        Supports both PVS and PCS SAM3 segmentation formats.

        Args:
            sam3_result: The output result from SAM 3 inference, either
                Sam3PromptResult from inference package or dict containing
                prompt_results with polygon predictions.
            resolution_wh: The width and height of the image used for mask
                generation.

        Returns:
            A new Detections object. The `class_id` field contains the prompt
                index for each polygon.

        Example:
            ```python
            import cv2
            import supervision as sv
            from inference.models.sam3 import SegmentAnything3
            from inference.core.entities.requests.sam3 import Sam3Prompt

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = SegmentAnything3(
                model_id="sam3/sam3_final",
                api_key="<ROBOFLOW_API_KEY>"
            )

            prompts = [
                Sam3Prompt(type="text", text="car"),
                Sam3Prompt(type="text", text="tire"),
            ]

            result = model.segment_image(
                image=image,
                prompts=prompts,
                output_prob_thresh=0.5,
                format="polygon"
            )

            height, width = image.shape[:2]
            detections = sv.Detections.from_sam3(
                sam3_result=result,
                resolution_wh=(width, height)
            )
            ```
        prompt_resultspredictionsr   )r   prompt_indexr   formatpolygonr|   r>   g      ?Nrn   )r   r   F)copy)outaxis)r;   r=   r>   r?   )r7   rV   r   getgetattrrp   	enumeraterY   zerosboolrq   int32r   rd   
logical_orappendrw   stackr   float32re   )rf   sam3_resultr   widthheightr|   confidences	class_idsr   rR   prompt_resultr   r   r   prediction_formatr   r>   	full_maskpolyr   r=   masks_npr;   s                          rI   	from_sam3zDetections.from_sam3  s   h -];v	k4((__-=rBN!m{&B$/$>PQR" %[2BBGN!gk=&I (/{M'J()" !*. 9 $	/A}-.+//rB,00C%m]BG&}naH) /
j$/(2x(@%(->)-K !+!<J!+c!BJ(/
Hd(K%(->)-K !(Wb!AJ!(\3!GJ!3588VUOSW3X	& BD hht288<G* 'vD  ;;t%;8DMM)TyAB Y'"":.  .9/$	/L 99;88E*H%RZZ(xx2::>XXis3	
 	
rK   c                   d|v rt        d|d   d          g g g }}}|du }|i }|j                         D ci c]  \  }}||
 }	}}|d   d   D ]  }
|
d   }|
d   }|d	   }|d
   }||d   z   }||d   z   }|D ]l  }|d   }|d   }|	j                  |d      }|r|t        |	      }||	|<   |6|j	                  ||||g       |j	                  |       |j	                  |       n  t        |      dk(  rt
        j                         S  | t        j                  |      t        j                  |      t        j                  |            S c c}}w )a  
        Creates a Detections instance from [Azure Image Analysis 4.0](
        https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/
        concept-object-detection-40).

        Args:
            azure_result: The result from Azure Image Analysis. It should
                contain detected objects and their bounding box coordinates.
            class_map: A mapping of class IDs to class names. If None, a new
                mapping is created dynamically.

        Returns:
            A new Detections object.

        Example:
            ```python
            import requests
            import supervision as sv

            image = open(input, "rb").read()

            endpoint = "https://.cognitiveservices.azure.com/"
            subscription_key = ""

            headers = {
                "Content-Type": "application/octet-stream",
                "Ocp-Apim-Subscription-Key": subscription_key
             }

            response = requests.post(endpoint,
                headers=self.headers,
                data=image
             ).json()

            detections = sv.Detections.from_azure_analyze_image(response)
            ```
        errorzAzure API returned an error messageNobjectsResultvaluesboundingBoxtagsr   ywhr>   namer   )r;   r?   r>   )	r   itemsr   rN   r   r9   rw   rY   rq   )rf   azure_result	class_mapr;   r   r   is_dynamic_mappingr   valueinverted_map	detectionr   r   x0y0x1y1tagr>   
class_nameclass_id_vals                        rI   from_azure_analyze_imagez#Detections.from_azure_analyze_imageJ  s   R l".|G/DY/O.PQ  (*2r9k&$.IEN__EV'WzsEs
'W'W%o6x@ 	3I]+DV$DcBcBd3iBd3iB 3 .
"%f+
+7+;+;J+M%,*>#&|#4L/;L,+KKRR 01&&z2$$\23	30 t9>##%%$XXi(xx,
 	
; (Xs   Ec                    t        j                  |d   ddddf         j                  d   dk(  r| j                         S  | |d   ddddf   |d   dddf   |d   dddf   j	                  t
                    S )aG  
        Creates a Detections instance from
            [PaddleDetection](https://github.com/PaddlePaddle/PaddleDetection)
            inference result.

        Args:
            paddledet_result: The output Results instance from PaddleDet.

        Returns:
            A new Detections object.

        Example:
            ```python
            import supervision as sv
            import paddle
            from ppdet.engine import Trainer
            from ppdet.core.workspace import load_config

            weights = ()
            config = ()

            cfg = load_config(config)
            trainer = Trainer(cfg, mode='test')
            trainer.load_weights(weights)

            paddledet_result = trainer.predict([images])[0]

            detections = sv.Detections.from_paddledet(paddledet_result)
            ```
        r   Nr      r   r   r`   )rY   r   r   rw   rd   re   )rf   paddledet_results     rI   from_paddledetzDetections.from_paddledet  s    B ::&v.q!A#v67==a@AE99;!&)!QqS&1'/15%f-ad3::3?
 	
rK   c                   t        d       t        j                  t        j                  t        j                  t        j                  t        j
                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  i}t        |t              r||   }nHt        |t              r 	 t        |j                               }||   }nt        dt        |       d       | j                  d||d|S # t        $ r4 t        d| dt        D cg c]  }|j                   nc c}w c}       w xY w)u1A  
        !!! deprecated "Deprecated"
            `Detections.from_lmm` is **deprecated** and will be removed in `supervision-0.31.0`.
            Please use `Detections.from_vlm` instead.

        Creates a Detections object from the given result string based on the specified
        Large Multimodal Model (LMM).

        | Name                | Enum (sv.LMM)        | Tasks                   | Required parameters         | Optional parameters |
        |---------------------|----------------------|-------------------------|-----------------------------|---------------------|
        | PaliGemma           | `PALIGEMMA`          | detection               | `resolution_wh`             | `classes`           |
        | PaliGemma 2         | `PALIGEMMA`          | detection               | `resolution_wh`             | `classes`           |
        | Qwen2.5-VL          | `QWEN_2_5_VL`        | detection               | `resolution_wh`, `input_wh` | `classes`           |
        | Google Gemini 2.0   | `GOOGLE_GEMINI_2_0`  | detection               | `resolution_wh`             | `classes`           |
        | Google Gemini 2.5   | `GOOGLE_GEMINI_2_5`  | detection, segmentation | `resolution_wh`             | `classes`           |
        | Moondream           | `MOONDREAM`          | detection               | `resolution_wh`             |                     |
        | DeepSeek-VL2        | `DEEPSEEK_VL_2`      | detection               | `resolution_wh`             | `classes`           |

        Args:
            lmm: The type of LMM (Large Multimodal Model) to use.
            result: The result string containing the detection data.
            **kwargs: Additional keyword arguments required by the specified LMM.

        Returns:
            A new Detections object.

        Raises:
            ValueError: If the LMM is invalid, required arguments are missing, or
                disallowed arguments are provided.
            ValueError: If the specified LMM is not supported.

        !!! example "PaliGemma"
            ```python

            import supervision as sv

            paligemma_result = "<loc0256><loc0256><loc0768><loc0768> cat"
            detections = sv.Detections.from_lmm(
                sv.LMM.PALIGEMMA,
                paligemma_result,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog']
            )
            detections.xyxy
            # array([[250., 250., 750., 750.]])

            detections.class_id
            # array([0])

            detections.data
            # {'class_name': array(['cat'], dtype='<U10')}
            ```

        !!! example "Qwen2.5-VL"

            ??? tip "Prompt engineering"

                To get the best results from Qwen2.5-VL, use clear and descriptive prompts
                that specify exactly what you want to detect.

                **For general object detection, use this comprehensive prompt:**

                ```
                Detect all objects in the image and return their locations and labels.
                ```

                **For specific object detection with detailed descriptions:**

                ```
                Detect the red object that is leading in this image and return its location and label.
                ```

                **For simple, targeted detection:**

                ```
                leading blue truck
                ```

                **Additional effective prompts:**

                ```
                Find all people and vehicles in this scene
                ```

                ```
                Locate all animals in the image
                ```

                ```
                Identify traffic signs and their positions
                ```

                **Tips for better results:**

                - Use descriptive language that clearly specifies what to look for
                - Include color, size, or position descriptors when targeting specific objects
                - Be specific about the type of objects you want to detect
                - The model responds well to both detailed instructions and concise phrases
                - Results are returned in JSON format with `bbox_2d` coordinates and `label` fields


            ```python
            import supervision as sv

            qwen_2_5_vl_result = """```json
            [
                {"bbox_2d": [139, 768, 315, 954], "label": "cat"},
                {"bbox_2d": [366, 679, 536, 849], "label": "dog"}
            ]
            ```"""
            detections = sv.Detections.from_lmm(
                sv.LMM.QWEN_2_5_VL,
                qwen_2_5_vl_result,
                input_wh=(1000, 1000),
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )
            detections.xyxy
            # array([[139., 768., 315., 954.], [366., 679., 536., 849.]])

            detections.class_id
            # array([0, 1])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U10')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Qwen3-VL"

            ```python
            import supervision as sv

            qwen_3_vl_result = """```json
            [
                {"bbox_2d": [139, 768, 315, 954], "label": "cat"},
                {"bbox_2d": [366, 679, 536, 849], "label": "dog"}
            ]
            ```"""
            detections = sv.Detections.from_lmm(
                sv.LMM.QWEN_3_VL,
                qwen_3_vl_result,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )
            detections.xyxy
            # array([[139., 768., 315., 954.], [366., 679., 536., 849.]])

            detections.class_id
            # array([0, 1])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U10')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Gemini 2.0"

            ??? tip "Prompt engineering"

                From Gemini 2.0 onwards, models are further trained to detect objects in
                an image and get their bounding box coordinates. The coordinates,
                relative to image dimensions, scale to [0, 1000]. You need to convert
                these normalized coordinates back to pixel coordinates using your
                original image size.

                According to the Gemini API documentation on image prompts (see
                https://ai.google.dev/gemini-api/docs/vision#image-input), when using a
                single image with text, the recommended approach is to place the text
                prompt after the image part in the contents array. This ordering has
                been shown to produce significantly better results in practice.

                For example, when calling the Gemini API directly, you can structure
                the request like this, with the image part first and the text prompt
                second in the `parts` list:

                ```json
                {
                  "model": "models/gemini-2.0-flash",
                  "contents": [
                    {
                      "role": "user",
                      "parts": [
                        {
                          "inline_data": {
                            "mime_type": "image/png",
                            "data": "<BASE64_IMAGE_BYTES>"
                          }
                        },
                        {
                          "text": "Detect all the cats and dogs in the image..."
                        }
                      ]
                    }
                  ]
                }
                ```
                To get the best results from Google Gemini 2.0, use the following prompt.

                ```
                Detect all the cats and dogs in the image. The box_2d should be
                [ymin, xmin, ymax, xmax] normalized to 0-1000.
                ```

            ```python
            import supervision as sv

            gemini_response_text = """```json
                [
                    {"box_2d": [543, 40, 728, 200], "label": "cat", "id": 1},
                    {"box_2d": [653, 352, 820, 522], "label": "dog", "id": 2}
                ]
            ```"""

            detections = sv.Detections.from_lmm(
                sv.LMM.GOOGLE_GEMINI_2_0,
                gemini_response_text,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )

            detections.xyxy
            # array([[543., 40., 728., 200.], [653., 352., 820., 522.]])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U26')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Gemini 2.5"

            ??? tip "Prompt engineering"

                To get the best results from Google Gemini 2.5, use the following prompt.

                This prompt is designed to detect all visible objects in the image,
                including small, distant, or partially visible ones, and to return
                tight bounding boxes.

                According to the Gemini API documentation on image prompts, when using
                a single image with text, the recommended approach is to place the text
                prompt after the image part in the `contents` array. See the official
                Gemini vision docs for details:
                https://ai.google.dev/gemini-api/docs/vision#multi-part-input

                For example, using the `google-generativeai` client:

                ```python
                from google.generativeai import types

                response = model.generate_content(
                    contents=[
                        types.Part.from_image(image_bytes),
                        "Carefully examine this image and detect ALL visible objects, including "
                        "small, distant, or partially visible ones.",
                    ],
                    generation_config=generation_config,
                    safety_settings=safety_settings,
                )
                ```

                This ordering (image first, then text) has been shown to produce
                significantly better results in practice.

                ```
                Carefully examine this image and detect ALL visible objects, including
                small, distant, or partially visible ones.

                IMPORTANT: Focus on finding as many objects as possible, even if you are
                only moderately confident.

                Make sure each bounding box is as tight as possible.

                Valid object classes: {class_list}

                For each detected object, provide:
                - "label": the exact class name from the list above
                - "confidence": your certainty (between 0.0 and 1.0)
                - "box_2d": the bounding box [ymin, xmin, ymax, xmax] normalized to 0-1000
                - "mask": the binary mask of the object as a base64-encoded string

                Detect everything that matches the valid classes. Do not be
                conservative; include objects even with moderate confidence.

                Return a JSON array, for example:
                [
                    {
                        "label": "person",
                        "confidence": 0.95,
                        "box_2d": [100, 200, 300, 400],
                        "mask": "..."
                    },
                    {
                        "label": "kite",
                        "confidence": 0.80,
                        "box_2d": [50, 150, 250, 350],
                        "mask": "..."
                    }
                ]
                ```

                When using the google-genai library, it is recommended to set
                thinking_budget=0 in thinking_config for more direct and faster responses.

                ```python
                from google.generativeai import types

                model.generate_content(
                    ...,
                    generation_config=generation_config,
                    safety_settings=safety_settings,
                    thinking_config=types.ThinkingConfig(
                        thinking_budget=0
                    )
                )
                ```

                For a shorter prompt focused only on segmentation masks, you can use:

                ```
                Return a JSON list of segmentation masks. Each entry should include the
                2D bounding box in the "box_2d" key, the segmentation mask in the "mask"
                key, and the text label in the "label" key. Use descriptive labels.
                ```

            ```python
            import supervision as sv

            gemini_response_text = """```json
                [
                    {"box_2d": [543, 40, 728, 200], "label": "cat", "id": 1},
                    {"box_2d": [653, 352, 820, 522], "label": "dog", "id": 2}
                ]
            ```"""

            detections = sv.Detections.from_lmm(
                sv.LMM.GOOGLE_GEMINI_2_5,
                gemini_response_text,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )

            detections.xyxy
            # array([[543., 40., 728., 200.], [653., 352., 820., 522.]])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U26')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Moondream"


            ??? tip "Prompt engineering"

                To get the best results from Moondream, use optimized prompts that leverage
                its object detection capabilities effectively.

                **For general object detection, use this simple prompt:**

                ```
                objects
                ```

                This single-word prompt instructs Moondream to detect all visible objects
                and return them in the proper JSON format with normalized coordinates.


            ```python
            import supervision as sv

            moondream_result = {
                'objects': [
                    {
                        'x_min': 0.5704046934843063,
                        'y_min': 0.20069346576929092,
                        'x_max': 0.7049859315156937,
                        'y_max': 0.3012596592307091
                    },
                    {
                        'x_min': 0.6210969910025597,
                        'y_min': 0.3300672620534897,
                        'x_max': 0.8417936339974403,
                        'y_max': 0.4961046129465103
                    }
                ]
            }

            detections = sv.Detections.from_lmm(
                sv.LMM.MOONDREAM,
                moondream_result,
                resolution_wh=(1000, 1000),
            )

            detections.xyxy
            # array([[1752.28,  818.82, 2165.72, 1229.14],
            #        [1908.01, 1346.67, 2585.99, 2024.11]])
            ```

        !!! example "DeepSeek-VL2"


            ??? tip "Prompt engineering"

                To get the best results from DeepSeek-VL2, use optimized prompts that leverage
                its object detection and visual grounding capabilities effectively.

                **For general object detection, use the following user prompt:**

                ```
                <image>\n<|ref|>The giraffe at the front<|/ref|>
                ```

                **For visual grounding, use the following user prompt:**

                ```
                <image>\n<|grounding|>Detect the giraffes
                ```

            ```python
            from PIL import Image
            import supervision as sv

            deepseek_vl2_result = "<|ref|>The giraffe at the back<|/ref|><|det|>[[580, 270, 999, 904]]<|/det|><|ref|>The giraffe at the front<|/ref|><|det|>[[26, 31, 632, 998]]<|/det|><|end▁of▁sentence|>"

            detections = sv.Detections.from_vlm(
                vlm=sv.VLM.DEEPSEEK_VL_2, result=deepseek_vl2_result, resolution_wh=image.size
            )

            detections.xyxy
            # array([[ 420,  293,  724,  982],
            #        [  18,   33,  458, 1084]])

            detections.class_id
            # array([0, 1])

            detections.data
            # {'class_name': array(['The giraffe at the back', 'The giraffe at the front'], dtype='<U24')}
            ```
        z`Detections.from_lmm` is deprecated since `supervision-0.26.0` and will be removed in `supervision-0.31.0`. Use `Detections.from_vlm` instead.zInvalid LMM string 'z'. Must be one of zInvalid type for 'lmm': z. Must be LMM or str.)vlmresultr   )r5   r(   	PALIGEMMAr)   
FLORENCE_2QWEN_2_5_VLDEEPSEEK_VL_2GOOGLE_GEMINI_2_0GOOGLE_GEMINI_2_5rV   rx   lowerr   r   typefrom_vlm)rf   lmmr  kwargs
lmm_to_vlmr  lmm_enumms           rI   from_lmmzDetections.from_lmm  s7   J 	1	
 MM3==NNCNNOOS__s00!!3#8#8!!3#8#8

 c3S/CS!syy{+ X&C *49+5JK  s||=F=f==   *3%/A),-A--.0 s   )D5 5E2E#"E2c                   t        |||      }|t        j                  k(  r5t        |t              sJ t        |fi |\  }}}t        |i} | |||      S |t        j                  k(  rZt        |t              sJ t        |fi |\  }}}t        |i}t        j                  t        |      t              } | ||||      S |t        j                  k(  rZt        |t              sJ t        |fi |\  }}}t        |i}t        j                  t        |      t              } | ||||      S |t        j                  k(  r5t        |t              sJ t!        |fi |\  }}}t        |i} | |||      S |t        j"                  k(  rt        |t$              sJ t'        |fi |\  }}	}
}t        |      dk(  r8| j)                         }t        t        j(                  dt              i|_        |S i }|		|	|t        <   |	||t,        <    | ||
|      S |t        j.                  k(  r5t        |t              sJ t1        |fi |\  }}}t        |i} | |||      S |t        j2                  k(  r't        |t$              sJ t5        |fi |} | |      S |t        j6                  k(  rBt        |t              sJ t9        |fi |}t        |d   i} | |d   |d   |d	   |d
   |      S | j)                         S )u<  

        Creates a Detections object from the given result string based on the specified
        Vision Language Model (VLM).

        | Name                | Enum (sv.VLM)        | Tasks                   | Required parameters         | Optional parameters |
        |---------------------|----------------------|-------------------------|-----------------------------|---------------------|
        | PaliGemma           | `PALIGEMMA`          | detection               | `resolution_wh`             | `classes`           |
        | PaliGemma 2         | `PALIGEMMA`          | detection               | `resolution_wh`             | `classes`           |
        | Qwen2.5-VL          | `QWEN_2_5_VL`        | detection               | `resolution_wh`, `input_wh` | `classes`           |
        | Qwen3-VL            | `QWEN_3_VL`          | detection               | `resolution_wh`,            | `classes`           |
        | Google Gemini 2.0   | `GOOGLE_GEMINI_2_0`  | detection               | `resolution_wh`             | `classes`           |
        | Google Gemini 2.5   | `GOOGLE_GEMINI_2_5`  | detection, segmentation | `resolution_wh`             | `classes`           |
        | Moondream           | `MOONDREAM`          | detection               | `resolution_wh`             |                     |
        | DeepSeek-VL2        | `DEEPSEEK_VL_2`      | detection               | `resolution_wh`             | `classes`           |

        Args:
            vlm: The type of VLM (Vision Language Model) to use.
            result: The result string containing the detection data.
            **kwargs: Additional keyword arguments required by the specified VLM.

        Returns:
            A new Detections object.

        Raises:
            ValueError: If the VLM is invalid, required arguments are missing, or
                disallowed arguments are provided.
            ValueError: If the specified VLM is not supported.

        !!! example "PaliGemma"
            ```python

            import supervision as sv

            paligemma_result = "<loc0256><loc0256><loc0768><loc0768> cat"
            detections = sv.Detections.from_vlm(
                sv.VLM.PALIGEMMA,
                paligemma_result,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog']
            )
            detections.xyxy
            # array([[250., 250., 750., 750.]])

            detections.class_id
            # array([0])

            detections.data
            # {'class_name': array(['cat'], dtype='<U10')}
            ```

        !!! example "Qwen2.5-VL"

            ??? tip "Prompt engineering"

                To get the best results from Qwen2.5-VL, use clear and descriptive prompts
                that specify exactly what you want to detect.

                **For general object detection, use this comprehensive prompt:**

                ```
                Detect all objects in the image and return their locations and labels.
                ```

                **For specific object detection with detailed descriptions:**

                ```
                Detect the red object that is leading in this image and return its location and label.
                ```

                **For simple, targeted detection:**

                ```
                leading blue truck
                ```

                **Additional effective prompts:**

                ```
                Find all people and vehicles in this scene
                ```

                ```
                Locate all animals in the image
                ```

                ```
                Identify traffic signs and their positions
                ```

                **Tips for better results:**

                - Use descriptive language that clearly specifies what to look for
                - Include color, size, or position descriptors when targeting specific objects
                - Be specific about the type of objects you want to detect
                - The model responds well to both detailed instructions and concise phrases
                - Results are returned in JSON format with `bbox_2d` coordinates and `label` fields


            ```python
            import supervision as sv

            qwen_2_5_vl_result = """```json
            [
                {"bbox_2d": [139, 768, 315, 954], "label": "cat"},
                {"bbox_2d": [366, 679, 536, 849], "label": "dog"}
            ]
            ```"""
            detections = sv.Detections.from_vlm(
                sv.VLM.QWEN_2_5_VL,
                qwen_2_5_vl_result,
                input_wh=(1000, 1000),
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )
            detections.xyxy
            # array([[139., 768., 315., 954.], [366., 679., 536., 849.]])

            detections.class_id
            # array([0, 1])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U10')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Qwen3-VL"

            ```python
            import supervision as sv

            qwen_3_vl_result = """```json
            [
                {"bbox_2d": [139, 768, 315, 954], "label": "cat"},
                {"bbox_2d": [366, 679, 536, 849], "label": "dog"}
            ]
            ```"""
            detections = sv.Detections.from_vlm(
                sv.VLM.QWEN_3_VL,
                qwen_3_vl_result,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )
            detections.xyxy
            # array([[139., 768., 315., 954.], [366., 679., 536., 849.]])

            detections.class_id
            # array([0, 1])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U10')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Gemini 2.0"

            ??? tip "Prompt engineering"

                From Gemini 2.0 onwards, models are further trained to detect objects in
                an image and get their bounding box coordinates. The coordinates,
                relative to image dimensions, scale to [0, 1000]. You need to convert
                these normalized coordinates back to pixel coordinates based on your
                original image size.
                According to the [Gemini API documentation on image prompts](
                https://ai.google.dev/gemini-api/docs/vision?lang=python#image_prompts), when using
                a single image with text, the recommended approach is to place the text
                prompt after the image part in the `contents` array (for example,
                `contents=[image_part, text_part]`). This ordering has been shown to
                produce significantly better results in practice.

                To get the best results from Google Gemini 2.0, use the following prompt.

                ```
                Detect all the cats and dogs in the image. The box_2d should be
                [ymin, xmin, ymax, xmax] normalized to 0-1000.
                ```

            ```python
            import supervision as sv

            gemini_response_text = """```json
                [
                    {"box_2d": [543, 40, 728, 200], "label": "cat", "id": 1},
                    {"box_2d": [653, 352, 820, 522], "label": "dog", "id": 2}
                ]
            ```"""

            detections = sv.Detections.from_vlm(
                sv.VLM.GOOGLE_GEMINI_2_0,
                gemini_response_text,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )

            detections.xyxy
            # array([[543., 40., 728., 200.], [653., 352., 820., 522.]])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U26')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Gemini 2.5"

            ??? tip "Prompt engineering"

                To get the best results from Google Gemini 2.5, use the following prompt.

                This prompt is designed to detect all visible objects in the image,
                including small, distant, or partially visible ones, and to return
                tight bounding boxes.

                According to the [Gemini API documentation on image prompts](
                https://ai.google.dev/gemini-api/docs/vision?hl=en),
                when using a single image with text, place the text prompt after the image
                part in the `contents` array. For example, with the `google-genai` client:

                ```python
                response = model.generate_content(
                    [
                        {
                            "role": "user",
                            "parts": [
                                types.Part.from_bytes(image_bytes, mime_type="image/png"),
                                types.Part.from_text(prompt),
                            ],
                        }
                    ]
                )
                ```

                This ordering has been shown to produce significantly better results in practice.

                ```
                Carefully examine this image and detect ALL visible objects, including
                small, distant, or partially visible ones.

                IMPORTANT: Focus on finding as many objects as possible, even if you are
                only moderately confident.

                Make sure each bounding box is as tight as possible.

                Valid object classes: {class_list}

                For each detected object, provide:
                - "label": the exact class name from the list above
                - "confidence": your certainty (between 0.0 and 1.0)
                - "box_2d": the bounding box [ymin, xmin, ymax, xmax] normalized to 0-1000
                - "mask": the binary mask of the object as a base64-encoded string

                Detect everything that matches the valid classes. Do not be
                conservative; include objects even with moderate confidence.

                Return a JSON array, for example:
                [
                    {
                        "label": "person",
                        "confidence": 0.95,
                        "box_2d": [100, 200, 300, 400],
                        "mask": "..."
                    },
                    {
                        "label": "kite",
                        "confidence": 0.80,
                        "box_2d": [50, 150, 250, 350],
                        "mask": "..."
                    }
                ]
                ```

                When using the google-genai library, it is recommended to set
                thinking_budget=0 in thinking_config for more direct and faster responses.

                ```python
                from google.generativeai import types

                model.generate_content(
                    ...,
                    generation_config=generation_config,
                    safety_settings=safety_settings,
                    thinking_config=types.ThinkingConfig(
                        thinking_budget=0
                    )
                )
                ```

                For a shorter prompt focused only on segmentation masks, you can use:

                ```
                Return a JSON list of segmentation masks. Each entry should include the
                2D bounding box in the "box_2d" key, the segmentation mask in the "mask"
                key, and the text label in the "label" key. Use descriptive labels.
                ```

            ```python
            import supervision as sv

            gemini_response_text = """```json
                [
                    {"box_2d": [543, 40, 728, 200], "label": "cat", "id": 1},
                    {"box_2d": [653, 352, 820, 522], "label": "dog", "id": 2}
                ]
            ```"""

            detections = sv.Detections.from_vlm(
                sv.VLM.GOOGLE_GEMINI_2_5,
                gemini_response_text,
                resolution_wh=(1000, 1000),
                classes=['cat', 'dog'],
            )

            detections.xyxy
            # array([[543., 40., 728., 200.], [653., 352., 820., 522.]])

            detections.data
            # {'class_name': array(['cat', 'dog'], dtype='<U26')}

            detections.class_id
            # array([0, 1])
            ```

        !!! example "Moondream"


            ??? tip "Prompt engineering"

                To get the best results from Moondream, use optimized prompts that leverage
                its object detection capabilities effectively.

                **For general object detection, use this simple prompt:**

                ```
                objects
                ```

                This single-word prompt instructs Moondream to detect all visible objects
                and return them in the proper JSON format with normalized coordinates.


            ```python
            import supervision as sv

            moondream_result = {
                'objects': [
                    {
                        'x_min': 0.5704046934843063,
                        'y_min': 0.20069346576929092,
                        'x_max': 0.7049859315156937,
                        'y_max': 0.3012596592307091
                    },
                    {
                        'x_min': 0.6210969910025597,
                        'y_min': 0.3300672620534897,
                        'x_max': 0.8417936339974403,
                        'y_max': 0.4961046129465103
                    }
                ]
            }

            detections = sv.Detections.from_vlm(
                sv.VLM.MOONDREAM,
                moondream_result,
                resolution_wh=(1000, 1000),
            )

            detections.xyxy
            # array([[1752.28,  818.82, 2165.72, 1229.14],
            #        [1908.01, 1346.67, 2585.99, 2024.11]])
            ```

        !!! example "DeepSeek-VL2"


            ??? tip "Prompt engineering"

                To get the best results from DeepSeek-VL2, use optimized prompts that leverage
                its object detection and visual grounding capabilities effectively.

                **For general object detection, use the following user prompt:**

                ```
                <image>\n<|ref|>The giraffe at the front<|/ref|>
                ```

                **For visual grounding, use the following user prompt:**

                ```
                <image>\n<|grounding|>Detect the giraffes
                ```

            ```python
            from PIL import Image
            import supervision as sv

            deepseek_vl2_result = "<|ref|>The giraffe at the back<|/ref|><|det|>[[580, 270, 999, 904]]<|/det|><|ref|>The giraffe at the front<|/ref|><|det|>[[26, 31, 632, 998]]<|/det|><|end▁of▁sentence|>"

            detections = sv.Detections.from_vlm(
                vlm=sv.VLM.DEEPSEEK_VL_2, result=deepseek_vl2_result, resolution_wh=image.size
            )

            detections.xyxy
            # array([[ 420,  293,  724,  982],
            #        [  18,   33,  458, 1084]])

            detections.class_id
            # array([0, 1])

            detections.data
            # {'class_name': array(['The giraffe at the back', 'The giraffe at the front'], dtype='<U24')}
            ```

        )r;   r?   rB   rn   )r;   r?   r>   rB   r   )r;   r=   rB   r;   r   r   r^   r   )r;   r?   r=   r>   rB   )r*   r)   r  rV   rx   r0   r   r  r1   rY   onesrN   r   	QWEN_3_VLr2   r  r+   r  r   r,   rw   rB   r   r  r-   	MOONDREAMr/   r  r.   )rf   r  r  r  r;   r?   r   rB   confidence_arrr   r=   rs   rw   gemini_results                 rI   r  zDetections.from_vlm  s   N 'sFF;#--fc***)7)I&)I&D(J%zDD D8$??#//!fc***)9&)KF)K&D(J):6D<>GGD	=N Hd  #--fc***)7)I&)I&D(J):6DWWSYe<NHd  ####fc***);F)Mf)M&D(J):6DD8$??#.. fd++++:6+LV+L(D&$4yA~		3RXXas5KL
D!.4*+#19-.Dt$77#'''fc***)?)Q&)Q&D(J):6DD8$??#--fd+++!&3F3DD>!#'''fc***26DVDM)=+;<D"1%&q)"1%(+  yy{rK   c                   t        |      dk(  r| j                         S t        j                  |D cg c]  }|d   	 c}      }t        j                  t        j
                  |d      t        j                  |d      f      }t        j                  |D cg c]  }t        |      dkD  r
|d   r|d   nd c}      }t        j                  |D cg c]  }|d   	 c}      } | |j                  t        j                        |j                  t        j                        t        |i      S c c}w c c}w c c}w )a  
        Create a Detections object from the
        [EasyOCR](https://github.com/JaidedAI/EasyOCR) result.

        Results are placed in the `data` field with the key `"class_name"`.

        Args:
            easyocr_results: The output Results instance from EasyOCR.

        Returns:
            A new Detections object.

        Example:
            ```python
            import supervision as sv
            import easyocr

            reader = easyocr.Reader(['en'])
            results = reader.readtext("<SOURCE_IMAGE_PATH>")
            detections = sv.Detections.from_easyocr(results)
            detected_text = detections["class_name"]
            ```
        r   r   r   r   )r;   r>   rB   )
rN   rw   rY   rq   hstackminmaxrd   r   r   )rf   easyocr_resultsr  r   r;   r>   ocr_texts          rI   from_easyocrzDetections.from_easyocr  s   2 1$99;xxAvAByy"&&A.t!0DEFXX . ![1_q	A

 88_E6VAYEFRZZ(!((4%x
 	
 B
 Fs   D2!D7D<c           	     $   g g g }}}t        |      dk(  r| j                         S |D ]  }|j                  }|j                  |j                  j                  t        j                        |j                  j                  t        j                        |j                  j                  t        j                        |j                  j                  t        j                        g       |j                  |j                         |j                  |j                           | t        t        j                  |t        j                              t        j                  |t        j                        t        j                  |t                    S )aS  
        Creates a Detections instance from the
        [ncnn](https://github.com/Tencent/ncnn) inference result.
        Supports object detection models.

        Args:
            ncnn_results: The output Results instance from ncnn.

        Returns:
            A new Detections object.

        Example:
            ```python
            import cv2
            from ncnn.model_zoo import get_model
            import supervision as sv

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = get_model(
                "yolov8s",
                target_size=640
                prob_threshold=0.5,
                nms_threshold=0.45,
                num_threads=4,
                use_gpu=True,
                )
            result = model(image)
            detections = sv.Detections.from_ncnn(result)
            ```
        r   rn   r`   )rN   rw   rectr   r   rd   rY   r   r   r   r   problabelr   rq   re   )rf   ncnn_resultsr   r   r   ncnn_resultr!  s          rI   	from_ncnnzDetections.from_ncnn  s   B (*2r9k|!99;' 	0K##DKKFFMM"**-FFMM"**-FFMM"**-FFMM"**-	 {//0[../	0 bhht2::>?xx2::>XXis3
 	
rK   c                     | t        j                  dt         j                        t        j                  g t         j                        t        j                  g t                    S )aF  
        Create an empty Detections object with no bounding boxes,
            confidences, or class IDs.

        Returns:
            An empty Detections object.

        Example:
            ```python
            from supervision import Detections

            empty_detections = Detections.empty()
            ```
        )r   r^   rn   r`   )rY   rw   r   rq   re   )rf   s    rI   rw   zDetections.empty  sC      &

3xx"**5XXb,
 	
rK   c                2    t        | j                        dk(  S )ap  
        Check whether the `Detections` object has zero bounding boxes.

        Returns:
            `True` if there are no detections, `False` otherwise.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> detections = sv.Detections(
            ...     xyxy=np.array([[10, 20, 110, 120]]),
            ...     class_id=np.array([1]),
            ...     tracker_id=np.array([1]),
            ... )
            >>> filtered = detections[detections.class_id == 99]
            >>> filtered.is_empty()
            True

            ```
        r   rM   rG   s    rI   is_emptyzDetections.is_empty&  s    , 499~""rK   c           	        D cg c]  }|j                         r| c}t              dk(  rt        j                         S D ]O  }t	        |j
                  |j                  |j                  |j                  |j                  |j                         Q t        j                  D cg c]  }|j
                   c}      }	 	 	 	 d	fd} |d      } |d      } |d      } |d      }	t        D cg c]  }|j                   c}      }
D cg c]  }|j                   }}t        |      } | |||||	|
|      S c c}w c c}w c c}w c c}w )
a  
        Merge a list of Detections objects into a single Detections object.

        This method takes a list of Detections objects and combines their
        respective fields (`xyxy`, `mask`, `confidence`, `class_id`, and `tracker_id`)
        into a single Detections object.

        For example, if merging Detections with 3 and 4 detected objects, this method
        will return a Detections with 7 objects (7 entries in `xyxy`, `mask`, etc).

        !!! Note

            When merging, empty `Detections` objects are ignored.

        Args:
            detections_list: A list of Detections objects to merge.

        Returns:
            A single Detections object containing the merged data from the input list.

        Example:
            ```python
            import numpy as np
            import supervision as sv

            detections_1 = sv.Detections(
                xyxy=np.array([[15, 15, 100, 100], [200, 200, 300, 300]]),
                class_id=np.array([1, 2]),
                data={'feature_vector': np.array([0.1, 0.2])}
            )

            detections_2 = sv.Detections(
                xyxy=np.array([[30, 30, 120, 120]]),
                class_id=np.array([1]),
                data={'feature_vector': np.array([0.3])}
            )

            merged_detections = sv.Detections.merge([detections_1, detections_2])

            merged_detections.xyxy
            array([[ 15,  15, 100, 100],
                   [200, 200, 300, 300],
                   [ 30,  30, 120, 120]])

            merged_detections.class_id
            array([1, 2, 1])

            merged_detections.data['feature_vector']
            array([0.1, 0.2, 0.3])
            ```
        r   rF   c                    t         fdD              ry t         fdD              rt        d  d       dk(  r{D cg c]  }|j                          }}t        d |D              rt	        j
                  |      S t        j                  |D cg c]  }t        j                  |       c}      S t        j                  D cg c]  }|j                          c}      S c c}w c c}w c c}w )Nc              3  D   K   | ]  }|j                        d u   y wrU   __getattribute__.0dr   s     rI   	<genexpr>z:Detections.merge.<locals>.stack_or_none.<locals>.<genexpr>  !     M1%%d+t3M    c              3  D   K   | ]  }|j                        d u   y wrU   r-  r/  s     rI   r2  z:Detections.merge.<locals>.stack_or_none.<locals>.<genexpr>  r3  r4  zAll or none of the 'z' fields must be Noner=   c              3  <   K   | ]  }t        |t                y wrU   )rV   r   )r0  r  s     rI   r2  z:Detections.merge.<locals>.stack_or_none.<locals>.<genexpr>  s     Aaz![1As   )
rX   anyr   r.  r   mergerY   vstackr   r  )r   r1  r|   r  detections_lists   `   rI   stack_or_nonez'Detections.merge.<locals>.stack_or_none  s     M_MMM_MM #7v=R!STTv~;JKa++D1KKA5AA&,,U33yy!?A"**Q-!?@@99P1a006PQQ L "@Ps   C-C2C7r=   r>   r?   r@   r;   r=   r>   r?   r@   rB   rD   )r   rx   returnr<   )r)  rN   r9   rw   r6   r;   r=   r>   r?   r@   rB   rY   r9  r   rD   r   )rf   r:  
detectionsr1  r;   r;  r=   r>   r?   r@   rB   metadata_listrD   s    `           rI   r8  zDetections.merge>  s]   l *9
%
@S@S@UJ
 1$##%%) 	J'____%00#,,%00__	 yy/:Q!&&:;	R	R9	R V$"<0
 ,"<0
?;a166;<?NO,,OO!-0!!
 	
Y
" ;, <Os   EE,E9EEc                   |t         j                  k(  rut        j                  | j                  dddf   | j                  dddf   z   dz  | j                  dddf   | j                  dddf   z   dz  g      j                         S |t         j                  k(  r-| j                  t        d      t        | j                        S |t         j                  k(  r^t        j                  | j                  dddf   | j                  dddf   | j                  dddf   z   dz  g      j                         S |t         j                  k(  r^t        j                  | j                  dddf   | j                  dddf   | j                  dddf   z   dz  g      j                         S |t         j                  k(  r^t        j                  | j                  dddf   | j                  dddf   z   dz  | j                  dddf   g      j                         S |t         j                  k(  rGt        j                  | j                  dddf   | j                  dddf   g      j                         S |t         j                  k(  rGt        j                  | j                  dddf   | j                  dddf   g      j                         S |t         j                  k(  r^t        j                  | j                  dddf   | j                  dddf   z   dz  | j                  dddf   g      j                         S |t         j                   k(  rGt        j                  | j                  dddf   | j                  dddf   g      j                         S |t         j"                  k(  rGt        j                  | j                  dddf   | j                  dddf   g      j                         S t        | d      )	a*  
        Calculates and returns the coordinates of a specific anchor point
        within the bounding boxes defined by the `xyxy` attribute. The anchor
        point can be any of the predefined positions in the `Position` enum,
        such as `CENTER`, `CENTER_LEFT`, `BOTTOM_RIGHT`, etc.

        Args:
            anchor: An enum specifying the position of the anchor point within the
                bounding box. Supported positions are defined in the `Position` enum.

        Returns:
            An array of shape `(n, 2)`, where `n` is the number of bounding
                boxes. Each row contains the `[x, y]` coordinates of the specified
                anchor point for the corresponding bounding box.

        Raises:
            ValueError: If the provided `anchor` is not supported.
        Nr   r   r   r   z>Cannot use `Position.CENTER_OF_MASS` without a detection mask.)r|   z is not supported.)r3   CENTERrY   rq   r;   	transposeCENTER_OF_MASSr=   r   r'   CENTER_LEFTCENTER_RIGHTBOTTOM_CENTERBOTTOM_LEFTBOTTOM_RIGHT
TOP_CENTERTOP_LEFT	TOP_RIGHT)rH   anchors     rI   get_anchors_coordinatesz"Detections.get_anchors_coordinates  sU   & X__$88YYq!t_tyyA6!;YYq!t_tyyA6!;
 ik x...yy  T  -499==x+++88IIadOYYq!t_tyyA6!;
 ik x,,,88IIadOYYq!t_tyyA6!;
 ik x---88))AqD/DIIadO3q8$))AqD/Jik x+++88TYYq!t_dii1o>?IIKKx,,,88TYYq!t_dii1o>?IIKKx***88))AqD/DIIadO3q8$))AqD/Jik x(((88TYYq!t_dii1o>?IIKKx)))88TYYq!t_dii1o>?IIKKF8#5677rK   c                   t        |t              r| j                  j                  |      S t	        |       dk(  r| S t        |t
              r|g}t        | j                  |   | j                  | j                  |   nd| j                  | j                  |   nd| j                  | j                  |   nd| j                  | j                  |   ndt        | j                  |      | j                        S )a[  
        Get a subset of the Detections object or access an item from its data field.

        When provided with an integer, slice, list of integers, or a numpy array, this
        method returns a new Detections object that represents a subset of the original
        detections. When provided with a string, it accesses the corresponding item in
        the data dictionary.

        Args:
            index: The index, indices, or key to access a subset of the Detections
                or an item from the data.

        Returns:
            A subset of the Detections object or an item from the data field.

        Example:
            ```python
            import supervision as sv

            detections = sv.Detections()

            first_detection = detections[0]
            first_10_detections = detections[0:10]
            some_detections = detections[[0, 2, 4]]
            class_0_detections = detections[detections.class_id == 0]
            high_confidence_detections = detections[detections.confidence > 0.5]

            feature_vector = detections['feature_vector']
            ```
        r   Nr<  )rV   rx   rB   r   rN   re   r9   r;   r=   r>   r?   r@   r   rD   )rH   indexs     rI   __getitem__zDetections.__getitem__  s    B eS!99==''t9>KeS!GE5!%)YY%:5!151Ltu-RV-1]]-FT]]5)D151Ltu-RVtyy%0]]
 	
rK   c                    t        |t        j                  t        f      st	        d      t        |t              rt        j
                  |      }|| j                  |<   y)a  
        Set a value in the data dictionary of the Detections object.

        Args:
            key: The key in the data dictionary to set.
            value: The value to set for the key.

        Example:
            ```python
            import cv2
            import supervision as sv
            from ultralytics import YOLO

            image = cv2.imread("<SOURCE_IMAGE_PATH>")
            model = YOLO('yolov8s.pt')

            result = model(image)[0]
            detections = sv.Detections.from_ultralytics(result)

            detections['names'] = [
                 model.model.names[class_id]
                 for class_id
                 in detections.class_id
             ]
            ```
        z$Value must be a np.ndarray or a listN)rV   rY   ndarraylist	TypeErrorrq   rB   )rH   r   r   s      rI   __setitem__zDetections.__setitem__	  sG    6 %"**d!34BCCeT"HHUOE		#rK   c                x   | j                   pt        | j                   t              r| j                   j                  S t	        j
                  | j                   D cg c]  }t	        j                  |       c}      S t        | j                  v rt        | j                  t                 S | j                  S c c}w )uF  
        Calculate the area of each detection in the set of object detections.

        Selection order:

        1. If ``mask`` is set, return the area of each mask.
        2. Else, if ``data[ORIENTED_BOX_COORDINATES]`` is set, return the area of
           the rotated body (shoelace formula on the four corners).
        3. Otherwise, return the axis-aligned box area (``box_area``).

        **OBB dispatch contract**: presence of ``data[ORIENTED_BOX_COORDINATES]``
        with shape ``(N, 4, 2)`` is the canonical signal that a detection carries
        oriented bounding box geometry. The same presence-of-key check governs
        ``with_nms``, ``with_nmm``, and this property — always store OBB corners
        under ``config.ORIENTED_BOX_COORDINATES`` with that shape.

        **Return dtype**: ``float64`` (OBB branch), input dtype (AABB fallback),
        ``int64`` (mask branch).

        Returns:
            An array containing the area of each detection
                in the format of `(area_1, area_2, ..., area_n)`,
                where n is the number of detections.

        Example:
            >>> import numpy as np
            >>> import supervision as sv
            >>> corners = np.array(
            ...     [[[0, 5], [5, 10], [10, 5], [5, 0]]], dtype=np.float32
            ... )
            >>> detections = sv.Detections(
            ...     xyxy=np.array([[0, 0, 10, 10]], dtype=np.float32),
            ...     class_id=np.array([0]),
            ...     data={"xyxyxyxy": corners},
            ... )
            >>> detections.area
            array([50.])
        )r=   rV   r   r   rY   rq   sumr   rB   r   box_area)rH   r=   s     rI   r   zDetections.area>	  s    P 99 $))[1yy~~%88dii@dRVVD\@AA#tyy0#DII.F$GHH}} As   B7c                    | j                   dddf   | j                   dddf   z
  | j                   dddf   | j                   dddf   z
  z  S )a+  
        Calculate the area of each bounding box in the set of object detections.

        Returns:
            An array of floats containing the area of each bounding
                box in the format of `(area_1, area_2, ..., area_n)`,
                where n is the number of detections.
        Nr   r   r   r   r  rG   s    rI   rX  zDetections.box_arean	  sL     		!Q$$))AqD/1dii1o		RSUVRV6WXXrK   c                B   | j                   dddf   | j                   dddf   z
  }| j                   dddf   | j                   dddf   z
  }t        j                  |t        j                  t        j                        }t        j
                  ||||dk7         |S )a#  
        Compute the aspect ratio (width divided by height) for each bounding box.

        Returns:
            Array of shape `(N,)` containing aspect ratios, where `N` is the
                number of boxes (width / height for each box).

        Examples:
            ```python
            import numpy as np
            import supervision as sv

            xyxy = np.array([
                [10, 10, 50, 50],
                [60, 10, 180, 50],
                [10, 60, 50, 180],
            ])

            detections = sv.Detections(xyxy=xyxy)

            detections.box_aspect_ratio
            # array([1.0, 3.0, 0.33333333])

            ar = detections.box_aspect_ratio
            detections[(ar < 2.0) & (ar > 0.5)].xyxy
            # array([[10., 10., 50., 50.]])
            ```
        Nr   r   r   r   rn   )r   where)r;   rY   	full_likenanfloat64divide)rH   widthsheightsaspect_ratioss       rI   box_aspect_ratiozDetections.box_aspect_ratioz	  s~    < 1a4499QT?2))AqD/DIIadO3VRVV2::F
		&'}GqLIrK         ?Fc                   t        |       dk(  r| S | j                  J d       |r<t        j                  | j                  | j                  j                  dd      f      }ni| j                  J d       t        j                  | j                  | j                  j                  dd      | j                  j                  dd      f      }| j                  t        || j                  ||      }ndt        | j                  v rDt        |t        j                  | j                  t           t        j                        ||      }nt        |||	      }t        t         | |         S )
a  
        Performs non-max suppression on detection set. Dispatch order: (1) if mask
        data present, IoU mask is used; (2) else if oriented-box coordinates
        (``data[ORIENTED_BOX_COORDINATES]``) present, oriented-box IoU is used; (3)
        otherwise, axis-aligned box IoU is used.

        Args:
            threshold: The intersection-over-union threshold
                to use for non-maximum suppression. The lower the value the more
                restrictive the NMS becomes. Defaults to 0.5.
            class_agnostic: Whether to perform class-agnostic
                non-maximum suppression. If True, the class_id of each detection
                will be ignored. Defaults to False.
            overlap_metric: Metric used to compute the degree of
                overlap between pairs of masks or boxes (e.g., IoU, IoS).

        Returns:
            A new Detections object containing the subset of detections
                after non-maximum suppression.

        Raises:
            AssertionError: If `confidence` is None and class_agnostic is False.
                If `class_id` is None and class_agnostic is False.
        r   z;Detections confidence must be given for NMS to be executed.r   zDetections class_id must be given for NMS to be executed. If you intended to perform class agnostic NMS set class_agnostic=True.r   r|   iou_thresholdoverlap_metricrn   r   oriented_boxesrh  ri  r   rh  ri  )rN   r>   rY   r  r;   reshaper?   r=   r$   r   rB   r&   r   r   r!   r	   r9   )rH   	thresholdclass_agnosticri  r   indicess         rI   with_nmszDetections.with_nms	  sL   < t9>K* 	
I	
* ))TYY0G0GA0N$OPK==, S, ))IIOO++B2MM))"a0K 99 .'ii'-	G &26'!zzII67rzz  (-G .''-G JW..rK   c           	     d   t        |       dk(  r| S | j                  J d       |r<t        j                  | j                  | j                  j                  dd      f      }ni| j                  J d       t        j                  | j                  | j                  j                  dd      | j                  j                  dd      f      }| j                  t        || j                  ||      }ndt        | j                  v rDt        |t        j                  | j                  t           t        j                        ||      }nt        |||	      }g }|D ]<  }|D cg c]  }t        t         | |          }	}|j#                  t%        |	             > t         j'                  |      S c c}w )
aX  
        Perform non-maximum merging on the current set of object detections.
        Dispatch order: (1) if mask data present, IoU mask is used; (2) else if
        oriented-box coordinates (``data[ORIENTED_BOX_COORDINATES]``) present,
        oriented-box IoU is used; (3) otherwise, axis-aligned box IoU is used.

        Args:
            threshold: The intersection-over-union threshold
                to use for non-maximum merging. Defaults to 0.5.
            class_agnostic: Whether to perform class-agnostic
                non-maximum merging. If True, the class_id of each detection
                will be ignored. Defaults to False.
            overlap_metric: Metric used to compute the degree of
                overlap between pairs of masks or boxes (e.g., IoU, IoS).

        Returns:
            A new Detections object containing the subset of detections
                after non-maximum merging.

        Note:
            For detections carrying oriented bounding box data
            (``data[ORIENTED_BOX_COORDINATES]``), each merge group's output OBB
            is the tightest rectangle at the winner's orientation enclosing all
            corners contributed by every detection in the group. The winner is
            the highest-confidence detection in the group. The axis-aligned
            ``xyxy`` field is updated to the tight bounding box of that rect.
            For zero-rotation OBBs this equals the axis-aligned union exactly;
            for rotated OBBs the merged rect inherits the winner's rotation angle.
            Groups of size 1 keep the original OBB unchanged.

        Raises:
            AssertionError: If `confidence` is None or `class_id` is None and
                class_agnostic is False.

        ![non-max-merging](https://media.roboflow.com/supervision-docs/non-max-merging.png){ align=center width="800" }
        r   z;Detections confidence must be given for NMM to be executed.rf  r   zDetections class_id must be given for NMM to be executed. If you intended to perform class agnostic NMM set class_agnostic=True.rg  rn   rj  rl  )rN   r>   rY   r  r;   rm  r?   r=   r#   r   rB   r%   r   r   r    r	   r9   r   _merge_detection_groupr8  )
rH   rn  ro  ri  r   merge_groupsr  merge_grouprR   groups
             rI   with_nmmzDetections.with_nmm	  s   T t9>K* 	
I	
* ))TYY0G0GA0N$OPK==, S, ))IIOO++B2MM))"a0K 99 -'ii'-	L &25'!zzII67rzz  (-L -''-L $&' 	9K8CD1T*d1g.DEDMM078	9 '' Es    F-)r=  None)r=  re   )r=  zIterator[tuple[npt.NDArray[np.generic], npt.NDArray[np.generic] | None, np.generic | None, np.generic | None, np.generic | None, dict[str, npt.NDArray[np.generic] | list[Any]]]])r[   objectr=  r   )rg   r   r=  r9   )ry   r   r=  r9   )r   r   r=  r9   )r   rC   r   tuple[int, int]r=  r9   )r   r   r=  r9   )r   r   r=  r9   rU   )r   rC   r   dict[int, str] | Noner=  r9   )r   r   r=  r9   )r   dict[str, Any] | Anyr=  r9   )r   zlist[dict[str, Any]]r=  r9   )r   r|  r   rz  r=  r9   )r   rC   r   r{  r=  r9   )r   r   r=  r9   )r  z	LMM | strr  str | dict[str, Any]r  r   r=  r9   )r  z	VLM | strr  r}  r  r   r=  r9   )r  z	list[Any]r=  r9   )r$  r   r=  r9   )r=  r9   )r=  r   )r:  list[Detections]r=  r9   )rL  r3   r=  r:   )rO  z7int | slice | list[int] | npt.NDArray[np.generic] | strr=  z7Detections | list[Any] | npt.NDArray[np.generic] | None)r   rx   r   z#npt.NDArray[np.generic] | list[Any]r=  rx  )r=  r:   )rn  r   ro  r   ri  r   r=  r9   )1r   
__module____qualname____doc____annotations__r=   r>   r?   r@   r   r   rB   rD   rJ   rO   rS   r\   classmethodri   r}   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r&  rw   r)  r8  rM  rP  rU  propertyr   rX  rc  r   IOUrq  rw  r   rK   rI   r9   r9   C   s   Un "!9=D
6=15J.5/3H,315J.5;@QU;VD
8V$T:Hn:
	
4
 
 
> O Ob "
 "
H *
!/*
@O*
	*
 *
X !
 !
F #
 #
J  +/N,N (N 
	N N` ,
 ,
\ 5
 5
n ') ')R z
.z
?Nz
	z
 z
x NRT
)T
6KT
	T
 T
l '
 '
R f>f>%9f>EHf>	f> f>P qq%9qEHq	q qf +
 +
Z 7
 7
r 
 
*#0 h
 h
T?8B/
L/
	@/
b!F - -^ 	Y 	Y " "L $(5(9(9	K/K/ K/ &	K/
 
K/^ $(5(9(9	\(\( \( &	\(
 
\(rK   r9   c                t   t        j                  | d      j                  t         j                        }| d   d   | d   d   z
  }t	        t        j
                  |d   |d               }t	        t        j                  |            t	        t        j                  |            }}t        j                  || g||ggt         j                        }||z  }t	        |dddf   j                               }t	        |dddf   j                               }	t	        |dddf   j                               }
t	        |dddf   j                               }t        j                  ||g| |ggt         j                        }t        j                  ||
g|	|
g|	|g||ggt         j                        |z  }|S )a  Merge multiple OBB corner arrays using winner-angle projection.

    The first entry in *corners_list* is the winner. Its orientation angle
    (derived from its first edge) defines the local frame in which the
    tightest enclosing axis-aligned rectangle is computed. That rectangle is
    then rotated back to produce the merged OBB.

    Args:
        corners_list: List of (4, 2) corner arrays. First is the winner.

    Returns:
        Merged OBB corners as a (4, 2) float32 array.
    r   r   r   rn   N)rY   concatenaterd   r   r   arctan2cossinrq   r  r  )corners_listall_cornerswinner_edgeangler  r  to_locallocal_cornersx_minx_maxy_miny_maxto_worldmergeds                 rI   _merge_obb_cornersr  K
  s     ..A6==bjjIK q/!$|Aq'99K"**[^[^<=ERVVE]#U266%=%9C xx#tsCj1DH(*M-1%))+,E-1%))+,E-1%))+,E-1%))+,E xx#ssdC[1DH
U^eU^eU^eU^L**	
 		  MrK   c           	     d   t        |       dk(  r| d   S t        j                  | D cg c]  }|j                  |j                  d   nd! c}t        j                        }t        t        j                  |            }| |   }t        j                  | D cg c]  }|j                  d    c}t        j                        }|dddf   |dddf   z
  |dddf   |dddf   z
  z  }|j                  rt        |j                               }|dkD  rGt        j                  t        t        j                  ||      |z        gt        j                        }n|j                  }nd}t        |j                  v }	|	rg }
| D ]  }t        j                  |j                  t                 }|j                  dk7  s|j                   dd dk7  rt#        d	|j                          |
j%                  |d   j'                  d
d              t)        |
      }t+        |t        j,                           }i |j                  t        |t        j,                     i}nt        j                  |dddf   j/                         |dddf   j/                         |dddf   j1                         |dddf   j1                         ggt        j                        }|j                  }| D cg c]  }|j2                  |j2                   }}|rFt        j4                  j7                  t        j8                  |d            t        j,                     }nd}t;        | D cg c]  }|j<                   c}      }t?        ||||j@                  |jB                  ||      S c c}w c c}w c c}w c c}w )a  Merge a group of single-object Detections into one merged detection.

    Used internally by :meth:`Detections.with_nmm` to combine each merge group
    into a single output detection. The highest-confidence detection is the
    "winner" whose class_id, tracker_id, and data fields are preserved.

    Args:
        detections: List of Detections, each containing exactly one object.

    Returns:
        A single merged Detections object of length 1.
    r   r   Ng        rn   r   r   )r^   r   z'corners must have shape (N, 4, 2); got r^   r   r<  )"rN   rY   rq   r>   r^  re   argmaxr;   r   r   rW  dotr   rB   r   ndimr   r   r   rm  r  r   newaxisr  r  r=   r   r   r  r   rD   r9   r?   r@   )r>  r1  r   
winner_idxwinnerall_xyxyareas
total_arear>   has_obbr  detcmerged_cornersr;   rB   r|   r=   rD   s                      rI   rs  rs  v
  s/    :!!}((GQR!ALL4a#	=RjjK RYY{+,J
#F xxJ7q7rzzJHad^hq!tn,!Q$(1a4.1PQE$599;'
>rvve[1J>?@jjJ
  **J
 '&++5G 	4C

388$<=>Avv{aggabkV3 #J177)!TUU!Q 23		4
 ,L9rzz :;T&++T7

9ST xx QTN&&(QTN&&(QTN&&(QTN&&(	 **

 {{ (>166+=QVV>E>}}##BNN5q$AB2::N:>aqzz>?H$$ y 	S 8X ? ?s   $NN#)N(;N(N-z0.29.0z0.32.0)deprecated_in	remove_inc           	     J   t        |       dk7  st        |      dk7  rt        d      t        | |       | j                  d   }|j                  d   }| j                  |j                  d}n| j                  J |j                  J |d   |d   z
  |d   |d   z
  z  }|d   |d   z
  |d   |d   z
  z  }|| j                  d   z  ||j                  d   z  z   ||z   z  }t        j                  |g      }t        j                  |dd |dd       \  }}t        j                  |dd |dd       \  }	}
t        j                  |||	|
gg      }| j                  |j                  d}n*t        j                  | j                  |j                        }| j                  |j                  | }n$| j                  d   |j                  d   k\  r| }n|}t        | j                  |j                  g      }t        ||||j                  |j                  |j                   |      S )a^  
    Merges two Detections objects into a single Detections object.
    Assumes each Detections contains exactly one object.

    A `winning` detection is determined based on the confidence score of the two
    input detections. This winning detection is then used to specify which
    `class_id`, `tracker_id`, and `data` to include in the merged Detections object.

    The resulting `confidence` of the merged object is calculated by the weighted
    contribution of each detection to the merged object.
    The bounding boxes and masks of the two input detections are merged into a
    single bounding box and mask, respectively.

    Args:
        detections_1: The first Detections object.
        detections_2: The second Detections object.

    Returns:
        A new Detections object, with merged attributes.

    Raises:
        ValueError: If the input Detections objects do not have exactly 1 detected
            object.

    Example:
        ```python
        import cv2
        import supervision as sv
        from inference import get_model

        image = cv2.imread("<SOURCE_IMAGE_PATH>")
        model = get_model(model_id="yolov8s-640")

        result = model.infer(image)[0]
        detections = sv.Detections.from_inference(result)

        merged_detections = merge_object_detection_pair(
            detections[0], detections[1])
        ```
    r   z6Both Detections should have exactly 1 detected object.r   Nr   r   r<  )rN   r   %_validate_fields_both_defined_or_noner;   r>   rY   rq   minimummaximumr=   r   r   rD   r9   r?   r@   rB   )detections_1detections_2xyxy_1xyxy_2merged_confidencedetection_1_areadetections_2_area	merged_x1	merged_y1	merged_x2	merged_y2merged_xyxymerged_maskwinning_detectionrD   s                  rI   !merge_inner_detection_object_pairr  
  sZ   X <A\!2a!7QRR),Eq!Fq!F&<+B+B+J &&222&&222"1Iq	1fQi&)6KL#AY2vay6!97LM|66q99,"9"9!"<<= 113 HH&7%89::fRaj&!*=Iy::fQRj&*=Iy((Y	9iHIJK \%6%6%>mmL$5$5|7H7HI&,*A*A*I(		 	 	#|'>'>q'A	A((|44l6K6KLMH$"++$//## rK   rd  c                   | d   }| dd D ]w  }|j                   1|j                   %t        |j                   |j                   |      d   }n$t        |j                  |j                  |      d   }||k  r |S t	        ||      }y |S )=  
    Given N detections each of length 1 (exactly one object inside), combine them into a
    single detection object of length 1. The contained inner object will be the merged
    result of all the input detections.

    For example, this lets you merge N boxes into one big box, N masks into one mask,
    etc.
    r   r   N)r=   r"   r   r;   r  )r>  rn  ri  r  r  ious         rI   merge_inner_detections_objectsr  +  s     a=L"12 	U(\->->-J !2!2L4E4E~VC   1 1<3D3DnUVWXC? 9|T	U rK   c                "    t        t        |       S )r  )r   r  )r>  s    rI   *merge_inner_detections_objects_without_iour  G  s     3Z@@rK   c                    t        |       }|D ]2  }t        | |      }t        ||      }|du |du k7  s%t        d| d       y)a  
    Verify that for each optional field in the Detections, both instances either have
    the field set to None or both have it set to non-None values.

    `data` field is ignored.

    Raises:
        ValueError: If one field is None and the other is not, for any of the fields.
    NzField 'z=' should be consistently None or not None in both Detections.)r4   r   r   )r  r  
attributes	attributevalue_1value_2s         rI   r  r  V  sc     (5J 	,	2,	2tOD1) %  rK   )targetr  r  c                    t        | |       y rU   )r   )r  r  s     rI   $validate_fields_both_defined_or_noner  n  s     	|$rK   )r  zlist[npt.NDArray[np.floating]]r=  znpt.NDArray[np.floating])r>  r~  r=  r9   )r  r9   r  r9   r=  r9   )r>  r~  rn  r   ri  r   r=  r9   )r  r9   r  r9   r=  rx  )T
__future__r   collections.abcr   dataclassesr   r   	functoolsr   typingr   r	   rc   rY   numpy.typingnpt	deprecater
   r   supervision.configr   r   "supervision.detection.compact_maskr   (supervision.detection.tools.transformersr   r   r   !supervision.detection.utils.boxesr   r   &supervision.detection.utils.convertersr   r   r   $supervision.detection.utils.internalr   r   r   r   r   r   r   'supervision.detection.utils.iou_and_nmsr   r   r    r!   r"   r#   r$   r%   r&   !supervision.detection.utils.masksr'   supervision.detection.vlmr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   supervision.geometry.corer3   supervision.utils.internalr4   r5   supervision.validatorsr6   r7   r9   r  rs  r  r  r  r  r  r  r   rK   rI   <module>r     s   " $ (     & ; 
 Q 
  
 
 
 H    / N T D(( D(( D((NP(0((VUp (h7YY,6YY 8Yx (h7 $1$5$5  " 	 86 (h7A AA 8A,6	0 0
%%,6%	%
%rK   