§
    >²¸ju‚  ã                  ó0  — 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 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)ÚCompactMask)Ú
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d8d„Zd9d	„Z	d:d„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dAd1„ZdBd3„ZdCd4„ZdDd6„Zd7S )EÚF1Scorea†  
    F1 Score is a metric used to evaluate object detection models. It is the harmonic
    mean of precision and recall, calculated at different IoU thresholds.

    In simple terms, F1 Score is a measure of a model's balance between precision and
    recall (accuracy and completeness), calculated as:

    `F1 = 2 * (precision * recall) / (precision + recall)`

    Examples:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> from supervision.metrics import F1Score
        >>> 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])
        ... )
        >>> f1_metric = F1Score()
        >>> f1_result = f1_metric.update(predictions, targets).compute()
        >>> round(float(f1_result.f1_50), 2)
        1.0

        ```

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

        Args:
            metric_target: The type of detection data to use.
            averaging_method: The averaging method used to compute the
                F1 scores. Determines how the F1 scores are aggregated across classes.
        N)Ú_metric_targetr   Ú_predictions_listÚ_targets_list)Úselfr   r   s      úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/metrics/f1_score.pyÚ__init__zF1Score.__init__E   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F1Score.resetX   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F1Score.update_   sÍ   € õ ˜+¥tÑ,Ô,ð 	(Ø&˜-ˆKÝ˜'¥4Ñ(Ô(ð 	 Ø�iˆGåˆ{ÑÔ�s 7™|œ|Ò+Ð+ÝðQ­c°+Ñ.>Ô.>ð Qð QÝ  ™\œ\ðQð Qð Qñô ð ð
 	Ô×%Ò% kÑ2Ô2Ð2ØÔ×!Ò! 'Ñ*Ô*Ð*àˆr#   ÚF1ScoreResultc                óf  — |                       | j        | j        ¦  «        }|                       | j        | j        t          j        ¦  «        |_        |                       | j        | j        t          j        ¦  «        |_        |                       | j        | j        t          j        ¦  «        |_	        |S )zÅ
        Calculate the F1 score metric based on the stored predictions and ground-truth
        data, at different IoU thresholds.

        Returns:
            The F1 score metric result.
        )
Ú_computer   r   r   ÚSMALLÚsmall_objectsÚMEDIUMÚmedium_objectsÚLARGEÚlarge_objects)r    Úresults     r!   ÚcomputezF1Score.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    �rè|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    rat3          t5          t6          j        t           j                 |	¦  «        t5          t6          j        t           j                 |¦  «        ¦  «        }n«| j        t.          j        k    rt?          |	|¦  «        }n…| j        t.          j         k    ratC          t5          t6          j        t           j                 |	¦  «        t5          t6          j        t           j                 |¦  «        ¦  «        }nt          d¦  «        ‚|t          j	        k    r|nd}tE          |||||¬¦  «        \  }}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¦  «         �Œ>|swtK          | j        | j&        t          j        |j'        d         ¦  «        t          j        d|j'        d         f¦  «        |t          j(        g t,          ¬¦  «        ddd¬¦	  «	        S d„ t          |Ž D ¦   «         } | j)        |Ž \  }}}tK          | 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   NzCF1Score metric requires `class_id` and `confidence` on predictions.©r   zCF1Score metric requires `class_id` on both predictions and targets.z4F1Score metric requires `confidence` on predictions.z-Unsupported metric target for IoU calculation)Útarget_scored_maské   ©Úaxis)	r   r   Ú	f1_scoresÚf1_per_classÚiou_thresholdsÚmatched_classesr7   r9   r;   c                ó8   — g | ]}t          j        |d ¦  «        ‘ŒS rF   )ÚnpÚconcatenate)Ú.0Úitemss     r!   ú
<listcomp>z$F1Score._compute.<locals>.<listcomp>O  s$   € ÐPÐPÐP¸5�bœn¨U°AÑ6Ô6ÐPÐPÐPr#   )*rP   ÚlinspaceÚfloat32ÚzipÚ_detections_contentÚonesr/   Úboolr   ÚANYr   r   ÚvalueÚclass_idÚ
confidencer0   ÚasarrayÚint32ÚappendÚzerosÚsizeÚbool_Úintr   ÚBOXESr   r   ÚnptÚNDArrayÚnumberÚMASKSr   ÚORIENTED_BOUNDING_BOXESr   r   Ú
zeros_likeÚanyr3   r   ÚshapeÚarrayÚ_compute_f1_for_classes)r    r>   r@   rA   rM   Ústatsr)   r+   Úprediction_contentsÚtarget_contentsÚprediction_size_maskÚtarget_size_maskÚprediction_class_idsÚprediction_confidenceÚtarget_class_idsÚiourG   ÚmatchesÚmatched_target_indicesÚignored_matchesÚvalid_target_matchÚmatched_scored_targetÚprediction_scoredÚprediction_keepÚconcatenated_statsrK   rL   Úunique_classess                               r!   r5   zF1Score._compute“   sf  € õ œ S¨$°½"¼*ÐEÑEÔEˆØˆå$'Ð(8¸,Ñ$GÔ$Gð [	ñ [	Ñ ˆ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Ý(ØRñô ð õ ,.¬:Ø#Ô,µB´Hð,ñ ,ô ,Ð(õ (*¤z°'Ô2BÍ"Ì(Ð'SÑ'SÔ'SÐ$Ý,.¬JØ#Ô.µb´jð-ñ -ô -Ð)ð Ô*­lÔ.@Ò@Ð@å+Ý ¥¤­R¬YÔ!7¸ÑIÔIÝ ¥¤­R¬YÔ!7Ð9LÑMÔMñô ˜˜ð Ô,µÔ0BÒBÐBÝ,¨_Ð>QÑRÔR˜˜ØÔ,µÔ0TÒTÐTå4Ý ¥¤­R¬YÔ!7¸ÑIÔIÝ ¥¤­R¬YÔ!7Ð9LÑMÔMñô ˜˜õ
 )Ø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Ýœ( >Ô#7¸Ô#:Ñ;Ô;ÝœX q¨.Ô*>¸qÔ*AÐ&BÑCÔCØ-Ý "¤¨µ3Ð 7Ñ 7Ô 7Ø"Ø#Ø"ð
ñ 
ô 
ð 
ð QÐPÅCÈÀKÐPÑPÔPÐØ2N°$Ô2NØð3
Ñ/ˆ	�< õ ØÔ-Ø!Ô2ØØ%Ø)Ø*ØØØð

ñ 

ô 

ð 
	
r#   rz   únpt.NDArray[np.bool_]r|   rw   únpt.NDArray[np.float32]rv   ú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 F1 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   rD   rI   rH   )rJ   Úweights)rP   ÚargsortÚuniquerQ   rb   rn   re   ÚsearchsortedÚ_compute_confusion_matrixÚ_compute_f1r   r   ÚMACROÚmeanÚMICROÚsumÚWEIGHTEDÚastyperV   Úaverage)r    rz   r|   rw   rv   r†   Úsorted_indicesr‚   Útrue_classesÚtrue_countsÚclass_countsÚconfusion_matrixrL   rK   Úconfusion_matrix_mergeds                  r!   rp   zF1Score._compute_f1_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ñ
ô 
Ðð
 ×'Ò'Ð(8Ñ9Ô9ˆð Ô ¥OÔ$9Ò9Ð9Ýœ °1Ð5Ñ5Ô5ˆIˆIØÔ"¥oÔ&;Ò;Ð;Ø&6×&:Ò&:¸1Ñ&=Ô&=Ð#Ø×(Ò(Ð)@ÑAÔAˆIˆIØÔ"¥oÔ&>Ò>Ð>Ø'×.Ò.­r¬zÑ:Ô:ˆLØ×ÒÑ!Ô! QÒ&Ð&õ œH \Ô%7¸Ô%:Ñ;Ô;�	�	åœJ |¸!À\ÐRÑRÔR�	à˜,¨Ð6Ð6r#   Úpredictions_classesÚtarget_classesry   rM   c                ó0   — t          | |||¦  «        \  }}|S )Nr   )r�   rž   ry   rM   Úresult_correctÚ_s         r!   Ú_match_detection_batchzF1Score._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.
        rH   r   é   rD   rI   )rn   rP   rb   Ú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_confusion_matrixs                    r!   rŽ   z!F1Score._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Ñ'Ñ'ð <LÐØ&Ð&r#   r›   c                ó  — | j         d         dk    st          d| j         › �¦  «        ‚| d         }| d         }| d         }d|z  |z   |z   }t          j        d|z  |t          j        |t          j        ¬¦  «        |d	k    ¬
¦  «        }|}|S )a_  
        Broadcastable function, computing the F1 score 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 F1 score for each element.
        éÿÿÿÿr¨   z/Confusion matrix must have shape (..., 3), got ).r   ).rH   ).é   r¹   rD   r   )ÚoutÚwhere)rn   r0   rP   Údividerl   r©   )r›   r³   r´   rµ   ÚdenominatorÚf1_scoreÚresult_f1_scores          r!   r�   zF1Score._compute_f1ä  s¾   € ð  Ô% bÔ)¨QÒ.Ð.Ýð,Ø#Ô)ð,ð ,ñô ð ð *¨&Ô1ˆØ*¨6Ô2ˆØ*¨6Ô2ˆð ˜.Ñ(¨?Ñ:¸_ÑLˆÝ”9Ø�ÑØÝ”˜kµ´Ð<Ñ<Ô<Ø Ò"ð	
ñ 
ô 
ˆð 4<ˆØÐr#   Ú
detectionsr   únpt.NDArray[Any] | CompactMaskc                ó^  — | j         t          j        k    r*t          t          j        t                   |j        ¦  «        S | j         t          j        k    rD|j	        �|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         › �¦  «        ‚)a  Return boxes, masks or oriented bounding boxes from detections.

        For the mask target this may return a
        :class:`~supervision.detection.compact_mask.CompactMask` rather than a
        dense boolean array when the detections carry compact masks.
        Nr   zGF1Score with `MetricTarget.MASKS` requires detections to include masks.rD   úInvalid metric target: )r   r   rf   r   rg   rh   r   Úxyxyrj   Úmaskr/   r0   Ú_make_empty_contentrk   ÚdataÚgetr	   rP   ro   rV   )r    rÀ   ÚobbÚ
result_obbs       r!   rX   zF1Score._detections_content  s  € ð Ô¥,Ô"4Ò4Ð4Ý�œ¥CÔ(¨*¬/Ñ:Ô:Ð:ØÔ¥,Ô"4Ò4Ð4ØŒÐ*à!”Ð&Ý�:‰Œ Ò"Ð"Ý ð%ñô ð ð ×+Ò+Ñ-Ô-Ð-ØÔ¥,Ô"FÒFÐFØ”/×%Ò%Õ&>Ñ?Ô?ˆCØˆ¥3 s¡8¤8¨a¢< <Ý68´h¸sÍ"Ì*Ð6UÑ6UÔ6U�
Ø!Ð!Ø×+Ò+Ñ-Ô-Ð-ÝÐH°4Ô3FÐHÐHÑIÔIÐIr#   únpt.NDArray[Any]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   é   rD   )r   r   r   )r   rÍ   r¹   rÃ   )
r   r   rf   rP   ÚemptyrV   rj   rZ   rk   r0   )r    Úempty_boxesÚempty_masksÚ	empty_obbs       r!   rÆ   zF1Score._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   r[   r   r   r\   rÄ   rÅ   r]   r^   Ú
tracker_idrÇ   rS   rP   ro   )r    rÀ   rA   Únew_detectionsÚsizesÚ	size_maskÚkeyr\   s           r!   Ú_filter_detections_by_sizez"F1Score._filter_detections_by_size0  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.
        )rW   ra   rÙ   )r    r>   r@   rA   Únew_predictions_listÚnew_targets_listr)   r+   s           r!   Ú'_filter_predictions_and_targets_by_sizez/F1Score._filter_predictions_and_targets_by_sizeJ  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$   r3   )r>   r?   r@   r?   rA   r   r$   r3   )rz   rƒ   r|   rƒ   rw   r„   rv   r…   r†   r…   r$   r‡   )
r�   r…   rž   r…   ry   r„   rM   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   rA   r   r$   r   )r>   r?   r@   r?   rA   r   r$   rÚ   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rf   r   r”   r"   r(   r2   r=   r   r[   r5   rp   Ústaticmethodr¢   rŽ   r�   rX   rÆ   rÙ   rÞ   © r#   r!   r   r   !   sx  € € € € € ð!ð !ðJ '3Ô&8Ø,;Ô,Dð2ð 2ð 2ð 2ð 2ð& ð  ð  ð  ðð ð ð ð>ð ð ð ð2 -?Ô,Bð	K
ð K
ð K
ð K
ð K
ðZ77ð 77ð 77ð 77ðr ð	ð 	ð 	ñ „\ð	ð ð<'ð <'ð <'ñ „\ð<'ð| ð ð  ð  ñ „\ð ðDJð Jð Jð Jð:
Jð 
Jð 
Jð 
Jðð ð ð ð46ð 6ð 6ð 6ð 6ð 6r#   r   r3   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 ) r3   ua  
    The results of the F1 score 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
            F1 scores. Determines how the F1 scores are aggregated across classes.
        f1_50: the F1 score at IoU threshold of `0.5`.
        f1_75: the F1 score at IoU threshold of `0.75`.
        f1_scores: the F1 scores at each IoU threshold.
            Shape: `(num_iou_thresholds,)`
        f1_per_class: the F1 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 `f1_per_class`. Classes that
            appear only in predictions (no ground-truth instances) are
            included; their per-threshold F1 values will be `0.0`.
        small_objects: the F1 metric results
            for small objects (area < 32Â²).
        medium_objects: the F1 metric results
            for medium objects (32Â² â‰¤ area < 96Â²).
        large_objects: the F1 metric results
            for large objects (area â‰¥ 96Â²).
    r   r   r   r   r$   Úfloatc                ó6   — t          | j        d         ¦  «        S )Nr   ©rç   rK   r'   s    r!   Úf1_50zF1ScoreResult.f1_50�  ó   € å�T”^ AÔ&Ñ'Ô'Ð'r#   c                ó6   — t          | j        d         ¦  «        S )Né   ré   r'   s    r!   Úf1_75zF1ScoreResult.f1_75…  rë   r#   r¦   rK   rL   r„   rM   r…   rN   zF1ScoreResult | Noner7   r9   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 F1Score
            >>> 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])
            ... )
            >>> f1_metric = F1Score()
            >>> f1_result = f1_metric.update(predictions, targets).compute()
            >>> print(f1_result)  # doctest: +ELLIPSIS
            F1ScoreResult:
            Metric target: MetricTarget.BOXES
            Averaging method: AveragingMethod.WEIGHTED
            F1 @ 50:     1.0000
            F1 @ 75:     1.0000
            F1 @ thresh: [1. ... 1.]
            IoU thresh:  [0.5  0.55 ... 0.95]
            F1 per class:
              0: [1. ... 1.]
            ...
            Medium objects:
              F1ScoreResult:
              Metric target: MetricTarget.BOXES
              Averaging method: AveragingMethod.WEIGHTED
              F1 @ 50:     0.0000
              ...

            ```
        z:
