roboflow/supervision · error · ValueError

Areas must be shaped (N,)

Error message

Areas must be shaped (N,)

What it means

Raised by get_area_size_category() when the areas array is not one-dimensional. The function maps each scalar area to a size bucket via boolean masks, so a 2-D input (e.g. an (N,1) column) breaks the elementwise bucketing. Validation happens before the thresholds are applied.

Source

Thrown at src/supervision/metrics/utils/object_size.py:156

    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])

        ```
    """
    if len(areas.shape) != 1:
        raise ValueError("Areas must be shaped (N,)")

    result = np.full(areas.shape, ObjectSizeCategory.ANY.value)
    sm, lg = SIZE_THRESHOLDS
    result[areas < sm] = ObjectSizeCategory.SMALL.value
    result[(areas >= sm) & (areas < lg)] = ObjectSizeCategory.MEDIUM.value
    result[areas >= lg] = ObjectSizeCategory.LARGE.value
    return result


def get_mask_size_category(
    mask: npt.NDArray[np.bool_] | CompactMask,
) -> npt.NDArray[np.int_]:
    """
    Get the size category of detection masks.

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

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Flatten before calling: np.asarray(areas).reshape(-1) or .ravel()
  2. Use single brackets when extracting from DataFrames: df['area'].values
  3. Avoid keepdims=True on the reduction that produces the areas

Example fix

# before
areas = df[['area']].to_numpy()        # (N, 1)
get_area_size_category(areas)          # ValueError

# after
areas = df['area'].to_numpy()          # (N,)
get_area_size_category(areas)
Defensive patterns

Strategy: validation

Validate before calling

areas = np.asarray(areas).reshape(-1)
if areas.ndim != 1:
    raise ValueError('areas must be 1-D')
cats = get_area_size_category(areas)

Type guard

import numpy as np

def is_flat_vector(arr: np.ndarray) -> bool:
    """True when arr is one-dimensional."""
    return arr.ndim == 1

Try / catch

try:
    cats = get_area_size_category(areas)
except ValueError as e:
    if 'shaped (N,)' in str(e):
        cats = get_area_size_category(np.asarray(areas).ravel())
    else:
        raise

Prevention

When it happens

Trigger: Calling get_area_size_category with areas shaped (N, 1) (common after np.sum(..., keepdims=True) or df[['area']].values), or with a scalar 0-D array.

Common situations: Areas extracted from a pandas DataFrame with double brackets producing (N,1); results of reductions with keepdims=True; nested lists passed directly instead of flattened.

Related errors


AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15). Data as JSON: /api/errors/a42c18538ce09df3. Report an issue: GitHub.