§
    >²¸j‚  ã                  ó$  — d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
Zd dlmZ d dlmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZmZmZ d dlmZ d dlmZm Z  d dl!m"Z" erd dl#Z$ G d„ ded         ¦  «        Z%e G d„ d¦  «        ¦   «         Z&dS )é    )Úannotations)Údeepcopy)Ú	dataclass)ÚTYPE_CHECKINGÚAnyÚcastN)ÚORIENTED_BOX_COORDINATES)Ú
Detections)Úbox_iou_batchÚmask_iou_batchÚoriented_box_iou_batch)ÚLEGACY_COLOR_PALETTE)ÚAveragingMethodÚMetricÚMetricTarget©Ú*_match_detection_batch_with_target_indices)ÚObjectSizeCategoryÚget_detection_size_category)Úensure_pandas_installedc                  óÒ   — e Zd ZdZej        ej        fd7d„Zd8d	„Z	d9d„Z
d:d„Zej        fd;d„Zd<d„Zed=d$„¦   «         Zed>d+„¦   «         Zed?d-„¦   «         Zd@d1„ZdAd2„ZdBd3„ZdCd5„Zd6S )DÚ	Precisiona/  
    Precision is a metric used to evaluate object detection models. It is the ratio of
    true positive detections to the total number of predicted detections. We calculate
    it at different IoU thresholds.

    In simple terms, Precision is a measure of a model's accuracy, calculated as:

    `Precision = TP / (TP + FP)`

    Here, `TP` is the number of true positives (correct detections), and `FP` is the
    number of false positive detections (detected, but incorrectly).

    Examples:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> from supervision.metrics import Precision
        >>> predictions = sv.Detections(
        ...     xyxy=np.array([[0, 0, 10, 10]]),
        ...     class_id=np.array([0]),
        ...     confidence=np.array([0.9])
        ... )
        >>> targets = sv.Detections(
        ...     xyxy=np.array([[0, 0, 10, 10]]),
        ...     class_id=np.array([0])
        ... )
        >>> precision_metric = Precision()
        >>> precision_result = precision_metric.update(predictions, targets).compute()
        >>> round(float(precision_result.precision_at_50), 2)
        1.0

        ```

    ![example_plot](
        https://media.roboflow.com/supervision-docs/metrics/precision_plot_example.png
    ){ align=center width="800" }
    Úmetric_targetr   Úaveraging_methodr   c                ó>   — || _         || _        g | _        g | _        dS )a  
        Initialize the Precision metric.

        Args:
            metric_target: The type of detection data to use.
            averaging_method: The averaging method used to compute the
                precision. Determines how the precision is aggregated across classes.
        N)Ú_metric_targetr   Ú_predictions_listÚ_targets_list)Úselfr   r   s      úm/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/metrics/precision.pyÚ__init__zPrecision.__init__G   s*   € ð ,ˆÔØ 0ˆÔà35ˆÔØ/1ˆÔÐÐó    ÚreturnÚNonec                ó"   — g | _         g | _        dS )zR
        Reset the metric to its initial state, clearing all stored data.
        N)r   r   ©r   s    r    ÚresetzPrecision.resetZ   s   € ð "$ˆÔØˆÔÐÐr"   ÚpredictionsúDetections | list[Detections]Útargetsc                ón  — t          |t          ¦  «        s|g}t          |t          ¦  «        s|g}t          |¦  «        t          |¦  «        k    r0t          dt          |¦  «        › dt          |¦  «        › d�¦  «        ‚| j                             |¦  «         | j                             |¦  «         | S )a  
        Add new predictions and targets to the metric, but do not compute the result.

        Args:
            predictions: The predicted detections.
            targets: The target detections.

        Returns:
            The updated metric instance.
        zThe number of predictions (z) and targets (z%) during the update must be the same.)Ú
isinstanceÚlistÚlenÚ
ValueErrorr   Úextendr   )r   r(   r*   s      r    ÚupdatezPrecision.updatea   sÍ   € õ ˜+¥tÑ,Ô,ð 	(Ø&˜-ˆKÝ˜'¥4Ñ(Ô(ð 	 Ø�iˆGåˆ{ÑÔ�s 7™|œ|Ò+Ð+ÝðQ­c°+Ñ.>Ô.>ð Qð QÝ  ™\œ\ðQð Qð Qñô ð ð
 	Ô×%Ò% kÑ2Ô2Ð2ØÔ×!Ò! 'Ñ*Ô*Ð*àˆr"   ÚPrecisionResultc                óf  — |                       | j        | j        ¦  «        }|                       | j        | j        t          j        ¦  «        |_        |                       | j        | j        t          j        ¦  «        |_        |                       | j        | j        t          j        ¦  «        |_	        |S )zÇ
        Calculate the precision metric based on the stored predictions and ground-truth
        data, at different IoU thresholds.

        Returns:
            The precision metric result.
        )
Ú_computer   r   r   ÚSMALLÚsmall_objectsÚMEDIUMÚmedium_objectsÚLARGEÚlarge_objects)r   Úresults     r    ÚcomputezPrecision.compute€   s�   € ð —’˜tÔ5°tÔ7IÑJÔJˆØ#Ÿ}š}ØÔ" DÔ$6Õ8JÔ8Pñ 
ô  
ˆÔð !%§¢ØÔ" DÔ$6Õ8JÔ8Qñ!
ô !
ˆÔð  $Ÿ}š}ØÔ" DÔ$6Õ8JÔ8Pñ 
ô  
ˆÔð ˆr"   Úpredictions_listúlist[Detections]Útargets_listÚsize_categoryr   c                ó  — t          j        dddt           j        ¬¦  «        }g }t          ||¦  «        D �]œ\  }}|                      |¦  «        }|                      |¦  «        }	t          j        t          |¦  «        t          ¬¦  «        }
t          j        t          |¦  «        t          ¬¦  «        }|t          j	        k    rbt          |¦  «        dk    rt          || j        ¦  «        |j        k    }
t          |¦  «        dk    rt          || j        ¦  «        |j        k    }t          |¦  «        dk    �r:t          |¦  «        dk    �r&|j        �|j        €t          d¦  «        ‚t          j        |j        t           j        ¬¦  «        |
         }t          j        |j        t           j        ¬¦  «        |
         }t          |¦  «        dk    r�Œ¢|                     t          j        t          |¦  «        |j        ft           j        ¬¦  «        t          j        t          |¦  «        |j        ft           j        ¬¦  «        ||t          j        dt           j        ¬¦  «        f¦  «         �Œ@t          |¦  «        dk    �rH|j        �|j        €t          d	¦  «        ‚t          |¦  «        dk    rÓt          j        |j        t           j        ¬¦  «        |         }t          |¦  «        dk    r�ŒÄ|                     t          j        d|j        ft          ¬¦  «        t          j        d|j        ft          ¬¦  «        t          j        dt           j        ¬¦  «        t          j        dt,          ¬¦  «        |f¦  «         �ŒW|j        €t          d
¦  «        ‚t          j        |j        t           j        ¬¦  «        }t          j        |j        t           j        ¬¦  «        }t          j        |j        t           j        ¬¦  «        }| j        t.          j        k    rt3          |	|¦  «        }n[| j        t.          j        k    rt7          |	|¦  «        }n5| j        t.          j        k    rt;          |	|¦  «        }nt          d¦  «        ‚|t          j	        k    r|nd}t=          |||||¬¦  «        \  }}t          j        |t          ¬¦  «        }|t          j	        k    rÏ|dk    }t          j        |t          ¬¦  «        }t          j         |¦  «        r|||                  ||<   |
dd…df         |z  }| || z  z  }t          j         | d¬¦  «        }||         ||         z  }||         }||         }||         }||         }t          |¦  «        dk    rt          |¦  «        dk    r�Œ‚|                     |||||f¦  «         �Œž|swtC          | j        | j"        t          j        |j#        d         ¦  «        t          j        d|j#        d         f¦  «        |t          j$        g t,          ¬¦  «        ddd¬¦	  «	        S d„ t          |Ž D ¦   «         } | j%        |Ž \  }}}tC          | j        | j"        ||||ddd¬¦	  «	        S )aŸ  Build per-image stats tuples and delegate to class-level computation.

        Each stats tuple is
        ``(matches, ignored_matches, confidence, class_ids, true_class_ids)``:
        - Both empty: skip (no information).
        - Targets empty, predictions present: all predictions are FPs; true_class_ids
          is ``zeros((0,))``.
        - Targets present: IoU matching produces ``matches`` array.
        g      à?gffffffî?é
   ©Údtyper   NzEPrecision metric requires `class_id` and `confidence` on predictions.©r   zEPrecision metric requires `class_id` on both predictions and targets.z6Precision metric requires `confidence` on predictions.z-Unsupported metric target for IoU calculation)Útarget_scored_maské   ©Úaxis)	r   r   Úprecision_scoresÚprecision_per_classÚiou_thresholdsÚmatched_classesr6   r8   r:   c                ó8   — g | ]}t          j        |d ¦  «        ‘ŒS rE   )ÚnpÚconcatenate)Ú.0Úitemss     r    ú
<listcomp>z&Precision._compute.<locals>.<listcomp>K  s$   € ÐPÐPÐP¸5�bœn¨U°AÑ6Ô6ÐPÐPÐPr"   )&rO   ÚlinspaceÚfloat32ÚzipÚ_detections_contentÚonesr.   Úboolr   ÚANYr   r   ÚvalueÚclass_idÚ
confidencer/   ÚasarrayÚint32ÚappendÚzerosÚsizeÚbool_Úintr   ÚBOXESr   ÚMASKSr   ÚORIENTED_BOUNDING_BOXESr   r   Ú
zeros_likeÚanyr2   r   ÚshapeÚarrayÚ_compute_precision_for_classes)r   r=   r?   r@   rL   Ústatsr(   r*   Úprediction_contentsÚtarget_contentsÚprediction_size_maskÚtarget_size_maskÚprediction_class_idsÚprediction_confidenceÚtarget_class_idsÚiourF   ÚmatchesÚmatched_target_indicesÚignored_matchesÚvalid_target_matchÚmatched_scored_targetÚprediction_scoredÚprediction_keepÚconcatenated_statsrJ   rK   Úunique_classess                               r    r4   zPrecision._compute•   s  € õ œ S¨$°½"¼*ÐEÑEÔEˆØˆå$'Ð(8¸,Ñ$GÔ$Gð U	ñ U	Ñ ˆK˜Ø"&×":Ò":¸;Ñ"GÔ"GÐØ"×6Ò6°wÑ?Ô?ˆOÝ#%¤7­3¨{Ñ+;Ô+;Å4Ð#HÑ#HÔ#HÐ Ý!œw¥s¨7¡|¤|½4Ð@Ñ@Ô@ÐØÕ 2Ô 6Ò6Ð6Ý�{Ñ#Ô# aÒ'Ð'å3°KÀÔATÑUÔUØ(Ô.ò/ð )õ �w‘<”< !Ò#Ð#å3°G¸TÔ=PÑQÔQØ(Ô.ò/ð %õ
 �7‰|Œ|˜qÒ Ñ ¥S¨Ñ%5Ô%5¸Ò%9Ñ%9ð Ô'Ð/°;Ô3IÐ3QÝ$ð*ñô ð õ (*¤z°+Ô2FÍbÌhÐ'WÑ'WÔ'WØ(ô(Ð$õ )+¬
ØÔ*µ"´*ð)ñ )ô )à&ô)(Ð%õ Ð+Ñ,Ô,°Ò1Ð1ÙØ—’åœÝ Ð!5Ñ6Ô6¸Ô8KÐLÝ"$¤(ðñ ô õ œÝ Ð!5Ñ6Ô6¸Ô8KÐLÝ"$¤(ðñ ô ð .Ø,Ýœ ­R¬XÐ6Ñ6Ô6ðñô ð ñ õ �W‘” Ò!Ñ!ØÔ'Ð/°7Ô3CÐ3KÝ$ð'ñô ð õ �{Ñ#Ô# qÒ(Ð(Ý')¤z°'Ô2BÍ"Ì(Ð'SÑ'SÔ'SØ(ô(Ð$õ Ð+Ñ,Ô,°Ò1Ð1Ù Ø—L’LåœH a¨Ô)<Ð%=ÅTÐJÑJÔJÝœH a¨Ô)<Ð%=ÅTÐJÑJÔJÝœH Tµ´Ð<Ñ<Ô<ÝœH TµÐ5Ñ5Ô5Ø,ðñô ð ñ ð #Ô-Ð5Ý(ØTñô ð õ ,.¬:Ø#Ô,µB´Hð,ñ ,ô ,Ð(õ (*¤z°'Ô2BÍ"Ì(Ð'SÑ'SÔ'SÐ$Ý,.¬JØ#Ô.µb´jð-ñ -ô -Ð)ð Ô*­lÔ.@Ò@Ð@Ý+¨OÐ=PÑQÔQ˜˜ØÔ,µÔ0BÒBÐBÝ,¨_Ð>QÑRÔR˜˜ØÔ,µÔ0TÒTÐTÝ4Ø+Ð-@ñô ˜˜õ )ØKñô ð ð )Õ,>Ô,BÒBÐBð )Ð(à!ð 'õ CØ0Ø,ØØ*Ø/Aðñ ô ñ 4�GÐ3õ ')¤m°GÅ4Ð&HÑ&HÔ&H�OØ$Õ(:Ô(>Ò>Ð>Ø-CÀqÒ-HÐ*Ý02´¸gÍTÐ0RÑ0RÔ0RÐ-Ýœ6Ð"4Ñ5Ô5ð à 0Ø$:Ð;MÔ$Nô!"ð 2Ð2DÑEð 1°°°°D°Ô9Ð<QÑQð *ð ,=Ð*<Ø.Ð2GÐ1GÑGñ+˜õ +-¬&°/Ð1AÈÐ*JÑ*JÔ*J˜à# OÔ4Ø3°OÔDñEð  ð +:¸/Ô*J˜Ø0EÀoÔ0VÐ-Ø/CÀOÔ/TÐ,Ø+;Ð<LÔ+MÐ(åÐ 4Ñ5Ô5¸Ò:Ð:Ý #Ð$4Ñ 5Ô 5¸Ò :Ð :á$Ø—L’Là#Ø+Ø1Ø0Ø,ðñô ð ùð ð 	Ý"Ø"Ô1Ø!%Ô!6Ý!#¤¨.Ô*>¸qÔ*AÑ!BÔ!BÝ$&¤H¨a°Ô1EÀaÔ1HÐ-IÑ$JÔ$JØ-Ý "¤¨µ3Ð 7Ñ 7Ô 7Ø"Ø#Ø"ð
ñ 
ô 
ð 
ð QÐPÅCÈÀKÐPÑPÔPÐà/ˆDÔ/Ð1CÐDñ 	>ÐÐ-¨~õ ØÔ-Ø!Ô2Ø-Ø 3Ø)Ø*ØØØð

ñ 

ô 

ð 
	
r"   rv   únpt.NDArray[np.bool_]rx   rs   únpt.NDArray[np.float32]rr   únpt.NDArray[np.int32]Útrue_class_idsúNtuple[npt.NDArray[np.float64], npt.NDArray[np.float64], npt.NDArray[np.int32]]c                óž  — t          j        | ¦  «        }||         }||         }||         }t          j        t          j        ||f¦  «        ¦  «        }t          j        |d¬¦  «        \  }}	t          j        |j        d         t          ¬¦  «        }
|	|
t          j        ||¦  «        <   |                      |||||
¦  «        }|  	                    |¦  «        }| j
        t          j        k    rt          j        |d¬¦  «        }nÃ| j
        t          j        k    r+|                     d¦  «        }|  	                    |¦  «        }nƒ| j
        t          j        k    rn|
                     t           j        ¦  «        }
|
                     ¦   «         dk    r t          j        |j        d         ¦  «        }nt          j        |d|
¬¦  «        }|||fS )zçCompute precision scores from concatenated stats across all images.

        ``unique_classes`` is the union of GT and predicted classes so that
        predictions of classes absent from GT still count as false positives.
        T)Úreturn_countsr   rC   rH   rG   )rI   Úweights)rO   ÚargsortÚuniquerP   ra   rj   rd   ÚsearchsortedÚ_compute_confusion_matrixÚ_compute_precisionr   r   ÚMACROÚmeanÚMICROÚsumÚWEIGHTEDÚastyperU   Úaverage)r   rv   rx   rs   rr   r‚   Úsorted_indicesr~   Útrue_classesÚtrue_countsÚclass_countsÚconfusion_matrixrK   rJ   Úconfusion_matrix_mergeds                  r    rl   z(Precision._compute_precision_for_classes\  sÖ  € õ" œÐ%:Ð$:Ñ;Ô;ˆØ˜.Ô)ˆØ)¨.Ô9ˆØ3°NÔCÐõ œÝŒN˜NÐ,@ÐAÑBÔBñ
ô 
ˆõ %'¤I¨nÈDÐ$QÑ$QÔ$QÑ!ˆ�kÝ”x Ô 4°QÔ 7½sÐCÑCÔCˆØFQˆ•R”_ ^°\ÑBÔBÑCð  ×9Ò9Ø�_Ð&:¸NÈLñ
ô 
Ðð
 #×5Ò5Ð6FÑGÔGÐð Ô ¥OÔ$9Ò9Ð9Ý!œwÐ':ÀÐCÑCÔCÐÐØÔ"¥oÔ&;Ò;Ð;Ø&6×&:Ò&:¸1Ñ&=Ô&=Ð#Ø#×6Ò6Ð7NÑOÔOÐÐØÔ"¥oÔ&>Ò>Ð>Ø'×.Ò.­r¬zÑ:Ô:ˆLØ×ÒÑ!Ô! QÒ&Ð&õ $&¤8Ð,?Ô,EÀaÔ,HÑ#IÔ#IÐ Ð å#%¤:Ø'¨a¸ð$ñ $ô $Ð ð  Ð!4°nÐDÐDr"   Úpredictions_classesÚtarget_classesru   rL   c                ó0   — t          | |||¦  «        \  }}|S )Nr   )r™   rš   ru   rL   Úresult_correctÚ_s         r    Ú_match_detection_batchz Precision._match_detection_batch—  s*   € õ GØ °°nñ
ô 
Ñˆ˜ð Ðr"   Úsorted_matchesÚsorted_ignored_matchesÚsorted_prediction_class_idsr~   r–   únpt.NDArray[np.float64]c                óØ  — | j         d         }|j         d         }t          j        ||dft          j        ¬¦  «        }t	          |¦  «        D �]\  }}	||	k    }
||         }|
                     ¦   «         }|dk    r>t          j        |¦  «        }t          j        |¦  «        }t          j        ||¦  «        }n‘|dk    rEt          j        |¦  «        }||
                               d¦  «        }t          j        |¦  «        }nF| |
                              d¦  «        }| |
          ||
          z                       d¦  «        }||z
  }t          j        |||gd¬¦  «        ||<   �Œ|}|S )aÀ  
        Compute the confusion matrix for each class and IoU threshold.

        Assumes the matches and prediction_class_ids are sorted by confidence
        in descending order.

        Args:
            sorted_matches: shape (P, Th), that is True
                if the prediction is a true positive at the given IoU threshold.
            sorted_ignored_matches: shape (P, Th), that is True
                if the prediction should not affect the given IoU threshold.
            sorted_prediction_class_ids: shape (P,), containing
                the class id for each prediction.
            unique_classes: shape (C,), containing the unique
                class ids.
            class_counts: shape (C,), containing the number
                of true instances for each class.

        Returns:
            shape (C, Th, 3), containing the true positives, false
                positives, and false negatives for each class and IoU threshold.
        rG   r   é   rC   rH   )rj   rO   ra   Úfloat64Ú	enumerater�   ÚfullÚstack)rŸ   r    r¡   r~   r–   Únum_thresholdsÚnum_classesr—   Ú	class_idxr\   Úis_classÚnum_trueÚnum_predictionsÚtrue_positivesÚfalse_positivesÚfalse_negativesÚresult_matrixs                    r    rŠ   z#Precision._compute_confusion_matrix£  sˆ  € ð> (Ô-¨aÔ0ˆØ$Ô*¨1Ô-ˆå46´HØ˜.¨!Ð,µB´Jð5
ñ 5
ô 5
Ðõ $-¨^Ñ#<Ô#<ð 	ñ 	ÑˆI�xØ2°hÒ>ˆHØ# IÔ.ˆHØ&Ÿlšl™nœnˆOà !Ò#Ð#Ý!#¤¨.Ñ!9Ô!9�Ý"$¤(¨>Ñ":Ô":�Ý"$¤'¨.¸(Ñ"CÔ"C��Ø˜Q’�Ý!#¤¨.Ñ!9Ô!9�Ø$:¸8Ô$DÐ#D×"IÒ"IÈ!Ñ"LÔ"L�Ý"$¤(¨>Ñ":Ô":��à!/°Ô!9×!=Ò!=¸aÑ!@Ô!@�à# HÔ-Ð-Ð1GÈÔ1QÐ0QÑQß’#�a‘&”&ð  ð #+¨^Ñ";�Ý*,¬(Ø °/ÐBÈð+ñ +ô +Ð˜YÑ'Ñ'ð 2BˆØÐr"   r—   c                óà   — | j         d         dk    st          d| j         › �¦  «        ‚| d         }| d         }||z   }t          j        ||t          j        |¦  «        |dk    ¬¦  «        }|}|S )aa  
        Broadcastable function, computing the precision from the confusion matrix.

        Args:
            confusion_matrix: shape (N, ..., 3), where the last dimension
                contains the true positives, false positives, and false negatives.

        Returns:
            shape (N, ...), containing the precision for each element.
        éÿÿÿÿr¤   z/Confusion matrix must have shape (..., 3), got ).r   ).rG   r   )ÚoutÚwhere)rj   r/   rO   Údividerh   )r—   r¯   r°   ÚdenominatorÚ	precisionÚresult_precisions         r    r‹   zPrecision._compute_precisioná  s�   € ð  Ô% bÔ)¨QÒ.Ð.Ýð,Ø#Ô)ð,ð ,ñô ð ð *¨&Ô1ˆØ*¨6Ô2ˆà$ Ñ6ˆÝ”IØØÝ”˜nÑ-Ô-Ø Ò"ð	
ñ 
ô 
ˆ	ð 5>ÐØÐr"   Ú
detectionsr
   únpt.NDArray[Any]c                ó¤  — | j         t          j        k    r*t          t          j        t                   |j        ¦  «        S | j         t          j        k    rg|j	        �*t          t          j        t                   |j	        ¦  «        S t          |¦  «        dk    rt          d¦  «        ‚|                      ¦   «         S | j         t          j        k    rj|j                             t           ¦  «        }|�5t          |¦  «        dk    r"t#          j        |t"          j        ¬¦  «        }|S |                      ¦   «         S t          d| j         › �¦  «        ‚)z?Return boxes, masks or oriented bounding boxes from detections.Nr   zIPrecision with `MetricTarget.MASKS` requires detections to include masks.rC   úInvalid metric target: )r   r   re   r   ÚnptÚNDArrayr   Úxyxyrf   Úmaskr.   r/   Ú_make_empty_contentrg   ÚdataÚgetr	   rO   rk   rU   )r   r»   ÚobbÚ
result_obbs       r    rW   zPrecision._detections_content  s  € àÔ¥,Ô"4Ò4Ð4Ý�œ¥CÔ(¨*¬/Ñ:Ô:Ð:ØÔ¥,Ô"4Ò4Ð4ØŒÐ*Ý�CœK­Ô,¨j¬oÑ>Ô>Ð>Ý�:‰Œ Ò"Ð"Ý ð%ñô ð ð ×+Ò+Ñ-Ô-Ð-ØÔ¥,Ô"FÒFÐFØ”/×%Ò%Õ&>Ñ?Ô?ˆCØˆ¥3 s¡8¤8¨a¢< <Ý68´h¸sÍ"Ì*Ð6UÑ6UÔ6U�
Ø!Ð!Ø×+Ò+Ñ-Ô-Ð-ÝÐH°4Ô3FÐHÐHÑIÔIÐIr"   c                óp  — | j         t          j        k    r"t          j        dt          j        ¬¦  «        }|S | j         t          j        k    rt          j        dt          ¬¦  «        }|S | j         t          j        k    r"t          j        dt          j        ¬¦  «        }|S t          d| j         › �¦  «        ‚)N)r   é   rC   )r   r   r   )r   rÉ   é   r¾   )
r   r   re   rO   ÚemptyrU   rf   rY   rg   r/   )r   Úempty_boxesÚempty_masksÚ	empty_obbs       r    rÃ   zPrecision._make_empty_content  s¡   € ØÔ¥,Ô"4Ò4Ð4Ý35´8¸FÍ"Ì*Ð3UÑ3UÔ3UˆKØÐàÔ¥,Ô"4Ò4Ð4Ý13´¸)Í4Ð1PÑ1PÔ1PˆKØÐàÔ¥,Ô"FÒFÐFÝ13´¸)Í2Ì:Ð1VÑ1VÔ1VˆIØÐåÐH°4Ô3FÐHÐHÑIÔIÐIr"   c                ó,  — t          |¦  «        }|                     ¦   «         s|t          j        k    r|S t	          || j        ¦  «        }||j        k    }|j        |         |_        |j        �|j        |         |_        |j	        �|j	        |         |_	        |j
        �|j
        |         |_
        |j        �|j        |         |_        |j        �A|j                             ¦   «         D ]'\  }}t          j        |¦  «        |         |j        |<   Œ(|S )zBReturn a copy of detections with contents filtered by object size.)r   Úis_emptyr   rZ   r   r   r[   rÁ   rÂ   r\   r]   Ú
tracker_idrÄ   rR   rO   rk   )r   r»   r@   Únew_detectionsÚsizesÚ	size_maskÚkeyr[   s           r    Ú_filter_detections_by_sizez$Precision._filter_detections_by_size&  s  € õ " *Ñ-Ô-ˆØ×ÒÑ Ô ð 	" MÕ5GÔ5KÒ$KÐ$KØ!Ð!å+¨N¸DÔ<OÑPÔPˆØ˜]Ô0Ò0ˆ	à,Ô1°)Ô<ˆÔØÔÐ*Ø"0Ô"5°iÔ"@ˆNÔØÔ"Ð.Ø&4Ô&=¸iÔ&HˆNÔ#ØÔ$Ð0Ø(6Ô(AÀ)Ô(LˆNÔ%ØÔ$Ð0Ø(6Ô(AÀ)Ô(LˆNÔ%ØÔÐ*Ø,Ô1×7Ò7Ñ9Ô9ð Fð F‘
��UÝ+-¬8°E©?¬?¸9Ô+E�Ô# CÑ(Ð(àÐr"   ú)tuple[list[Detections], list[Detections]]c                óâ   — g }g }t          ||¦  «        D ]W\  }}|                     |                      ||¦  «        ¦  «         |                     |                      ||¦  «        ¦  «         ŒX||fS )zI
        Filter predictions and targets by object size category.
        )rV   r`   rÖ   )r   r=   r?   r@   Únew_predictions_listÚnew_targets_listr(   r*   s           r    Ú'_filter_predictions_and_targets_by_sizez1Precision._filter_predictions_and_targets_by_size@  sš   € ð  "ÐØÐÝ$'Ð(8¸,Ñ$GÔ$Gð 	ð 	Ñ ˆK˜Ø ×'Ò'Ø×/Ò/°¸]ÑKÔKñô ð ð ×#Ò#Ø×/Ò/°¸ÑGÔGñô ð ð ð $Ð%5Ð5Ð5r"   N)r   r   r   r   ©r#   r$   )r(   r)   r*   r)   r#   r   )r#   r2   )r=   r>   r?   r>   r@   r   r#   r2   )rv   r   rx   r   rs   r€   rr   r�   r‚   r�   r#   rƒ   )
r™   r�   rš   r�   ru   r€   rL   r€   r#   r   )rŸ   r   r    r   r¡   r�   r~   r�   r–   r�   r#   r¢   )r—   r¢   r#   r¢   )r»   r
   r#   r¼   )r#   r¼   )r»   r
   r@   r   r#   r
   )r=   r>   r?   r>   r@   r   r#   r×   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   re   r   r�   r!   r'   r1   r<   r   rZ   r4   rl   Ústaticmethodrž   rŠ   r‹   rW   rÃ   rÖ   rÛ   © r"   r    r   r       s|  € € € € € ð$ð $ðP '3Ô&8Ø,;Ô,Dð2ð 2ð 2ð 2ð 2ð& ð  ð  ð  ðð ð ð ð>ð ð ð ð2 -?Ô,Bð	E
