Draw Utils¶
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
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
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 |
opacity |
float
|
The opacity of rectangle when drawn on the scene. |
1
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scene with the rectangle drawn on it |
Source code in supervision/draw/utils.py
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 a filled 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 |
opacity |
float
|
The opacity of polygon when drawn on the scene. |
1
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The scene with the polygon drawn on it. |
Source code in supervision/draw/utils.py
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 |
Optional[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
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 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 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. |
Source code in supervision/draw/utils.py
Represents a color in RGB format.
This class provides methods to work with colors, including creating colors from hex codes, converting colors to hex strings, RGB tuples, and BGR tuples.
Attributes:
Name | Type | Description |
---|---|---|
r |
int
|
Red channel value (0-255). |
g |
int
|
Green channel value (0-255). |
b |
int
|
Blue channel value (0-255). |
Constant | Hex Code | RGB |
---|---|---|
WHITE |
#FFFFFF |
(255, 255, 255) |
BLACK |
#000000 |
(0, 0, 0) |
RED |
#FF0000 |
(255, 0, 0) |
GREEN |
#00FF00 |
(0, 255, 0) |
BLUE |
#0000FF |
(0, 0, 255) |
YELLOW |
#FFFF00 |
(255, 255, 0) |
ROBOFLOW |
#A351FB |
(163, 81, 251) |
Source code in supervision/draw/color.py
65 66 67 68 69 70 71 72 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
Functions¶
as_bgr()
¶
Returns the color as a BGR tuple.
Returns:
Type | Description |
---|---|
Tuple[int, int, int]
|
Tuple[int, int, int]: BGR tuple. |
Source code in supervision/draw/color.py
as_hex()
¶
Converts the Color instance to a hex string.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The hexadecimal color string. |
Source code in supervision/draw/color.py
as_rgb()
¶
Returns the color as an RGB tuple.
Returns:
Type | Description |
---|---|
Tuple[int, int, int]
|
Tuple[int, int, int]: RGB tuple. |
Source code in supervision/draw/color.py
from_bgr_tuple(color_tuple)
classmethod
¶
Create a Color instance from a BGR tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_tuple |
Tuple[int, int, int]
|
A tuple representing the color in BGR format, where each element is an integer in the range 0-255. |
required |
Returns:
Name | Type | Description |
---|---|---|
Color |
Color
|
An instance representing the color. |
Source code in supervision/draw/color.py
from_hex(color_hex)
classmethod
¶
Create a Color instance from a hex string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_hex |
str
|
The hex string representing the color. This string can start with '#' followed by either 3 or 6 hexadecimal characters. In case of 3 characters, each character is repeated to form the full 6-character hex code. |
required |
Returns:
Name | Type | Description |
---|---|---|
Color |
Color
|
An instance representing the color. |
Example
Source code in supervision/draw/color.py
from_rgb_tuple(color_tuple)
classmethod
¶
Create a Color instance from an RGB tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_tuple |
Tuple[int, int, int]
|
A tuple representing the color in RGB format, where each element is an integer in the range 0-255. |
required |
Returns:
Name | Type | Description |
---|---|---|
Color |
Color
|
An instance representing the color. |
Source code in supervision/draw/color.py
Source code in supervision/draw/color.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
Functions¶
DEFAULT()
¶
Returns a default color palette.
Returns:
Name | Type | Description |
---|---|---|
ColorPalette |
ColorPalette
|
A ColorPalette instance with default colors. |
Example
Source code in supervision/draw/color.py
ROBOFLOW()
¶
Returns a Roboflow color palette.
Returns:
Name | Type | Description |
---|---|---|
ColorPalette |
ColorPalette
|
A ColorPalette instance with Roboflow colors. |
Example
Source code in supervision/draw/color.py
__len__()
¶
Returns the number of colors in the palette.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of colors. |
by_idx(idx)
¶
Return the color at a given index in the palette.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx |
int
|
Index of the color in the palette. |
required |
Returns:
Name | Type | Description |
---|---|---|
Color |
Color
|
Color at the given index. |
Example
Source code in supervision/draw/color.py
from_hex(color_hex_list)
classmethod
¶
Create a ColorPalette instance from a list of hex strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_hex_list |
List[str]
|
List of color hex strings. |
required |
Returns:
Name | Type | Description |
---|---|---|
ColorPalette |
ColorPalette
|
A ColorPalette instance. |
Example
Source code in supervision/draw/color.py
from_matplotlib(palette_name, color_count)
classmethod
¶
Create a ColorPalette instance from a Matplotlib color palette.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
palette_name |
str
|
Name of the Matplotlib palette. |
required |
color_count |
int
|
Number of colors to sample from the palette. |
required |
Returns:
Name | Type | Description |
---|---|---|
ColorPalette |
ColorPalette
|
A ColorPalette instance. |