§
    >²¸j#*  ã                  óä   — 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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d%d„Zej        fd&d „ZdS )'é    )Úannotations)ÚEnum)ÚTYPE_CHECKINGÚcastN)ÚAREA_DATA_FIELDÚORIENTED_BOX_COORDINATES)ÚCompactMask)Úcount_mask_pixels)ÚMetricTarget)Ú
Detections)i   i $  c                  ó"   — e Zd ZdZdZdZdZdZdS )Ú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© ó    úu/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/metrics/utils/object_size.pyr   r      s.   € € € € € ðð ð( €CØ€EØ€FØ€E€E€Er   r   Údataú.npt.NDArray[np.number] | npt.NDArray[np.bool_]Úmetric_targetr   Úreturnúnpt.NDArray[np.int_]c                óÖ  — |t           j        k    r9t          t          j        t
          j                 | ¦  «        }t          |¦  «        S |t           j        k    r9t          t          j        t
          j	                 | ¦  «        }t          |¦  «        S |t           j        k    r9t          t          j        t
          j                 | ¦  «        }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   ÚBOXESr   ÚnptÚNDArrayÚnpÚnumberÚget_bbox_size_categoryÚMASKSÚbool_Úget_mask_size_categoryÚORIENTED_BOUNDING_BOXESÚget_obb_size_categoryÚ
