§
    >²¸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 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 d dlm Z m!Z! d dl"m#Z# erd dl$Z%e G d„ d¦  «        ¦   «         Z& G d„ de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)ÚMetricÚMetricTarget©Ú*_match_detection_batch_with_target_indices)ÚObjectSizeCategoryÚget_detection_size_category)Úensure_pandas_installedc                  óÎ   — e Zd ZU dZded<   edd„¦   «         Z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ed<   dd„Z	dd„Z
dd„ZdS ) ÚMeanAverageRecallResultu´  
    The results of the Mean Average Recall 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.
        mAR_at_1: the Mean Average Recall, when considering only the top
            highest confidence detection for each image.
        mAR_at_10: the Mean Average Recall, when considering top 10
            highest confidence detections for each image.
        mAR_at_100: the Mean Average Recall, when considering top 100
            highest confidence detections for each image.
        recall_per_class: the recall scores per max detection count, class and
            IoU threshold. Shape:
            `(num_max_detections, num_target_classes, num_iou_thresholds)`.
        max_detections: the array with maximum number of detections
            considered.
        iou_thresholds: the IoU thresholds used in the calculations.
        matched_classes: the class IDs of all matched classes.
            Corresponds to the class axis of `recall_per_class`.
        small_objects: the Mean Average Recall
            metric results for small objects (area < 32Â²).
        medium_objects: the Mean Average Recall
            metric results for medium objects (32Â² â‰¤ area < 96Â²).
        large_objects: the Mean Average Recall
            metric results for large objects (area â‰¥ 96Â²).
    r   Úmetric_targetÚreturnÚfloatc                ó6   — t          | j        d         ¦  «        S )Nr   ©r   Úrecall_scores©Úselfs    úw/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/metrics/mean_average_recall.pyÚmAR_at_1z MeanAverageRecallResult.mAR_at_1C   ó   € å�TÔ'¨Ô*Ñ+Ô+Ð+ó    c                ó6   — t          | j        d         ¦  «        S )Né   r   r   s    r!   Ú	mAR_at_10z!MeanAverageRecallResult.mAR_at_10G   r#   r$   c                ó6   — t          | j        d         ¦  «        S )Né   r   r   s    r!   Ú
