def test_bgr2rgb(self): in_img = np.random.rand(10, 10, 3).astype(np.float32) out_img = mmcv.bgr2rgb(in_img) assert out_img.shape == in_img.shape assert_array_equal(out_img[..., 0], in_img[..., 2]) assert_array_equal(out_img[..., 1], in_img[..., 1]) assert_array_equal(out_img[..., 2], in_img[..., 0])
def show_result_pyplot(model, img, result, score_thr=0.3, fig_size=(15, 10), out_file=None): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, score_thr=score_thr, show=False, out_file=out_file) if out_file is None: plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.show()
def show_result_pyplot(img, result, class_names, score_thr=0.3, fig_size=(15, 10), out_file=None): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. out_file (str, optional): If specified, the visualization result will be written to the out file instead of shown in a window. """ img = show_result(img, result, class_names, score_thr=score_thr, show=False, out_file=out_file) if out_file is None: plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img))
def main(): parser = ArgumentParser() parser.add_argument('img_dir', help='Image dir') parser.add_argument('config_dir', help='Config dir') parser.add_argument('checkpoint_dir', help='Checkpoint dir') parser.add_argument('result_dir', help='result dir') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--score-thr', type=float, default=0.9, help='bbox score threshold') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config_dir, args.checkpoint_dir, device=args.device) # test some img select_result = [] for img_file in os.listdir(args.img_dir): print(img_file) img = args.img_dir + img_file result = inference_detector(model, img) # show the results result_pic = show_result_pyplot(model, img, result, score_thr=args.score_thr) plt.figure(figsize=(15, 10)) plt.imshow(mmcv.bgr2rgb(result_pic)) plt.title('result') plt.tight_layout() plt.savefig(args.result_dir + img_file)
def save_result_pyplot(model, img, result, output_path, score_thr=0.3, fig_size=(15, 10)): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result( img, result, score_thr=score_thr, show=False, bbox_color='red', text_color='red', thickness=4, font_scale=0.75, ) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.savefig(output_path, format='jpg') plt.cla()
def show_result_pyplot(index, model, img, result, score_thr=0.3, fig_size=(15, 10)): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, score_thr=score_thr, show=False, thickness=5) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.axis('off') plt.savefig('./result_images/' + index[-16:], bbox_inches='tight', pad_inches=0) plt.clf() plt.close('all')
def show_result_pyplot(model, img, result, score_thr=0.3, fig_size=(15, 10), title='result', block=True): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. title (str): Title of the pyplot figure. block (bool): Whether to block GUI. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, score_thr=score_thr, show=False) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.title(title) plt.tight_layout() plt.show(block=block)
def show_pose_result(model, img, result, kpt_score_thr=0.3, skeleton=None, fig_size=(15, 10)): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str | np.ndarray): Image filename or loaded image. result (list[dict]): The results to draw over `img` (bbox_result, pose_result). kpt_score_thr (float): The threshold to visualize the keypoints. skeleton (list[tuple()]): fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, skeleton, kpt_score_thr=kpt_score_thr, show=False) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.show()
def show_result_pyplot(img: Union[str, np.ndarray], result: np.ndarray, palette: Optional[Iterable] = None, fig_size: Iterable[int] = (15, 10), opacity: float = 0.5, title: str = '', block: bool = True): img = mmcv.imread(img) img = img.copy() seg = result[0] seg = mmcv.imresize(seg, img.shape[:2][::-1]) palette = np.array(palette) assert palette.shape[1] == 3 assert len(palette.shape) == 2 assert 0 < opacity <= 1.0 color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) for label, color in enumerate(palette): color_seg[seg == label, :] = color # convert to BGR color_seg = color_seg[..., ::-1] img = img * (1 - opacity) + color_seg * opacity img = img.astype(np.uint8) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.title(title) plt.tight_layout() plt.show(block=block)
def _totensor(img, bgr2rgb, float32): if img.shape[2] == 3 and bgr2rgb: img = mmcv.bgr2rgb(img) img = torch.from_numpy(img.transpose(2, 0, 1)) if float32: img = img.float() return img
def show_result_pyplot(model, img_path, result, score_thr=0.3, fig_size=(15, 10)): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img, coordinates_list, label_txts = model.show_result(img_path, result, score_thr=score_thr, show=False) # color_scheme = extract_color_scheme(img, coordinates_list) # color_scheme = scheme(img, coordinates_list) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.show() # img = mmcv.bgr2rgb(img) save_path = os.path.join('results', img_path.split('/')[-1]) mmcv.imwrite(img, save_path) return img, coordinates_list, label_txts
def convert_color(img_group, to_grayscale, to_rgb): if to_grayscale: return [mmcv.bgr2gray(img, keepdim=True) for img in img_group] elif to_rgb: return [mmcv.bgr2rgb(img) for img in img_group] else: return img_group
def save_result_pyplot(img, result, class_names, images_path, results_path, score_thr=0.3, fig_size=(15, 10)): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. results_path: The path to the directory where the images should be saved images_path: The path to the directory of the images score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ img_result = show_result(join(images_path, img), result, class_names, score_thr=score_thr, show=False) plt.figure(figsize=fig_size) plt.imsave(join(results_path, img), mmcv.bgr2rgb(img_result))
def get_display_img(args, item, pipelines): """get image to display.""" # srcs picture could be in RGB or BGR order due to different backends. if args.bgr2rgb: item['img'] = mmcv.bgr2rgb(item['img']) src_image = item['img'].copy() pipeline_images = [src_image] # get intermediate images through pipelines if args.mode in ['transformed', 'concat', 'pipeline']: for pipeline in pipelines.values(): item = pipeline(item) trans_image = copy.deepcopy(item['img']) trans_image = np.ascontiguousarray(trans_image, dtype=np.uint8) pipeline_images.append(trans_image) # concatenate images to be showed according to mode if args.mode == 'original': image = prepare_imgs(args, [src_image], ['src']) elif args.mode == 'transformed': image = prepare_imgs(args, [pipeline_images[-1]], ['transformed']) elif args.mode == 'concat': steps = ['src', 'transformed'] image = prepare_imgs(args, [pipeline_images[0], pipeline_images[-1]], steps) elif args.mode == 'pipeline': steps = ['src'] + list(pipelines.keys()) image = prepare_imgs(args, pipeline_images, steps) return image
def show_result_pyplot(model, img, result, palette=None, fig_size=(15, 10), opacity=0.5): """Visualize the segmentation results on the image. Args: model (nn.Module): The loaded segmentor. img (str or np.ndarray): Image filename or loaded image. result (list): The segmentation result. palette (list[list[int]]] | None): The palette of segmentation map. If None is given, random palette will be generated. Default: None fig_size (tuple): Figure size of the pyplot figure. opacity(float): Opacity of painted segmentation map. Default 0.5. Must be in (0, 1] range. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, palette=palette, show=False, opacity=opacity) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.show()
def get_image(self, image, transform_I): # transform img = mmcv.bgr2rgb(image) crop_size = Image.fromarray(img).size img = transform_I(Image.fromarray(img)) return img, crop_size
def imshow_bboxes_w_ids(img, bboxes, ids, thickness=1, font_scale=0.5, out_file=None): assert bboxes.ndim == 2 assert ids.ndim == 1 assert bboxes.shape[0] == ids.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 if isinstance(img, str): img = plt.imread(img) else: img = mmcv.bgr2rgb(img) plt.imshow(img) plt.gca().set_axis_off() plt.autoscale(False) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=None, wspace=None) plt.margins(0, 0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) for bbox, id in zip(bboxes, ids): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) w = bbox_int[2] - bbox_int[0] + 1 h = bbox_int[3] - bbox_int[1] + 1 color = random_color(id) plt.gca().add_patch( Rectangle(left_top, w, h, thickness, edgecolor=color, facecolor='none')) label_text = '{}'.format(int(id)) bg_height = 15 bg_width = 12 bg_width = len(label_text) * bg_width plt.gca().add_patch( Rectangle((left_top[0], left_top[1] - bg_height), bg_width, bg_height, thickness, edgecolor=color, facecolor=color)) plt.text(left_top[0] - 1, left_top[1], label_text, fontsize=5) if out_file is not None: plt.savefig(out_file, dpi=300, bbox_inches='tight', pad_inches=0.0) plt.show() plt.clf() return img
def _totensor(img, bgr2rgb, float32): if img.shape[2] == 3 and bgr2rgb: img = mmcv.bgr2rgb(img) #img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #img = torch.from_numpy(img.transpose(2, 0, 1)) img = trans(img) if float32: img = img.float() return img
def get_image(self, image, transform_I, size, crop_coords): # crop img = mmcv.bgr2rgb(image) if self.opt.isTrain and not self.use_for_finetune: img = self.crop(Image.fromarray(img), crop_coords) else: img = Image.fromarray(img) crop_size = img.size # transform img = transform_I(img) return img, crop_size
def get_image(self, image, transform_I, crop_coords): # transform img = mmcv.bgr2rgb(image) img = self.crop(Image.fromarray(img), crop_coords) # debug crop_size = img.size # crop_size = Image.fromarray(img).size # debug # img = transform_I(Image.fromarray(img)) img = transform_I(img) return img, crop_size
def show_ann(coco, img, ann_info): """Show the img. :param coco: the class of coco :type coco: object :param img: image :type img: ndarray :param ann_info: the annotation information :type ann_info: tuple """ plt.imshow(mmcv.bgr2rgb(img)) plt.axis('off') coco.showAnns(ann_info) plt.show()
def show_result_pyplot(model, img, result, fig_size=(15, 10)): """Visualize the classification results on the image. Args: model (nn.Module): The loaded classifier. img (str or np.ndarray): Image filename or loaded image. result (list): The classification result. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, show=False) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.show()
def show_result_pyplot(model, img_file, result, score_thr=0.3, fig_size=(16, 9)): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img_file, result, score_thr=score_thr, show=False) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) plt.savefig(img_file.split('.')[0] + '_det_results.jpg', dpi=120)
def get_result_image(img): model = load_model() result = inference_detector(model, img) if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, score_thr=0.3, show=False) plt.figure(figsize=(10, 10)) plt.axis('off') plt.imshow(mmcv.bgr2rgb(img)) plt.tight_layout() now = datetime.now() dt = now.strftime("%m%d%H%M%S") result_img_path = './static/outputs/output_' + dt + '.png' plt.savefig(result_img_path) result_img_path = result_img_path[9:] return result_img_path
def nondist_validation(self, dataloader, current_iter, tb_logger, save_img): assert dataloader is None, 'Validation dataloader should be None.' self.test() result = tensor2img(self.output, min_max=(-1, 1)) if self.opt['is_train']: save_img_path = osp.join(self.opt['path']['visualization'], 'train', f'train_{current_iter}.png') else: save_img_path = osp.join(self.opt['path']['visualization'], 'test', f'test_{self.opt["name"]}.png') mmcv.imwrite(result, save_img_path) # add sample images to tb_logger result = (result / 255.).astype(np.float32) result = mmcv.bgr2rgb(result) if tb_logger is not None: tb_logger.add_image( 'samples', result, global_step=current_iter, dataformats='HWC')
def show_result_pyplot(model, img, result, score_thr=0.3, fig_size=(15, 10)): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, score_thr=score_thr, show=False) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) # plt.savefig(save_pth + "/{}.png".format(im_str.split('.')[0]), bbox_inches='tight') plt.show()
def get_display_img(item, pipeline, mode, bgr2rgb): """get image to display.""" if bgr2rgb: item['img'] = mmcv.bgr2rgb(item['img']) src_image = item['img'].copy() # get transformed picture if mode in ['pipeline', 'concat']: item = pipeline(item) trans_image = item['img'] trans_image = np.ascontiguousarray(trans_image, dtype=np.uint8) if mode == 'concat': image = concat(src_image, trans_image) elif mode == 'original': image = src_image elif mode == 'pipeline': image = trans_image return image
def vis3d(): v_path = os.path.join(root, 'unzip', 'test_video/id03862/jc6k4sbenMY/00366' + '.mp4' ) lmark_path = os.path.join(root, 'unzip', 'test_video/id03862/jc6k4sbenMY/00366_prnet' + '.npy' ) # lmark_path = os.path.join(root, 'unzip', 'test_video/id04276/k0zLls_oen0/00341_prnet' + '.npy' ) cap = cv2.VideoCapture(v_path) lmark = np.load(lmark_path) print (lmark.shape) print (lmark[0]) count = 0 tmp = v_path.split('/') real_video = mmcv.VideoReader(v_path) for count in range(0,100): input = real_video[count] input = mmcv.bgr2rgb(input) preds = lmark#[count] #TODO: Make this nice fig = plt.figure(figsize=plt.figaspect(.5)) ax = fig.add_subplot(1, 2, 1) ax.imshow(input) ax.plot(preds[0:17,0],preds[0:17,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[17:22,0],preds[17:22,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[22:27,0],preds[22:27,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[27:31,0],preds[27:31,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[31:36,0],preds[31:36,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[36:42,0],preds[36:42,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[42:48,0],preds[42:48,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[48:60,0],preds[48:60,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.plot(preds[60:68,0],preds[60:68,1],marker='o',markersize=1,linestyle='-',color='w',lw=1) ax.axis('off') ax = fig.add_subplot(1, 2, 2, projection='3d') surf = ax.scatter(preds[:,0]*1.2,preds[:,1],preds[:,2],c="cyan", alpha=1.0, edgecolor='b') ax.plot3D(preds[:17,0]*1.2,preds[:17,1], preds[:17,2], color='blue' ) ax.plot3D(preds[17:22,0]*1.2,preds[17:22,1],preds[17:22,2], color='blue') ax.plot3D(preds[22:27,0]*1.2,preds[22:27,1],preds[22:27,2], color='blue') ax.plot3D(preds[27:31,0]*1.2,preds[27:31,1],preds[27:31,2], color='blue') ax.plot3D(preds[31:36,0]*1.2,preds[31:36,1],preds[31:36,2], color='blue') ax.plot3D(preds[36:42,0]*1.2,preds[36:42,1],preds[36:42,2], color='blue') ax.plot3D(preds[42:48,0]*1.2,preds[42:48,1],preds[42:48,2], color='blue') ax.plot3D(preds[48:,0]*1.2,preds[48:,1],preds[48:,2], color='blue' ) ax.view_init(elev=90., azim=90.) ax.set_xlim(ax.get_xlim()[::-1]) plt.show()
def show_result_pyplot(model, img, result, palette=None, fig_size=(15, 10)): """Visualize the segmentation results on the image. Args: model (nn.Module): The loaded segmentor. img (str or np.ndarray): Image filename or loaded image. result (list): The segmentation result. palette (list[list[int]]] | None): The palette of segmentation map. If None is given, random palette will be generated. Default: None fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, palette=palette, show=False) plt.figure(figsize=fig_size) plt.imshow(mmcv.bgr2rgb(img)) #plt.imsave('my_demo.png', mmcv.bgr2rgb(img)) plt.savefig('my_demo.png')
def show_result_pyplot(model, img, result, score_thr=0.3, fig_size=(15, 10), name=1): """Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. fig_size (tuple): Figure size of the pyplot figure. """ if hasattr(model, 'module'): model = model.module img = model.show_result(img, result, score_thr=score_thr, show=False) # plt.figure(figsize=fig_size) # plt.imshow(mmcv.bgr2rgb(img)) # import pdb; pdb.set_trace() plt.imsave('/home/pengyi/temp{}.jpg'.format(name), mmcv.bgr2rgb(img)) plt.show()