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
- Check `len(polygon) > 0` before calling and skip/default when empty
- Fix the upstream contour/point extraction so empty polygons are never forwarded
- 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
- Validate zone polygons at input time (require >= 3 points for zones)
- Guard contour-to-polygon code paths for empty findContours results
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
- LabelMe polygon shape (label={label}) has {len(points)} poin
- The magnitude of the vector cannot be zero.
- box coordinates must be real-valued
- xyxy must be a 2D np.ndarray with shape {expected_shape}, bu
- class_id must be a 1D np.ndarray with shape {expected_shape}
AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15).
Data as JSON: /api/errors/6331a8d90e30ebed.
Report an issue: GitHub.