mAR_at_100z"MeanAverageRecallResult.mAR_at_100K   r#   r$   únpt.NDArray[np.float64]r   Úrecall_per_classúnpt.NDArray[np.int32]Úmax_detectionsúnpt.NDArray[np.float32]Úiou_thresholdsÚmatched_classeszMeanAverageRecallResult | NoneÚsmall_objectsÚmedium_objectsÚlarge_objectsÚstrc                óÆ  — | j         j        › d| j        › d| j        d›d| j        d›d| j        d›d| j        › d| j        › d�}| j        j	        d	k    r|d
z  }t          | j        | j        ¦  «        D ]5\  }}|d|› d�z  }t          | j        |¦  «        D ]\  }}|d|› d|› d�z  }ŒŒ6d}| 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 MeanAverageRecall
            >>> 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])
            ... )
            >>> mar_metric = MeanAverageRecall()
            >>> mar_result = mar_metric.update(predictions, targets).compute()
            >>> print(mar_result)  # doctest: +ELLIPSIS
            MeanAverageRecallResult:
            Metric target:  MetricTarget.BOXES
            mAR @ 1:    1.0000
            mAR @ 10:   1.0000
            mAR @ 100:  1.0000
            max detections: [  1  10 100]
            IoU thresh:     [0.5  0.55 ... 0.95]
            mAR per class:
              @1:
                0: [1. ... 1.]
              @10:
                0: [1. ... 1.]
              @100:
                0: [1. ... 1.]
            ...
            Medium objects:
              MeanAverageRecallResult:
              Metric target:  MetricTarget.BOXES
              mAR @ 1:    0.0000
              ...

            ```
        z:
Metric target:  z
mAR @ 1:    z.4fz
mAR @ 10:   z
mAR @ 100:  z
max detections: z
IoU thresh:     z
mAR per class:
r   z  No results
z  @z:
z    z: ú
z  Nz
Small objects:
z
Medium objects:
z
Large objects:
)Ú	__class__Ú__name__r   r"   r'   r*   r.   r0   r,   ÚsizeÚzipr1   r2   r5   Úreplacer3   r4   )r    Úout_strr.   Úrecalls_at_kÚclass_idÚrecall_of_classÚindentÚindenteds           r!   Ú__str__zMeanAverageRecallResult.__str__Y   s  € ðZ Œ~Ô&ð  ð  Ø#Ô1ð ð  àœ=Ð0ð ð  ð  œ>Ð1ð ð  ð  œ?Ð2ð	 ð  ð
  $Ô2ð ð  ð  $Ô2ð ð  ð  ð 	ð Ô Ô%¨Ò*Ð*ØÐ'Ñ'ˆGÝ,/ØÔ Ô!6ñ-
ô -
ð 	Bð 	BÑ(ˆN˜Lð Ð0˜^Ð0Ð0Ð0Ñ0ˆGÝ-0°Ô1EÀ|Ñ-TÔ-Tð Bð BÑ)�˜/ØÐA (ÐAÐA¨oÐAÐAÐAÑA��ðBð ˆØÔÐ)Ø¥ 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        | 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mAR @ 1zmAR @ 10z	mAR @ 100Úsmall_objects_Úmedium_objects_Úlarge_objects_)Úindex)r   Úpandasr"   r'   r*   r2   Ú	to_pandasÚitemsr3   r4   Ú	DataFrame)r    ÚpdÚpandas_dataÚsmall_objects_dfÚkeyÚvalueÚmedium_objects_dfÚlarge_objects_dfs           r!   rL   z!MeanAverageRecallResult.to_pandas¥   sM  € õ 	 Ñ!Ô!Ð!ØÐÐÐð ”}ØœØœð'
ð '
ˆð ÔÐ)Ø#Ô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$   ÚNonec                ó¾  — ddl m} g d¢}| j        | j        | j        g}t
          d         gdz  }| j        �9| j        }|g d¢z  }||j        |j        |j        gz  }|t
          d         gdz  z  }| j        �9| j        }|g d¢z  }||j        |j        |j        gz  }|t
          d         gdz  z  }| j        �9| j        }|g d	¢z  }||j        |j        |j        gz  }|t
          d
         gdz  z  }d|j	        d<   | 
                    d¬¦  «        \  }}	|	                     dd¦  «         |	                     dd¬¦  «         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 Mean Average Recall results.

        ![example_plot](            https://media.roboflow.com/supervision-docs/metrics/mAR_plot_example.png            ){ align=center width="800" }
        r   )ÚpyplotrF   é   N)zSmall: mAR @ 1zSmall: mAR @ 10zSmall: mAR @ 100)zMedium: mAR @ 1zMedium: mAR @ 10zMedium: mAR @ 100r)   )zLarge: mAR @ 1zLarge: mAR @ 10zLarge: mAR @ 100é   Ú	monospacezfont.family)é
   é   )Úfigsizer&   ÚValueÚbold)Ú
fontweightz-Mean Average Recall, by Object Size
(target: ú)Úcenter)ÚcolorÚaligné-   Úright)ÚrotationÚhag{®Gáz”?z.2fÚbottom)ri   Úvaz
sans-serif)Ú
matplotlibrX   r"   r'   r*   r   r2   r3   r4   ÚrcParamsÚsubplotsÚset_ylimÚ
set_ylabelr   rS   Ú	set_titleÚrangeÚlenÚbarÚ
set_xticksÚset_xticklabelsÚ
get_heightÚtextÚget_xÚ	get_widthÚtight_layoutÚshow)r    ÚpltÚlabelsÚvaluesÚcolorsr2   r3   r4   Ú_ÚaxÚtitleÚx_positionsÚbarsrt   Úy_values                  r!   ÚplotzMeanAverageRecallResult.plotÄ   sÕ  € ð 	-Ð,Ð,Ð,Ð,Ð,à5Ð5Ð5ˆØ”- ¤°´ÐAˆÝ& qÔ)Ð*¨QÑ.ˆàÔÐ)Ø Ô.ˆMØÐOÐOÐOÑOˆFØØÔ&ØÔ'ØÔ(ðñ ˆFð
 Õ+¨AÔ.Ð/°!Ñ3Ñ3ˆFàÔÐ*Ø!Ô0ˆNØÐRÐRÐRÑRˆFØØÔ'ØÔ(ØÔ)ðñ ˆFð
 Õ+¨AÔ.Ð/°!Ñ3Ñ3ˆFàÔÐ)Ø Ô.ˆMØÐOÐOÐOÑOˆFØØÔ&ØÔ'ØÔ(ðñ ˆFð
 Õ+¨AÔ.Ð/°!Ñ3Ñ3ˆFà&1ˆŒ�]Ñ#à—’ W�Ñ-Ô-‰ˆˆ2Ø
�Š�A�qÑÔÐØ
�Š�g¨&ˆÑ1Ô1Ð1àX¸TÔ=OÔ=UÐXÐXÐXð 	ð 	�Š�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   r5   )r   rD   ©r   rV   )r9   Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__Úpropertyr"   r'   r*   rC   rL   r‡   © r$   r!   r   r   !   s5  € € € € € € ðð ð<  ÐÐÑàð,ð ,ð ,ñ „Xð,ð ð,ð ,ð ,ñ „Xð,ð ð,ð ,ð ,ñ „Xð,ð +Ð*Ð*Ñ*Ø-Ð-Ð-Ñ-Ø)Ð)Ð)Ñ)Ø+Ð+Ð+Ñ+Ø*Ð*Ð*Ñ*à1Ð1Ð1Ñ1Ø2Ð2Ð2Ñ2Ø1Ð1Ð1Ñ1ðJð Jð Jð JðX4ð 4ð 4ð 4ð>Ið Ið Ið Ið Ið Ir$   r   c                  óÆ   — e Zd ZdZej        fd8d„Zd9d„Zd:d„Zd;d„Z	e
j        fd<d„Zd=d„Zd>d"„Zed?d&„¦   «         Zed@d-„¦   «         ZedAd/„¦   «         ZdBd3„ZdCd5„ZdDd6„Zd7S )EÚMeanAverageRecalla<  
    Mean Average Recall (mAR) measures how well the model detects
    and retrieves relevant objects by averaging recall over multiple
    IoU thresholds, classes and detection limits.

    Intuitively, while Recall measures the ability to find all relevant
    objects, mAR narrows down how many detections are considered for each
    image. For example, mAR @ 100 considers the top 100 highest confidence
    detections for each image. mAR @ 1 considers only the highest
    confidence detection for each image.

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

        ```

    ![example_plot](
        https://media.roboflow.com/supervision-docs/metrics/mAR_plot_example.png
    ){ align=center width="800" }
    r   r   c                óf   — || _         g | _        g | _        t          j        g d¢¦  «        | _        dS )z‰
        Initialize the Mean Average Recall metric.

        Args:
            metric_target: The type of detection data to use.
        )r&   r\   éd   N)Ú_metric_targetÚ_predictions_listÚ_targets_listÚnpÚarrayr.   )r    r   s     r!   Ú__init__zMeanAverageRecall.__init__6  s8   € ð ,ˆÔà35ˆÔØ/1ˆÔå œh | | |Ñ4Ô4ˆÔÐÐr$   r   rV   c                ó"   — g | _         g | _        dS )zR
        Reset the metric to its initial state, clearing all stored data.
        N)r”   r•   r   s    r!   ÚresetzMeanAverageRecall.resetG  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Úlistrs   Ú
