Draw Utils¶
draw_line
Draws a line on a given scene.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
The scene on which the line will be drawn |
required |
start |
Point
|
The starting point of the line |
required |
end |
Point
|
The end point of the line |
required |
color |
Color
|
The color of the line |
required |
thickness |
int
|
The thickness of the line |
2
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scene with the line drawn on it |
Source code in supervision/draw/utils.py
draw_rectangle
Draws a rectangle on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
The scene on which the rectangle will be drawn |
required |
rect |
Rect
|
The rectangle to be drawn |
required |
color |
Color
|
The color of the rectangle |
required |
thickness |
int
|
The thickness of the rectangle border |
2
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scene with the rectangle drawn on it |
Source code in supervision/draw/utils.py
draw_filled_rectangle
Draws a filled rectangle on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
The scene on which the rectangle will be drawn |
required |
rect |
Rect
|
The rectangle to be drawn |
required |
color |
Color
|
The color of the rectangle |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scene with the rectangle drawn on it |
Source code in supervision/draw/utils.py
draw_polygon
Draw a polygon on a scene.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
The scene to draw the polygon on. |
required |
polygon |
ndarray
|
The polygon to be drawn, given as a list of vertices. |
required |
color |
Color
|
The color of the polygon. |
required |
thickness |
int
|
The thickness of the polygon lines, by default 2. |
2
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scene with the polygon drawn on it. |
Source code in supervision/draw/utils.py
draw_text
Draw text with background on a scene.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
A 2-dimensional numpy ndarray representing an image or scene |
required |
text |
str
|
The text to be drawn. |
required |
text_anchor |
Point
|
The anchor point for the text, represented as a Point object with x and y attributes. |
required |
text_color |
Color
|
The color of the text. Defaults to black. |
BLACK
|
text_scale |
float
|
The scale of the text. Defaults to 0.5. |
0.5
|
text_thickness |
int
|
The thickness of the text. Defaults to 1. |
1
|
text_padding |
int
|
The amount of padding to add around the text when drawing a rectangle in the background. Defaults to 10. |
10
|
text_font |
int
|
The font to use for the text. Defaults to cv2.FONT_HERSHEY_SIMPLEX. |
FONT_HERSHEY_SIMPLEX
|
background_color |
Color
|
The color of the background rectangle, if one is to be drawn. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The input scene with the text drawn on it. |
Examples:
import numpy as np
scene = np.zeros((100, 100, 3), dtype=np.uint8)
text_anchor = Point(x=50, y=50)
scene = draw_text(scene=scene, text="Hello, world!",text_anchor=text_anchor)
Source code in supervision/draw/utils.py
draw_image
Draws an image onto a given scene with specified opacity and dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
Background image where the new image will be drawn. |
required |
image |
Union[str, ndarray]
|
Image to draw. |
required |
opacity |
float
|
Opacity of the image to be drawn. |
required |
rect |
Rect
|
Rectangle specifying where to draw the image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The updated scene. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the image path does not exist. |
ValueError
|
For invalid opacity or rectangle dimensions. |
Source code in supervision/draw/utils.py
calculate_dynamic_font_scale
Calculate a dynamic font scale based on the resolution of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resolution_wh |
Tuple[int, int]
|
A tuple representing the width and height of the image. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The calculated font scale factor. |
Source code in supervision/draw/utils.py
calculate_dynamic_line_thickness
Calculate a dynamic line thickness based on the resolution of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resolution_wh |
Tuple[int, int]
|
A tuple representing the width and height of the image. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The calculated line thickness in pixels. |