ð E
ð E
ð E
ð E
ðN9Eð 9Eð 9Eð 9Eðv ð	ð 	ð 	ñ „\ð	ð ð;ð ;ð ;ñ „\ð;ðz ð ð  ð  ñ „\ð ð@Jð Jð Jð Jð*Jð Jð Jð Jðð ð ð ð46ð 6ð 6ð 6ð 6ð 6r"   r   r2   c                  ó¶   — e Zd ZU dZded<   ded<   edd„¦   «         Zedd	„¦   «         Zd
ed<   d
ed<   ded<   ded<   ded<   ded<   ded<   dd„Zdd„Z	dd„Z
dS ) r2   u¶  
    The results of the precision metric calculation.

    Defaults to `0` if no detections or targets were provided.

    Attributes:
        metric_target: the type of data used for the metric -
            boxes, masks or oriented bounding boxes.
        averaging_method: the averaging method used to compute the
            precision. Determines how the precision is aggregated across classes.
        precision_at_50: the precision at IoU threshold of `0.5`.
        precision_at_75: the precision at IoU threshold of `0.75`.
        precision_scores: the precision scores at each IoU threshold.
            Shape: `(num_iou_thresholds,)`
        precision_per_class: the precision scores per class and
            IoU threshold. Shape: `(num_classes, num_iou_thresholds)`
        iou_thresholds: the IoU thresholds used in the calculations.
        matched_classes: the class IDs present in either predictions or ground
            truth. Corresponds to the rows of `precision_per_class`. Classes
            that appear only in predictions (no ground-truth instances) are
            included; their per-threshold precision values will be `0.0`.
        small_objects: the Precision metric results
            for small objects (area < 32Â²).
        medium_objects: the Precision metric results
            for medium objects (32Â² â‰¤ area < 96Â²).
        large_objects: the Precision metric results
            for large objects (area â‰¥ 96Â²).
    r   r   r   r   r#   Úfloatc                ó6   — t          | j        d         ¦  «        S )Nr   ©rä   rJ   r&   s    r    Úprecision_at_50zPrecisionResult.precision_at_50w  ó   € å�TÔ*¨1Ô-Ñ.Ô.Ð.r"   c                ó6   — t          | j        d         ¦  «        S )Né   ræ   r&   s    r    Úprecision_at_75zPrecisionResult.precision_at_75{  rè   r"   r¢   rJ   rK   r€   rL   r�   rM   zPrecisionResult | Noner6   r8   r:   Ústrc                ó|  — | j         j        › d| j        › d| j        › d| j        d›d| j        d›d| j        › d| j        › d�}| j        j	        d	k    r|d