ValueErrorr”   Úextendr•   )r    r›   r�   s      r!   ÚupdatezMeanAverageRecall.updateN  sÍ   € õ ˜+¥tÑ,Ô,ð 	(Ø&˜-ˆKÝ˜'¥4Ñ(Ô(ð 	 Ø�iˆGåˆ{ÑÔ�s 7™|œ|Ò+Ð+ÝðQ­c°+Ñ.>Ô.>ð Qð QÝ  ™\œ\ðQð Qð Qñô ð ð
 	Ô×%Ò% kÑ2Ô2Ð2ØÔ×!Ò! 'Ñ*Ô*Ð*àˆr$   r   c                óf  — |                       | j        | j        ¦  «        }|                       | j        | j        t          j        ¦  «        |_        |                       | j        | j        t          j        ¦  «        |_        |                       | j        | j        t          j        ¦  «        |_	        |S )zó
        Calculate the Mean Average Recall metric based on the stored predictions
        and ground-truth, at different IoU thresholds and maximum detection counts.

        Returns:
            The Mean Average Recall metric result.
        )
Ú_computer”   r•   r   ÚSMALLr2   ÚMEDIUMr3   ÚLARGEr4   )r    Úresults     r!   ÚcomputezMeanAverageRecall.computem  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                óf	  ‡ ‡— ‰t           j        k    rˆ ˆfd„|D ¦   «         }t          j        dddt          j        ¬¦  «        }g }t          ||¦  «        D �]—\  }}‰                      |¦  «        }‰                      |¦  «        }	t          |¦  «        dk    �rS|j        �|j        €t          d¦  «        ‚t          |¦  «        dk    rÒt          j
        |j        t          j        ¬¦  «        }
