Visualizer that draws data about detection/segmentation on images. It contains methods like `draw_{text,box,circle,line,binary_mask,polygon}` that draw primitive objects to images, as well as high-level wrappers like `draw_{instance_predictions,sem_seg,panoptic_seg_predictions,data
| 329 | |
| 330 | |
| 331 | class Visualizer: |
| 332 | """ |
| 333 | Visualizer that draws data about detection/segmentation on images. |
| 334 | |
| 335 | It contains methods like `draw_{text,box,circle,line,binary_mask,polygon}` |
| 336 | that draw primitive objects to images, as well as high-level wrappers like |
| 337 | `draw_{instance_predictions,sem_seg,panoptic_seg_predictions,dataset_dict}` |
| 338 | that draw composite data in some pre-defined style. |
| 339 | |
| 340 | Note that the exact visualization style for the high-level wrappers are subject to change. |
| 341 | Style such as color, opacity, label contents, visibility of labels, or even the visibility |
| 342 | of objects themselves (e.g. when the object is too small) may change according |
| 343 | to different heuristics, as long as the results still look visually reasonable. |
| 344 | |
| 345 | To obtain a consistent style, you can implement custom drawing functions with the |
| 346 | abovementioned primitive methods instead. If you need more customized visualization |
| 347 | styles, you can process the data yourself following their format documented in |
| 348 | tutorials (:doc:`/tutorials/models`, :doc:`/tutorials/datasets`). This class does not |
| 349 | intend to satisfy everyone's preference on drawing styles. |
| 350 | |
| 351 | This visualizer focuses on high rendering quality rather than performance. It is not |
| 352 | designed to be used for real-time applications. |
| 353 | """ |
| 354 | |
| 355 | # TODO implement a fast, rasterized version using OpenCV |
| 356 | |
| 357 | def __init__(self, img_rgb, metadata=None, scale=1.0, instance_mode=ColorMode.IMAGE): |
| 358 | """ |
| 359 | Args: |
| 360 | img_rgb: a numpy array of shape (H, W, C), where H and W correspond to |
| 361 | the height and width of the image respectively. C is the number of |
| 362 | color channels. The image is required to be in RGB format since that |
| 363 | is a requirement of the Matplotlib library. The image is also expected |
| 364 | to be in the range [0, 255]. |
| 365 | metadata (Metadata): dataset metadata (e.g. class names and colors) |
| 366 | instance_mode (ColorMode): defines one of the pre-defined style for drawing |
| 367 | instances on an image. |
| 368 | """ |
| 369 | self.img = np.asarray(img_rgb).clip(0, 255).astype(np.uint8) |
| 370 | if metadata is None: |
| 371 | metadata = MetadataCatalog.get("__nonexist__") |
| 372 | self.metadata = metadata |
| 373 | self.output = VisImage(self.img, scale=scale) |
| 374 | self.cpu_device = torch.device("cpu") |
| 375 | |
| 376 | # too small texts are useless, therefore clamp to 9 |
| 377 | self._default_font_size = max( |
| 378 | np.sqrt(self.output.height * self.output.width) // 90, 10 // scale |
| 379 | ) |
| 380 | self._default_font_size = 18 |
| 381 | self._instance_mode = instance_mode |
| 382 | self.keypoint_threshold = _KEYPOINT_THRESHOLD |
| 383 | |
| 384 | def draw_instance_predictions(self, predictions): |
| 385 | """ |
| 386 | Draw instance-level prediction results on an image. |
| 387 | |
| 388 | Args: |
no outgoing calls
no test coverage detected