roboflow/supervision · error · ValueError

Polygon must have at least one vertex.

Error message

Polygon must have at least one vertex.

What it means

The polygon centroid routine in src/supervision/geometry/utils.py:41 (shoelace-formula based, see PR #1084) requires at least one vertex; an empty polygon has no defined centroid. It raises ValueError before attempting any arithmetic so callers get a clear failure instead of NaN coordinates.

Source

Thrown at src/supervision/geometry/utils.py:41

    Examples:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> polygon = np.array([[0, 0], [0, 2], [2, 2], [2, 0]])
        >>> center = sv.get_polygon_center(polygon=polygon)
        >>> float(center.x)
        1.0
        >>> float(center.y)
        1.0

        ```
    """

    # This is one of the 3 candidate algorithms considered for centroid calculation.
    # For a more detailed discussion, see PR #1084 and commit eb33176

    if len(polygon) == 0:
        raise ValueError("Polygon must have at least one vertex.")

    shift_polygon = np.roll(polygon, -1, axis=0)
    signed_areas = (
        polygon[..., 0] * shift_polygon[..., 1]
        - polygon[..., 1] * shift_polygon[..., 0]
    ) / 2
    if signed_areas.sum() == 0:
        center = np.mean(polygon, axis=0).round()
        return Point(x=center[0], y=center[1])
    centroids = (polygon + shift_polygon) / 3.0
    center = np.average(centroids, axis=0, weights=signed_areas).round()

    return Point(x=center[0], y=center[1])

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Check `len(polygon) > 0` before calling and skip/default when empty
  2. Fix the upstream contour/point extraction so empty polygons are never forwarded
  3. Validate polygon input at the UI/config boundary (require at least 3 points for a meaningful zone)

Example fix

// before
center = get_polygon_center(np.array([], dtype=np.float32).reshape(0, 2))

// after
polygon = np.asarray(polygon, dtype=np.float32).reshape(-1, 2)
if polygon.size == 0:
    continue  # or raise a domain-specific error
center = get_polygon_center(polygon)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def has_vertices(polygon) -> bool:
    """Polygon centroid requires at least one (x, y) vertex."""
    return len(np.asarray(polygon).reshape(-1, 2)) > 0

Prevention

When it happens

Trigger: Passing an empty `(0, 2)` array to `get_polygon_center` (or APIs that call it, e.g. `PolygonZone`/`PolygonAnnotator` with an empty polygon); passing a mask whose contour extraction returned zero points; slicing a polygon array with an index bug that yields an empty result.

Common situations: Building zones from user-drawn polygons where the user clicked zero times; `cv2.findContours` returning no contours on an all-black mask and forwarding the empty result; off-by-one slicing like `polygon[1:1]`.

Related errors


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