
    ^j                       d dl mZ d dlZd dl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 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 d dlmZmZ d dlm Z  d dl!m"Z"  e"e#      Z$erd dl%Z&e	 G d d             Z' G d d      Z(dZ)dZ*dZ+ ejX                  ejZ                        j\                  Z/ G d de      Z0 G d d      Z1 G d d      Z2 G d de      Z3y)    )annotationsN)defaultdict)deepcopy)	dataclass)Enum)TYPE_CHECKINGAny)pyplot)
Detections)box_iou_batch_with_jaccard)LEGACY_COLOR_PALETTE)MetricMetricTarget)ensure_pandas_installed)_get_loggerc                      e Zd ZU dZded<   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Z	ded<   dZ
ded<   dZded<   ddZddZddZy)MeanAveragePrecisionResultu!  
    The result of the Mean Average Precision calculation.

    Defaults to `0` when no detections or targets are present.

    Attributes:
        metric_target: the type of data used for the metric -
            boxes, masks or oriented bounding boxes.
        is_class_agnostic: When computing class-agnostic results, class ID
            is set to `-1`.
        mAP_scores: the mAP scores at each IoU threshold.
            Shape: `(num_iou_thresholds,)`
        ap_per_class: the average precision scores per
            class and IoU threshold. Shape: `(num_target_classes, num_iou_thresholds)`
        iou_thresholds: the IoU thresholds used in the calculations.
        matched_classes: the class IDs of all matched classes.
            Corresponds to the rows of `ap_per_class`.
        small_objects: the mAP results
            for small objects (area < 32²).
        medium_objects: the mAP results
            for medium objects (32² ≤ area < 96²).
        large_objects: the mAP results
            for large objects (area ≥ 96²).
    r   metric_targetboolis_class_agnosticc                    | j                   | j                   dkD     }t        |      dkD  rt        |j                               S y)z5the mAP score at IoU thresholds from `0.5` to `0.95`.r   )
mAP_scoreslenfloatmean)selfvalid_scoress     u/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/metrics/mean_average_precision.pymap50_95z#MeanAveragePrecisionResult.map50_95;   s?     t';<|q **,--    c                2    t        | j                  d         S )z(the mAP score at IoU threshold of `0.5`.r   r   r   r   s    r   map50z MeanAveragePrecisionResult.map50D        T__Q'((r!   c                2    t        | j                  d         S )z)the mAP score at IoU threshold of `0.75`.   r#   r$   s    r   map75z MeanAveragePrecisionResult.map75I   r&   r!   znpt.NDArray[np.float64]r   ap_per_classiou_thresholdsznpt.NDArray[np.int32]matched_classesNz!MeanAveragePrecisionResult | Nonesmall_objectsmedium_objectslarge_objectsc                   | j                   | j                  | j                  ,d| j                  dd| j                  dd| j
                  dS d| j                  dd| j                  dd| j
                  dd| j                   j                  dd| j                  j                  dd| j                  j                  dS )aI  
        Formats the evaluation output metrics to match the structure used by pycocotools

        Example:
           ```pycon
           >>> import numpy as np
           >>> import supervision as sv
           >>> from supervision.metrics import MeanAveragePrecision
           >>> 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])
           ... )
           >>> map_metric = MeanAveragePrecision()
           >>> map_result = map_metric.update(predictions, targets).compute()
           >>> print(map_result)  # doctest: +ELLIPSIS
           Average Precision (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = ...
           Average Precision (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = ...
           Average Precision (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = ...
           Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = ...
           Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = ...
           Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = ...

           ```
        zHAverage Precision (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = z.3fzI
Average Precision (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = zI
Average Precision (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = zI
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = zI
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = zI
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = )r-   r.   r/   r    r%   r)   r$   s    r   __str__z"MeanAveragePrecisionResult.__str__V   s    > &""*!!)##'=="5 6##'::c"2 3##'::c"24#}}S1 2#zz#. /#zz#. /#11::3? @#22;;C@ A#11::3?A	
r!   c                4   t                ddl}| j                  | j                  | j                  d}| j
                  :| j
                  j                         }|j                         D ]  \  }}||d| <    | j                  :| j                  j                         }|j                         D ]  \  }}||d| <    | j                  :| j                  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	mAP@50:95zmAP@50zmAP@75small_objects_medium_objects_large_objects_)index)r   pandasr    r%   r)   r-   	to_pandasitemsr.   r/   	DataFrame)r   pdpandas_datasmall_objects_dfkeyvaluemedium_objects_dflarge_objects_dfs           r   r9   z$MeanAveragePrecisionResult.to_pandas   s4    	 ! jjjj
 )#11;;=.446 <
U6;nSE23<* $ 3 3 = = ?/557 =
U7<ocU34=)#11;;=.446 <
U6;nSE23< r||#
 	
r!   c                X   g d}| j                   | j                  | j                  g}t        d   gdz  }| j                  [|g dz  }|| j                  j                   | j                  j                  | j                  j                  gz  }|t        d   gdz  z  }| j
                  [|g dz  }|| j
                  j                   | j
                  j                  | j
                  j                  gz  }|t        d   gdz  z  }| j                  [|g dz  }|| j                  j                   | j                  j                  | j                  j                  gz  }|t        d	   gdz  z  }d
