def visualize(self, i): img = self._get_image(i) depth = self._get_depth(i) f, axes = plt.subplots(1, 2, sharey=True) vis_image(img, ax=axes[0]) axes[1].imshow(depth) plt.show()
def visualize_result(idx): fig = plt.figure() ax = fig.add_subplot(121) ax2 = fig.add_subplot(122) anno = annotations["root"][idx] print(anno) img_path = os.path.join(panoptic,anno["img_paths"]) img=chainercv.utils.read_image(img_path) vis_image(img,ax=ax) joint=np.array(anno["joint_self"]) joint=joint[:,:2] ax.scatter(*joint.transpose()) umin = joint[:, 0].min() umax = joint[:, 0].max() ulen = umax-umin vmin = joint[:, 1].min() vmax = joint[:, 1].max() vlen = vmax-vmin boxlen = int(1.5*max(ulen,vlen)) uc, vc = (umax+umin)/2, (vmax+vmin)/2 uc = int(uc) vc = int(vc) _,imH,imW=img.shape vmin=max(0,vc-boxlen//2) vmax=min(imH,vc+boxlen//2) umin=max(0,uc-boxlen//2) umax=min(imW,uc+boxlen//2) vis_image(img[:,vmin:vmax,umin:umax], ax=ax2) joint= joint[:,:2]-np.array([[umin,vmin]]) ax2.scatter(*joint.transpose())
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model') parser.add_argument('--dataset', choices=('camvid', ), default='camvid') parser.add_argument('image') args = parser.parse_args() if args.dataset == 'camvid': if args.pretrained_model is None: args.pretrained_model = 'camvid' label_names = camvid_label_names colors = camvid_label_colors model = SegNetBasic(n_class=len(label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) # Do not overlay the label image on the color image vis_semantic_segmentation(None, label, label_names, colors, ax=ax2) plt.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='cityscapes') parser.add_argument('--min-input-size', type=int, default=None) parser.add_argument('image') args = parser.parse_args() model = DeepLabV3plusXception65( pretrained_model=args.pretrained_model, min_input_size=args.min_input_size) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) # Do not overlay the label image on the color image vis_semantic_segmentation( None, label, voc_semantic_segmentation_label_names, voc_semantic_segmentation_label_colors, ax=ax2) plt.show()
def visualize_result(idx): fig = plt.figure() ax = fig.add_subplot(121) ax2 = fig.add_subplot(122) file = json_files[idx] with open(file, 'r') as f: anno = json.load(f) print(anno) joint=np.array(anno["hand_pts"]) is_left=anno["is_left"] img_path = os.path.splitext(file)[0]+".jpg" img = chainercv.utils.read_image(img_path) vis_image(img, ax=ax) umin = joint[:, 0].min() umax = joint[:, 0].max() ulen = umax-umin vmin = joint[:, 1].min() vmax = joint[:, 1].max() vlen = vmax-vmin boxlen = int(1.5*max(ulen,vlen)) uc, vc = (umax+umin)/2, (vmax+vmin)/2 uc = int(uc) vc = int(vc) _,imH,imW=img.shape vmin=max(0,vc-boxlen//2) vmax=min(imH,vc+boxlen//2) umin=max(0,uc-boxlen//2) umax=min(imW,uc+boxlen//2) vis_image(img[:,vmin:vmax,umin:umax], ax=ax2) joint= joint[:,:2]-np.array([[umin,vmin]]) ax2.scatter(*joint.transpose())
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-g', type=int, default=-1) parser.add_argument('--pretrained-model') parser.add_argument('--input-size', type=int, default=713) parser.add_argument('image') args = parser.parse_args() label_names = cityscapes_semantic_segmentation_label_names colors = cityscapes_semantic_segmentation_label_colors n_class = len(label_names) input_size = (args.input_size, args.input_size) model = PSPNetResNet101(n_class, args.pretrained_model, input_size) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu(args.gpu) img = read_image(args.image) labels = model.predict([img]) label = labels[0] fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) ax2, legend_handles = vis_semantic_segmentation( img, label, label_names, colors, ax=ax2) ax2.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2) plt.show()
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', default='camvid') parser.add_argument('image') args = parser.parse_args() model = SegNetBasic( n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plot.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) vis_semantic_segmentation( label, camvid_label_names, camvid_label_colors, ax=ax2) plot.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model') parser.add_argument('image') args = parser.parse_args() model = ResNet50( pretrained_model=args.pretrained_model, n_class=len(voc_bbox_label_names)) model.pick = 'fc6' if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) predict_func = PredictFunc(model, thresh=0.5) labels, scores = predict_func([img]) label = labels[0] score = scores[0] print('predicted labels') for lb, sc in zip(label, score): print('names={} score={:.4f}'.format( voc_bbox_label_names[lb], sc)) vis_image(img) plt.show()
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', default='camvid') parser.add_argument('image') args = parser.parse_args() model = SegNetBasic( n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plot.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) vis_label(label, camvid_label_names, camvid_label_colors, ax=ax2) plot.show()
def demo_enet(): """Demo ENet.""" chainer.config.train = False chainer.config.enable_backprop = False config, img_path = parse_args() test_data = load_dataset_test(config["dataset"]) test_iter = create_iterator_test(test_data, config['iterator']) model = get_model(config["model"]) devices = parse_devices(config['gpus']) if devices: model.to_gpu(devices['main']) img = read_image(img_path) img = img.transpose(1, 2, 0) img = cv2.resize(img, (1024, 512)).transpose(2, 0, 1) for i in range(2): s = time.time() pred = model.predict(img)[0] print("time: {}".format(time.time() - s)) # Save the result image ax = vis_image(img) _, legend_handles = vis_semantic_segmentation( pred, label_colors=cityscapes_label_colors, label_names=cityscapes_label_names, alpha=1.0, ax=ax) ax.legend(handles=legend_handles, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plot.axis('off') plot.show() fig, (ax1, ax2) = plot.subplots(ncols=2, figsize=(10, 3)) img = read_image( img_path.replace("leftImg8bit", "gtFine").replace("gtFine.png", "gtFine_color.png")) img = img.transpose(1, 2, 0) ax1.set_title("GroundTruth") img = cv2.resize(img, (1024, 512)).transpose(2, 0, 1) ax1 = vis_image(img, ax1) ax2 = vis_image(img, ax2) ax2, legend_handles = vis_semantic_segmentation( pred, label_colors=cityscapes_label_colors, label_names=cityscapes_label_names, alpha=1.0, ax=ax2) ax2.set_title("Prediction") ax1.axis("off") ax2.axis("off") plot.tight_layout() plot.savefig("./compare.png", dpi=400, bbox_inches='tight') plot.show()
def mtsm(self, img, old_boxes, new_boxes, Spixels, contours, s_boxes, s_superpixels, save=True, path=None ): """ Visualize the change in the bboxes after box alignment and straddling expansion Args: img : Input image old_boxes: Old bboxes new_boxes: Updated bboxes Spixels : set of superpixels """ # fig = plt.figure(figsize=(16, 7)) fig = plt.figure() ax1 = fig.add_subplot(221) plt.xticks([]) plt.yticks([]) plt.title('Image') vis_image(img, ax=ax1) ax2 = fig.add_subplot(222) plt.xticks([]) plt.yticks([]) plt.title('Original box') vis_bbox(img, old_boxes, ax=ax2, linewidth=1.0) ax3 = fig.add_subplot(223) plt.xticks([]) plt.yticks([]) plt.title('After box-alignment') vis_bbox(img, new_boxes, ax=ax3, linewidth=1.0) spixel_ = Spixels[0] for spixel in Spixels[1:]: spixel_ = np.bitwise_or(spixel_, spixel) spixel_[spixel_ > 0] = 1 plt.imshow(spixel_, cmap='gray', alpha=0.5, interpolation='none') ax4 = fig.add_subplot(224) plt.xticks([]) plt.yticks([]) plt.title('After straddling expansion') vis_bbox(img, s_boxes, ax=ax4, linewidth=1.0) spixel_ = s_superpixels[0] for spixel in s_superpixels: spixel_ = np.bitwise_or(spixel_, spixel) spixel_[spixel_ > 0] = 1 plt.imshow(spixel_, cmap='gray', alpha=0.5, interpolation='none') if save: if path is None: raise ValueError('Path should be set') plt.savefig(path+'.png') else: plt.show() plt.close(fig)
def visualize(self, i): img = self._get_image(i) depth = self._get_depth(i) lbl_img = self._get_lbl_img(i) f, axes = plt.subplots(1, 3, sharey=True) vis_image(img, ax=axes[0]) axes[1].imshow(depth) _, legend_handles = vis_semantic_segmentation( img, lbl_img + 1, label_names=['background'] + self.label_names, ax=axes[2]) axes[2].legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2) plt.show()
def test_vis_image(self): if optional_modules: img = np.random.randint(0, 255, size=(3, 32, 32)).astype(np.float32) ax = vis_image(img) self.assertTrue(isinstance(ax, matplotlib.axes.Axes))
def demo_linknet(): """Demo LinkNet.""" chainer.config.train = False chainer.config.enable_backprop = False config, img_path = parse_args() test_data = load_dataset_test(config["dataset"]) test_iter = create_iterator_test(test_data, config['iterator']) model = get_model(config["model"]) devices = parse_devices(config['gpus']) if devices: model.to_gpu(devices['main']) img = read_image(img_path) # img = img.transpose(1, 2, 0) # img = cv2.resize(img, (512, 256)).transpose(2, 0, 1) for i in range(2): s = time.time() pred = model.predict(img)[0] print("time: {}".format(time.time() - s)) # Save the result image ax = vis_image(img) _, legend_handles = vis_semantic_segmentation( pred, label_colors=cityscapes_label_colors, label_names=cityscapes_label_names, alpha=1.0, ax=ax) ax.legend(handles=legend_handles, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plot.show()
def test_vis_image(self): if optional_modules: img = np.random.randint( 0, 255, size=(3, 32, 32)).astype(np.float32) ax = vis_image(img) self.assertTrue(isinstance(ax, matplotlib.axes.Axes))
def visualize(data_idx, frame_idx, cam_idx): data_folder = annotated_dirs[data_idx] img_path = os.path.join(dataset_dir, "annotated_frames", data_folder, "{}_webcam_{}.jpg".format(frame_idx, cam_idx)) img_path = os.path.join(dataset_dir, "augmented_samples", data_folder, "{}_webcam_{}.jpg".format(frame_idx, cam_idx)) joint_path = os.path.join(dataset_dir, "annotated_frames", data_folder, "{}_joints.txt".format(frame_idx)) df = pd.read_csv(joint_path, sep=' ', usecols=[1, 2, 3], header=None) joint = df.to_numpy() joint = joint[:21] # ignore last keypoint i.e. PALM_NORMAL calib_dir = os.path.join(dataset_dir, "calibrations", data_folder, "webcam_{}".format(cam_idx)) print(img_path, joint_path, calib_dir) with open(os.path.join(calib_dir, "rvec.pkl"), 'rb') as f: calibR = pickle.load(f, encoding='latin1') with open(os.path.join(calib_dir, "tvec.pkl"), 'rb') as f: calibT = pickle.load(f, encoding='latin1') R, _ = cv2.Rodrigues(calibR) joint = joint.dot(R.transpose()) + calibT.transpose() print(joint.shape) #uv, _ = cv2.projectPoints(joint, calibR, calibT, cam_intr, distCoeffs) uv = joint.dot(cam_intr.transpose()) uv = uv / uv[:, 2:] uv = uv[:, :2] print(uv.shape) fig = plt.figure() ax = fig.add_subplot(121) ax3 = fig.add_subplot(122, projection="3d") img = chainercv.utils.read_image(img_path) vis_image(img, ax=ax) for xyz in joint: ax3.scatter(*xyz) ax.scatter(*uv.transpose()) for s, t in EDGES: xyz = joint[[s, t]] ax3.plot(*xyz.transpose()) ax3.set_xlabel("x") ax3.set_ylabel("y") ax3.set_zlabel("z") ax3.view_init(-65, -90)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-g', type=int, default=-1) parser.add_argument('--pretrained-model') parser.add_argument('--input-size', type=int, default=448) args = parser.parse_args() label_names = voc_semantic_segmentation_label_names colors = voc_semantic_segmentation_label_colors n_class = len(label_names) input_size = (args.input_size, args.input_size) model = get_pspnet_resnet50(n_class) chainer.serializers.load_npz(args.pretrained_model, model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu(args.gpu) dataset = get_sbd_augmented_voc() for i in range(1, 100): img = dataset[i][0] # img = read_image(args.image) labels = model.predict([img]) label = labels[0] from chainercv.utils import write_image write_image(label[None], '{}.png'.format(i)) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) ax2, legend_handles = vis_semantic_segmentation(img, label, label_names, colors, ax=ax2) ax2.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2) plt.show()
def save(self, in_data): result, output, gt_bbox = in_data patient_id, bbox, label, score = result _, image, h_seg, h_hor, h_ver = output _, gt_bbox = gt_bbox ax = vis_pred_gt_bboxes(image[0], bbox, gt_bbox) if self.overlay_seg: seg = F.sigmoid(h_seg[0, :, :]).array seg = np.repeat(seg, self.downscale * 2, axis=0) seg = np.repeat(seg, self.downscale * 2, axis=1) mask_image = np.stack( (np.zeros_like(seg), np.zeros_like(seg), np.zeros_like(seg) + 255, seg * 255)).astype(np.uint8) vis_image(mask_image, ax=ax) file_path = os.path.join(self.dst_dir, '{}.png'.format(patient_id)) print("Saving demo to '{}'...".format(file_path)) plt.savefig(file_path) plt.close()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-g', type=int, default=-1) parser.add_argument('--pretrained-model') parser.add_argument('--input-size', type=int, default=448) args = parser.parse_args() label_names = voc_semantic_segmentation_label_names colors = voc_semantic_segmentation_label_colors n_class = len(label_names) input_size = (args.input_size, args.input_size) model = get_pspnet_resnet50(n_class) chainer.serializers.load_npz(args.pretrained_model, model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu(args.gpu) dataset = get_sbd_augmented_voc() for i in range(1, 100): img = dataset[i][0] # img = read_image(args.image) labels = model.predict([img]) label = labels[0] from chainercv.utils import write_image write_image( label[None], '{}.png'.format(i)) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) ax2, legend_handles = vis_semantic_segmentation( img, label, label_names, colors, ax=ax2) ax2.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2) plt.show()
def crop_around_3d_center(subject_id, action, seq_idx, frame_id): fig = plt.figure(figsize=(10,5)) ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122,projection="3d") label_3d(ax2) ax2.view_init(-90,-90) example = get_example(subject_id, action, seq_idx, frame_id) cam_joints = example["cam_joints"][:,::-1] vu, z_ = zyx2vu(cam_joints, return_z=True) vu_com,z_com = calc_com(vu,z_) zyx_com= vu2zyx(vu_com[np.newaxis], z_com[np.newaxis]).squeeze() z_com,y_com,x_com=zyx_com xmin,ymin,xmax,ymax=x_com-crop3dW/2,y_com-crop3dH/2,x_com+crop3dW/2,y_com+crop3dH/2 print(xmin,ymin,xmax,ymax,z_com) [ [vmin,umin], [vmax,umax], ]=zyx2vu(np.array([ [z_com,ymin,xmin], [z_com,ymax,xmax], ])).astype(int) domain = [vmin, umin, vmax, umax] img = example["image"] cropped, param = crop_domain(img, domain) visualizations.vis_image(cropped, ax=ax1) offset_vu=np.array([param["y_offset"], param["x_offset"]]) vu = offset_vu+vu color = [COLOR_MAP[k]/255 for k in KEYPOINT_NAMES] ax1.scatter(*vu[:,::-1].transpose(), color=color) ax2.scatter(*((cam_joints-zyx_com)[:,::-1]).transpose(),color=color) for s, t in EDGES: joint_s_and_t = cam_joints[[s,t]]-zyx_com color = COLOR_MAP[s, t]/255 vu_s_and_t=vu[[s,t]] ax1.plot(*vu_s_and_t[:,::-1].transpose(),color=color) ax2.plot(*joint_s_and_t[:,::-1].transpose(), color=color)
def visualize_synth(synth, idx): fig=plt.figure(figsize=(10,5)) ax1=fig.add_subplot(121) ax2=fig.add_subplot(122) manual_train_dir = os.path.join(manual_annotations_dir, "synth{}".format(synth)) json_files = sorted(glob.glob(os.path.join(manual_train_dir, "*.json"))) file=json_files[idx] with open(file,'r') as f: anns=json.load(f) print(anns) joint=np.array(anns["hand_pts"]) print(joint.shape) is_left=anns["is_left"] print(file) imfile = file.replace("json","jpg") print(imfile) import imageio img=chainercv.utils.read_image(imfile) vis_image(img,ax=ax1) umin = joint[:, 0].min() umax = joint[:, 0].max() ulen = umax-umin vmin = joint[:, 1].min() vmax = joint[:, 1].max() vlen = vmax-vmin boxlen = int(1.5*max(ulen,vlen)) uc, vc = (umax+umin)/2, (vmax+vmin)/2 uc = int(uc) vc = int(vc) _,imH,imW=img.shape vmin=max(0,vc-boxlen//2) vmax=min(imH,vc+boxlen//2) umin=max(0,uc-boxlen//2) umax=min(imW,uc+boxlen//2) vis_image(img[:,vmin:vmax,umin:umax], ax=ax2) joint= joint[:,:2]-np.array([[umin,vmin]]) plt.scatter(*joint.transpose())
from chainercv.visualizations import vis_image from chainercv.visualizations import vis_label import matplotlib.pyplot as plot fig = plot.figure(figsize=(26, 10)) ax1 = fig.add_subplot(1, 2, 1) plot.axis('off') ax2 = fig.add_subplot(1, 2, 2) plot.axis('off') dataset = VOCDetectionDataset() img, bbox, label = dataset[310] vis_bbox(img, bbox, label, label_names=voc_detection_label_names, ax=ax1) dataset = VOCSemanticSegmentationDataset() img, label = dataset[30] vis_image(img, ax=ax2) _, legend_handles = vis_label( label, label_names=voc_semantic_segmentation_label_names, label_colors=voc_semantic_segmentation_label_colors, alpha=0.9, ax=ax2) # ax2.legend(handles=legend_handles, bbox_to_anchor=(1, 1), loc=2) plot.tight_layout() plot.savefig('../images/vis_visualization.png') plot.show()
def test_vis_image(self): ax = vis_image(self.img) self.assertTrue(isinstance(ax, matplotlib.axes.Axes))
def _debug_imshow(self, img): if self.debug: vis_image(img) plt.show()
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 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
def test_vis_image(self): if optional_modules: ax = vis_image(self.img) self.assertTrue(isinstance(ax, matplotlib.axes.Axes))
kp_coord_uv = anno['uv_vis'][:, :2] # visibility of the keypoints, boolean kp_visible = (anno['uv_vis'][:, 2] == 1) kp_coord_xyz = anno['xyz'] # x, y, z coordinates of the keypoints, in meters camera_intrinsic_matrix = anno['K'] # matrix containing intrinsic parameters # Project world coordinates into the camera frame kp_coord_uv_proj = np.matmul(kp_coord_xyz, np.transpose(camera_intrinsic_matrix)) kp_coord_uv_proj = kp_coord_uv_proj[:, :2] / kp_coord_uv_proj[:, 2:] from chainercv.utils import read_image from chainercv.visualizations import vis_image fig = plt.figure(figsize=(10, 8)) ax1 = fig.add_subplot(111) vis_image(read_image(img_file), ax=ax1) ax1.plot(kp_coord_uv[kp_visible, 0], kp_coord_uv[kp_visible, 1], 'ro') ax1.plot(kp_coord_uv_proj[kp_visible, 0], kp_coord_uv_proj[kp_visible, 1], 'gx') fig = plt.figure(figsize=(10, 8)) ax1 = fig.add_subplot(111) ax1.imshow(depth) ax1.plot(kp_coord_uv[kp_visible, 0], kp_coord_uv[kp_visible, 1], 'ro') ax1.plot(kp_coord_uv_proj[kp_visible, 0], kp_coord_uv_proj[kp_visible, 1], 'gx') vis_image(255 * read_image(mask_file, dtype=np.uint8)) np.unique(cv2.imread(mask_file))
import numpy as np # - # load annotations of this set data_type = "/root/dataset/RHD_published_v2/training" with open(os.path.join(data_type, 'anno_training.pickle'), 'rb') as f: anno_all = pickle.load(f) sample_id = 0 anno = anno_all[sample_id] file_format = "{:05d}.png".format(sample_id) img_file = os.path.join(data_type, "color", file_format) mask_file = os.path.join(data_type, "mask", file_format) depth_file = os.path.join(data_type, "depth", file_format) vis_image(255 * read_image(mask_file, dtype=np.uint8)) mask_image = np.squeeze(read_image(mask_file, dtype=np.uint8, color=False)) # # Mask # # ``` # Segmentation masks available: # 0: background, 1: person, # 2-4: left thumb [tip to palm], 5-7: left index, ..., 14-16: left pinky, 17: palm, # 18-20: right thumb, ..., right palm: 33 # ``` # + ONESIDE_KEYPOINT_NAMES = [] for k in ["wrist", "thumb", "index", "middle", "ring", "little"]:
elif args.model == 'cityscapes': model = PSPNet(pretrained_model='cityscapes') labels = cityscapes_label_names colors = cityscapes_label_colors elif args.model == 'ade20k': model = PSPNet(pretrained_model='ade20k') labels = ade20k_label_names colors = ade20k_label_colors if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu(args.gpu) img = read_image(args.img_fn) pred = model.predict([img])[0] # Save the result image ax = vis_image(img) _, legend_handles = vis_semantic_segmentation(pred, labels, colors, alpha=1.0, ax=ax) ax.legend(handles=legend_handles, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) base = os.path.splitext(os.path.basename(args.img_fn))[0] out_fn = os.path.join(args.out_dir, 'predict_{}.png'.format(base)) plot.savefig(out_fn, bbox_inches='tight', dpi=400)
import numpy as np import PIL def resize(img, size): # Same resize method as torchvision H, W = size img = img.transpose(1, 2, 0).astype(np.uint8) p = PIL.Image.fromarray(img, mode='RGB') p = np.array(p.resize((W, H))) return p.transpose(2, 0, 1).astype(np.float32) if __name__ == '__main__': from linemod_dataset import LinemodDataset from chainercv.visualizations import vis_image import matplotlib.pyplot as plt dataset = LinemodDataset('..') img, _, _ = dataset[0] img = resize(img, (543, 543)) vis_image(img) plt.show()
import argparse if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--model', '-m', type=str, default="model/ResNet-50-model.npz") parser.add_argument('--image', '-i', type=str, default="data/cat.jpeg") args = parser.parse_args() img = utils.read_image(args.image, color=True) img = F.expand_dims(img, 0) img = F.resize_images(img, (224, 224)) ax = vis_image(F.squeeze(img, 0).data) model = ResNetCAM() chainer.serializers.load_npz(args.model, model) with chainer.using_config('train', False): conv_outputs, preds = model(img) resized_conv_outputs = F.resize_images(conv_outputs, (224, 224)) to_multiply_tensor = model.fc6.W[2].reshape(1, 2048) heatmap = np.flip( F.tensordot(to_multiply_tensor, resized_conv_outputs).data, 1) ax.imshow(heatmap, cmap='jet', alpha=.4) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False)
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