z  }t          | j        | j        ¦  «        D ]\  }}|d|› d|› d�z  }Œd}| j        �6|t          | j        ¦  «                             dd|› �¦  «        z   }|d|› �z  }| j        �6|t          | j        ¦  «                             dd|› �¦  «        z   }|d|› �z  }| j        �6|t          | j        ¦  «                             dd|› �¦  «        z   }|d|› �z  }|S )a~  
        Format as a pretty string.

        Example:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> from supervision.metrics import Precision
            >>> predictions = sv.Detections(
            ...     xyxy=np.array([[0, 0, 10, 10]]),
            ...     class_id=np.array([0]),
            ...     confidence=np.array([0.9])
            ... )
            >>> targets = sv.Detections(
            ...     xyxy=np.array([[0, 0, 10, 10]]),
            ...     class_id=np.array([0])
            ... )
            >>> precision_metric = Precision()
            >>> precision_result = precision_metric.update(
            ...     predictions, targets
            ... ).compute()
            >>> print(precision_result)  # doctest: +ELLIPSIS
            PrecisionResult:
            Metric target:    MetricTarget.BOXES
            Averaging method: AveragingMethod.WEIGHTED
            P @ 50:     1.0000
            P @ 75:     1.0000
            P @ thresh: [1. ... 1.]
            IoU thresh: [0.5  0.55 ... 0.95]
            Precision per class:
              0: [1. ... 1.]
            ...
            Medium objects:
              PrecisionResult:
              Metric target:    MetricTarget.BOXES
              Averaging method: AveragingMethod.WEIGHTED
              P @ 50:     0.0000
              ...

            ```
        z:
Metric target:    z
Averaging method: z
P @ 50:     z.4fz
P @ 75:     z
P @ thresh: z
IoU thresh: z
Precision per class:
r   z  No results
z  z: ú
Nz
Small objects:
z
Medium objects:
z
Large objects:
)Ú	__class__rÝ   r   r   rç   rë   rJ   rL   rK   rb   rV   rM   r6   rì   Úreplacer8   r:   )r   Úout_strr\   Úprecision_of_classÚindentÚindenteds         r    Ú__str__zPrecisionResult.__str__ˆ  sÙ  € ðV Œ~Ô&ð &ð &Ø!%Ô!3ð&ð &à!%Ô!6ð&ð &ð  Ô/Ð7ð&ð &ð  Ô/Ð7ð	&ð &ð
  Ô0ð&ð &ð  Ô.ð&ð &ð &ð 	ð Ô#Ô(¨AÒ-Ð-ØÐ'Ñ'ˆGÝ,/ØÔ  $Ô":ñ-
ô -
ð 	?ð 	?Ñ(ˆHÐ(ð Ð>˜HÐ>Ð>Ð(:Ð>Ð>Ð>Ñ>ˆGˆGàˆØÔÐ)Ø¥ DÔ$6Ñ 7Ô 7× ?Ò ?ÀÀmÈ6ÀmÀmÑ TÔ TÑTˆHØÐ6¨HÐ6Ð6Ñ6ˆGØÔÐ*Ø¥ DÔ$7Ñ 8Ô 8× @Ò @ÀÀ}ÈFÀ}À}Ñ UÔ UÑUˆHØÐ7¨XÐ7Ð7Ñ7ˆGØÔÐ)Ø¥ DÔ$6Ñ 7Ô 7× ?Ò ?ÀÀmÈ6ÀmÀmÑ TÔ TÑTˆHØÐ6¨HÐ6Ð6Ñ6ˆGàˆr"   úpd.DataFramec                óö  — t          ¦   «          ddl}| j        | j        dœ}| j        �;| j                             ¦   «         }|                     ¦   «         D ]\  }}||d|› �<   Œ| j        �;| j                             ¦   «         }|                     ¦   «         D ]\  }}||d|› �<   Œ| j        �;| j                             ¦   «         }|                     ¦   «         D ]\  }}||d|› �<   Œ |j	        |dg¬¦  «        S )zt
        Convert the result to a pandas DataFrame.

        Returns:
            The result as a DataFrame.
        r   N)zP@50zP@75Úsmall_objects_Úmedium_objects_Úlarge_objects_)Úindex)
r   Úpandasrç   rë   r6   Ú	to_pandasrR   r8   r:   Ú	DataFrame)r   ÚpdÚpandas_dataÚsmall_objects_dfrÕ   r[   Úmedium_objects_dfÚlarge_objects_dfs           r    rý   zPrecisionResult.to_pandasÐ  sJ  € õ 	 Ñ!Ô!Ð!ØÐÐÐð Ô(ØÔ(ð
ð 
ˆð
 ÔÐ)Ø#Ô1×;Ò;Ñ=Ô=ÐØ.×4Ò4Ñ6Ô6ð <ð <‘
��UØ6;�Ð2¨SÐ2Ð2Ñ3Ð3ØÔÐ*Ø $Ô 3× =Ò =Ñ ?Ô ?ÐØ/×5Ò5Ñ7Ô7ð =ð =‘
��UØ7<�Ð3¨cÐ3Ð3Ñ4Ð4ØÔÐ)Ø#Ô1×;Ò;Ñ=Ô=ÐØ.×4Ò4Ñ6Ô6ð <ð <‘
��UØ6;�Ð2¨SÐ2Ð2Ñ3Ð3àˆrŒ|˜K°¨sÐ3Ñ3Ô3Ð3r"   r$   c                ó¨  — ddl m} ddg}| j        | j        g}t          d         gdz  }| j        �3| j        }|ddgz  }||j        |j        gz  }|t          d	         gdz  z  }| j        �3| j        }|d
dgz  }||j        |j        gz  }|t          d         gdz  z  }| j        �3| j        }|ddgz  }||j        |j        gz  }|t          d         gdz  z  }d|j        d<   | 	                    d¬¦  «        \  }}	|	 
                    dd¦  «         |	                     dd¬¦  «         d| j        j        › d| j        j        › d�}
|	                     |
d¬¦  «         t!          t#          |¦  «        ¦  «        }|	                     |||d¬¦  «        }|	                     |¦  «         |	                     |dd¬¦  «         |D ]_}|                     ¦   «         }|	                     |                     ¦   «         |                     ¦   «         dz  z   |dz   |d ›dd!¬"¦  «         Œ`d#|j        d<   |                     ¦   «          |                     ¦   «          dS )$zÈ
        Plot the precision results.

        ![example_plot](
            https://media.roboflow.com/supervision-docs/metrics/precision_plot_example.png
        ){ align=center width="800" }
        r   )ÚpyplotzPrecision@50zPrecision@75rÊ   NzSmall: P@50zSmall: P@75r¤   zMedium: P@50zMedium: P@75zLarge: P@50zLarge: P@75rÉ   Ú	monospacezfont.family)rB   é   )ÚfigsizerG   ÚValueÚbold)Ú
