
    ^j0                        d dl mZ d dlmZ d dl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 erd dlmZ d	Z G d
 de      Z	 	 	 	 	 	 ddZddZ	 	 	 	 ddZddZej0                  f	 	 	 	 	 ddZy)    )annotations)Enum)TYPE_CHECKINGN)ORIENTED_BOX_COORDINATES)CompactMask)MetricTarget)
Detections)i   i $  c                       e Zd ZdZdZdZdZdZy)ObjectSizeCategorya  
    Enum for object size categories based on area in pixels.

    Small: area < 32^2
    Medium: 32^2 <= area < 96^2
    Large: area >= 96^2

    Example:
        ```pycon
        >>> from supervision.metrics.utils.object_size import ObjectSizeCategory
        >>> ObjectSizeCategory.SMALL.value
        1
        >>> ObjectSizeCategory.MEDIUM.value
        2
        >>> ObjectSizeCategory.LARGE.value
        3

        ```
             N)__name__
__module____qualname____doc__ANYSMALLMEDIUMLARGE     p/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/metrics/utils/object_size.pyr   r      s    ( CEFEr   r   c                    |t         j                  k(  rt        |       S |t         j                  k(  rt	        |       S |t         j
                  k(  rt        |       S t        d      )aq  
    Get the size category of an object. Distinguish based on the metric target.

    Args:
        data: The object data, shaped (N, ...).
        metric_target: Determines whether boxes, masks or
            oriented bounding boxes are used.

    Returns:
        The size category of each object, matching
        the enum values of ObjectSizeCategory. Shaped (N,).

    Example:
        ```pycon
        >>> import numpy as np
        >>> from supervision.metrics.core import MetricTarget
        >>> from supervision.metrics.utils.object_size import get_object_size_category
        >>> xyxy = np.array([
        ...     [0, 0, 10, 10],    # 100 (Small)
        ...     [0, 0, 50, 50],    # 2500 (Medium)
        ...     [0, 0, 100, 100]   # 10000 (Large)
        ... ])
        >>> get_object_size_category(xyxy, MetricTarget.BOXES)
        array([1, 2, 3])

        ```
    Invalid metric type)r   BOXESget_bbox_size_categoryMASKSget_mask_size_categoryORIENTED_BOUNDING_BOXESget_obb_size_category
ValueError)datametric_targets     r   get_object_size_categoryr&   .   s[    < ***%d++***%d++<<<$T**
*
++r   c                   t        | j                        dk7  s| j                  d   dk7  rt        d      | dddf   | dddf   z
  }| dddf   | dddf   z
  }||z  }t        j                  |j                  t
        j                  j                        }t        \  }}t
        j                  j                  |||k  <   t
        j                  j                  |||k\  ||k  z  <   t
        j                  j                  |||k\  <   |S )a  
    Get the size category of a bounding boxes array.

    Args:
        xyxy: The bounding boxes array shaped (N, 4).

    Returns:
        The size category of each bounding box, matching
        the enum values of ObjectSizeCategory. Shaped (N,).

    Example:
        ```pycon
        >>> import numpy as np
        >>> from supervision.metrics.utils.object_size import get_bbox_size_category
        >>> xyxy = np.array([
        ...     [0, 0, 31, 31],    # 961 (Small)
        ...     [0, 0, 32, 32],    # 1024 (Medium)
        ...     [0, 0, 95, 95],    # 9025 (Medium)
        ...     [0, 0, 96, 96]     # 9216 (Large)
        ... ])
        >>> get_bbox_size_category(xyxy)
        array([1, 2, 2, 3])

        ```
    r   r      z$Bounding boxes must be shaped (N, 4)Nr   r   )lenshaper#   npfullr   r   valueSIZE_THRESHOLDSr   r   r   )xyxywidthheightareasresultSMLGs          r   r   r   U   s    4 4::!tzz!}1?@@AJad#E!Q$Z$q!t*$FFNEWWU[["4"8"8">">?FFB+1177F52:+=+D+D+J+JFERKEBJ'(,2288F5B;Mr   c                   t        | t              r| j                  }n:t        | j                        dk7  rt        d      t        j                  | d      }t        j                  |j                  t        j                  j                        }t        \  }}t        j                  j                  |||k  <   t        j                  j                  |||k\  ||k  z  <   t        j                  j                  |||k\  <   |S )a  
    Get the size category of detection masks.

    Args:
        mask: The mask array shaped (N, H, W), or a
            :class:`~supervision.detection.compact_mask.CompactMask`.

    Returns:
        The size category of each mask, matching
        the enum values of ObjectSizeCategory. Shaped (N,).

    Example:
        ```pycon
        >>> import numpy as np
        >>> from supervision.metrics.utils.object_size import get_mask_size_category
        >>> mask = np.zeros((3, 100, 100), dtype=bool)
        >>> mask[0, 0:10, 0:10] = True   # 100 (Small)
        >>> mask[1, 0:50, 0:50] = True   # 2500 (Medium)
        >>> mask[2, 0:100, 0:100] = True # 10000 (Large)
        >>> get_mask_size_category(mask)
        array([1, 2, 3])

        ```
    r   zMasks must be shaped (N, H, W))r   r   )axis)
