```python:inference/core/workflows/core_steps/classical_cv/mask_area_measurement/v1.py
import cv2 as cv
import numpy as np
import supervision as sv


def get_detection_area(detection: sv.Detections, index: int) -> float:
    """Compute the area of a single detection in square pixels.

    If a segmentation mask is available, the area is computed from the largest
    contour found in the mask using ``cv2.contourArea``.  Otherwise the area
    falls back to bounding-box width * height.

    Args:
        detection: A supervision Detections object.
        index: Index of the detection to measure.

    Returns:
        Area in square pixels.
    """
    if detection.mask is not None:
        mask = detection.mask[index]
        # Only convert if needed to avoid unnecessary copy
        if mask.dtype != np.uint8:
            mask = mask.astype(np.uint8)
        contours, _ = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        if contours:
            # Find the largest contour area in a single pass to avoid
            # calling cv.contourArea twice for the same contour.
            largest_contour = None
            max_area = 0.0
            for c in contours:
                a = cv.contourArea(c)
                if a > max_area:
                    max_area = a
                    largest_contour = c
            if max_area > 0:
                return float(max_area)


    bbox = detection.xyxy[index]
    w = bbox[2] - bbox[0]
    h = bbox[3] - bbox[1]
    return float(w * h)
```