t        j                  d<   t        j                  d      \  }}|j                  dd       |j                  dd       |j                  dd       t        t        |            }|j                  |||d      }|j!                  |       |j#                  |dd       |D ]O  }|j%                         }	|j'                  |j)                         |j+                         dz  z   |	dz   |	ddd       Q dt        j                  d<   t        j,                          t        j.                          y)z
        Plot the mAP results.

        ![example_plot](
            https://media.roboflow.com/supervision-docs/metrics/mAP_plot_example.png
        ){ align=center width="800" }
        r3   r      N)zSmall: mAP@50:95zSmall: mAP@50zSmall: mAP@75)zMedium: mAP@50:95zMedium: mAP@50zMedium: mAP@75   )zLarge: mAP@50:95zLarge: mAP@50zLarge: mAP@75   	monospacezfont.family)
      )figsize   Valuebold)
fontweightzMean Average Precisioncenter)coloralign-   right)rotationhag{Gz?z.2fbottom)rU   vaz
sans-serif)r    r%   r)   r   r-   r.   r/   pltrcParamssubplotsset_ylim
set_ylabel	set_titleranger   bar
set_xticksset_xticklabels
get_heighttextget_x	get_widthtight_layoutshow)
r   labelsvaluescolors_axx_positionsbarsr_   y_values
             r   plotzMeanAveragePrecisionResult.plot   s    3--TZZ8&q)*Q.)LLF""++""((""(( F
 +A./!33F*OOF##,,##))##)) F
 +A./!33F)LLF""++""((""(( F
 +A./!33F&1]#W-2
Aq
g&1
-&ACK(vvk6xvH
k"
6B7; 	Cnn&GGG		cmmo11$3-  	 '3]#
r!   )returnr   )rq   str)rq   zpd.DataFramerq   None)__name__
__module____qualname____doc____annotations__propertyr    r%   r)   r-   r.   r/   r1   r9   rp    r!   r   r   r      s    2    ) ) ) ) ('))++**7;M4;8<N5<7;M4;9
v!
FBr!   r   c                      e Zd ZdZdddZedd       ZddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 ddZ		 	 d	 	 	 	 	 ddZ
ddd	Zdd
Zy)EvaluationDatasetzh
    Class used representing a dataset in the right format needed by the
    `COCOEvaluator` class.
    Nc                    t               | _        t               | _        t               | _        t               | _        t        t              | _        t        t              | _        |y|| _        | j                          y)z
        Constructor of EvaluationDataset object used to evaluate models with
        Mean Average Precision.

        Args:
            targets: The targets (ground truth) of the dataset in a the
                COCO format.
        N)
dictdatasetannscatsimgsr   listimg_to_annscat_to_imgscreate_class_members)r   targetss     r   __init__zEvaluationDataset.__init__   s_     (,v$(F	$(F	$(F	1<T1B1<T1B? !!#r!   c                     | d       S )Nr   r{   )clss    r   emptyzEvaluationDataset.empty  s    4  r!   c                P   i i i }}}t        t              t        t              }}d| j                  v r3| j                  d   D ]!  }||d      j                  |       |||d   <   # d| j                  v r| j                  d   D ]
  }|||d   <    d| j                  v r| j                  d   D ]
  }|||d   <    d| j                  v r<d| j                  v r.| j                  d   D ]  }||d      j                  |d           || _        || _        || _        || _        || _        y)z8
        Create index elements for the dataset.
        r   image_ididimages
categoriescategory_idN)	r   r   r   appendr   r   r   r   r   )	r   r   r   r   r   r   annimgcats	            r   r   z&EvaluationDataset.create_class_members  sH    r2Dd#.t#4k$6G[DLL(||M2 &C
O,33C8"%SY& t||#||H- &"%SY& 4<<'||L1 &"%SY& DLL(\T\\-I||M2 HC./66s:GH 	&&		r!   c                "   |s|s|s| j                   d   }n|rS|D cg c]  }|| j                  v r| j                  |   ! }}t        t        j                  j                  |            }n| j                   d   }|s|n|D cg c]  }|d   |v s| c}}|s|n'|D cg c]  }|d   |d   cxk  r	|d   k  sn n| c}}|du r|D cg c]  }|d   dk(  s|d    }	}|	S |D cg c]  }|d   	 }	}|	S c c}w c c}w c c}w c c}w c c}w )	a  
        Get annotation ids that satisfy given filter conditions.

        Args:
            img_ids: ids of the images that we want to retrieve.
            cat_ids: ids of the categories that we want to retrieve.
            area_range: area range of the annotations that we want to retrieve
                in the format [min_area, max_area].
            iscrowd: if annotations to retrieve are `iscrowded=1`.
        r   r   r   arearK   Tiscrowdr   )r   r   r   	itertoolschainfrom_iterable)
r   img_idscat_ids
area_ranger   r   img_idlistsr   idss
             r   get_annotation_idsz$EvaluationDataset.get_annotation_ids:  sL   $ wz<<.D #*!1!11 $$V, 
 IOO99%@A||M2
  %)KcS-?7-JcK  "  $(:a=3v;+VTU+VC  d?(,DI!0C3t9DCD 
 )--3t9-C-
