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