def visualize_instances(image, boxes, masks, class_ids, class_names, scores=None, ax=None, file_name=None, colors=None, making_video=False, making_image=False, open_cv=False): # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] if not making_video: # Generate random colors colors = visualize.random_colors(N) fig, ax = get_ax() # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') masked_image = image.astype(np.uint32).copy() for i in range(N): if not making_video: color = colors[i] else: # all objects of a class get same color class_id = class_ids[i] color = colors[class_id - 1] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] # show bbox p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label class_id = class_ids[i] score = scores[i] if scores is not None else None try: label = class_names[class_id] except IndexError: label = 'label_error' x = random.randint(x1, (x1 + x2) // 2) caption = "{} {:.3f}".format(label, score) if score else label ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none") # Mask mask = masks[:, :, i] masked_image = visualize.apply_mask(masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) # To transform the drawn figure into ndarray X fig.canvas.draw() string = fig.canvas.tostring_rgb() masked = masked_image.astype(np.uint8) X = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') X = X.reshape(fig.canvas.get_width_height()[::-1] + (3, )) # open cv's RGB style: BGR # if open_cv: X = X[..., ::-1] if making_video: plt.close() # return masked_image.astype(np.uint8) # cv2.imshow('frame', X) # cv2.waitKey(1) return X elif making_image: # plt.savefig(file_name) cv2.imwrite(file_name, X)
def display_siamese_instances(target, image, boxes, masks, class_ids, scores=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None): """ boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. masks: [height, width, num_instances] class_ids: [num_instances] class_names: list of class names of the dataset scores: (optional) confidence scores for each box title: (optional) Figure title show_mask, show_bbox: To show masks and bounding boxes or not figsize: (optional) the size of the image colors: (optional) An array or colors to use with each object captions: (optional) A list of strings to use as captions for each object """ # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] # If no axis is passed, create one and automatically call show() auto_show = False if not ax: from matplotlib.gridspec import GridSpec # Use GridSpec to show target smaller than image fig = plt.figure(figsize=figsize) gs = GridSpec(3, 3) ax = plt.subplot(gs[:, 1:]) target_ax = plt.subplot(gs[1, 0]) auto_show = True # Generate random colors colors = colors or visualize.random_colors(N) # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') ax.set_title(title) target_height, target_width = target.shape[:2] target_ax.set_ylim(target_height + 10, -10) target_ax.set_xlim(-10, target_width + 10) target_ax.axis('off') # target_ax.set_title('target') masked_image = image.astype(np.uint32).copy() for i in range(N): color = colors[i] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None x = random.randint(x1, (x1 + x2) // 2) caption = "{:.3f}".format(score) if score else 'no score' else: caption = captions[i] ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none") # Mask mask = masks[:, :, i] if show_mask: masked_image = visualize.apply_mask(masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) target_ax.imshow(target.astype(np.uint8)) if auto_show: plt.show() return
def display_grid(target_list, image_list, boxes_list, masks_list, class_ids_list, scores_list=None, category_names_list=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None, show_scores=True, target_shift=10, fontsize=14, linewidth=2, save=False): """ boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. masks: [height, width, num_instances] class_ids: [num_instances] class_names: list of class names of the dataset scores: (optional) confidence scores for each box title: (optional) Figure title show_mask, show_bbox: To show masks and bounding boxes or not figsize: (optional) the size of the image colors: (optional) An array or colors to use with each object captions: (optional) A list of strings to use as captions for each object """ if type(target_list) == list: M = int(np.sqrt(len(target_list))) if len(target_list) - M**2 > 1e-3: M = M + 1 else: M = 1 target_list = [target_list] image_list = [image_list] boxes_list = [boxes_list] masks_list = [masks_list] class_ids_list = [class_ids_list] if scores_list is not None: scores_list = [scores_list] # If no axis is passed, create one and automatically call show() auto_show = False if not ax: from matplotlib.gridspec import GridSpec # Use GridSpec to show target smaller than image fig = plt.figure(figsize=figsize) gs = GridSpec(M, M, hspace=0.1, wspace=0.02, left=0, right=1, bottom=0, top=1) # auto_show = True REMOVE index = 0 for m1 in range(M): for m2 in range(M): ax = plt.subplot(gs[m1, m2]) if index >= len(target_list): continue target = target_list[index] image = image_list[index] boxes = boxes_list[index] masks = masks_list[index] class_ids = class_ids_list[index] scores = scores_list[index] # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] # Generate random colors colors = visualize.random_colors(N) # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height, 0) ax.set_xlim(0, width) ax.axis('off') ax.set_title(title) masked_image = image.astype(np.uint32).copy() for i in range(N): color = colors[i] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=linewidth, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None x = random.randint(x1, (x1 + x2) // 2) caption = "{:.3f}".format(score) if score else 'no score' else: caption = captions[i] if show_scores: ax.text(x1, y1 + 8, caption, color='w', size=int(10 / 14 * fontsize), backgroundcolor="none") # Mask mask = masks[:, :, i] if show_mask: masked_image = visualize.apply_mask( masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) target_height, target_width = target.shape[:2] target_height = target_height // 2 target_width = target_width // 2 target_area = target_height * target_width target_scaling = np.sqrt((192 // 2 * 96 // 2) / target_area) target_height = int(target_height * target_scaling) target_width = int(target_width * target_scaling) ax.imshow(target, extent=[ target_shift, target_shift + target_width * 2, height - target_shift, height - target_shift - target_height * 2 ], zorder=9) rect = visualize.patches.Rectangle( (target_shift, height - target_shift), target_width * 2, -target_height * 2, linewidth=5, edgecolor='white', facecolor='none', zorder=10) ax.add_patch(rect) if category_names_list is not None: plt.title(category_names_list[index], fontsize=fontsize) index = index + 1 if auto_show: plt.show() if save: fig.savefig('grid.pdf', bbox_inches='tight') return