= L E-s/   $C8C=C=D9DDD(Dc                D   |s|s|s| j                   d   }nc| j                   d   }|s|n|D cg c]  }|d   |v s| c}}|s|n|D cg c]  }|d   |v s| c}}|s|n|D cg c]  }|d   |v s| c}}|D cg c]  }|d   	 }}|S c c}w c c}w c c}w c c}w )aX  
        Get category ids that satisfy given filter conditions.

        Args:
            cat_names: names of the categories to retrieve.
            supercategory_names: names of the supercategories to retrieve.
            cat_ids: ids of the categories to retrieve.

        Returns:
            ids: integer array of category ids.
        r   namesupercategoryr   )r   )r   	cat_namessupercategory_namesr   r   r   r   s          r   get_category_idsz"EvaluationDataset.get_category_idsp  s    $ !4W<<-D<<-D
 ! %)FcS[I-EcF  +  $(3+?CV+VC  $)V#TgAU#)V  %))Ss4y))
# G *W)s(   BB
BB&B3B>Bc                8   |s'|s%| j                   j                         }t        |      S |rt        |      n	t               }|rNt	        |      D ]@  \  }}|dk(  r|st        | j
                  |         }&|t        | j
                  |         z  }B t        |      S )z
        Get image ids that satisfy given filter conditions.

        Args:
            img_ids: ids of the images to retrieve.
            cat_ids: ids of the categories to retrieve.

        Returns:
            ids: integer array of image ids.
        r   )r   keysr   set	enumerater   )r   r   r   r   ids_seticat_ids          r   get_image_idszEvaluationDataset.get_image_ids  s      w)).."C9")#g,su&w/ =	66'!$"2"26":;Gs4#3#3F#;<<G	= G}r!   c                N    |g S |D cg c]  }| j                   |    c}S c c}w )z
        Get annotations with the specified ids.

        Args:
            ids: integer ids specifying annotations.

        Returns:
            anns: loaded annotations.
        )r   )r   r   idxs      r   get_annotationsz!EvaluationDataset.get_annotations  s*     ;I*-.3		#...s   "c           	     |   t         j                         }| j                  d   D cg c]  }| c}|j                  d<   t        |t              st        d      t        |      dk(  rg |j                  d<   |S |D cg c]  }|d   	 }}t        |      t        |      t        | j                               z  k(  sJ d       d|d   v rt        d      d	|d   v rt        d
      d|d   v rt        d      d|d   v r|d   d   g k(  st        j                  | j                  d         |j                  d<   t        |      D ]K  \  }}|d   \  }}}	}
|||	z   |||
z   g\  }}}}d	|vr||||||||gg|d	<   d|vr|	|
z  |d<   |dz   |d<   d|d<   M ||j                  d<   |j                          |S c c}w c c}w )z
        Load prediction result into an EvaluationDataset object.

        Args:
            predictions: prediction result.

        Returns:
            EvaluationDataset object representing the predictions.
        r   zresults must be a listr   r   r   z-Results do not correspond to current coco setcaptionz5Evaluating predictions with caption is not supported.segmentationz:Evaluating predictions with segmentation is not supported.	keypointsz7Evaluating predictions with keypoints is not supported.bboxr   r   rK   r   r   )r}   r   r   
