Args: im (PIL.Image.Image): PIL image np_boxes (np.ndarray): shape:[N,6], N: number of box, matix element:[class, score, x_min, y_min, x_max, y_max] np_masks (np.ndarray): shape:[N, im_h, im_w] labels (list): labels:['class1', ..., 'classn'] t
(im, np_boxes, np_masks, labels, threshold=0.5)
| 87 | |
| 88 | |
| 89 | def draw_mask(im, np_boxes, np_masks, labels, threshold=0.5): |
| 90 | """ |
| 91 | Args: |
| 92 | im (PIL.Image.Image): PIL image |
| 93 | np_boxes (np.ndarray): shape:[N,6], N: number of box, |
| 94 | matix element:[class, score, x_min, y_min, x_max, y_max] |
| 95 | np_masks (np.ndarray): shape:[N, im_h, im_w] |
| 96 | labels (list): labels:['class1', ..., 'classn'] |
| 97 | threshold (float): threshold of mask |
| 98 | Returns: |
| 99 | im (PIL.Image.Image): visualized image |
| 100 | """ |
| 101 | color_list = get_color_map_list(len(labels)) |
| 102 | w_ratio = 0.4 |
| 103 | alpha = 0.7 |
| 104 | im = np.array(im).astype('float32') |
| 105 | clsid2color = {} |
| 106 | expect_boxes = (np_boxes[:, 1] > threshold) & (np_boxes[:, 0] > -1) |
| 107 | np_boxes = np_boxes[expect_boxes, :] |
| 108 | np_masks = np_masks[expect_boxes, :, :] |
| 109 | im_h, im_w = im.shape[:2] |
| 110 | np_masks = np_masks[:, :im_h, :im_w] |
| 111 | for i in range(len(np_masks)): |
| 112 | clsid, score = int(np_boxes[i][0]), np_boxes[i][1] |
| 113 | mask = np_masks[i] |
| 114 | if clsid not in clsid2color: |
| 115 | clsid2color[clsid] = color_list[clsid] |
| 116 | color_mask = clsid2color[clsid] |
| 117 | for c in range(3): |
| 118 | color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255 |
| 119 | idx = np.nonzero(mask) |
| 120 | color_mask = np.array(color_mask) |
| 121 | im[idx[0], idx[1], :] *= 1.0 - alpha |
| 122 | im[idx[0], idx[1], :] += alpha * color_mask |
| 123 | return Image.fromarray(im.astype('uint8')) |
| 124 | |
| 125 | |
| 126 | def draw_box(im, np_boxes, labels, threshold=0.5): |
no test coverage detected