t          |
¦  «        dk    rŒ­|                     t          j        d|j        ‰ j        j        ft           ¬¦  «        t          j        d|j        ft           ¬¦  «        t          j        dt"          ¬¦  «        t          j        dt"          ¬¦  «        |
f¦  «         �ŒF|j        €t          d	¦  «        ‚t          j
        |j        t          j        ¬¦  «        }t          j
        |j        t          j        ¬¦  «        }
t          j
        |j        t          j        ¬¦  «        }‰ j        t(          j        k    rat-          t/          t0          j        t          j                 |	¦  «        t/          t0          j        t          j                 |¦  «        ¦  «        }n«‰ j        t(          j        k    rt9          |	|¦  «        }n…‰ j        t(          j        k    rat=          t/          t0          j        t          j                 |	¦  «        t/          t0          j        t          j                 |¦  «        ¦  «        }nt          d
¦  «        ‚t          j        | ¦  «        }‰                       ||         |
|d d …|f         |¦  «        }t          j        t          |¦  «        |j        ft           ¬¦  «        }|                     ||t          j!        t          |¦  «        ¦  «        ||         |
f¦  «         �Œ™|s‰ j        j"        d         }tG          ‰ j        t          j        |¦  «        t          j        |d|j"        d         f¦  «        ‰ j        |t          j$        g t"          ¬¦  «        d d d ¬¦	  «	        S d„ t          |Ž D ¦   «         } ‰ j%        |Ž \  }}}tG          ‰ j        ||‰ j        ||d d d ¬¦	  «	        S )Nc                ó<   •— g | ]}‰                      |‰¦  «        ‘ŒS rŽ   )Ú_filter_detections_by_size)Ú.0r�   r    r®   s     €€r!   ú
<listcomp>z.MeanAverageRecall._compute.<locals>.<listcomp>Œ  s9   ø€ ð ð ð àð ×/Ò/°¸ÑGÔGðð ð r$   g      à?gffffffî?r\   ©Údtyper   zMMeanAverageRecall metric requires `class_id` on both predictions and targets.©r   z>MeanAverageRecall metric requires `confidence` on predictions.z-Unsupported metric target for IoU calculation)	r   r   r,   r.   r0   r1   r2   r3   r4   c                ó8   — g | ]}t          j        |d ¦  «        ‘ŒS r¶   )r–   Úconcatenate)r²   rM   s     r!   r³   z.MeanAverageRecall._compute.<locals>.<listcomp>ó  s$   € ÐPÐPÐP¸5�bœn¨U°AÑ6Ô6ÐPÐPÐPr$   )&r   ÚANYr–   ÚlinspaceÚfloat32r;   Ú_detections_contentrs   r?   r¡   ÚasarrayÚint32ÚappendÚzerosr:   r.   ÚboolÚintÚ
confidencer“   r   ÚBOXESr   r   ÚnptÚNDArrayÚnumberÚMASKSr   ÚORIENTED_BOUNDING_BOXESr   ÚargsortÚ_match_top_predictionsÚarangeÚshaper   r—   Ú#_compute_average_recall_for_classes)r    r«   r­   r®   r0   Ústatsr›   r�   Úprediction_contentsÚtarget_contentsÚtarget_class_idsÚprediction_class_idsÚprediction_confidenceÚiouÚsorted_indicesÚmatchesÚignored_matchesÚmax_detection_countÚconcatenated_statsÚrecall_scores_per_kÚrecall_per_class_at_kÚunique_classess   `  `                  r!   r¥   zMeanAverageRecall._compute‚  sÎ  øø€ ð Õ.Ô2Ò2Ð2ðð ð ð ð à+ðñ ô ˆLõ
 œ S¨$°½"¼*ÐEÑEÔEˆØˆå$'Ð(8¸,Ñ$GÔ$Gð M	ñ M	Ñ ˆK˜Ø"&×":Ò":¸;Ñ"GÔ"GÐØ"×6Ò6°wÑ?Ô?ˆOÝ�7‰|Œ|˜aÒÑØÔ'Ð/°7Ô3CÐ3KÝ$ð3ñô ð õ �{Ñ#Ô# qÒ(Ð(Ý')¤z°'Ô2BÍ"Ì(Ð'SÑ'SÔ'SÐ$ÝÐ+Ñ,Ô,°Ò1Ð1Ø Ø—L’LåœHØ!" NÔ$7¸Ô9LÔ9QÐ RÝ&*ðñ ô õ œH a¨Ô)<Ð%=ÅTÐJÑJÔJÝœH TµÐ5Ñ5Ô5ÝœH TµÐ5Ñ5Ô5Ø,ð	ñô ð ñ ð #Ô-Ð5Ý(ð+ñô ð õ ,.¬:Ø#Ô,µ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ñô ð õ &(¤ZÐ1FÐ0FÑ%GÔ%G�NØ"×9Ò9Ø,¨^Ô<Ø(Ø˜A˜A˜A˜~Ð-Ô.Ø&ñ	ô �Gõ ')¤hÝ˜^Ñ,Ô,¨nÔ.AÐBÍ$ð'ñ 'ô '�Oð —L’Là#Ø+ÝœI¥cÐ*?Ñ&@Ô&@ÑAÔAØ0°Ô@Ø,ðñô ð ùð ð 	Ø"&Ô"5Ô";¸AÔ">ÐÝ*Ø"Ô1Ý œhÐ':Ñ;Ô;Ý!#¤Ø(¨!¨^Ô-AÀ!Ô-DÐEñ"ô "ð  $Ô2Ø-Ý "¤¨µ3Ð 7Ñ 7Ô 7Ø"Ø#Ø"ðñ ô ð ð QÐPÅCÈÀKÐPÑPÔPÐà4ˆDÔ4Ð6HÐIñ 	CÐÐ2°Nõ 'ØÔ-Ø-Ø2ØÔ.Ø)Ø*ØØØð