isinstancer   
ValueErrorr   r   r   NotImplementedErrorcopyr   r   r   )r   predictionspredictions_datasetr   predr   r   xywhx1x2y1y2s                  r   load_predictionsz"EvaluationDataset.load_predictions  s-    0557@DX@V0W0W##H-+t,566 {q 9;''6&&,78DtJ88 3xCHs4+=+=+?'@@A 	
;	
A
 A&%G  {1~-%L  KN*%I  {1~%k!nV.D.J8<\*9''5
 '{3 $	T!&\
1a"#QUAq1u!5BB "--/RRR,L+MD(%#$q5DL 1WT
"#Y$ 6A##M2002""i 1X 9s   	F4=F9)N)r   dict[str, Any] | None)rq   r}   rs   )NNNF)
r   list[int] | Noner   r   r   ztuple[float, float] | Noner   r   rq   	list[int])NNN)r   list[str] | Noner   r   r   r   rq   r   )NN)r   r   r   r   rq   r   )r   r   rq   list[dict[str, Any]])r   r   rq   r}   )ru   rv   rw   rx   r   classmethodr   r   r   r   r   r   r   r{   r!   r   r}   r}      s    
$2 ! !@ %)$(154!4 "4 /	4
 4 
4p '+04$(	,#, ., "	,
 
,` %)$(! " 
	>/@#r!   r}   i   i $  g    _Bc                       e Zd ZdZdZdZdZdZy)
ObjectSizez
    Enum for object size.
    allsmallmediumlargeN)ru   rv   rw   rx   ALLSMALLMEDIUMLARGEr{   r!   r   r   r     s     CEFEr!   r   c                      e Zd ZdZddZy)COCOEvaluatorParametersz&
    Parameters for COCOEvaluator
    c           	        g | _         g | _        t        j                  ddt	        t        j
                  d            dz   dt        j                        | _        t        j                  ddt	        t        j
                  d	            dz   dt        j                        | _        g d
| _	        dt        gdt        gt        t        gt        t        gg| _        y)z(Initialize all parameters for evaluation      ?gffffff?g!@rK   T)endpointdtype        g      ?g      Y@)rK   rH   d   r   N)r   r   nplinspaceintroundfloat32iou_thrsrec_thrsmax_detsMAX_ALL_OBJECT_AREASMALL_OBJECT_AREAMEDIUM_OBJECT_AREAr   r$   s    r   r   z COCOEvaluatorParameters.__init__'  s     #%"$ ,-.2**
 ,-.2**
 % #$!" 23!45	.
r!   Nrs   )ru   rv   rw   rx   r   r{   r!   r   r   r   "  s    
r!   r   c                  d    e Zd ZdZ	 	 	 	 d
dZddZddZ	 	 	 	 	 	 	 	 	 	 ddZddZddZ	ddZ
y	)COCOEvaluatorz2
    Evaluator class to compute COCO metrics.
    c                   |t        d      |t        d      || _        || _        t        t              | _        i | _        t        t              | _        t        t              | _        t               | _
        g | _        i | _        t        | j                  j                               | j                  _        t        | j                  j!                               | j                  _        y)z
        Constructor of COCOEvaluator object.

        Args:
            coco_targets: The dataset with the ground truths.
            coco_predictions: The dataset with the predictions.
        Nzcoco_targets must be providedz!coco_predictions must be provided)r   coco_targetscoco_predictionsr   r   	eval_imgsresults_targets_predictionsr   paramsstatsioussortedr   r   r   r   )r   r   r   s      r   r   zCOCOEvaluator.__init__M  s     <==#@AA( 0 *$/')ALTAREPQUEV-/ "
02	$T%6%6%D%D%FG$T%6%6%G%G%IJr!   c                   | j                   j                  | j                  j                  | j                  j                        }| j                   j                  |      }| j                  j                  | j                  j                  | j                  j                        }| j                  j                  |      }|D ]  }d|v r|d   nd|d<   d|v xr |d   |d<     t        t              | _	        |D ](  }| j                  |d   |d   f   j                  |       * t        t              | _        |D ](  }| j                  |d   |d   f   j                  |       * t        t              | _        i | _        y)zA
        Prepare targets and predictions for evaluation.
        )r   r   ignorer   r   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   annotation_idsr   prediction_idsr   gtdts          r    _prepare_targets_and_predictionsz.COCOEvaluator._prepare_targets_and_predictionsq  sy   
 **==KK''1D1D > 
 ##33NC..AAKK''1D1D B 
 ++;;NK  	=B+3r>2h<qBxL$?<r)}BxL	=
 $D) 	HBMM"Z."]*;;<CCBG	H (- 	LBbnb.??@GGK	L %T*r!   c                   | j                   ||f   }| j                  ||f   }t        |      dk(  r5t        |      dk(  r't        j                  g t        j
                        }|S t        j                  |D cg c]  }|d    
 c}d      }|D cg c]  }||   	 }}t        |      | j                  j                  d   kD  r|d| j                  j                  d    }|D 	cg c]  }	|	d   	 }
}	|D cg c]  }|d   	 }}|D cg c]  }t        |d          }}t        |
||      j                  t        j
                        }|S c c}w c c}w c c}	w c c}w c c}w )	a  
        Compute the IoU between the targets and predictions for a given image and
        category.

        Args:
            img_id: The image id.
            cat_id: The category id.

        Returns:
            The IoU between the targets and predictions.
        r   r   scorestablekindr   r   r   )r   r   r   r   arrayr   argsortr   r   r   r   astype)r   r   r   r   r   empty_resultdindsr   ggt_boxesdt_boxesois_crowdious                  r   _compute_iouzCOCOEvaluator._compute_iou  sU    ]]66>*vv~. r7a<CGqL46HHRrzz4RL zz31AgJ;3(C!"be"" r7T[[))"--A,,R01B')*!AfI**')*!AfI** 1331D9&33(8XFMMJJ
 
# 4" +* 4s   9EE&E8E
E!c                V   | j                   ||f   }| j                  ||f   }t        |      dk(  rt        |      dk(  ry|\  }}|D ]"  }	|	d   s||	d   cxk  r|k  sn d|	d<   d|	d<   $ t        j                  |D 	cg c]  }	|	d   	 c}	d      }
|
D cg c]  }||   	 }}t        j                  |D cg c]  }|d	    
 c}d      }|d| D cg c]  }||   	 }}t        | j
                  ||f         dkD  r| j
                  ||f   dd|
f   n| j
                  ||f   }t        | j                  j                        }t        |      }t        |      }t        j                  ||f      }t        j                  ||f      }t        j                  |D 	cg c]  }	|	d   	 c}	      }t        j                  ||f      }t        |      dk7  rt        | j                  j                        D ]  \  }}t        |      D ]  \  }}t        |d
g      }d}t        |      D ]X  \  }}	t        |	j                  dd            }|||f   dkD  r|s.|dkD  r||   dk(  r
||   dk(  r n|||f   |k\  sP|||f   }|}Z |dk7  s||   |||f<   ||   d   |||f<   |d   |||f<     t        j                  |D cg c]  }|d   |k  xs |d   |kD   c}      j                  dt        |      f      }t        j                  |t        j                   |dk(  t        j"                  ||d                  }|||||D cg c]  }|d   	 c}|D 	cg c]  }	|	d   	 c}	|||D cg c]  }|d	   	 c}||dS c c}	w c c}w c c}w c c}w c c}	w c c}w c c}w c c}	w c c}w )a-  
        Perform evaluation for single category and image.
        Args:
            img_id: The image id.
            cat_id: The category id.
            area_range: The area range.
            max_det: The maximum number of detections.

        Returns:
            The evaluation results.
        r   Nr   r   rK   _ignorer  r  r  gA?r   r   r   )r   r   r   max_detdt_idsgt_ids	dtMatches	gtMatchesdtScoresgtIgnoredtIgnore)r   r   r   r   r	  r   r   r   zerosr  r   minr   getreshape
logical_orlogical_andrepeat)r   r   r   r   r  r   r   min_areamax_arear  	gt_sortedr   r  	dt_sortedr   num_thresholdsnum_ground_truthsnum_detections
gt_matches
dt_matches	gt_ignore	dt_ignore	tresh_idxthreshdet_idxdetiou_best_matchbest_match_idxg_idxr   area_range_masks                                  r   _evaluate_imagezCOCOEvaluator._evaluate_image  s   & $(==#@#'#4#4VV^#D r7a<CGqL'(  	!A{8qy#DH#D ) )		! JJb9)9I	&'be'' JJR878xH	&q12be22
 499VV^,-1 IIffn%al366>* 	 T[[112GR XX~/@AB
XX~~>?
HHB7qa	l78	HHnn=>	t9>%.t{{/C/C%D  J!	6$-bM JLGS%(&))<%=N%'N$-bM 3q #&aeeIq&9":%i&67!;G$ +R/ ). 9 !! )% 0A 5!  />A-1'5.-AN-2N%3* &+8A.8Q	)W"459;N9KD9Q
9g#56@CD	
9n#<==J JF ((CEFaQvY!9QvY%99F

'1c"g,
 	
 MMNNa?NA!N
	 !$(*+1qw+(*+1qw+##-/070!!
 	
U  :'  92$ 8P G  ,+ 1s6   ?M>N9NN NNNN!+N&c                   t        | j                  j                        }t        | j                  j                        }t        | j                  j                        }t        | j                  j
                        }t        | j                  j                        }t        | j                  j                        }t        j                  |||||ft        j                         }t        j                  ||||ft        j                         }t        j                  |||||ft        j                         }	t        | j                  j                        }
| j                  j
                  D ch c]  }t        |       }}t        | j                  j                        }t        | j                  j                        }t        | j                  j                        D cg c]  \  }}||
v s| }}}| j                  j                  D cg c]	  }||v s| }}t        | j                  j
                        D cg c]  \  }}t        |      |v r| }}}t        | j                  j                        D cg c]  \  }}||v s| }}}t        |      D ]  \  }}||z  |z  }t        |      D ]  \  }}||z  }t        |      D ]  \  }} |D cg c]  }| j                  ||z   |z       }!}|!D "cg c]  }"|"|"	 }!}"t        |!      dk(  rJt        j                  |!D "cg c]
  }"|"d   d|   c}"      }#t        j                   |# d      }$|#|$   }%t        j                  |!D "cg c]  }"|"d   ddd| f    c}"d	      dd|$f   }&t        j                  |!D "cg c]  }"|"d
   ddd| f    c}"d	      dd|$f   }'t        j                  |!D "cg c]  }"|"d   	 c}"      }(t        j"                  |(dk(        })|)dk(  rEt        j$                  |&t        j&                  |'            }*t        j$                  t        j&                  |&      t        j&                  |'            }+t        j(                  |*d	      j+                  t        j                        },t        j(                  |+d	      j+                  t        j                        }-t        t-        |,|-            D ]  \  }.\  }/}0t        j.                  |/      }/t        j.                  |0      }0t        |/      }1|/t        j                  |)      z  }2|/|0|/z   t0        z   z  j3                         }3dg|z  }4dg|z  }5|1r|2d   nd||.|||f<   t5        |1dz
  dd      D ]  }|3|   |3|dz
     kD  s|3|   |3|dz
  <    t        j6                  |2| j                  j                  d      }6|6j3                         }7t        |7      D ]P  \  }8}9t9        |9      }:d|:cxk  rt        |3      k  s&n )d|:cxk  rt        |%      k  s>n A|3|:   |4|8<   |%|:   |5|8<   R t        j.                  |4t        j                        ||.dd|||f<   t        j.                  |5t        j                        |	|.dd|||f<       | j                  |||||gt:        j:                  j=                         j?                  d      |||	d| _         	 	 	 	 dd};tC        tD              jG                  tD        jH                        }<| j                  j                  jG                  d      }=|dddddd|<|=f   }> |;|>      \  }?}@tC        tD              jG                  tD        jJ                        }A|dddddd|A|=f   }B |;|B      \  }C}DtC        tD              jG                  tD        jL                        }E|dddddd|E|=f   }F |;|F      \  }G}HtC        tD              jG                  tD        jN                        }I|dddddd|I|=f   }J |;|J      \  }K}L| j                  |||||gt:        j:                  j=                         j?                  d      |||	|?|@|C|D|G|H|K|Ld| _         yc c}w c c}}w c c}w c c}}w c c}}w c c}w c c}"w c c}"w c c}"w c c}"w c c}"w )z^
        Accumulate per image evaluation results and store the result in self.results
        r  Nr   r  r  r  r  rK   axisr  r  r   r   left)sidez%Y-%m-%d %H:%M:%S)r   countsdate	precisionrecallscoresc                    | dk7  t        j                  | t        j                  d            	 	 	 	 dfd} |d      } |d      j                  dd      }||fS )z<Compute average precision while handling -1 sentinel values.r   r   c           	     <   j                  | t        j                        }j                  |       }t        j                  ||t        j                  |j
                  dt        j                        |dkD        }|j                  t        j                        S )N)r<  r   r;        r  r   )outwhere)sumr   float64dividefullshaper
  r   )r<  sumsr?  means
valid_maskvalid_precisions       r   mean_with_maskzTCOCOEvaluator._accumulate.<locals>.compute_average_precision.<locals>.mean_with_mask  sv     '**BJJ*G#T2		

D

C 1*	 ||BJJ//r!   )rK   rE   rK   r   )r<  zint | tuple[int, ...]rq   npt.NDArray[np.float32])r   rH  r   	transpose)precision_slicerR  r   r*   rP  rQ  s       @@r   compute_average_precisionz<COCOEvaluator._accumulate.<locals>.compute_average_precision  sl     )B.J hhz?BJJsOTO0+0(0 (/J)!,66q!<L|++r!   r   )r   r?  r@  rA  rB  rC  mAP_scores_all_sizesap_per_class_all_sizesmAP_scores_smallap_per_class_smallmAP_scores_mediumap_per_class_mediummAP_scores_largeap_per_class_large)rU  rS  rq   z7tuple[npt.NDArray[np.float32], npt.NDArray[np.float32]])(r   r   r   r   r   r   r   r   r   onesr   r   tupler   r   concatenater	  count_nonzeror$  logical_notcumsumr
  zipr  EPStolistr^   searchsortedr   datetimenowstrftimer   r   r   r7   r   r   r   r   )Mr   num_iou_thresholdsnum_recall_thresholdsnum_categoriesnum_area_rangesnum_max_detectionsnum_imgsrA  rB  rC  set_categoriesaset_area_rangesset_max_detectionsset_image_idsnkselected_category_idsmselected_max_detectionsr   r   selected_area_ranges_idsr   
image_indscat_idxcat_eval_idx
cat_offsetarea_idxarea_eval_idxarea_offsetmax_det_idxr  eval_img_datae	dt_scoresr  dt_scores_sortedr.  
dt_ignored
gt_ignorednum_non_ignored_gttrue_positivesfalse_positivestp_sumfp_sumiou_thresh_idxtpfpnum_tpsrcprprecision_at_recallscore_at_recallrecall_indsrecall_inds_listripos_idx_valuepos_idx_intrV  area_range_idxmax_100_dets_idxaverage_precision_all_sizesrW  rX  small_area_range_idxaverage_precision_smallrY  rZ  medium_area_range_idxaverage_precision_mediumr[  r\  large_area_range_idxaverage_precision_larger]  r^  sM                                                                                r   _accumulatezCOCOEvaluator._accumulate9  s   
 !!5!56 #DKK$8$8 9T[[001dkk445 !5!56t{{**+ WW"%" **	
 	
	 ''BTU**
 
 ''"%" **	
 	
 T[[001"kk443
E!H3
 3
 !!5!56DKK//0 $DKK$7$78!
!QA<OA!
 !

 {{++#
q4F/FA#
 #
 't{{'='=>$
TT{o- $
  $
 $DKK$7$78
!QA<NA

 
 &//D%E a	!G\%7(BJ ,55M+N ]'-+h6 -66M,N Y(KNX%IJzK'?!'CD%M % 1>$O1Q$OM$O =)Q.  !#;HIa:q1I!I ::yjx@D'0$ "$?LM!;1W95MTU"g"J "$>KL:q!G)|4LST"g"J
 "$0V1:0V!WJ)+)9)9*/)J& *Q.  &(^^"BNN:$>&N ')nnz2BNN:4N'O  YY~A>EEBJJEWFYYQ?FFRZZFXF 5>c&&>Q4R ,0RXXb\XXb\"%b'"**-?"@@ BGcM2::</2e6K.K++.%2G*G
 '.BrF1 ~w+MN
 "'w{Ar!: 2A!!ur!a%y0,.qE1q5	2 =?OO 4 46= 7B6H6H6J(1:;K1L T-B/2=/AK K9#b'9a; ?QT 0R ? ;=[/ 3B 76F{6S 3T HH%8

K ".!Wh"ST
 HH_BJJG ~q'8[PQU,[Y	]	a	H kk"%" %%))+445HI"
 	,4	,D	,2 j)//
?;;//55c:&/q!^%55'
#
 8Q'8
44
  $J/55j6F6FG"+q!)+;;#
 0I#0
,,
 !%Z 0 6 6z7H7H I#,q!*,<<$
  2K$2
..
  $J/55j6F6FG"+q!)+;;#
 0I#0
,,
 kk"%" %%))+445HI"$8&< 0"4!2#6 0"4)
a3
!
#
$

"% %P J N M 1WsZ   2b &b%3b%	b+b+b0b6b6b<
?c
c
/c5c-c%cc                     ddt         j                  df	 	 	 	 	 	 	 	 	 d fdd fd}t         j                        dk7  r |       j	                          _        yy)	zM
        Compute and display summary metrics for evaluation results.
        TNr   c           
     D   d}| rdnd}| rdnd}|7j                   j                  d   ddj                   j                  d	   dn|d}t        t              }|j	                  |      }	j                   j
                  j	                  |      }
| rVj                  d
   }|4t        j                  |j                   j                  k(        d   }||   }|d d d d d d |	|
f   }nRj                  d   }|4t        j                  |j                   j                  k(        d   }||   }|d d d d |	|
f   }t        ||d	kD           dk(  rd}n$t        t        j                  ||d	kD                 }t        j                  |j                  ||||||             |S )NzC {:<18} {} @[ IoU={:<9} | area={:>6s} | maxDets={:>3d} ] = {:0.10f}zAverage PrecisionzAverage Recallz(AP)z(AR)r   z0.2f:r   rA  rB  rF  )r   r   r   r   r7   r   r   r   rH  r   r   r   loggerinfoformat)use_apiou_thrr   r   iStrtitleStrtypeStriou_strall_object_sizesr  max_detections_idxstmean_sr   s                 r   
_summarizez8COCOEvaluator._pycocotools_summarize.<locals>._summarizeN  s    YD.4*:JH &fFG ? ;;''*40$++2F2Fr2J41PQ~ 
  $J/-33J?N!%!5!5!;!;H!E LL-&DKK,@,@!@A!DA!AaA~/AAB LL*&DKK,@,@!@A!DA!AaN,>>?1QV9~"rwwqRy12KKHgw
HfU Mr!   c                 :   t        j                  dt         j                        }  d      | d<    ddj                  j                  d         | d	<    dd
j                  j                  d         | d<    dt
        j                  j                  j                  d         | d<    dt
        j                  j                  j                  d         | d<    dt
        j                  j                  j                  d         | d<    dj                  j                  d         | d<    dj                  j                  d	         | d<    dj                  j                  d         | d<    dt
        j                  j                  j                  d         | d<    dt
        j                  j                  j                  d         | d<    dt
        j                  j                  j                  d         | d<   | S )N)   r  T)r  r   r   rE   )r  r  r   rK   g      ?)r  r   r   rD   rF   r(   F)r  r   rI         	   rH      )	r   r  r   r   r   r   r   r   r   )r   r  r   s    r   _summarize_predictionszDCOCOEvaluator._pycocotools_summarize.<locals>._summarize_predictionsy  s   -/XXe2::-NE!.E!H!S4;;3G3G3JE!H "TDKK4H4H4KE!H "%++--a0E!H
 "%,,--a0E!H
 "%++--a0E!H
 "9M9Ma9PQE!H!9M9Ma9PQE!H!9M9Ma9PQE!H!%++--a0E!H
 #%,,--a0E"I
 #%++--a0E"I
 Lr!   r   )
r  r   r  zfloat | Noner   r   r   r   rq   r   )rq   rS  )r   r   r   r   rg  r   )r   r  r  s   ` @r   _pycocotools_summarizez$COCOEvaluator._pycocotools_summarizeI  sz      $(%/^^	)	)	!)	 #)	 	)	
 )	V*	X t||!/188:DJ "r!   c                   t        t        j                  | j                  j                              | j                  _        t        t        j                  | j                  j
                              | j                  _        t        | j                  j                        | j                  _        | j                          | j                  j                  D ci c]2  }| j                  j
                  D ]  }||f| j                  ||       4 c}}| _
        | j                  j                  d   }| j                  j
                  D cg c]L  }| j                  j                  D ]1  }| j                  j                  D ]  }| j                  ||||       3 N c}}}| _        | j                          yc c}}w c c}}}w )z
        Start the per image evaluation on all images and keeep results in
        self.eval_imgs (a list of dictionaries).
        r   N)r   r   uniquer   r   r   r   r   r  r  r   r   r9  r   r  )r   r   r   r  r   s        r   evaluatezCOCOEvaluator.evaluate  sw    #299T[[-@-@#AB"299T[[-@-@#AB%dkk&:&:;--/
 ++--
