def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='sbd') parser.add_argument('image') args = parser.parse_args() model = FCISPSROIAlignResNet101(n_fg_class=20, pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = read_image(args.image, color=True) masks, labels, scores = model.predict([img]) mask, label, score = masks[0], labels[0], scores[0] bbox = mask_to_bbox(mask) colors = voc_colormap(list(range(1, len(mask) + 1))) ax = vis_bbox(img, bbox, instance_colors=colors, alpha=0.5, linewidth=1.5) vis_instance_segmentation( None, mask, label, score, label_names=sbd_instance_segmentation_label_names, instance_colors=colors, alpha=0.7, ax=ax) plt.show()
def vis_6d_pose_estimation(camera_pcd, object_pcds, pose): pcds = [] pcd = copy.deepcopy(camera_pcd) pcd.transform(flip_rot_matrix) pcds.append(pcd) obj_pcds = copy.deepcopy(object_pcds) colors = voc_colormap(list(range(1, len(pose) + 1))) for obj_pcd, pse, color in zip(obj_pcds, pose, colors): obj_pcd.paint_uniform_color(color) obj_pcd.transform(pse.transpose((1, 0))) obj_pcd.transform(flip_rot_matrix) pcds.append(obj_pcd) open3d.draw_geometries(pcds)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default=None) parser.add_argument('--dataset', choices=('sbd', 'coco'), default='sbd') parser.add_argument('image') args = parser.parse_args() if args.dataset == 'sbd': if args.pretrained_model is None: args.pretrained_model = 'sbd' label_names = sbd_instance_segmentation_label_names model = FCISResNet101(n_fg_class=len(label_names), pretrained_model=args.pretrained_model) elif args.dataset == 'coco': if args.pretrained_model is None: args.pretrained_model = 'coco' label_names = coco_instance_segmentation_label_names proposal_creator_params = FCISResNet101.proposal_creator_params proposal_creator_params['min_size'] = 2 model = FCISResNet101(n_fg_class=len(label_names), anchor_scales=(4, 8, 16, 32), pretrained_model=args.pretrained_model, proposal_creator_params=proposal_creator_params) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = read_image(args.image, color=True) masks, labels, scores = model.predict([img]) mask, label, score = masks[0], labels[0], scores[0] bbox = mask_to_bbox(mask) colors = voc_colormap(list(range(1, len(mask) + 1))) ax = vis_bbox(img, bbox, instance_colors=colors, alpha=0.5, linewidth=1.5) vis_instance_segmentation(None, mask, label, score, label_names=label_names, instance_colors=colors, alpha=0.7, ax=ax) plt.show()
def vis_instance_segmentation_video(imgs, masks, repeat=False, ax=None, fig=None): """Vis video with bounding boxes """ import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation if fig is None: fig = plt.figure() if ax is not None: raise ValueError('when ax is not None, fig should be not None') if ax is None: ax = fig.add_subplot(1, 1, 1) assert len(imgs) > 0 assert len(imgs) == len(masks) n_inst = 10 instance_colors = voc_colormap(list(range(1, n_inst + 1))) instance_colors = np.array(instance_colors) overlayed_img = _get_overlayed_image(imgs[0], masks[0], instance_colors) img_ax = ax.imshow(overlayed_img) def init(): return img_ax, def update(data): img, mask = data overlayed_img = _get_overlayed_image(img, mask, instance_colors) img_ax.set_data(overlayed_img) return img_ax, fps = 60 ani = FuncAnimation( fig, update, frames=zip(imgs[1:], masks[1:]), init_func=init, blit=True, interval=1000/fps, repeat=repeat) return ani
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='coco') parser.add_argument('image') args = parser.parse_args() proposal_creator_params = { 'nms_thresh': 0.7, 'n_train_pre_nms': 12000, 'n_train_post_nms': 2000, 'n_test_pre_nms': 6000, 'n_test_post_nms': 1000, 'force_cpu_nms': False, 'min_size': 0 } model = FCISPSROIAlignResNet101( n_fg_class=len(coco_instance_segmentation_label_names), min_size=800, max_size=1333, anchor_scales=(2, 4, 8, 16, 32), pretrained_model=args.pretrained_model, proposal_creator_params=proposal_creator_params) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = read_image(args.image, color=True) masks, labels, scores = model.predict([img]) mask, label, score = masks[0], labels[0], scores[0] bbox = mask_to_bbox(mask) colors = voc_colormap(list(range(1, len(mask) + 1))) ax = vis_bbox( img, bbox, instance_colors=colors, alpha=0.5, linewidth=1.5) vis_instance_segmentation( None, mask, label, score, label_names=coco_instance_segmentation_label_names, instance_colors=colors, alpha=0.7, ax=ax) plt.show()
def vis_bbox(img, bbox, label=None, score=None, label_names=None, instance_colors=None, alpha=0.9, linewidth=3.0, ax=None): """Visualize bounding boxes inside image. Example: >>> from chainercv.datasets import VOCBboxDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from light_head_rcnn.visualizations import vis_bbox >>> import matplotlib.pyplot as plt >>> dataset = VOCBboxDataset() >>> img, bbox, label = dataset[60] >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names) >>> plt.show() This example visualizes by displaying the same colors for bounding boxes assigned to the same labels. >>> from chainercv.datasets import VOCBboxDataset >>> from chainercv.datasets import voc_bbox_label_names >>> from chainercv.visualizations.colormap import voc_colormap >>> from light_head_rcnn.visualizations import vis_bbox >>> import matplotlib.pyplot as plt >>> dataset = VOCBboxDataset() >>> img, bbox, label = dataset[61] >>> colors = voc_colormap(label + 1) >>> vis_bbox(img, bbox, label, ... label_names=voc_bbox_label_names, ... instance_colors=colors) >>> plt.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. If this is :obj:`None`, no image is displayed. bbox (~numpy.ndarray): An array of shape :math:`(R, 4)`, where :math:`R` is the number of bounding boxes in the image. Each element is organized by :math:`(y_{min}, x_{min}, y_{max}, x_{max})` in the second axis. label (~numpy.ndarray): An integer array of shape :math:`(R,)`. The values correspond to id for label names stored in :obj:`label_names`. This is optional. score (~numpy.ndarray): A float array of shape :math:`(R,)`. Each value indicates how confident the prediction is. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. If this is :obj:`None`, labels will be skipped. instance_colors (iterable of tuples): List of colors. Each color is RGB format and the range of its values is :math:`[0, 255]`. The :obj:`i`-th element is the color used to visualize the :obj:`i`-th instance. If :obj:`instance_colors` is :obj:`None`, the red is used for all boxes. alpha (float): The value which determines transparency of the bounding boxes. The range of this value is :math:`[0, 1]`. linewidth (float): The thickness of the edges of the bounding boxes. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: ~matploblib.axes.Axes: Returns the Axes object with the plot for further tweaking. """ from matplotlib import pyplot as plt if label is not None and not len(bbox) == len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and not len(bbox) == len(score): raise ValueError('The length of score must be same as that of bbox') # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) # If there is no bounding box to display, visualize the image and exit. if len(bbox) == 0: return ax n_inst = len(bbox) if instance_colors is None: instance_colors = voc_colormap(list(range(1, n_inst + 1))) instance_colors = np.array(instance_colors) for i, bb in enumerate(bbox): bb = np.round(bb).astype(np.int32) y_min, x_min, y_max, x_max = bb color = instance_colors[i % len(instance_colors)] / 255 ax.add_patch( plt.Rectangle((x_min, y_min), x_max - x_min, y_max - y_min, fill=False, edgecolor=color, linewidth=linewidth, alpha=alpha)) caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0: ax.text(x_min, y_min, ': '.join(caption), style='italic', bbox={ 'facecolor': color, 'alpha': alpha }, fontsize=8, color='white') return ax
filename='snapshot_epoch_{.updater.epoch}.npz'), trigger=(10, 'epoch')) trainer.run() """Here we are detecting the 2 classes in the image with bounding boxes. Along with it, it will display the class name and the accuracy for that particular prediction. It also displays the count for each class.""" import collections from chainercv import utils from chainercv.visualizations.colormap import voc_colormap from chainercv.visualizations import vis_bbox import matplotlib.pyplot as plt img = utils.read_image('NewDataset/JPEGImages/img1 res.jpg', color=True) bboxes, labels,scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] colors = voc_colormap(label + 1) vis_bbox(img, bbox, label, score*100, label_names=bball_labels, instance_colors=colors, alpha=0.9, linewidth=2.0) def CountFrequency(arr): return collections.Counter(arr) # Driver function if __name__ == "__main__": arr = label freq = CountFrequency(arr) # iterate dictionary named as freq to print # count of each element for key, value in freq.items(): if key==0:
def vis_semantic_segmentation(img, label, label_names=None, label_colors=None, ignore_label_color=(0, 0, 0), alpha=1, all_label_names_in_legend=False, ax=None): """Visualize a semantic segmentation. Example: >>> from chainercv.datasets import VOCSemanticSegmentationDataset >>> from chainercv.datasets \ ... import voc_semantic_segmentation_label_colors >>> from chainercv.datasets \ ... import voc_semantic_segmentation_label_names >>> from chainercv.visualizations import vis_semantic_segmentation >>> import matplotlib.pyplot as plt >>> dataset = VOCSemanticSegmentationDataset() >>> img, label = dataset[60] >>> ax, legend_handles = vis_semantic_segmentation( ... img, label, ... label_names=voc_semantic_segmentation_label_names, ... label_colors=voc_semantic_segmentation_label_colors, ... alpha=0.9) >>> ax.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2) >>> plt.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, height, width)`. This is in RGB format and the range of its value is :math:`[0, 255]`. If this is :obj:`None`, no image is displayed. label (~numpy.ndarray): An integer array of shape :math:`(height, width)`. The values correspond to id for label names stored in :obj:`label_names`. label_names (iterable of strings): Name of labels ordered according to label ids. label_colors: (iterable of tuple): An iterable of colors for regular labels. Each color is RGB format and the range of its values is :math:`[0, 255]`. If :obj:`colors` is :obj:`None`, the default color map is used. ignore_label_color (tuple): Color for ignored label. This is RGB format and the range of its values is :math:`[0, 255]`. The default value is :obj:`(0, 0, 0)`. alpha (float): The value which determines transparency of the figure. The range of this value is :math:`[0, 1]`. If this value is :obj:`0`, the figure will be completely transparent. The default value is :obj:`1`. This option is useful for overlaying the label on the source image. all_label_names_in_legend (bool): Determines whether to include all label names in a legend. If this is :obj:`False`, the legend does not contain the names of unused labels. An unused label is defined as a label that does not appear in :obj:`label`. The default value is :obj:`False`. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: matploblib.axes.Axes and list of matplotlib.patches.Patch: Returns :obj:`ax` and :obj:`legend_handles`. :obj:`ax` is an :class:`matploblib.axes.Axes` with the plot. It can be used for further tweaking. :obj:`legend_handles` is a list of legends. It can be passed :func:`matploblib.pyplot.legend` to show a legend. """ import matplotlib from matplotlib.patches import Patch if label_names is not None: n_class = len(label_names) elif label_colors is not None: n_class = len(label_colors) else: n_class = label.max() + 1 if label_colors is not None and not len(label_colors) == n_class: raise ValueError( 'The size of label_colors is not same as the number of classes') if label.max() >= n_class: raise ValueError('The values of label exceed the number of classes') # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) if label_names is None: label_names = [str(l) for l in range(label.max() + 1)] if label_colors is None: label_colors = voc_colormap(list(range(n_class))) # [0, 255] -> [0, 1] label_colors = np.array(label_colors) / 255 cmap = matplotlib.colors.ListedColormap(label_colors) canvas_img = cmap(label / (n_class - 1), alpha=alpha) # [0, 255] -> [0, 1] ignore_label_color = np.array(ignore_label_color) / 255, canvas_img[label < 0, :3] = ignore_label_color ax.imshow(canvas_img) legend_handles = [] if all_label_names_in_legend: legend_labels = [l for l in np.unique(label) if l >= 0] else: legend_labels = range(n_class) for l in legend_labels: legend_handles.append( Patch(color=cmap(l / (n_class - 1)), label=label_names[l])) return ax, legend_handles
def callback(image, pointcloud2, pub_detection_3d, pub_result_image, detector_2d, camera_info, extrinsics): rgb_image = ros_numpy.image.image_to_numpy(image) bboxes, labels, scores = detector_2d.predict(rgb_image) bboxes, labels, scores = bboxes[0], labels[0], scores[0] # If no objects are detected then an empty Detection3DResult is sent. if len(bboxes) == 0: pub_detection_3d.publish(Detection3DResult()) return # Coordinate transform of points from depth coord to color coord. R = np.array(extrinsics.rotation).reshape(3, 3) t = np.array(extrinsics.translation) points = ros_numpy.point_cloud2.pointcloud2_to_xyz_array(pointcloud2) points = points.dot(R) + t # Compute 3D centroids of the detected objects bboxes_points = extract_3d_points_in_bbox_frustum( bboxes, points, camera_info) centroids = [] for bbox_points in bboxes_points: centroid = mean_center_extractor(bbox_points) centroids.append(centroid) centroids = np.array(centroids) # Store and publish the result detection_result = Detection3DResult() for label, score, bbox, centroid in zip(labels, scores, bboxes, centroids): class_name = detector_2d.class_name_from_number[label] y_min, x_min, y_max, x_max = bbox position = Point(x=centroid[0], y=centroid[1], z=centroid[2]) detection_3d = Detection3D( class_id=label, class_name=class_name, score=score, y_min=y_min, x_min=x_min, y_max=y_max, x_max=x_max, position=position) detection_result.detections.append(detection_3d) detection_result.num_detections = len(detection_result.detections) pub_detection_3d.publish(detection_result) # Publish the result as an image if pub_result_image is not None: # Draw a result image as numpy.ndarray fig = Figure() canvas = FigureCanvasAgg(fig) ax = fig.gca() vis_bbox( rgb_image.transpose(2, 0, 1), bboxes, labels, score=scores, label_names=detector_2d.class_name_from_number, instance_colors=voc_colormap(labels + 1), linewidth=2.0, ax=ax) np.set_printoptions(precision=3) for text, centroid in zip(ax.texts, centroids): text.set_text('{}\nxyz: {}'.format(text.get_text(), centroid)) ax.axis('off') canvas.draw() width, height = (fig.get_size_inches() * fig.get_dpi()).astype(int) result_image = np.frombuffer(canvas.tostring_rgb(), dtype=np.uint8) result_image = result_image.reshape(height, width, 3) result_ros_image = ros_numpy.image.numpy_to_image(result_image, 'rgb8') pub_result_image.publish(result_ros_image)
def vis_instance_segmentation(img, mask, label=None, score=None, label_names=None, instance_colors=None, alpha=0.7, sort_by_score=True, ax=None): """Visualize instance segmentation. Example: This example visualizes an image and an instance segmentation. >>> from chainercv.datasets import SBDInstanceSegmentationDataset >>> from chainercv.datasets \ ... import sbd_instance_segmentation_label_names >>> from chainercv.visualizations import vis_instance_segmentation >>> import matplotlib.pyplot as plt >>> dataset = SBDInstanceSegmentationDataset() >>> img, mask, label = dataset[0] >>> vis_instance_segmentation( ... img, mask, label, ... label_names=sbd_instance_segmentation_label_names) >>> plt.show() This example visualizes an image, an instance segmentation and bounding boxes. >>> from chainercv.datasets import SBDInstanceSegmentationDataset >>> from chainercv.datasets \ ... import sbd_instance_segmentation_label_names >>> from chainercv.visualizations import vis_bbox >>> from chainercv.visualizations import vis_instance_segmentation >>> from chainercv.visualizations.colormap import voc_colormap >>> from chainercv.utils import mask_to_bbox >>> import matplotlib.pyplot as plt >>> dataset = SBDInstanceSegmentationDataset() >>> img, mask, label = dataset[0] >>> bbox = mask_to_bbox(mask) >>> colors = voc_colormap(list(range(1, len(mask) + 1))) >>> ax = vis_bbox(img, bbox, label, ... label_names=sbd_instance_segmentation_label_names, ... instance_colors=colors, alpha=0.7, linewidth=0.5) >>> vis_instance_segmentation( ... None, mask, instance_colors=colors, alpha=0.7, ax=ax) >>> plt.show() Args: img (~numpy.ndarray): See the table below. If this is :obj:`None`, no image is displayed. mask (~numpy.ndarray): See the table below. label (~numpy.ndarray): See the table below. This is optional. score (~numpy.ndarray): See the table below. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. instance_colors (iterable of tuple): List of colors. Each color is RGB format and the range of its values is :math:`[0, 255]`. The :obj:`i`-th element is the color used to visualize the :obj:`i`-th instance. If :obj:`instance_colors` is :obj:`None`, the default color map is used. alpha (float): The value which determines transparency of the figure. The range of this value is :math:`[0, 1]`. If this value is :obj:`0`, the figure will be completely transparent. The default value is :obj:`0.7`. This option is useful for overlaying the label on the source image. sort_by_score (bool): When :obj:`True`, instances with high scores are always visualized in front of instances with low scores. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. .. csv-table:: :header: name, shape, dtype, format :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ "RGB, :math:`[0, 255]`" :obj:`mask`, ":math:`(R, H, W)`", :obj:`bool`, -- :obj:`label`, ":math:`(R,)`", :obj:`int32`, \ ":math:`[0, \#fg\_class - 1]`" :obj:`score`, ":math:`(R,)`", :obj:`float32`, -- Returns: matploblib.axes.Axes: Returns :obj:`ax`. :obj:`ax` is an :class:`matploblib.axes.Axes` with the plot. """ # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) if score is not None and len(mask) != len(score): raise ValueError('The length of score must be same as that of mask') if label is not None and len(mask) != len(label): raise ValueError('The length of label must be same as that of mask') if sort_by_score and score is not None: order = np.argsort(score) mask = mask[order] score = score[order] if label is not None: label = label[order] bbox = mask_to_bbox(mask) n_inst = len(bbox) if instance_colors is None: instance_colors = voc_colormap(list(range(1, n_inst + 1))) instance_colors = np.array(instance_colors) _, H, W = mask.shape canvas_img = np.zeros((H, W, 4), dtype=np.uint8) for i, (bb, msk) in enumerate(zip(bbox, mask)): # The length of `colors` can be smaller than the number of instances # if a non-default `colors` is used. color = instance_colors[i % len(instance_colors)] rgba = np.append(color, alpha * 255) bb = np.round(bb).astype(np.int32) y_min, x_min, y_max, x_max = bb if y_max > y_min and x_max > x_min: canvas_img[msk] = rgba caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0: ax.text((x_max + x_min) / 2, y_min, ': '.join(caption), style='italic', bbox={ 'facecolor': color / 255, 'alpha': alpha }, fontsize=8, color='white') ax.imshow(canvas_img) return ax
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='sbd') parser.add_argument('video') args = parser.parse_args() model = FCISResNet101( n_fg_class=20, pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() if args.video == "0": vid = cv2.VideoCapture(0) else: vid = cv2.VideoCapture(args.video) if not vid.isOpened(): raise ImportError("Couldn't open video file or webcam.") # Compute aspect ratio of video vidw = vid.get(cv2.CAP_PROP_FRAME_WIDTH) vidh = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) # vidar = vidw / vidh print(vidw) print(vidh) accum_time = 0 curr_fps = 0 fps = "FPS: ??" prev_time = timer() frame_count = 1 while True: ret, frame = vid.read() if ret == False: print("Done!") return # BGR -> RGB rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Result image result = frame.copy() # (H, W, C) -> (C, H, W) img = np.asarray(rgb, dtype=np.float32).transpose((2, 0, 1)) # Object Detection masks, labels, scores = model.predict([img]) mask, label, score = masks[0], labels[0], scores[0] bbox = mask_to_bbox(mask) colors = voc_colormap(list(range(1, len(mask) + 1))) # For Colors n_inst = len(bbox) instance_colors = voc_colormap(list(range(1, n_inst + 1))) instance_colors = np.array(instance_colors) # For Mask _, H, W = mask.shape canvas_img = np.zeros((H, W, 4), dtype=np.uint8) alf_img = np.zeros((H, W, 1), dtype=np.uint8) if len(bbox) != 0: # for i, bb in enumerate(bbox): for i, (bb, msk) in enumerate(zip(bbox, mask)): # print(i) lb = label[i] conf = score[i].tolist() ymin = int(bb[0]) xmin = int(bb[1]) ymax = int(bb[2]) xmax = int(bb[3]) class_num = int(lb) # Draw box # cv2.rectangle(result, (xmin, ymin), (xmax, ymax), (0,255,0), 2) text = sbd_instance_segmentation_label_names[ class_num] + " " + ('%.2f' % conf) print(text) # text_pos 1 test_x = round(xmax - xmin / 2) - 30 test_y = round(ymax - ymin / 2) - 30 text_top = (test_x, test_y - 10) text_bot = (test_x + 80, test_y + 5) text_pos = (test_x + 5, test_y) # text_pos 2 # text_top = (xmin, ymin - 10) # text_bot = (xmin + 80, ymin + 5) # text_pos = (xmin + 5, ymin) # Draw label cv2.rectangle(result, text_top, text_bot, (255, 255, 255), -1) cv2.putText(result, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0), 1) # Draw msk 1 color = instance_colors[i % len(instance_colors)] rgba = np.append(color, 0.7 * 255) # alpha=0.7 if ymax > ymin and xmax > xmin: canvas_img[msk] = rgba mask_img = np.asarray(canvas_img) tmp_bgr = cv2.split(result) mask_result = cv2.merge(tmp_bgr + [alf_img]) mask_result = cv2.addWeighted(mask_result, 1, mask_img, 0.5, 0) # Draw msk 2 # rgba = np.append((0,255,0), 0.7 * 255) # alpha=0.7 # if ymax > ymin and xmax > xmin: # canvas_img[msk] = rgba # mask_img = np.asarray(canvas_img) # tmp_bgr = cv2.split(result) # mask_result = cv2.merge(tmp_bgr + [alf_img]) # mask_result = cv2.addWeighted(mask_result, 1, mask_img, 0.5, 0) # Calculate FPS curr_time = timer() exec_time = curr_time - prev_time prev_time = curr_time accum_time = accum_time + exec_time curr_fps = curr_fps + 1 if accum_time > 1: accum_time = accum_time - 1 fps = "FPS:" + str(curr_fps) curr_fps = 0 # Draw FPS in top right corner cv2.rectangle(result, (590, 0), (640, 17), (0, 0, 0), -1) cv2.putText(result, fps, (595, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) # Draw Frame Number cv2.rectangle(result, (0, 0), (50, 17), (0, 0, 0), -1) cv2.putText(result, str(frame_count), (0, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) # Output Result # cv2.imshow("BBOX Result", result) # cv2.imshow("Mask img", mask_img) cv2.imshow("Fcis Result", mask_result) # For Debug print("===== BBOX Result =====") print(type(result)) print(result.shape) print(type(result.shape)) print("===== Mask img =====") print(type(mask_img)) print(mask_img.shape) print(type(mask_img.shape)) # Stop Processing if cv2.waitKey(1) & 0xFF == ord('q'): break frame_count += 1
def vis_instance_segmentation(img, mask, label=None, score=None, label_names=None, colors=None, alpha=0.7, ax=None): """Visualize instance segmentation. Example: >>> from chainercv.datasets import SBDInstanceSegmentationDataset >>> from chainercv.datasets \ ... import sbd_instance_segmentation_label_names >>> from chainercv.visualizations import vis_instance_segmentation >>> import matplotlib.pyplot as plot >>> dataset = SBDInstanceSegmentationDataset() >>> img, mask, label = dataset[0] >>> vis_instance_segmentation( ... img, mask, label, ... label_names=sbd_instance_segmentation_label_names) >>> plot.show() Args: img (~numpy.ndarray): An array of shape :math:`(3, H, W)`. This is in RGB format and the range of its value is :math:`[0, 255]`. If this is :obj:`None`, no image is displayed. mask (~numpy.ndarray): A bool array of shape :math`(R, H, W)`. If there is an object, the value of the pixel is :obj:`True`, and otherwise, it is :obj:`False`. label (~numpy.ndarray): An integer array of shape :math:`(R, )`. The values correspond to id for label names stored in :obj:`label_names`. score (~numpy.ndarray): A float array of shape :math:`(R,)`. Each value indicates how confident the prediction is. This is optional. label_names (iterable of strings): Name of labels ordered according to label ids. colors: (iterable of tuple): List of colors. Each color is RGB format and the range of its values is :math:`[0, 255]`. The :obj:`i`-th element is the color used to visualize the :obj:`i`-th instance. If :obj:`colors` is :obj:`None`, the default color map is used. alpha (float): The value which determines transparency of the figure. The range of this value is :math:`[0, 1]`. If this value is :obj:`0`, the figure will be completely transparent. The default value is :obj:`0.7`. This option is useful for overlaying the label on the source image. ax (matplotlib.axes.Axis): The visualization is displayed on this axis. If this is :obj:`None` (default), a new axis is created. Returns: matploblib.axes.Axes: Returns :obj:`ax`. :obj:`ax` is an :class:`matploblib.axes.Axes` with the plot. """ # Returns newly instantiated matplotlib.axes.Axes object if ax is None ax = vis_image(img, ax=ax) bbox = mask_to_bbox(mask) if len(bbox) != len(mask): raise ValueError('The length of mask must be same as that of bbox') if label is not None and len(bbox) != len(label): raise ValueError('The length of label must be same as that of bbox') if score is not None and len(bbox) != len(score): raise ValueError('The length of score must be same as that of bbox') n_inst = len(bbox) if colors is None: colors = [voc_colormap(l) for l in range(1, n_inst + 1)] colors = np.array(colors) _, H, W = mask.shape canvas_img = np.zeros((H, W, 4), dtype=np.uint8) for i, (bb, msk) in enumerate(zip(bbox, mask)): # The length of `colors` can be smaller than the number of instances # if a non-default `colors` is used. color = colors[i % len(colors)] rgba = np.append(color, alpha * 255) bb = np.round(bb).astype(np.int32) y_min, x_min, y_max, x_max = bb if y_max > y_min and x_max > x_min: canvas_img[msk] = rgba caption = [] if label is not None and label_names is not None: lb = label[i] if not (0 <= lb < len(label_names)): raise ValueError('No corresponding name is given') caption.append(label_names[lb]) if score is not None: sc = score[i] caption.append('{:.2f}'.format(sc)) if len(caption) > 0: ax.text((x_max + x_min) / 2, y_min, ': '.join(caption), style='italic', bbox={ 'facecolor': color / 255, 'alpha': alpha }, fontsize=8, color='white') ax.imshow(canvas_img) return ax