ñ 

ô 

ð 
	
r$   Úsorted_prediction_class_idsr-   rÒ   Ú
sorted_iour/   r0   únpt.NDArray[np.bool_]c           	     óR  — |j         d         }t          j        ||j        | j        j        ft
          ¬¦  «        }t          | j        ¦  «        D ]W\  }}t          t          |¦  «        |¦  «        }	t          |d|	…         ||dd…d|	…f         |¦  «        \  }
}|
|d|	…dd…|f<   ŒX|S )a«  Match one image's predictions separately for each detection limit.

        The matcher pairs by highest IoU rather than by confidence, so matching every
        prediction and then keeping the top K would let a prediction ranked below K
        take a target from one ranked within it. Each limit therefore matches only its
        own top K predictions.

        Args:
            sorted_prediction_class_ids: shape (P,), prediction class ids sorted by
                descending confidence.
            target_class_ids: shape (T,), target class ids.
            sorted_iou: shape (T, P), IoU with prediction columns in the same order.
            iou_thresholds: shape (Th,), IoU thresholds.

        Returns:
            shape (P, Th, K), whether each prediction is a true positive when only
                the top `max_detections[k]` predictions are matched; predictions
                ranked below that limit are `False`.
        r   r´   N)
rÍ   r–   rÀ   r:   r.   rÁ   Ú	enumerateÚminrÂ   r   )r    rÞ   rÒ   rß   r0   Úprediction_countr×   Úlimit_indexr.   Ú	top_countÚlimit_matchesr�   s               r!   rË   z(MeanAverageRecall._match_top_predictions  s×   € ð4 7Ô<¸QÔ?ÐÝ”(Ø˜~Ô2°DÔ4GÔ4LÐMÝð
ñ 
ô 
ˆõ ,5°TÔ5HÑ+IÔ+Ið 	@ð 	@Ñ'ˆK˜Ý�C Ñ/Ô/Ð1AÑBÔBˆIÝIØ+¨J¨Y¨JÔ7Ø Ø˜1˜1˜1˜j˜y˜j˜=Ô)Øñ	 ô  ÑˆM˜1ð 3@ˆG�J�Y�J    ;Ð.Ñ/Ð/Øˆr$   r×   rØ   Úprediction_indicesrÓ   Útrue_class_idsúNtuple[npt.NDArray[np.float64], npt.NDArray[np.float64], npt.NDArray[np.int32]]c                ó’  — t          j        |d¬¦  «        \  }}|j        dk    rc| j        j        d         }|j        d         }	t          j        |t           j        ¬¦  «        t          j        |d|	ft           j        ¬¦  «        |fS g }
t          | j        ¦  «        D ]e\  }}||k     }|                      ||dd…|f         ||         ||         ||¦  «        }|  	                    |¦  «        }|
 
                    |¦  «         Œft          j        |