++--
  Vd//??

	 ++&&r*
 ++--
 
"kk44
 ++--	
    WE
E
E
 	%

s   7F9AF?N)r   r}   r   r}   rs   )r   r   r   r   rq   rS  )
r   r   r   r   r   z!list[float] | tuple[float, float]r  r   rq   r   )ru   rv   rw   rx   r   r  r  r9  r  r  r  r{   r!   r   r   r   H  s{    "K-"KAR"KH D'R{
{
 {
 6	{

 {
 
{
zN
`];~r!   r   c                      e Zd ZdZej
                  dddf	 	 	 	 	 	 	 d
dZddZ	 	 	 	 	 	 ddZ	 	 	 	 ddZ		 	 	 	 ddZ
dd	Zy)MeanAveragePrecisiona  
    Mean Average Precision (mAP) is a metric used to evaluate object detection models.
    It is the average of the precision-recall curves at different IoU thresholds.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> from supervision.metrics import MeanAveragePrecision
        >>> 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])
        ... )
        >>> map_metric = MeanAveragePrecision()
        >>> map_result = map_metric.update(predictions, targets).compute()
        >>> round(float(map_result.map50), 2)
        1.0
        >>> print(map_result)
        Average Precision (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 1.000
        Average Precision (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 1.000
        Average Precision (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 1.000
        Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 1.000
        Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
        Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000

        ```

    ![example_plot](
        https://media.roboflow.com/supervision-docs/metrics/mAP_plot_example.png
    ){ align=center width="800" }
    FNc                X    || _         || _        g | _        g | _        || _        || _        y)aW  
        Initialize the Mean Average Precision metric.

        Args:
            metric_target: The type of detection data to use.
            class_agnostic: Whether to treat all data as a single class.
            class_mapping: A dictionary to map class IDs to new IDs.
            image_indices: The indices of the images to use.
        N)_metric_target_class_agnostic_predictions_list_targets_list_class_mapping_image_indices)r   r   class_agnosticclass_mappingimage_indicess        r   r   zMeanAveragePrecision.__init__  s4      ,-35/1++r!   c                     g | _         g | _        y)zR
        Reset the metric to its initial state, clearing all stored data.
        N)r  r  r$   s    r   resetzMeanAveragePrecision.reset  s     "$r!   c                   t        |t              s|g}t        |t              s|g}t        |      t        |      k7  r$t        dt        |       dt        |       d      | j                  r\t        |      }t        |      }|D ]  }|j                  d|j                  dd   |D ]  }|j                  d|j                  dd   | j                  j                  |       | 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 ground-truth detections.

        Returns:
            The updated metric instance.
        The number of predictions () and targets (z%) during the update must be the same.Nr   )
r   r   r   r   r  r   class_idr  extendr  )r   r   r   
predictiontargets        r   updatezMeanAveragePrecision.update  s    +t,&-K'4(iG{s7|+-c+.>-? @ \N*OQ 
 ";/Kw'G) 0
