class_docstr: 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,dataset_dict}`
  that draw composite data in some pre-defined style.
  
  Note that the exact visualization style for the high-level wrappers are subject to change.
  Style such as color, opacity, label contents, visibility of labels, or even the visibility
  of objects themselves (e.g. when the object is too small) may change according
  to different heuristics, as long as the results still look visually reasonable.
  
  To obtain a consistent style, you can implement custom drawing functions with the
  abovementioned primitive methods instead. If you need more customized visualization
  styles, you can process the data yourself following their format documented in
  tutorials (:doc:`/tutorials/models`, :doc:`/tutorials/datasets`). This class does not
  intend to satisfy everyone's preference on drawing styles.
  
  This visualizer focuses on high rendering quality rather than performance. It is not
  designed to be used for real-time applications.
class_docstr_tok: ["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",",","dataset_dict","}","`","that","draw","composite","data","in","some","pre-defined","style",".","Note","that","the","exact","visualization","style","for","the","high-level","wrappers","are","subject","to","change",".","Style","such","as","color",",","opacity",",","label","contents",",","visibility","of","labels",",","or","even","the","visibility","of","objects","themselves","(","e.g",".","when","the","object","is","too","small",")","may","change","according","to","different","heuristics",",","as","long","as","the","results","still","look","visually","reasonable",".","To","obtain","a","consistent","style",",","you","can","implement","custom","drawing","functions","with","the","abovementioned","primitive","methods","instead",".","If","you","need","more","customized","visualization","styles",",","you","can","process","the","data","yourself","following","their","format","documented","in","tutorials","(",":","doc",":","`","\/tutorials\/models","`",",",":","doc",":","`","\/tutorials\/datasets","`",")",".","This","class","does","not","intend","to","satisfy","everyone","'s","preference","on","drawing","styles",".","This","visualizer","focuses","on","high","rendering","quality","rather","than","performance",".","It","is","not","designed","to","be","used","for","real-time","applications","."]
docstr: Args:
      boxes (Boxes, RotatedBoxes or ndarray): either a :class:`Boxes`,
          or an Nx4 numpy array of XYXY_ABS format for the N objects in a single image,
          or a :class:`RotatedBoxes`,
          or an Nx5 numpy array of (x_center, y_center, width, height, angle_degrees) format
          for the N objects in a single image,
      labels (list[str]): the text to be displayed for each instance.
      masks (masks-like object): Supported types are:
  
          * :class:`detectron2.structures.PolygonMasks`,
            :class:`detectron2.structures.BitMasks`.
          * list[list[ndarray]]: contains the segmentation masks for all objects in one image.
            The first level of the list corresponds to individual instances. The second
            level to all the polygon that compose the instance, and the third level
            to the polygon coordinates. The third level should have the format of
            [x0, y0, x1, y1, ..., xn, yn] (n >= 3).
          * list[ndarray]: each ndarray is a binary mask of shape (H, W).
          * list[dict]: each dict is a COCO-style RLE.
      keypoints (Keypoint or array like): an array-like object of shape (N, K, 3),
          where the N is the number of instances and K is the number of keypoints.
          The last dimension corresponds to (x, y, visibility or score).
      assigned_colors (list[matplotlib.colors]): a list of colors, where each color
          corresponds to each mask or box in the image. Refer to 'matplotlib.colors'
          for full list of formats that the colors are accepted in.
  Returns:
      output (VisImage): image object with visualizations.