isinstancer   arear)   r*   r#   r+   sumr,   r   r   r-   r.   r   r   r   )maskr2   r3   r4   r5   s        r   r    r    ~   s    6 $$		tzz?a=>>t&)WWU[["4"8"8">">?FFB+1177F52:+=+D+D+J+JFERKEBJ'(,2288F5B;Mr   c                   t        | j                        dk7  s$| j                  d   dk7  s| j                  d   dk7  rt        d      | dddddf   }| dddddf   }|j                  \  }}}}|j                  \  }}}	}
dt	        j
                  ||z  ||	z  z   ||
z  z   ||z  z   ||z  ||z  z   ||	z  z   ||
z  z   z
        z  }t	        j                  |j                  t        j                  j                        }t        \  }}t        j                  j                  |||k  <   t        j                  j                  |||k\  ||k  z  <   t        j                  j                  |||k\  <   |S )	a  
    Get the size category of a oriented bounding boxes array.

    Args:
        xyxyxyxy: The bounding boxes array shaped (N, 4, 2).

    Returns:
        The size category of each bounding box, matching
        the enum values of ObjectSizeCategory. Shaped (N,).

    Example:
        ```pycon
        >>> import numpy as np
        >>> from supervision.metrics.utils.object_size import get_obb_size_category
        >>> obb = np.array([
        ...     [[0, 0], [10, 0], [10, 10], [0, 10]],   # 100 (Small)
        ...     [[0, 0], [50, 0], [50, 50], [0, 50]],   # 2500 (Medium)
        ...     [[0, 0], [100, 0], [100, 100], [0, 100]] # 10000 (Large)
        ... ])
        >>> get_obb_size_category(obb)
        array([1, 2, 3])

        ```
    r   r   r(   r   z0Oriented bounding boxes must be shaped (N, 4, 2)Nr   g      ?)r)   r*   r#   Tr+   absr,   r   r   r-   r.   r   r   r   )xyxyxyxyxyx1x2x3x4y1y2y3y4r2   r3   r4   r5   s                  r   r"   r"      sl   2 8>>a8>>!#4#9X^^A=NRS=SKLL 	AqAAqASSNBBSSNBB"&&	b27	R"W	$rBw	.7R"WrBw&b0	2 E
 WWU[["4"8"8">">?FFB+1177F52:+=+D+D+J+JFERKEBJ'(,2288F5B;Mr   c                   |t         j                  k(  rt        | j                        S |t         j                  k(  r,| j
                  t        d      t        | j
                        S |t         j                  k(  rY| j                  j                  t              t        d      t        t        j                  | j                  t                       S t        d      )aX  
    Get the size category of a detections object.

    Args:
        detections: The detections object.
        metric_target: Determines whether boxes, masks or
            oriented bounding boxes are used.

    Returns:
        The size category of each bounding box, matching
        the enum values of ObjectSizeCategory. Shaped (N,).
    z Detections mask is not availablez4Detections oriented bounding boxes are not availabler   )r   r   r   r/   r   r;   r#   r    r!   r$   getr   r"   r+   array)
detectionsr%   s     r   get_detection_size_categoryrN      s     ***%joo66***??"?@@%joo66<<<??78@STT$HHZ__%=>?
 	
 *
++r   )r$   znpt.NDArrayr%   r   returnnpt.NDArray[np.int_])r/   npt.NDArray[np.float32]rO   rP   )r;   z#npt.NDArray[np.bool_] | CompactMaskrO   rP   )r?   rQ   rO   rP   )rM   r	   r%   r   rO   rP   )
__future__r   enumr   typingr   numpyr+   numpy.typingnptsupervision.configr   "supervision.detection.compact_maskr   supervision.metrics.corer   supervision.detection.corer	   r.   r   r&   r   r    r"   r   rN   r   r   r   <module>r\      s    "      7 : 15  6$,
$,&2$,$,N&R'
-''T+^ ;G:L:L,,+7,,r   