&&2-/J''*0 " ,??.)+FOOA&, 	%%k2!!'*r!   c           
        t        t        |            D cg c]  }d|i }}| j                  !|D cg c]  }d| j                  |d      i }}g }t        |      D ][  \  }}| j                  | j                  |   }|j                  /t        |j                        D ]  \  }}	|	d   |	d   |	d   |	d   z
  |	d   |	d   z
  g}
d}|j
                  ?|j
                  |   }| j                  | j                  t        |         }nt        |      }d}|j                  )d|j                  v rt        |j                  d   |         }||
d   |
d   z  }d}|j                  )d|j                  v rt        |j                  d   |         }||||
|t        |      dz   dd	} |j                  |        ^ |D ch c]  }|j                  d
       }}|D cg c]  }d|i }}|||dS c c}w c c}w c c}w c c}w )zJTransform targets into a dictionary that can be used by the COCO evaluatorr   Nr   rK   rE   rD   r   r   )r   r   r   r   r   r   r   r   )r   r   r   )r^   r   r  r   xyxyr  r  r   datar   r   r!  )r   r   r   r   r   r   r   image_targets
target_idxr  xywhr   cls_idr   r   dict_annotation
annotationall_cat_idsr   r   s                       r   _prepare_targetsz%MeanAveragePrecision._prepare_targets9  sb    05S\/BCV4.CC*HNOtT00T;<OFO,.'0'9 *	4#Hm""...x8 !!)$-m.@.@$A "4 
DQa$q'DG*;T!WtAw=NO   ))5*33J?F**6&*&9&9#f+&F&)&k  %%1f@R@R6R !3!3F!;J!GHD<7T!W,D %%1i=CUCU6U!-"4"4Y"?
"KLG !& ( #.k*Q.# #""?3E"4*	4X HSSz~~m4SS3>?tVn?
? &$
 	