docstr_tok: ["Args",":","boxes","(","Boxes",",","RotatedBoxes","or","ndarray",")",":","either","a",":","class",":","`","Boxes","`",",","or","an","Nx4","numpy","array","of","XYXY_ABS","format","for","the","N","objects","in","a","single","image",",","or","a",":","class",":","`","RotatedBoxes","`",",","or","an","Nx5","numpy","array","of","(","x_center",",","y_center",",","width",",","height",",","angle_degrees",")","format","for","the","N","objects","in","a","single","image",",","labels","(","list","[","str","]",")",":","the","text","to","be","displayed","for","each","instance",".","masks","(","masks-like","object",")",":","Supported","types","are",":","*",":","class",":","`","detectron2.structures.PolygonMasks","`",",",":","class",":","`","detectron2.structures.BitMasks","`",".","*","list","[","list","[","ndarray","]","]",":","contains","the","segmentation","masks","for","all","objects","in","one","image",".","The","first","level","of","the","list","corresponds","to","individual","instances",".","The","second","level","to","all","the","polygon","that","compose","the","instance",",","and","the","third","level","to","the","polygon","coordinates",".","The","third","level","should","have","the","format","of","[","x0",",","y0",",","x1",",","y1",",","...",",","xn",",","yn","]","(","n",">","=","3",")",".","*","list","[","ndarray","]",":","each","ndarray","is","a","binary","mask","of","shape","(","H",",","W",")",".","*","list","[","dict","]",":","each","dict","is","a","COCO-style","RLE",".","keypoints","(","Keypoint","or","array","like",")",":","an","array-like","object","of","shape","(","N",",","K",",","3",")",",","where","the","N","is","the","number","of","instances","and","K","is","the","number","of","keypoints",".","The","last","dimension","corresponds","to","(","x",",","y",",","visibility","or","score",")",".","assigned_colors","(","list","[","matplotlib.colors","]",")",":","a","list","of","colors",",","where","each","color","corresponds","to","each","mask","or","box","in","the","image",".","Refer","to","'matplotlib.colors'","for","full","list","of","formats","that","the","colors","are","accepted","in",".","Returns",":","output","(","VisImage",")",":","image","object","with","visualizations","."]
code: def overlay_instances(
      self,
      *,
      boxes=None,
      labels=None,
      masks=None,
      keypoints=None,
      assigned_colors=None,
      alpha=0.5,
  ):
      """
      Args:
          boxes (Boxes, RotatedBoxes or ndarray): either a :class:`Boxes`,
              or an Nx4 numpy array of XYXY_ABS format for the N objects in a single image,
              or a :class:`RotatedBoxes`,
              or an Nx5 numpy array of (x_center, y_center, width, height, angle_degrees) format
              for the N objects in a single image,
          labels (list[str]): the text to be displayed for each instance.
          masks (masks-like object): Supported types are:
  
              * :class:`detectron2.structures.PolygonMasks`,
                :class:`detectron2.structures.BitMasks`.
              * list[list[ndarray]]: contains the segmentation masks for all objects in one image.
                The first level of the list corresponds to individual instances. The second
                level to all the polygon that compose the instance, and the third level
                to the polygon coordinates. The third level should have the format of
                [x0, y0, x1, y1, ..., xn, yn] (n >= 3).
              * list[ndarray]: each ndarray is a binary mask of shape (H, W).
              * list[dict]: each dict is a COCO-style RLE.
          keypoints (Keypoint or array like): an array-like object of shape (N, K, 3),
              where the N is the number of instances and K is the number of keypoints.
              The last dimension corresponds to (x, y, visibility or score).
          assigned_colors (list[matplotlib.colors]): a list of colors, where each color
              corresponds to each mask or box in the image. Refer to 'matplotlib.colors'
              for full list of formats that the colors are accepted in.
      Returns:
          output (VisImage): image object with visualizations.
      """
      num_instances = 0
      if boxes is not None:
          boxes = self._convert_boxes(boxes)
          num_instances = len(boxes)
      if masks is not None:
          masks = self._convert_masks(masks)
          if num_instances:
              assert len(masks) == num_instances
          else:
              num_instances = len(masks)
      if keypoints is not None:
          if num_instances:
              assert len(keypoints) == num_instances
          else:
              num_instances = len(keypoints)
          keypoints = self._convert_keypoints(keypoints)
      if labels is not None:
          assert len(labels) == num_instances
      if assigned_colors is None:
          assigned_colors = [
              random_color(rgb=True, maximum=1)
              for _ in range(num_instances)
          ]
      if num_instances == 0:
          return self.output
      if boxes is not None and boxes.shape[1] == 5:
          return self.overlay_rotated_instances(
              boxes=boxes,
              labels=labels,
              assigned_colors=assigned_colors,
          )
  
      # Display in largest to smallest order to reduce occlusion.
      areas = None
      if boxes is not None:
          areas = np.prod(boxes[:, 2:] - boxes[:, :2], axis=1)
      elif masks is not None:
          areas = np.asarray([x.area() for x in masks])
  
      if areas is not None:
          sorted_idxs = np.argsort(-areas).tolist()
          # Re-order overlapped instances in descending order.
          boxes = boxes[sorted_idxs] if boxes is not None else None
          labels = (
              [labels[k] for k in sorted_idxs]
              if labels is not None
              else None
          )
          masks = (
              [masks[idx] for idx in sorted_idxs]
              if masks is not None
              else None
          )
          assigned_colors = [
              assigned_colors[idx] for idx in sorted_idxs
          ]
          keypoints = (
              keypoints[sorted_idxs] if keypoints is not None else None
          )
  
      for i in range(num_instances):
          color = assigned_colors[i]
          if boxes is not None:
              self.draw_box(boxes[i], edge_color=color)
  
          if masks is not None:
              for segment in masks[i].polygons:
                  self.draw_polygon(
                      segment.reshape(-1, 2), color, alpha=alpha
                  )
  
          if labels is not None:
              # first get a box
              if boxes is not None:
                  x0, y0, x1, y1 = boxes[i]
                  text_pos = (
                      x0,
                      y0,
                  )  # if drawing boxes, put text on the box corner.
                  horiz_align = "left"
              elif masks is not None:
                  # skip small mask without polygon
                  if len(masks[i].polygons) == 0:
                      continue
  
                  x0, y0, x1, y1 = masks[i].bbox()
  
                  # draw text in the center (defined by median) when box is not drawn
                  # median is less sensitive to outliers.
                  text_pos = np.median(masks[i].mask.nonzero(), axis=1)[
                      ::-1
                  ]
                  horiz_align = "center"
              else:
                  continue  # drawing the box confidence for keypoints isn't very useful.
              # for small objects, draw text at the side to avoid occlusion
              instance_area = (y1 - y0) * (x1 - x0)
              if (
                  instance_area
                  < _SMALL_OBJECT_AREA_THRESH * self.output.scale
                  or y1 - y0 < 40 * self.output.scale
              ):
                  if y1 >= self.output.height - 5:
                      text_pos = (x1, y0)
                  else:
                      text_pos = (x0, y1)
  
              height_ratio = (y1 - y0) / np.sqrt(
                  self.output.height * self.output.width
              )
              lighter_color = self._change_color_brightness(
                  color, brightness_factor=0.7
              )
              font_size = (
                  np.clip((height_ratio - 0.02) / 0.08 + 1, 1.2, 2)
                  * 0.5
                  * self._default_font_size
              )
              self.draw_text(
                  labels[i],
                  text_pos,
                  color=lighter_color,
                  horizontal_alignment=horiz_align,
                  font_size=font_size,
              )
  
      # draw keypoints
      if keypoints is not None:
          for keypoints_per_instance in keypoints:
              self.draw_and_connect_keypoints(keypoints_per_instance)
  
      return self.output
  
imports: import colorsys
  import logging
  import math
  import numpy
  from enum import Enum, unique
  import cv2
  import matplotlib
  import matplotlib.colors
  import matplotlib.figure
  import pycocotools.mask
  import torch
  from matplotlib.backends.backend_agg import FigureCanvasAgg
  from PIL import Image
  from detectron2.data import MetadataCatalog
  from detectron2.structures import BitMasks, Boxes, BoxMode, Keypoints, PolygonMasks, RotatedBoxes
  from detectron2.utils.file_io import PathManager
  from .colormap import random_color
label_desc: Use image node_id 6 for calling the Visualizer obj's underlying member method code with example usage: obj.overlay_instances() and returns: self, self, self