ValueError)r   r    Ú	bbox_dataÚ	mask_dataÚobb_datas        r   Úget_object_size_categoryr4   /   s¯   € ð> �Ô*Ò*Ð*Ý�œ¥R¤YÔ/°Ñ6Ô6ˆ	Ý% iÑ0Ô0Ð0Ø�Ô*Ò*Ð*Ý�œ¥R¤XÔ.°Ñ5Ô5ˆ	Ý% iÑ0Ô0Ð0Ø�Ô<Ò<Ð<Ý�œ¥B¤IÔ.°Ñ5Ô5ˆÝ$ XÑ.Ô.Ð.Ý
Ð*Ñ
+Ô
+Ð+r   Úxyxyúnpt.NDArray[np.number]c                óè  — t          | j        ¦  «        dk    s| j        d         dk    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Úshaper0   r(   Úfullr   r   ÚvalueÚSIZE_THRESHOLDSr   r   r   )r5   ÚwidthÚheightÚareasÚresultÚSMÚLGs          r   r*   r*   Z   sð   € õ4 ˆ4Œ:�„˜!ÒÐ˜tœz¨!œ}°Ò1Ð1ÝÐ?Ñ@Ô@Ð@à����A�ŒJ˜˜a˜a˜a ˜dœÑ#€EØ�!�!�!�Q�$ŒZ˜$˜q˜q˜q !˜tœ*Ñ$€FØ�F‰N€EåŒW�U”[Õ"4Ô"8Ô">Ñ?Ô?€FÝ�F€BˆÝ+Ô1Ô7€Fˆ5�2Š:ÑÝ+=Ô+DÔ+J€FˆE�RŠK˜E BšJÑ'Ñ(Ý,Ô2Ô8€Fˆ5�BŠ;ÑØ€Mr   rA   c                óX  — t          | j        ¦  «        dk    rt          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 object size categories from per-detection pixel areas.

    Args:
        areas: One-dimensional pixel areas shaped (N,).

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

    Raises:
        ValueError: If `areas` is not one-dimensional.

    Example:
        ```pycon
        >>> import numpy as np
        >>> from supervision.metrics.utils.object_size import get_area_size_category
        >>> get_area_size_category(np.array([100, 2500, 10000]))
        array([1, 2, 3])

        ```
    r   zAreas must be shaped (N,)r9   )rA   rB   ÚsmÚlgs       r   Úget_area_size_categoryrH   ƒ   s•   € õ0 ˆ5Œ;ÑÔ˜1ÒÐÝÐ4Ñ5Ô5Ð5åŒW�U”[Õ"4Ô"8Ô">Ñ?Ô?€FÝ�F€BˆÝ+Ô1Ô7€Fˆ5�2Š:ÑÝ+=Ô+DÔ+J€FˆE�RŠK˜E BšJÑ'Ñ(Ý,Ô2Ô8€Fˆ5�BŠ;ÑØ€Mr   Úmaskú#npt.NDArray[np.bool_] | CompactMaskc                ó°  — t          | t          ¦  «        r| j        }n6t          | j        ¦  «        dk    rt          d¦  «        ‚t          | ¦  «        }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))Ú
isinstancer	   Úarear:   r;   r0   r
   r(   r<   r   r   r=   r>   r   r   r   )rI   rA   rB   rC   rD   s        r   r-   r-   ¦   s½   € õ6 �$�Ñ$Ô$ð 
(Ø”	ˆˆåˆtŒz‰?Œ?˜aÒÐÝÐ=Ñ>Ô>Ð>õ " $Ñ'Ô'ˆåŒW�U”[Õ"4Ô"8Ô">Ñ?Ô?€FÝ�F€BˆÝ+Ô1Ô7€Fˆ5�2Š:ÑÝ+=Ô+DÔ+J€FˆE�RŠK˜E BšJÑ'Ñ(Ý,Ô2Ô8€Fˆ5�BŠ;ÑØ€Mr   Úxyxyxyxyc                ó�  — t          | j        ¦  «        dk    s"| j        d         dk    s| j        d         dk    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   r8   r   z0Oriented bounding boxes must be shaped (N, 4, 2)Nr   g      à?)r:   r;   r0   ÚTr(   Úabsr<   r   r   r=   r>   r   r   r   )rN   ÚxÚyÚx1Úx2Úx3Úx4Úy1Úy2Úy3Úy4rA   rB   rC   rD   s                  r   r/   r/   Õ   sk  € õ2 ˆ8Œ>ÑÔ˜aÒÐ 8¤>°!Ô#4¸Ò#9Ð#9¸X¼^ÈAÔ=NÐRSÒ=SÐ=SÝÐKÑLÔLÐLð 	����A�A�A�q�Ô€AØ����A�A�A�q�Ô€AØ”S�N€BˆˆB�Ø”S�N€BˆˆB�Ø•"”&Ø	ˆb‰�2˜‘7Ñ	˜R "™WÑ	$ r¨B¡wÑ	.Ø�‰7�R˜"‘WÑ˜r B™wÑ&¨¨b©Ñ0ñ	2ñô ñ €Eõ
 ŒW�U”[Õ"4Ô"8Ô">Ñ?Ô?€FÝ�F€BˆÝ+Ô1Ô7€Fˆ5�2Š:ÑÝ+=Ô+DÔ+J€FˆE�RŠK˜E BšJÑ'Ñ(Ý,Ô2Ô8€Fˆ5�BŠ;ÑØ€Mr   Ú
detectionsr   c           	     ó.  — | j                              t          ¦  «        }|�vt          j        |t          j        ¬¦  «        }t          |j        ¦  «        dk    s t          |¦  «        t          | ¦  «        k    rt          d¦  «        ‚t          |¦  «        S |t          j        k    rt          | j        ¦  «        S |t          j        k    r'| j        }|€t          d¦  «        ‚t!          |¦  «        S |t          j        k    r…| j                              t$          ¦  «        }|€t          d¦  «        ‚t'          t)          t*          j        t          j                 t          j        |t          j        ¬¦  «        ¦  «        ¦  «        S t          d¦  «        ‚)aP  Get the size category of each detection.

    Explicit area metadata takes precedence for every metric target, matching
    COCO-style evaluation. Geometry, mask, or oriented-box content remains the
    fallback when area metadata is absent.

    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,).

    Raises:
        ValueError: If area metadata is not one-dimensional or is not aligned
            with the detections.

    Example:
        ```pycon
        >>> import numpy as np
        >>> from supervision.config import AREA_DATA_FIELD
        >>> from supervision.detection.core import Detections
        >>> detections = Detections(
        ...     xyxy=np.array([[0, 0, 10, 10]], dtype=np.float32),
        ...     data={AREA_DATA_FIELD: np.array([2500.0])},
        ... )
        >>> get_detection_size_category(detections)
        array([2])

        ```
    N)Údtyper   zGDetection area metadata must be shaped (N,) and aligned with detectionsz Detections mask is not availablez4Detections oriented bounding boxes are not availabler$   )r   Úgetr   r(   ÚasarrayÚfloat64r:   r;   r0   rH   r   r%   r*   r5   r+   rI   r-   r.   r   r/   r   r&   r'   r)   Úfloat32)r\   r    Ú	area_datarA   rI   Úoriented_box_coordinatess         r   Úget_detection_size_categoryre     sh  € ðH ”×#Ò#¥OÑ4Ô4€IØÐÝ”
˜9­B¬JÐ7Ñ7Ô7ˆÝˆuŒ{ÑÔ˜qÒ Ð ¥C¨¡J¤Jµ#°j±/´/Ò$AÐ$AÝð"ñô ð õ & eÑ,Ô,Ð,à�Ô*Ò*Ð*Ý% j¤oÑ6Ô6Ð6Ø�Ô*Ò*Ð*ØŒˆØˆ<ÝÐ?Ñ@Ô@Ð@Ý% dÑ+Ô+Ð+Ø�Ô<Ò<Ð<Ø#-¤?×#6Ò#6Õ7OÑ#PÔ#PÐ Ø#Ð+ÝÐSÑTÔTÐTÝ$ÝÝ”�BœIÔ&Ý”
Ð3½2¼:ÐFÑFÔFñô ñ
ô 
ð 	
õ Ð*Ñ
+Ô
+Ð+r   )r   r   r    r   r!   r"   )r5   r6   r!   r"   )rA   r6   r!   r"   )rI   rJ   r!   r"   )rN   r6   r!   r"   )r\   r   r    r   r!   r"   )Ú
__future__r   Úenumr   Útypingr   r   Únumpyr(   Únumpy.typingr&   Úsupervision.configr   r   Ú"supervision.detection.compact_maskr	   Ú!supervision.detection.utils.masksr
   Úsupervision.metrics.corer   Úsupervision.detection.corer   r>   r   r4   r*   rH   r-   r/   r%   re   r   r   r   ú<module>rp      s‹  ðØ "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ø Ð Ð Ð Ð Ð à HÐ HÐ HÐ HÐ HÐ HÐ HÐ HØ :Ð :Ð :Ð :Ð :Ð :Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1àð 6Ø5Ð5Ð5Ð5Ð5Ð5à €ðð ð ð ð ˜ñ ô ð ð6(,ð (,ð (,ð (,ðV&ð &ð &ð &ðR ð  ð  ð  ðF,ð ,ð ,ð ,ð^+ð +ð +ð +ð^ ;GÔ:Lð?,ð ?,ð ?,ð ?,ð ?,ð ?,ð ?,r   