fontweightz#Precision, by Object Size
(target: z, averaging: ú)Úcenter)ÚcolorÚaligné-   Úright)ÚrotationÚhag{®Gáz”?z.2fÚbottom)r  Úvaz
sans-serif)Ú
matplotlibr  rç   rë   r   r6   r8   r:   ÚrcParamsÚsubplotsÚset_ylimÚ
set_ylabelr   r[   r   Ú	set_titleÚranger.   ÚbarÚ
set_xticksÚset_xticklabelsÚ
get_heightÚtextÚget_xÚ	get_widthÚtight_layoutÚshow)r   ÚpltÚlabelsÚvaluesÚcolorsr6   r8   r:   r�   ÚaxÚtitleÚx_positionsÚbarsr  Úy_values                  r    ÚplotzPrecisionResult.plotî  s¾  € ð 	-Ð,Ð,Ð,Ð,Ð,à  .Ð1ˆØÔ&¨Ô(<Ð=ˆÝ& qÔ)Ð*¨QÑ.ˆàÔÐ)Ø Ô.ˆMØ�} mÐ4Ñ4ˆFØ�}Ô4°mÔ6SÐTÑTˆFØÕ+¨AÔ.Ð/°!Ñ3Ñ3ˆFàÔÐ*Ø!Ô0ˆNØ�~ ~Ð6Ñ6ˆFØ�~Ô5°~Ô7UÐVÑVˆFØÕ+¨AÔ.Ð/°!Ñ3Ñ3ˆFàÔÐ)Ø Ô.ˆMØ�} mÐ4Ñ4ˆFØ�}Ô4°mÔ6SÐTÑTˆFØÕ+¨AÔ.Ð/°!Ñ3Ñ3ˆFà&1ˆŒ�]Ñ#à—’ W�Ñ-Ô-‰ˆˆ2Ø
�Š�A�qÑÔÐØ
�Š�g¨&ˆÑ1Ô1Ð1ð:ØÔ,Ô2ð:ð :àÔ0Ô6ð:ð :ð :ð 	ð
 	�Š�U vˆÑ.Ô.Ð.å�C ™KœKÑ(Ô(ˆØ�vŠv�k 6°¸xˆvÑHÔHˆà
�Š�kÑ"Ô"Ð"Ø
×Ò˜6¨B°7ÐÑ;Ô;Ð;àð 	ð 	ˆCØ—n’nÑ&Ô&ˆGØ�GŠGØ—	’	‘”˜cŸmšm™oœo°Ñ1Ñ1Ø˜$‘ØÐ Ð ØØð ñ ô ð ð ð '3ˆŒ�]Ñ#à×ÒÑÔÐØ�Š‰
Œ
ˆ
ˆ
ˆ
r"   N)r#   rä   )r#   rì   )r#   rö   rÜ   )rÝ   rÞ   rß   rà   Ú__annotations__Úpropertyrç   rë   rõ   rý   r/  râ   r"   r    r2   r2   U  s  € € € € € € ðð ð:  ÐÐÑØ%Ð%Ð%Ñ%àð/ð /ð /ñ „Xð/ð ð/ð /ð /ñ „Xð/ð .Ð-Ð-Ñ-Ø0Ð0Ð0Ñ0Ø+Ð+Ð+Ñ+Ø*Ð*Ð*Ñ*à)Ð)Ð)Ñ)Ø*Ð*Ð*Ñ*Ø)Ð)Ð)Ñ)ðFð Fð Fð FðP4ð 4ð 4ð 4ð<@ð @ð @ð @ð @ð @r"   )'Ú
__future__r   Úcopyr   Údataclassesr   Útypingr   r   r   ÚnumpyrO   Únumpy.typingr¿   Úsupervision.configr	   Úsupervision.detection.corer
   Ú'supervision.detection.utils.iou_and_nmsr   r   r   Úsupervision.draw.colorr   Úsupervision.metrics.corer   r   r   Ú"supervision.metrics.utils.matchingr   Ú%supervision.metrics.utils.object_sizer   r   Úsupervision.metrics.utils.utilsr   rü   rÿ   r   r2   râ   r"   r    ú<module>r@     sï  ðØ "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +à Ð Ð Ð Ø Ð Ð Ð Ð Ð à 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1ðð ð ð ð ð ð ð ð ð ð
 8Ð 7Ð 7Ð 7Ð 7Ð 7Ø JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ Jðð ð ð ð ð ðð ð ð ð ð ð ð ð DÐ CÐ CÐ CÐ CÐ Càð ØÐÐÐðr6ð r6ð r6ð r6ð r6�Ð(Ô)ñ r6ô r6ð r6ðj ðXð Xð Xð Xð Xñ Xô Xñ „ðXð Xð Xr"   