Metric target: z
Averaging method: z
F1 @ 50:     z.4fz
F1 @ 75:     z
F1 @ thresh: z
IoU thresh:  z
F1 per class:
r   z  No results
z  z: ú
Nz
Small objects:
z
Medium objects:
z
Large objects:
)Ú	__class__rà   r   r   rê   rî   rK   rM   rL   rc   rW   rN   r7   rï   Úreplacer9   r;   )r    Úout_strr]   Úf1_of_classÚindentÚindenteds         r!   Ú__str__zF1ScoreResult.__str__’  sÍ  € ðR Œ~Ô&ð ð Ø"Ô0ðð à!%Ô!6ðð ð !œJÐ.ðð ð !œJÐ.ð	ð ð
 !œNðð ð !Ô/ðð ð ð 	ð ÔÔ! QÒ&Ð&ØÐ'Ñ'ˆGÝ%(¨Ô)=¸tÔ?PÑ%QÔ%Qð 	8ð 	8Ñ!ˆH�kØÐ7˜HÐ7Ð7¨Ð7Ð7Ð7Ñ7ˆ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)úF1@50úF1@75Úsmall_objects_Úmedium_objects_Úlarge_objects_)Úindex)
r   Úpandasrê   rî   r7   Ú	to_pandasrS   r9   r;   Ú	DataFrame)r    ÚpdÚpandas_dataÚsmall_objects_dfrØ   r\   Úmedium_objects_dfÚlarge_objects_dfs           r!   r  zF1ScoreResult.to_pandasÖ  sH  € õ 	 Ñ!Ô!Ð!ØÐÐÐð ”ZØ”Zð'
ð '
ˆð
 ÔÐ)Ø#Ô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 F1 results.

        ![example_plot](
            https://media.roboflow.com/supervision-docs/metrics/f1_plot_example.png
        ){ align=center width="800" }
        r   )Úpyplotrû   rü   r¹   NzSmall: F1@50zSmall: F1@75r¨   zMedium: F1@50zMedium: F1@75zLarge: F1@50zLarge: F1@75rÍ   Ú	monospacezfont.family)rC   é   )ÚfigsizerH   ÚValueÚbold)Ú
