Visualize object predictions with bounding boxes and category names. Args: image: Input image as numpy array. object_prediction_list: List of prediction.ObjectPrediction instances. rect_th: rectangle thickness text_size: size of the category name over box
(
image: np.ndarray,
object_prediction_list: list,
rect_th: int | None = None,
text_size: float | None = None,
text_th: int | None = None,
color: tuple | None = None,
hide_labels: bool = False,
hide_conf: bool = False,
output_dir: str | None = None,
file_name: str | None = "prediction_visual",
export_format: str | None = "png",
)
| 505 | |
| 506 | |
| 507 | def visualize_object_predictions( |
| 508 | image: np.ndarray, |
| 509 | object_prediction_list: list, |
| 510 | rect_th: int | None = None, |
| 511 | text_size: float | None = None, |
| 512 | text_th: int | None = None, |
| 513 | color: tuple | None = None, |
| 514 | hide_labels: bool = False, |
| 515 | hide_conf: bool = False, |
| 516 | output_dir: str | None = None, |
| 517 | file_name: str | None = "prediction_visual", |
| 518 | export_format: str | None = "png", |
| 519 | ) -> dict: |
| 520 | """Visualize object predictions with bounding boxes and category names. |
| 521 | |
| 522 | Args: |
| 523 | image: Input image as numpy array. |
| 524 | object_prediction_list: List of prediction.ObjectPrediction instances. |
| 525 | rect_th: rectangle thickness |
| 526 | text_size: size of the category name over box |
| 527 | text_th: text thickness |
| 528 | color: annotation color in the form: (0, 255, 0) |
| 529 | hide_labels: hide labels |
| 530 | hide_conf: hide confidence |
| 531 | output_dir: directory for resulting visualization to be exported |
| 532 | file_name: exported file will be saved as: output_dir+file_name+".png" |
| 533 | export_format: can be specified as 'jpg' or 'png' |
| 534 | """ |
| 535 | elapsed_time = time.time() |
| 536 | # deepcopy image so that original is not altered |
| 537 | image = copy.deepcopy(image) |
| 538 | # select predefined classwise color palette if not specified |
| 539 | if color is None: |
| 540 | colors = Colors() |
| 541 | else: |
| 542 | colors = None |
| 543 | # set rect_th for boxes |
| 544 | rect_th = rect_th or max(round(sum(image.shape) / 2 * 0.003), 2) |
| 545 | # set text_th for category names |
| 546 | text_th = text_th or max(rect_th - 1, 1) |
| 547 | # set text_size for category names |
| 548 | text_size = text_size or rect_th / 3 |
| 549 | |
| 550 | # add masks or obb polygons to image if present |
| 551 | for object_prediction in object_prediction_list: |
| 552 | # deepcopy object_prediction_list so that original is not altered |
| 553 | object_prediction = object_prediction.deepcopy() |
| 554 | # arange label to be displayed |
| 555 | label = f"{object_prediction.category.name}" |
| 556 | if not hide_conf: |
| 557 | label += f" {object_prediction.score.value:.2f}" |
| 558 | # set color |
| 559 | if colors is not None: |
| 560 | color = colors(object_prediction.category.id) |
| 561 | # visualize masks or obb polygons if present |
| 562 | has_mask = object_prediction.mask is not None |
| 563 | is_obb_pred = False |
| 564 | if has_mask: |
no test coverage detected