¦  «        }t          j        |d¬¦  «        }t          j        |d¬¦  «        }|||fS )	a8  Compute recall per detection limit and class from all images' matches.

        `matches` has shape (P, Th, K) and holds, for each detection limit, the
        matches of that limit's own top predictions; the other arrays have one row
        or entry per prediction, or per target for `true_class_ids`.
        T)Úreturn_countsr   r&   r´   Nr)   ©Úaxis)r–   Úuniquer:   r.   rÍ   rÀ   Úfloat64râ   Ú_compute_confusion_matrixÚ_compute_recallr¿   r—   Úmean)r    r×   rØ   rè   rÓ   ré   rÝ   Úclass_countsrÙ   Únum_thresholdsr>   rå   r.   Úis_within_limitÚconfusion_matrixr,   rÜ   Úaverage_recall_per_classr   s                      r!   rÎ   z5MeanAverageRecall._compute_average_recall_for_classes.  sq  € õ$ (*¤y°ÈtÐ'TÑ'TÔ'TÑ$ˆ˜àÔ !Ò#Ð#Ø"&Ô"5Ô";¸AÔ">ÐØ$œ]¨1Ô-ˆNå”Ð,µB´JÐ?Ñ?Ô?Ý”Ð-¨q°.ÐAÍÌÐTÑTÔTØðð ð 79ˆÝ+4°TÔ5HÑ+IÔ+Ið 	2ð 	2Ñ'ˆK˜à0°>ÒAˆOØ#×=Ò=Ø˜¨¨¨¨KÐ7Ô8Ø Ô0Ø$ _Ô5ØØñ ô  Ðð  $×3Ò3Ð4DÑEÔEÐØ×ÒÐ 0Ñ1Ô1Ð1Ð1õ !#¤¨Ñ 6Ô 6ÐÝ#%¤7Ð+@ÀqÐ#IÑ#IÔ#IÐ õ œÐ 8¸qÐAÑAÔAˆàÐ3°^ÐCÐCr$   Úpredictions_classesÚtarget_classesrÕ   c                ó0   — t          | |||¦  «        \  }}|S )Nr   )rù   rú   rÕ   r0   Úresult_correctr�   s         r!   Ú_match_detection_batchz(MeanAverageRecall._match_detection_batchd  s*   € õ GØ °°nñ
ô 
Ñˆ˜ð Ðr$   Úsorted_matchesÚsorted_ignored_matchesrÝ   únpt.NDArray[np.integer]rô   r+   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        |¦  «        }nD| |
         }||
         }|                     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.
            max_detections: The maximum number of detections to
                consider for each class. Extra detections are considered false
                positives. By default, all detections are considered.

        Returns:
            shape (C, Th, 3), containing the true positives, false
                positives, and false negatives for each class and IoU threshold.
        r&   r   rY   r´   rí   )rÍ   r–   rÀ   rð   râ   ÚsumÚfullÚstack)rþ   rÿ   rÞ   rÝ   rô   rõ   Únum_classesr÷   Ú	class_idxr?   Úis_classÚnum_trueÚnum_predictionsÚtrue_positivesÚfalse_positivesÚfalse_negativesÚlimited_matchesÚlimited_ignored_matchesÚresult_confusion_matrixs                      r!   rñ   z+MeanAverageRecall._compute_confusion_matrixp  s�  € ðB (Ô-¨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�Ý"$¤(¨>Ñ":Ô":��à"0°Ô":�Ø*@ÀÔ*JÐ'Ø!0×!4Ò!4°QÑ!7Ô!7�à$3Ð#3Ð7NÐ6NÑ#N×"SÒ"SÐTUÑ"VÔ"V�Ø"*¨^Ñ";�å*,¬(Ø °/ÐBÈð+ñ +ô +Ð˜YÑ'Ñ'ð <LÐØ&Ð&r$   r÷   c                óø   — | j         d         dk    st          d| j         › �¦  «        ‚| d         }| d         }||z   }t          j        ||t          j        |t          j        ¬¦  «        |dk    ¬¦  «        }|}|S )	a[  
        Broadcastable function, computing the recall 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 recall for each element.
        éÿÿÿÿrY   z/Confusion matrix must have shape (..., 3), got ).r   ).r)   r´   r   )ÚoutÚwhere)rÍ   r¡   r–   ÚdivideÚ