fontweightz"F1 Score, 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   r7   r9   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Úcolorsr7   r9   r;   r¡   ÚaxÚtitleÚx_positionsÚbarsr"  Úy_values                  r!   ÚplotzF1ScoreResult.plotô  s¼  € ð 	-Ð,Ð,Ð,Ð,Ð,à˜7Ð#ˆØ”*˜dœjÐ)ˆÝ& qÔ)Ð*¨QÑ.ˆàÔÐ)Ø Ô.ˆMØ�~ ~Ð6Ñ6ˆFØ�}Ô*¨MÔ,?Ð@Ñ@ˆFØÕ+¨AÔ.Ð/°!Ñ3Ñ3ˆFàÔÐ*Ø!Ô0ˆNØ�¨Ð8Ñ8ˆFØ�~Ô+¨^Ô-AÐBÑBˆFØÕ+¨AÔ.Ð/°!Ñ3Ñ3ˆFàÔÐ)Ø Ô.ˆMØ�~ ~Ð6Ñ6ˆFØ�}Ô*¨MÔ,?Ð@Ñ@ˆ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  r4  rå   r#   r!   r3   r3   _  s  € € € € € € ðð ð:  ÐÐÑØ%Ð%Ð%Ñ%àð(ð (ð (ñ „Xð(ð ð(ð (ð (ñ „Xð(ð 'Ð&Ð&Ñ&Ø)Ð)Ð)Ñ)Ø+Ð+Ð+Ñ+Ø*Ð*Ð*Ñ*à'Ð'Ð'Ñ'Ø(Ð(Ð(Ñ(Ø'Ð'Ð'Ñ'ðBð Bð Bð BðH4ð 4ð 4ð 4ð<?ð ?ð ?ð ?ð ?ð ?r#   ))Ú
__future__r   Úcopyr   Údataclassesr   Útypingr   r   r   ÚnumpyrP   Únumpy.typingrg   Úsupervision.configr	   Ú"supervision.detection.compact_maskr
   Ú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   r3   rå   r#   r!   ú<module>rF     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àð ØÐÐÐð{6ð {6ð {6ð {6ð {6ˆf�_Ô%ñ {6ô {6ð {6ð| ðSð Sð Sð Sð Sñ Sô Sñ „ðSð Sð Sr#   