Draw instance-level prediction results on an image. Args: predictions (Instances): the output of an instance detection/segmentation model. Following fields will be used to draw: "pred_boxes", "pred_classes", "scores", "pred_masks" (or "pr
(self, predictions)
| 357 | self._instance_mode = instance_mode |
| 358 | |
| 359 | def draw_instance_predictions(self, predictions): |
| 360 | """ |
| 361 | Draw instance-level prediction results on an image. |
| 362 | |
| 363 | Args: |
| 364 | predictions (Instances): the output of an instance detection/segmentation |
| 365 | model. Following fields will be used to draw: |
| 366 | "pred_boxes", "pred_classes", "scores", "pred_masks" (or "pred_masks_rle"). |
| 367 | |
| 368 | Returns: |
| 369 | output (VisImage): image object with visualizations. |
| 370 | """ |
| 371 | boxes = predictions.pred_boxes if predictions.has("pred_boxes") else None |
| 372 | scores = predictions.scores if predictions.has("scores") else None |
| 373 | classes = predictions.pred_classes if predictions.has("pred_classes") else None |
| 374 | labels = _create_text_labels(classes, scores, self.metadata.get("thing_classes", None)) |
| 375 | keypoints = predictions.pred_keypoints if predictions.has("pred_keypoints") else None |
| 376 | |
| 377 | if predictions.has("pred_masks"): |
| 378 | masks = np.asarray(predictions.pred_masks) |
| 379 | masks = [GenericMask(x, self.output.height, self.output.width) for x in masks] |
| 380 | else: |
| 381 | masks = None |
| 382 | |
| 383 | if self._instance_mode == ColorMode.SEGMENTATION and self.metadata.get("thing_colors"): |
| 384 | colors = [ |
| 385 | self._jitter([x / 255 for x in self.metadata.thing_colors[c]]) for c in classes |
| 386 | ] |
| 387 | alpha = 0.8 |
| 388 | else: |
| 389 | colors = None |
| 390 | alpha = 0.5 |
| 391 | |
| 392 | if self._instance_mode == ColorMode.IMAGE_BW: |
| 393 | self.output.img = self._create_grayscale_image( |
| 394 | (predictions.pred_masks.any(dim=0) > 0).numpy() |
| 395 | if predictions.has("pred_masks") |
| 396 | else None |
| 397 | ) |
| 398 | alpha = 0.3 |
| 399 | |
| 400 | self.overlay_instances( |
| 401 | masks=masks, |
| 402 | boxes=boxes, |
| 403 | labels=labels, |
| 404 | keypoints=keypoints, |
| 405 | assigned_colors=colors, |
| 406 | alpha=alpha, |
| 407 | ) |
| 408 | return self.output |
| 409 | |
| 410 | def draw_sem_seg(self, sem_seg, area_threshold=None, alpha=0.8): |
| 411 | """ |