code: def draw_segm(
      image,
      im_id,
      catid2name,
      segms,
      threshold,
      alpha=0.7,
      draw_box=True,
  ):
      """
      Draw segmentation on image
      """
      mask_color_id = 0
      w_ratio = 0.4
      color_list = colormap(rgb=True)
      img_array = np.array(image).astype("float32")
      for dt in np.array(segms):
          if im_id != dt["image_id"]:
              continue
          segm, score, catid = (
              dt["segmentation"],
              dt["score"],
              dt["category_id"],
          )
          if score < threshold:
              continue
          import pycocotools.mask as mask_util
  
          mask = mask_util.decode(segm) * 255
          color_mask = color_list[mask_color_id % len(color_list), 0:3]
          mask_color_id += 1
          for c in range(3):
              color_mask[c] = (
                  color_mask[c] * (1 - w_ratio) + w_ratio * 255
              )
          idx = np.nonzero(mask)
          img_array[idx[0], idx[1], :] *= 1.0 - alpha
          img_array[idx[0], idx[1], :] += alpha * color_mask
  
          if not draw_box:
              center_y, center_x = ndimage.measurements.center_of_mass(
                  mask
              )
              label_text = "{}".format(catid2name[catid])
              vis_pos = (max(int(center_x) - 10, 0), int(center_y))
              cv2.putText(
                  img_array,
                  label_text,
                  vis_pos,
                  cv2.FONT_HERSHEY_COMPLEX,
                  0.3,
                  (255, 255, 255),
              )
          else:
              mask = mask_util.decode(segm) * 255
              sum_x = np.sum(mask, axis=0)
              x = np.where(sum_x > 0.5)[0]
              sum_y = np.sum(mask, axis=1)
              y = np.where(sum_y > 0.5)[0]
              x0, x1, y0, y1 = x[0], x[-1], y[0], y[-1]
              cv2.rectangle(
                  img_array,
                  (x0, y0),
                  (x1, y1),
                  tuple(color_mask.astype("int32").tolist()),
                  1,
              )
              bbox_text = "%s %.2f" % (catid2name[catid], score)
              t_size = cv2.getTextSize(bbox_text, 0, 0.3, thickness=1)[
                  0
              ]
              cv2.rectangle(
                  img_array,
                  (x0, y0),
                  (x0 + t_size[0], y0 - t_size[1] - 3),
                  tuple(color_mask.astype("int32").tolist()),
                  -1,
              )
              cv2.putText(
                  img_array,
                  bbox_text,
                  (x0, y0 - 2),
                  cv2.FONT_HERSHEY_SIMPLEX,
                  0.3,
                  (0, 0, 0),
                  1,
                  lineType=cv2.LINE_AA,
              )
  
      return Image.fromarray(img_array.astype("uint8"))
  
file_path: PaddleDetection/ppdet/utils/visualizer.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  import os
  import numpy
  from PIL import Image, ImageDraw, ImageFont
  import cv2
  import math
  from .colormap import colormap
  from ppdet.utils.logger import setup_logger
  from ppdet.utils.compact import imagedraw_textsize_c
  from ppdet.utils.download import get_path
label_desc: Use image node_id 5 for calling a global function with example usage: draw_segm(image, im_id, catid2name, segms, threshold, alpha, draw_box) and returns: Image