Geometry
supervision.geometry.utils.get_polygon_center(polygon: npt.NDArray[np.float64]) -> Point
¶
Calculate the center of a polygon. The center is calculated as the center of the solid figure formed by the points of the polygon
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[float64]
|
A 2-dimensional numpy ndarray representing the vertices of the polygon. |
required |
Returns:
| Type | Description |
|---|---|
Point
|
The center of the polygon, represented as a Point object with x and y attributes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the polygon has no vertices. |
Examples:
>>> 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
Source code in src/supervision/geometry/utils.py
supervision.geometry.core.Position
¶
Bases: Enum
Enum representing the position of an anchor point.
Source code in src/supervision/geometry/core.py
Methods:¶
supervision.geometry.core.Point
dataclass
¶
Represents a point in 2D space.
Attributes:
| Name | Type | Description |
|---|---|---|
x |
float
|
The x-coordinate of the point. |
y |
float
|
The y-coordinate of the point. |
Example
>>> from supervision.geometry.core import Point
>>> point = Point(x=10.0, y=20.0)
>>> point.as_xy_int_tuple()
(10, 20)
>>> point.as_xy_float_tuple()
(10.0, 20.0)
Source code in src/supervision/geometry/core.py
Methods:¶
as_xy_float_tuple() -> tuple[float, float]
¶
Returns the point as a tuple of floats.
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
The point as (x, y) floats. |
as_xy_int_tuple() -> tuple[int, int]
¶
Returns the point as a tuple of integers.
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
The point as (x, y) integers. |
supervision.geometry.core.Rect
dataclass
¶
Represents a rectangle in 2D space.
Attributes:
| Name | Type | Description |
|---|---|---|
x |
float
|
The x-coordinate of the top-left corner of the rectangle. |
y |
float
|
The y-coordinate of the top-left corner of the rectangle. |
width |
float
|
The width of the rectangle. |
height |
float
|
The height of the rectangle. |
Example
>>> from supervision.geometry.core import Rect
>>> rect = Rect(x=10.0, y=20.0, width=30.0, height=40.0)
>>> rect.top_left
Point(x=10.0, y=20.0)
>>> rect.bottom_right
Point(x=40.0, y=60.0)
>>> rect.as_xyxy_int_tuple()
(10, 20, 40, 60)
Source code in src/supervision/geometry/core.py
Attributes¶
bottom_right: Point
property
¶
Return the bottom-right corner as a Point.
top_left: Point
property
¶
Return the top-left corner as a Point.
Methods:¶
as_xyxy_int_tuple() -> tuple[int, int, int, int]
¶
Return (x_min, y_min, x_max, y_max) coordinates as integers.
from_xyxy(xyxy: tuple[float, float, float, float]) -> Rect
classmethod
¶
Create a rectangle from (x_min, y_min, x_max, y_max) coordinates.
pad(padding: int) -> Rect
¶
Return a rectangle expanded by padding pixels on every side.
Source code in src/supervision/geometry/core.py
supervision.geometry.core.Vector
dataclass
¶
Represents a vector in 2D space, defined by a start and an end point.
Attributes:
| Name | Type | Description |
|---|---|---|
start |
Point
|
The starting point of the vector. |
end |
Point
|
The end point of the vector. |
Example
>>> from supervision.geometry.core import Point, Vector
>>> start_point = Point(x=0.0, y=0.0)
>>> end_point = Point(x=3.0, y=4.0)
>>> vector = Vector(start=start_point, end=end_point)
>>> vector.magnitude
5.0
>>> vector.center
Point(x=1.5, y=2.0)
Source code in src/supervision/geometry/core.py
Attributes¶
center: Point
property
¶
Calculate the center point of the vector.
Returns:
| Type | Description |
|---|---|
Point
|
The center point of the vector. |
magnitude: float
property
¶
Calculate the magnitude (length) of the vector.
Returns:
| Type | Description |
|---|---|
float
|
The magnitude of the vector. |
Methods:¶
cross_product(point: Point) -> float
¶
Calculate the 2D cross product (also known as the vector product or outer product) of the vector and a point, treated as vectors in 2D space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Point
|
The point to be evaluated, treated as the endpoint of a vector originating from the 'start' of the main vector. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The scalar value of the cross product. It is positive if 'point' lies to the left of the vector (when moving from 'start' to 'end'), negative if it lies to the right, and 0 if it is collinear with the vector. |