zeros_likerð   )r÷   r
  r  ÚdenominatorÚrecallÚresult_recalls         r!   rò   z!MeanAverageRecall._compute_recall³  s£   € ð  Ô% bÔ)¨QÒ.Ð.Ýð,Ø#Ô)ð,ð ,ñô ð ð *¨&Ô1ˆØ*¨6Ô2ˆà$ Ñ6ˆÝ”ØØÝ”˜kµ´Ð<Ñ<Ô<Ø Ò"ð	
ñ 
ô 
ˆð 28ˆØÐ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   zQMeanAverageRecall with `MetricTarget.MASKS` requires detections to include masks.r´   úInvalid metric target: )r“   r   rÄ   r   rÅ   rÆ   r   ÚxyxyrÈ   Úmaskrs   r¡   Ú_make_empty_contentrÉ   ÚdataÚgetr	   r–   r—   r»   )r    r  ÚobbÚ
result_obbs       r!   r¼   z%MeanAverageRecall._detections_contentÔ  s  € ð Ô¥,Ô"4Ò4Ð4Ý�œ¥CÔ(¨*¬/Ñ:Ô:Ð:ØÔ¥,Ô"4Ò4Ð4ØŒÐ*à!”Ð&Ý�:‰Œ Ò"Ð"Ý ð3ñô ð ð ×+Ò+Ñ-Ô-Ð-ØÔ¥,Ô"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   rZ   r´   )r   r   r   )r   rZ   r)   r  )
r“   r   rÄ   r–   Úemptyr»   rÈ   rÁ   rÉ   r¡   )r    Úempty_boxesÚempty_masksÚ	empty_obbs       r!   r  z%MeanAverageRecall._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“   rS   r  r  r?   rÃ   Ú
tracker_idr   rM   r–   r—   )r    r  r®   Únew_detectionsÚsizesÚ	size_maskrR   rS   s           r!   r±   z,MeanAverageRecall._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$   N)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/   r0   r/   r   rà   )r×   rà   rØ   rà   rè   r-   rÓ   r-   ré   r-   r   rê   )
rù   r-   rú   r-   rÕ   r/   r0   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   )r9   r‰   rŠ   r‹   r   rÄ   r˜   rš   r£   rª   r   r¹   r¥   rË   rÎ   Ústaticmethodrý   rñ   rò   r¼   r  r±   rŽ   r$   r!   r�   r�     s{  € € € € € ð#ð #ðN '3Ô&8ð5ð 5ð 5ð 5ð 5ð" ð  ð  ð  ðð ð ð ð>ð ð ð ð2 -?Ô,Bð	@
ð @
ð @
ð @
ð @
ðD(ð (ð (ð (ðT4Dð 4Dð 4Dð 4Dðl ð	ð 	ð 	ñ „\ð	ð ð@'ð @'ð @'ñ „\ð@'ðD ðð ð ñ „\ðð@Jð Jð Jð Jð:Jð Jð Jð Jðð ð ð ð ð r$   r�   )(Ú
__future__r   Úcopyr   Údataclassesr   Útypingr   r   r   Únumpyr–   Únumpy.typingrÅ   Úsupervision.configr	   Ú"supervision.detection.compact_maskr
   Úsupervision.detection.corer   Ú'supervision.detection.utils.iou_and_nmsr   r   r   Úsupervision.draw.colorr   Úsupervision.metrics.corer   r   Ú"supervision.metrics.utils.matchingr   Ú%supervision.metrics.utils.object_sizer   r   Úsupervision.metrics.utils.utilsr   rK   rO   r   r�   rŽ   r$   r!   ú<module>r@     sû  ðØ "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +à Ð Ð Ð Ø Ð Ð Ð Ð Ð à 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø :Ð :Ð :Ð :Ð :Ð :Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1ðð ð ð ð ð ð ð ð ð ð
 8Ð 7Ð 7Ð 7Ð 7Ð 7Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9ðð ð ð ð ð ðð ð ð ð ð ð ð ð DÐ CÐ CÐ CÐ CÐ Càð ØÐÐÐð ðkð kð kð kð kñ kô kñ „ðkð\Hð Hð Hð Hð H˜Ð8Ô9ñ Hô Hð Hð Hð Hr$   