Geometry
supervision.geometry.utils.get_polygon_center(polygon: npt.NDArray[np.number]) -> 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[number]
|
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
:class:~supervision.geometry.core.Point.
top_left: Point
property
¶
Return the top-left corner as a :class:~supervision.geometry.core.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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
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. |
Example
For a vector from start to end, the cross product is positive
for a point to its left, negative for a point to its right, and zero
for a point on the same line.
>>> from supervision.geometry.core import Point, Vector
>>> start = Point(x=10.0, y=10.0)
>>> end = Point(x=12.0, y=10.0)
>>> vector = Vector(start=start, end=end)
>>> vector.cross_product(Point(x=11.0, y=11.0))
2.0
>>> vector.cross_product(Point(x=11.0, y=9.0))
-2.0
>>> vector.cross_product(Point(x=11.0, y=10.0))
0.0
Source code in src/supervision/geometry/core.py
supervision.geometry.core.CoordinatesTransformation
¶
Bases: Protocol
Map points between a reference frame and the current frame.
Zones and lines are defined in the coordinates of a reference frame, the
"absolute" frame.
:meth:~supervision.geometry.core.CoordinatesTransformation.rel_to_abs maps
current-frame points into it, and
:meth:~supervision.geometry.core.CoordinatesTransformation.abs_to_rel maps
them back. Any object with these two methods satisfies the protocol.
Example
>>> import numpy as np
>>> class Shift:
... def abs_to_rel(self, points): return points + [100, 0]
... def rel_to_abs(self, points): return points - [100, 0]
>>> Shift().rel_to_abs(np.array([[150.0, 50.0]]))
array([[50., 50.]])
Source code in src/supervision/geometry/core.py
Methods:¶
abs_to_rel(points: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]
¶
Map points from reference-frame to current-frame coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[float64]
|
Points of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Points of shape |
Source code in src/supervision/geometry/core.py
rel_to_abs(points: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]
¶
Map points from current-frame to reference-frame coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[float64]
|
Points of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Points of shape |