i DO^ T?s   G2G7G<Hc           
        g }t        |      D ]H  \  }}| j                  | j                  |   }|j                  /t        |j                        D ]  \  }}|d   |d   |d   |d   z
  |d   |d   z
  g}d}d}	|j                  ?|j                  |   }
| j                  | j                  t        |
         }nt        |
      }|j                  t        |j                  |         }	d}|j                  )d|j                  v rt        |j                  d   |         }||d   |d   z  }|||	||t        |      dz   d}|j                  |        K |S )	z`Transform predictions into a list of predictions that can be used by the COCO
        evaluator.Nr   rK   rE   rD   r   r   )r   r   r  r   r   r   )r   r  r  r  r  r   
confidencer   r  r   r   )r   r   r   r   image_predictionspred_idxr  r  r   r  r  r   dict_predictions                r   _prepare_predictionsz)MeanAveragePrecision._prepare_predictionsw  s   
 24+4[+A *	9'H'""...x8 %%-"+,=,B,B"C #9$Qa$q'DG*;T!WtAw=NO$--9.77AF**6&*&9&9#f+&F&)&k$//;!"3">">x"HIE %**6"3"8"88 !2!7!7!?!IJD<7T!W,D !) "#. ./!3# !''8G#9*	9V  r!   c                <   t        | j                        }t        | j                        }||k7  rt        d| d| d      | j	                  | j                        }| j                  | j                        }t        |      }|j                  |      }t        ||      }|j                          t        | j                  | j                  |j                  d   |j                  d   |j                  j                  t!        j"                  |j                  j$                              }t        | j                  | j                  |j                  d   |j                  d	   |j                  j                  t!        j"                  |j                  j$                              }	t        | j                  | j                  |j                  d
   |j                  d   |j                  j                  t!        j"                  |j                  j$                              }
t        | j                  | j                  |j                  d   |j                  d   |j                  j                  t!        j"                  |j                  j$                        ||	|
	      }|S )a6  
        Calculate Mean Average Precision based on predicted and ground-truth
        detections at different thresholds using the COCO evaluation metrics.
        Source: https://github.com/rafaelpadilla/review_object_detection_metrics

        Returns:
            The Mean Average Precision result.
        r  r  z)) during the evaluation must be the same.r   rY  rZ  )r   r   r   r*   r+   r,   r[  r\  r]  r^  rW  rX  )	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   total_images_predictionstotal_images_targetsdict_targetslst_predictionscoco_gtcoco_detcocoEval	mAP_small
mAP_medium	mAP_large
mAP_results               r   computezMeanAveragePrecision.compute  sJ    $'t'='=#> "4#5#56#';;-.F-G H12 3 
 ,,T-?-?@33D4J4JK#L9++O< (3 	 /--"22''(:;!))*>?#??33HHX__%<%<=
	 0--"22''(;<!))*?@#??33HHX__%<%<=

 /--"22''(:;!))*>?#??33HHX__%<%<=
	 0--"22''(>?!))*BC#??33HHX__%<%<=#%#


 r!   )r   r   r  r   r  zdict[int, int] | Noner  r   rs   )r   Detections | list[Detections]r   r  rq   r  )r   list[Detections]rq   zdict[str, list[dict[str, Any]]])r   r  rq   r   )rq   r   )ru   rv   rw   rx   r   BOXESr   r  r  r  r  r  r{   r!   r   r  r    s    #N '3&8&8$/3*.,#, , -	,
 (,0 (2( /( 
	(T<
'<
	(<
|1 +1 	1 fFr!   r  )4
__future__r   r   ri  r   collectionsr   r   dataclassesr   enumr   typingr   r	   numpyr   numpy.typingnpt
matplotlibr
   rX   supervision.detection.corer   'supervision.detection.utils.iou_and_nmsr   supervision.draw.colorr   supervision.metrics.corer   r   supervision.metrics.utils.utilsr   supervision.utils.loggerr   ru   r  r8   r<   r   r}   r   r   r   finfor   epsrf  r   r   r   r  r{   r!   r   <module>r     s    "    #  !  %   $ 1 N 7 9 C 0	X	 X X XvR# R#l     bhhrzz #
 #
L	 	Df6 fr!   