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
| 310 | |
| 311 | |
| 312 | class Visualizer: |
| 313 | """ |
| 314 | Visualizer that draws data about detection/segmentation on images. |
| 315 | |
| 316 | It contains methods like `draw_{text,box,circle,line,binary_mask,polygon}` |
| 317 | that draw primitive objects to images, as well as high-level wrappers like |
| 318 | `draw_{instance_predictions,sem_seg,panoptic_seg_predictions,dataset_dict}` |
| 319 | that draw composite data in some pre-defined style. |
| 320 | |
| 321 | Note that the exact visualization style for the high-level wrappers are subject to change. |
| 322 | Style such as color, opacity, label contents, visibility of labels, or even the visibility |
| 323 | of objects themselves (e.g. when the object is too small) may change according |
| 324 | to different heuristics, as long as the results still look visually reasonable. |
| 325 | To obtain a consistent style, implement custom drawing functions with the primitive |
| 326 | methods instead. |
| 327 | |
| 328 | This visualizer focuses on high rendering quality rather than performance. It is not |
| 329 | designed to be used for real-time applications. |
| 330 | """ |
| 331 | |
| 332 | # TODO implement a fast, rasterized version using OpenCV |
| 333 | |
| 334 | def __init__(self, img_rgb, metadata=None, scale=1.0, instance_mode=ColorMode.IMAGE): |
| 335 | """ |
| 336 | Args: |
| 337 | img_rgb: a numpy array of shape (H, W, C), where H and W correspond to |
| 338 | the height and width of the image respectively. C is the number of |
| 339 | color channels. The image is required to be in RGB format since that |
| 340 | is a requirement of the Matplotlib library. The image is also expected |
| 341 | to be in the range [0, 255]. |
| 342 | metadata (Metadata): image metadata. |
| 343 | instance_mode (ColorMode): defines one of the pre-defined style for drawing |
| 344 | instances on an image. |
| 345 | """ |
| 346 | self.img = np.asarray(img_rgb).clip(0, 255).astype(np.uint8) |
| 347 | if metadata is None: |
| 348 | metadata = MetadataCatalog.get("__nonexist__") |
| 349 | self.metadata = metadata |
| 350 | self.output = VisImage(self.img, scale=scale) |
| 351 | self.cpu_device = torch.device("cpu") |
| 352 | |
| 353 | # too small texts are useless, therefore clamp to 9 |
| 354 | self._default_font_size = max( |
| 355 | np.sqrt(self.output.height * self.output.width) // 90, 10 // scale |
| 356 | ) |
| 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. |
no outgoing calls