image_utils
attempt_loading_image_from_string(value, cv_imread_flags=cv2.IMREAD_COLOR)
¶
Attempt to load an image from a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Union[str, bytes, bytearray, _IOBase]
|
The image data in string format. |
required |
cv_imread_flags
|
int
|
OpenCV flags used for image reading. |
IMREAD_COLOR
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, bool]
|
Tuple[np.ndarray, bool]: A tuple of the loaded image in numpy array format and a boolean flag indicating if the image is in BGR format. |
Source code in inference/core/utils/image_utils.py
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 |
|
choose_image_decoding_flags(disable_preproc_auto_orient)
¶
Choose the appropriate OpenCV image decoding flags.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
disable_preproc_auto_orient
|
bool
|
Flag to disable preprocessing auto-orientation. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
OpenCV image decoding flags. |
Source code in inference/core/utils/image_utils.py
106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
convert_gray_image_to_bgr(image)
¶
Convert a grayscale image to BGR format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
ndarray
|
The grayscale image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The converted BGR image. |
Source code in inference/core/utils/image_utils.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
|
encode_image_to_jpeg_bytes(image, jpeg_quality=90)
¶
Encode a numpy image to JPEG format in bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
ndarray
|
The numpy array representing a BGR image. |
required |
jpeg_quality
|
int
|
Quality of the JPEG image. |
90
|
Returns:
Name | Type | Description |
---|---|---|
bytes |
bytes
|
The JPEG encoded image. |
Source code in inference/core/utils/image_utils.py
576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
|
extract_image_payload_and_type(value)
¶
Extract the image payload and type from the given value.
This function supports different types of image inputs (e.g., InferenceRequestImage, dict, etc.) and extracts the relevant data and image type for further processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The input value which can be an image or information to derive the image. |
required |
Returns:
Type | Description |
---|---|
Tuple[Any, Optional[ImageType]]
|
Tuple[Any, Optional[ImageType]]: A tuple containing the extracted image data and the corresponding image type. |
Source code in inference/core/utils/image_utils.py
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 |
|
load_image(value, disable_preproc_auto_orient=False)
¶
Loads an image based on the specified type and value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
Image value which could be an instance of InferenceRequestImage, a dict with 'type' and 'value' keys, or inferred based on the value's content. |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, bool]
|
Image.Image: The loaded PIL image, converted to RGB. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the specified image type is not supported. |
InvalidNumpyInput
|
If the numpy input method is used and the input data is invalid. |
Source code in inference/core/utils/image_utils.py
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 |
|
load_image_base64(value, cv_imread_flags=cv2.IMREAD_COLOR)
¶
Loads an image from a base64 encoded string using OpenCV.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
Base64 encoded string representing the image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The loaded image as a numpy array. |
Source code in inference/core/utils/image_utils.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
load_image_from_buffer(value, cv_imread_flags=cv2.IMREAD_COLOR)
¶
Loads an image from a multipart-encoded input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
Multipart-encoded input representing the image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Image.Image: The loaded PIL image. |
Source code in inference/core/utils/image_utils.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
load_image_from_encoded_bytes(value, cv_imread_flags=cv2.IMREAD_COLOR)
¶
Load an image from encoded bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
bytes
|
The byte sequence representing the image. |
required |
cv_imread_flags
|
int
|
OpenCV flags used for image reading. |
IMREAD_COLOR
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The loaded image as a numpy array. |
Source code in inference/core/utils/image_utils.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
load_image_from_numpy_str(value)
¶
Loads an image from a numpy array string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Union[bytes, str]
|
Base64 string or byte sequence representing the pickled numpy array of the image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Image.Image: The loaded PIL image. |
Raises:
Type | Description |
---|---|
InvalidNumpyInput
|
If the numpy data is invalid. |
Source code in inference/core/utils/image_utils.py
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 |
|
load_image_from_url(value, cv_imread_flags=cv2.IMREAD_COLOR)
¶
Loads an image from a given URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
URL of the image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Image.Image: The loaded PIL image. |
Source code in inference/core/utils/image_utils.py
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 408 409 410 411 412 413 414 415 |
|
load_image_with_inferred_type(value, cv_imread_flags=cv2.IMREAD_COLOR)
¶
Load an image by inferring its type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The image data. |
required |
cv_imread_flags
|
int
|
Flags used for OpenCV's imread function. |
IMREAD_COLOR
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, bool]
|
Tuple[np.ndarray, bool]: Loaded image as a numpy array and a boolean indicating if the image is in BGR format. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the image type could not be inferred. |
Source code in inference/core/utils/image_utils.py
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 |
|
load_image_with_known_type(value, image_type, cv_imread_flags=cv2.IMREAD_COLOR)
¶
Load an image using the known image type.
Supports various image types (e.g., NUMPY, PILLOW, etc.) and loads them into a numpy array format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The image data. |
required |
image_type
|
ImageType
|
The type of the image. |
required |
cv_imread_flags
|
int
|
Flags used for OpenCV's imread function. |
IMREAD_COLOR
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, bool]
|
Tuple[np.ndarray, bool]: A tuple of the loaded image as a numpy array and a boolean indicating if the image is in BGR format. |
Source code in inference/core/utils/image_utils.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
np_image_to_base64(image)
¶
TODO: This function is broken: https://github.com/roboflow/inference/issues/439 Convert a numpy image to a base64 encoded byte string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
ndarray
|
The numpy array representing an image. |
required |
Returns:
Name | Type | Description |
---|---|---|
bytes |
bytes
|
The base64 encoded image. |
Source code in inference/core/utils/image_utils.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
|
validate_numpy_image(data)
¶
Validate if the provided data is a valid numpy image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
The numpy array representing an image. |
required |
Raises:
Type | Description |
---|---|
InvalidNumpyInput
|
If the provided data is not a valid numpy image. |
Source code in inference/core/utils/image_utils.py
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 |
|
xyxy_to_xywh(xyxy)
¶
Convert bounding box format from (xmin, ymin, xmax, ymax) to (xcenter, ycenter, width, height).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy
|
List[int]
|
List containing the coordinates in (xmin, ymin, xmax, ymax) format. |
required |
Returns:
Type | Description |
---|---|
List[int]: List containing the converted coordinates in (xcenter, ycenter, width, height) format. |
Source code in inference/core/utils/image_utils.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
|