def show_pred_gt(preds, gts, show=False, win_name='', wait_time=0, out_file=None): """Show detection and ground truth for one image. Args: preds (list[list[float]]): The detection boundary list. gts (list[list[float]]): The ground truth boundary list. show (bool): Whether to show the image. win_name (str): The window name. wait_time (int): The value of waitKey param. out_file (str): The filename of the output. """ assert utils.is_2dlist(preds) assert utils.is_2dlist(gts) assert isinstance(show, bool) assert isinstance(win_name, str) assert isinstance(wait_time, int) assert utils.is_none_or_type(out_file, str) p_xy = [p for boundary in preds for p in boundary] gt_xy = [g for gt in gts for g in gt] max_xy = np.max(np.array(p_xy + gt_xy).reshape(-1, 2), axis=0) width = int(max_xy[0]) + 100 height = int(max_xy[1]) + 100 img = np.ones((height, width, 3), np.int8) * 255 pred_color = mmcv.color_val('red') gt_color = mmcv.color_val('blue') thickness = 1 for boundary in preds: cv2.polylines( img, [np.array(boundary).astype(np.int32).reshape(-1, 1, 2)], True, color=pred_color, thickness=thickness) for gt in gts: cv2.polylines( img, [np.array(gt).astype(np.int32).reshape(-1, 1, 2)], True, color=gt_color, thickness=thickness) if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def single_pp(self, result, model): for arr, output, export, res in zip(self.args.arrays, self.args.output, self.args.export, result): if export: mmcv.dump(res, export, indent=4) if output or self.args.imshow: res_img = model.show_result(arr, res, out_file=output) if self.args.imshow: mmcv.imshow(res_img, 'inference results') if self.args.print_result: print(res, end='\n\n') return result
def show_cam_grad(grayscale_cam, src_img, title, out_path=None): """fuse src_img and grayscale_cam and show or save.""" grayscale_cam = grayscale_cam[0, :] src_img = np.float32(src_img) / 255 visualization_img = show_cam_on_image(src_img, grayscale_cam, use_rgb=False) if out_path: mmcv.imwrite(visualization_img, str(out_path)) else: mmcv.imshow(visualization_img, win_name=title)
def main(): args = parse_args() model = init_model(args.config, args.checkpoint, device=torch.device('cuda', args.device)) output = generation_inference(model, args.img_path, args.unpaired_path) mmcv.imwrite(output, args.save_path) if args.imshow: mmcv.imshow(output, 'predicted generation result')
def main(): args = parse_args() model = init_model( args.config, args.checkpoint, device=torch.device('cuda', args.device)) result = inpainting_inference(model, args.masked_img_path, args.mask_path) result = tensor2img(result, min_max=(-1, 1))[..., ::-1] mmcv.imwrite(result, args.save_path) if args.imshow: mmcv.imshow(result, 'predicted inpainting result')
def imshow_text_label(img, pred_label, gt_label, show=False, win_name='', wait_time=-1, out_file=None): """Draw predicted texts and ground truth texts on images. Args: img (str or np.ndarray): Image filename or loaded image. pred_label (str): Predicted texts. gt_label (str): Ground truth texts. show (bool): Whether to show the image. win_name (str): The window name. wait_time (int): Value of waitKey param. out_file (str): The filename of the output. """ assert isinstance(img, (np.ndarray, str)) assert isinstance(pred_label, str) assert isinstance(gt_label, str) assert isinstance(show, bool) assert isinstance(win_name, str) assert isinstance(wait_time, int) img = mmcv.imread(img) src_h, src_w = img.shape[:2] resize_height = 64 resize_width = int(1.0 * src_w / src_h * resize_height) img = cv2.resize(img, (resize_width, resize_height)) h, w = img.shape[:2] pred_img = np.ones((h, w, 3), dtype=np.uint8) * 255 gt_img = np.ones((h, w, 3), dtype=np.uint8) * 255 cv2.putText(pred_img, pred_label, (5, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2) images = [pred_img, img] if gt_label != '': cv2.putText(gt_img, gt_label, (5, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2) images.append(gt_img) img = tile_image(images) if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def main(): args = parse_args() model = init_model(args.config, args.checkpoint, device=torch.device('cuda', args.device)) pred_alpha = matting_inference(model, args.img_path, args.trimap_path) * 255 mmcv.imwrite(pred_alpha, args.save_path) if args.imshow: mmcv.imshow(pred_alpha, 'predicted alpha matte')
def main(): args = parse_args() model = init_model(args.config, args.checkpoint, device=torch.device('cuda', args.device)) output = restoration_inference(model, args.img_path) output = tensor2img(output) # print(np.shape(output)) mmcv.imwrite(output, args.save_path) if args.imshow: mmcv.imshow(output, 'predicted restoration result')
def imshow_edge_node(img, result, boxes, idx_to_cls={}, show=False, win_name='', wait_time=-1, out_file=None, ignore_classes=[]): img = mmcv.imread(img) h, w = img.shape[:2] pred_img = np.ones((h, w * 2, 3), dtype=np.uint8) * 255 max_value, max_idx = torch.max(result['nodes'].detach().cpu(), -1) node_pred_label = max_idx.numpy().tolist() node_pred_score = max_value.numpy().tolist() vis_img = np.ones((h, int(w * 1.5), 3), dtype=np.uint8) * 255 vis_img[:, :w] = img for i, box in enumerate(boxes): new_box = [[box[0], box[1]], [box[2], box[1]], [box[2], box[3]], [box[0], box[3]]] Pts = np.array([new_box], np.int32) x_max = int(max([point[0] for point in new_box])) y_max = int(max([point[1] for point in new_box])) pred_label = str(node_pred_label[i]) if pred_label in idx_to_cls: pred_label = idx_to_cls[pred_label] pred_score = '{:.2f}'.format(node_pred_score[i]) text = pred_label + '(' + pred_score + ')' if pred_label not in ignore_classes: cv2.putText(vis_img, text, (x_max, y_max), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1) cv2.polylines(vis_img, [Pts.reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=1) # vis_img[:, w:] = pred_img if show: mmcv.imshow(vis_img, win_name, wait_time) if out_file is not None: mmcv.imwrite(vis_img, out_file) return vis_img, node_pred_label
def test_imshow(self, debug=True): """ Usage: export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 export TIME_STR=1 export PYTHONPATH=./exp:./stylegan2-pytorch:./ python -c "from exp.tests.test_styleganv2 import Testing_stylegan2;\ Testing_stylegan2().test_train_ffhq_128()" :return: """ if 'CUDA_VISIBLE_DEVICES' not in os.environ: os.environ['CUDA_VISIBLE_DEVICES'] = '0' if 'TIME_STR' not in os.environ: os.environ['TIME_STR'] = '0' if utils.is_debugging() else '0' from template_lib.v2.config_cfgnode.argparser import \ (get_command_and_outdir, setup_outdir_and_yaml, get_append_cmd_str, start_cmd_run) tl_opts = ' '.join(sys.argv[sys.argv.index('--tl_opts') + 1:]) if '--tl_opts' in sys.argv else '' print(f'tl_opts:\n {tl_opts}') command, outdir = get_command_and_outdir( self, func_name=sys._getframe().f_code.co_name, file=__file__) argv_str = f""" --tl_config_file none --tl_command none --tl_outdir {outdir} --tl_opts {tl_opts} """ args, cfg = setup_outdir_and_yaml(argv_str, return_cfg=True) n_gpus = len(os.environ['CUDA_VISIBLE_DEVICES'].split(',')) import mmcv import numpy as np img_path = "template_lib/datasets/images/zebra_GT_target_origin.png" mmcv.imshow(img_path) # show image with bounding boxes img = np.random.rand(100, 100, 3) bboxes = np.array([[0, 0, 50, 50], [20, 20, 60, 60]]) mmcv.imshow_bboxes(img, bboxes) pass
def main(): args = parse_args() assert args.out or args.show, \ ('Please specify at least one operation (save/show the ' 'video) with the argument "--out" or "--show"') model = init_detector(args.config, args.checkpoint, device=args.device) if args.nvdecode: VideoCapture = ffmpegcv.VideoCaptureNV else: VideoCapture = ffmpegcv.VideoCapture video_origin = VideoCapture(args.video) img_metas = prefetch_img_metas(model.cfg, (video_origin.width, video_origin.height)) resize_wh = img_metas['pad_shape'][1::-1] video_resize = VideoCapture(args.video, resize=resize_wh, resize_keepratio=True, resize_keepratioalign='topleft', pix_fmt='rgb24') video_writer = None if args.out: video_writer = ffmpegcv.VideoWriter(args.out, fps=video_origin.fps) with torch.no_grad(): for frame_resize, frame_origin in zip( mmcv.track_iter_progress(video_resize), video_origin): data = process_img(frame_resize, img_metas, args.device) result = model(return_loss=False, rescale=True, **data)[0] frame_mask = model.show_result(frame_origin, result, score_thr=args.score_thr) if args.show: cv2.namedWindow('video', 0) mmcv.imshow(frame_mask, 'video', args.wait_time) if args.out: video_writer.write(frame_mask) if video_writer: video_writer.release() video_origin.release() video_resize.release() cv2.destroyAllWindows()
def show_weights(weights_list, flag): from mmdet.debug_utils import show_lines, show_points from configs.common import IMAGE_SIZE import numpy as np import mmcv print('\n\n\n') print(flag) for weights in weights_list: if weights.dim() > 1: weights = weights[:,0] s = np.sqrt(weights.shape[0]).astype(np.int32) try: img = weights.reshape(s,s).cpu().data.numpy().astype(np.float32) except: import pdb; pdb.set_trace() # XXX BREAKPOINT pass mmcv.imshow(img) pass
def main(): args = parse_args() if not os.path.isfile(args.img_path): raise ValueError('It seems that you did not input a valid ' '"image_path". Please double check your input, or ' 'you may want to use "restoration_video_demo.py" ' 'for video restoration.') model = init_model( args.config, args.checkpoint, device=torch.device('cuda', args.device)) output = restoration_face_inference(model, args.img_path, args.upscale_factor, args.face_size) mmcv.imwrite(output, args.save_path) if args.imshow: mmcv.imshow(output, 'predicted restoration result')
def single_pp(self, result, model): for arr, output, export, res in zip(self.args.arrays, self.args.output, self.args.export, result): if export: mmcv.dump(res, export, indent=4) if output or self.args.imshow: if model == 'Tesseract_det': res_img = TextDetectorMixin(show_score=False).show_result( arr, res, out_file=output) elif model == 'Tesseract_recog': res_img = BaseRecognizer.show_result( arr, res, out_file=output) else: res_img = model.show_result(arr, res, out_file=output) if self.args.imshow: mmcv.imshow(res_img, 'inference results') if self.args.print_result: print(res, end='\n\n') return result
def main(): args = parse_args() # rename_pth(args.checkpoint) # print('rename success') model = init_model(args.config, args.checkpoint, device=torch.device('cuda', args.device)) for i in model.state_dict(): print(i) pred_alpha = matting_inference(model, args.img_path, args.trimap_path) * 255 # print(pred_alpha) mmcv.imwrite(pred_alpha, args.save_path) if args.imshow: mmcv.imshow(pred_alpha, 'predicted alpha matte')
def main(): parser = ArgumentParser() parser.add_argument('img', help='Image file.') parser.add_argument('config', help='Config file.') parser.add_argument('checkpoint', help='Checkpoint file.') parser.add_argument('save_path', help='Path to save visualized image.') parser.add_argument('--device', default='cuda:0', help='Device used for inference.') parser.add_argument('--imshow', action='store_true', help='Whether show image with OpenCV.') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) if model.cfg.data.test['type'] == 'ConcatDataset': model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][ 0].pipeline # test a single image print(args.img) s = time.time() with open(args.img, 'r') as ff: lines = ff.readlines() for i, line in enumerate(lines): imgPath = line.strip() imgPath = f"qtests/{imgPath}" imgName = imgPath.split('/')[-1] print(imgPath) result = model_inference(model, imgPath) print(result) #show the results img = model.show_result(imgPath, result, out_file=None, show=False) print("time", time.time() - s) mmcv.imwrite(img, f"{args.save_path}/{imgName}.jpg") if args.imshow: mmcv.imshow(img, 'predicted results')
def main(): parser = ArgumentParser() parser.add_argument('config', help='Config file.') parser.add_argument('checkpoint', help='Checkpoint file.') parser.add_argument('save_path', help='Folder to save visualized images.') parser.add_argument('--images', nargs='+', help='Image files to be predicted with batch mode, ' 'separated by space, like "image_1.jpg image2.jpg".') parser.add_argument('--device', default='cuda:0', help='Device used for inference.') parser.add_argument('--imshow', action='store_true', help='Whether show image with OpenCV.') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) if model.cfg.data.test['type'] == 'ConcatDataset': model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][ 0].pipeline # test multiple images results = model_inference(model, args.images, batch_mode=True) print(f'results: {results}') save_path = Path(args.save_path) for img_path, result in zip(args.images, results): out_file = save_path / f'result_{Path(img_path).stem}.png' # show the results img = model.show_result(img_path, result, out_file=str(out_file), show=False) if args.imshow: mmcv.imshow(img, f'predicted results ({img_path})')
def _show_objs_ls_points_ls( img, objs_ls=None, obj_rep='XYXYSin2', points_ls=None, obj_colors='random', obj_scores_ls=None, point_colors='red', point_scores_ls=None, out_file=None, obj_thickness=1, point_thickness=1, draw_rooms=False, only_save=False, dash=False, ): ''' img: [h,w,3] or [h,w,1], or [h,w] or (h_size, w_size) ''' assert obj_rep in OBJ_REPS_PARSE._obj_reps img = _draw_objs_ls_points_ls(img, objs_ls, obj_rep, points_ls, obj_colors, point_colors=point_colors, out_file=out_file, obj_thickness=obj_thickness, point_thickness=point_thickness, obj_scores_ls=obj_scores_ls, point_scores_ls=point_scores_ls, draw_rooms=draw_rooms, dash=dash) if not only_save: mmcv.imshow(img) return img
def main(): bgr_img = mmcv.imread(image_path) h, w, _ = bgr_img.shape # convert color rgb_img = mmcv.bgr2rgb(bgr_img) # resize resize_img = mmcv.imresize(rgb_img, size=(256, 256)) # rotate rotate_img = mmcv.imrotate(rgb_img, angle=45) # flip flip_img = mmcv.imflip(rgb_img, direction='horizontal') # crop if h <= w: y_min, y_max = 0, h x_min = int((w - h) / 2) x_max = x_min + h else: x_min, x_max = 0, h y_min = int((h - w) / 2) y_max = y_min + w bbox = np.array([x_min, y_min, x_max, y_max]) crop_img = mmcv.imcrop(rgb_img, bbox) # padding max_size = max(h, w) pad_img = mmcv.impad(rgb_img, shape=(max_size, max_size), padding_mode='constant') mmcv.imshow(mmcv.rgb2bgr(pad_img))
def _show_lines_ls_points_ls(img, lines_ls=None, points_ls=None, line_colors='green', point_colors='red', out_file=None, line_thickness=1, point_thickness=1, only_save=False, box=False): ''' img: [h,w,3] or [h,w,1], or [h,w] or (h_size, w_size) ''' img = _draw_lines_ls_points_ls(img, lines_ls, points_ls, line_colors, point_colors, out_file, line_thickness, point_thickness, box=box) if not only_save: mmcv.imshow(img)
def main(): parser = ArgumentParser() parser.add_argument('img', help='Image file.') parser.add_argument('config', help='Config file.') parser.add_argument('checkpoint', help='Checkpoint file.') parser.add_argument('out_file', help='Path to save visualized image.') parser.add_argument('--device', default='cuda:0', help='Device used for inference.') parser.add_argument('--imshow', action='store_true', help='Whether show image with OpenCV.') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) if model.cfg.data.test['type'] == 'ConcatDataset': model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][ 0].pipeline # test a single image result = model_inference(model, args.img) print(f'result: {result}') # show the results img = model.show_result(args.img, result, out_file=args.out_file, show=False) if img is None: img = mmcv.imread(args.img) mmcv.imwrite(img, args.out_file) if args.imshow: mmcv.imshow(img, 'predicted results')
def show_multi_modality_result(img, gt_bboxes, pred_bboxes, proj_mat, out_dir, filename, box_mode='lidar', img_metas=None, show=True, gt_bbox_color=(61, 102, 255), pred_bbox_color=(241, 101, 72)): """Convert multi-modality detection results into 2D results. Project the predicted 3D bbox to 2D image plane and visualize them. Args: img (np.ndarray): The numpy array of image in cv2 fashion. gt_bboxes (:obj:`BaseInstance3DBoxes`): Ground truth boxes. pred_bboxes (:obj:`BaseInstance3DBoxes`): Predicted boxes. proj_mat (numpy.array, shape=[4, 4]): The projection matrix according to the camera intrinsic parameters. out_dir (str): Path of output directory. filename (str): Filename of the current frame. box_mode (str): Coordinate system the boxes are in. Should be one of 'depth', 'lidar' and 'camera'. Defaults to 'lidar'. img_metas (dict): Used in projecting depth bbox. show (bool): Visualize the results online. Defaults to False. gt_bbox_color (str or tuple(int)): Color of bbox lines. The tuple of color should be in BGR order. Default: (255, 102, 61) pred_bbox_color (str or tuple(int)): Color of bbox lines. The tuple of color should be in BGR order. Default: (72, 101, 241) """ if box_mode == 'depth': draw_bbox = draw_depth_bbox3d_on_img elif box_mode == 'lidar': draw_bbox = draw_lidar_bbox3d_on_img elif box_mode == 'camera': draw_bbox = draw_camera_bbox3d_on_img else: raise NotImplementedError(f'unsupported box mode {box_mode}') result_path = osp.join(out_dir, filename) mmcv.mkdir_or_exist(result_path) if show: show_img = img.copy() if gt_bboxes is not None: show_img = draw_bbox(gt_bboxes, show_img, proj_mat, img_metas, color=gt_bbox_color) if pred_bboxes is not None: show_img = draw_bbox(pred_bboxes, show_img, proj_mat, img_metas, color=pred_bbox_color) mmcv.imshow(show_img, win_name='project_bbox3d_img', wait_time=0) if img is not None: mmcv.imwrite(img, osp.join(result_path, f'{filename}_img.png')) if gt_bboxes is not None: gt_img = draw_bbox(gt_bboxes, img, proj_mat, img_metas, color=gt_bbox_color) mmcv.imwrite(gt_img, osp.join(result_path, f'{filename}_gt.png')) if pred_bboxes is not None: pred_img = draw_bbox(pred_bboxes, img, proj_mat, img_metas, color=pred_bbox_color) mmcv.imwrite(pred_img, osp.join(result_path, f'{filename}_pred.png'))
# -*- coding: utf-8 -* - ''' MMCV显示图片相关 参考:https://mmcv.readthedocs.io/en/latest/visualization.html ''' import mmcv import numpy as np # 显示图片文件 mmcv.imshow("cluo.jpg") # 显示numpy数组格式的图片 img = np.random.rand(100,100,3)*255 mmcv.imshow(img) # 显示图片并伴有bounding box img = np.random.rand(100,100,3)*255 bboxes = np.array([[0,0,50,50],[20,20,60,60]]) mmcv.imshow_bboxes(img,bboxes,colors=["red"],show=False,out_file="cluo_bbox.jpg") # 显示图片并伴有bounding box以及框的标题 labels = np.array([1,3]) classes = ["类别0","类别1","类别2","类别3"] mmcv.imshow_det_bboxes(img,bboxes,labels,classes,bbox_color="green",text_color="blue",out_file="cluo_bbox_label.jpg")
def imshow_semantic(img, seg, class_names, palette=None, win_name='', show=False, wait_time=0, out_file=None, opacity=0.5): """Draw `result` over `img`. Args: img (str or Tensor): The image to be displayed. seg (Tensor): The semantic segmentation results to draw over `img`. class_names (list[str]): Names of each classes. palette (list[list[int]]] | np.ndarray | None): The palette of segmentation map. If None is given, random palette will be generated. Default: None win_name (str): The window name. wait_time (int): Value of waitKey param. Default: 0. show (bool): Whether to show the image. Default: False. out_file (str or None): The filename to write the image. Default: None. opacity(float): Opacity of painted segmentation map. Default 0.5. Must be in (0, 1] range. Returns: img (Tensor): Only if not `show` or `out_file` """ img = mmcv.imread(img) img = img.copy() if palette is None: palette = np.random.randint(0, 255, size=(len(class_names), 3)) palette = np.array(palette) assert palette.shape[0] == len(class_names) 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) # if out_file specified, do not show image in window if out_file is not None: show = False if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) if not (show or out_file): warnings.warn('show==False and out_file is not specified, only ' 'result image will be returned') return img
def _cv2_show_wrong_tracks(img, bboxes, ids, error_types, thickness=2, font_scale=0.4, text_width=10, text_height=15, show=False, wait_time=100, out_file=None): """Show the wrong tracks with opencv. Args: img (str or ndarray): The image to be displayed. bboxes (ndarray): A ndarray of shape (k, 5). ids (ndarray): A ndarray of shape (k, ). error_types (ndarray): A ndarray of shape (k, ), where 0 denotes false positives, 1 denotes false negative and 2 denotes ID switch. thickness (int, optional): Thickness of lines. Defaults to 2. font_scale (float, optional): Font scale to draw id and score. Defaults to 0.4. text_width (int, optional): Width to draw id and score. Defaults to 10. text_height (int, optional): Height to draw id and score. Defaults to 15. show (bool, optional): Whether to show the image on the fly. Defaults to False. wait_time (int, optional): Value of waitKey param. Defaults to 100. out_file (str, optional): The filename to write the image. Defaults to None. Returns: ndarray: Visualized image. """ assert bboxes.ndim == 2, \ f' bboxes ndim should be 2, but its ndim is {bboxes.ndim}.' assert ids.ndim == 1, \ f' ids ndim should be 1, but its ndim is {ids.ndim}.' assert error_types.ndim == 1, \ f' error_types ndim should be 1, but its ndim is {error_types.ndim}.' assert bboxes.shape[0] == ids.shape[0], \ 'bboxes.shape[0] and ids.shape[0] should have the same length.' assert bboxes.shape[1] == 5, \ f' bboxes.shape[1] should be 5, but its {bboxes.shape[1]}.' bbox_colors = sns.color_palette() # red, yellow, blue bbox_colors = [bbox_colors[3], bbox_colors[1], bbox_colors[0]] bbox_colors = [[int(255 * _c) for _c in bbox_color][::-1] for bbox_color in bbox_colors] if isinstance(img, str): img = mmcv.imread(img) else: assert img.ndim == 3 img_shape = img.shape bboxes[:, 0::2] = np.clip(bboxes[:, 0::2], 0, img_shape[1]) bboxes[:, 1::2] = np.clip(bboxes[:, 1::2], 0, img_shape[0]) for bbox, error_type, id in zip(bboxes, error_types, ids): x1, y1, x2, y2 = bbox[:4].astype(np.int32) score = float(bbox[-1]) # bbox bbox_color = bbox_colors[error_type] cv2.rectangle(img, (x1, y1), (x2, y2), bbox_color, thickness=thickness) # FN does not have id and score if error_type == 1: continue # score text = '{:.02f}'.format(score) width = (len(text) - 1) * text_width img[y1:y1 + text_height, x1:x1 + width, :] = bbox_color cv2.putText(img, text, (x1, y1 + text_height - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, color=(0, 0, 0)) # id text = str(id) width = len(text) * text_width img[y1 + text_height:y1 + text_height * 2, x1:x1 + width, :] = bbox_color cv2.putText(img, str(id), (x1, y1 + text_height * 2 - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, color=(0, 0, 0)) if show: mmcv.imshow(img, wait_time=wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def _cv2_show_tracks(img, bboxes, labels, ids, masks=None, classes=None, score_thr=0.0, thickness=2, font_scale=0.4, show=False, wait_time=0, out_file=None): """Show the tracks with opencv.""" assert bboxes.ndim == 2 assert labels.ndim == 1 assert ids.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 5 if isinstance(img, str): img = mmcv.imread(img) img_shape = img.shape bboxes[:, 0::2] = np.clip(bboxes[:, 0::2], 0, img_shape[1]) bboxes[:, 1::2] = np.clip(bboxes[:, 1::2], 0, img_shape[0]) inds = np.where(bboxes[:, -1] > score_thr)[0] bboxes = bboxes[inds] labels = labels[inds] ids = ids[inds] if masks is not None: assert masks.ndim == 3 masks = masks[inds] assert masks.shape[0] == bboxes.shape[0] text_width, text_height = 9, 13 for i, (bbox, label, id) in enumerate(zip(bboxes, labels, ids)): x1, y1, x2, y2 = bbox[:4].astype(np.int32) score = float(bbox[-1]) # bbox bbox_color = random_color(id) bbox_color = [int(255 * _c) for _c in bbox_color][::-1] cv2.rectangle(img, (x1, y1), (x2, y2), bbox_color, thickness=thickness) # score text = '{:.02f}'.format(score) if classes is not None: text += f'|{classes[label]}' width = len(text) * text_width img[y1:y1 + text_height, x1:x1 + width, :] = bbox_color cv2.putText(img, text, (x1, y1 + text_height - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, color=(0, 0, 0)) # id text = str(id) width = len(text) * text_width img[y1 + text_height:y1 + 2 * text_height, x1:x1 + width, :] = bbox_color cv2.putText(img, str(id), (x1, y1 + 2 * text_height - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, color=(0, 0, 0)) # mask if masks is not None: mask = masks[i].astype(bool) mask_color = np.array(bbox_color, dtype=np.uint8).reshape(1, -1) img[mask] = img[mask] * 0.5 + mask_color * 0.5 if show: mmcv.imshow(img, wait_time=wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def imshow_text_char_boundary(img, text_quads, boundaries, char_quads, chars, show=False, thickness=1, font_scale=0.5, win_name='', wait_time=-1, out_file=None): """Draw text boxes and char boxes on img. Args: img (str or ndarray): The img to be displayed. text_quads (list[list[int|float]]): The text boxes. boundaries (list[list[int|float]]): The boundary list. char_quads (list[list[list[int|float]]]): A 2d list of char boxes. char_quads[i] is for the ith text, and char_quads[i][j] is the jth char of the ith text. chars (list[list[char]]). The string for each text box. thickness (int): Thickness of lines. font_scale (float): Font scales of texts. show (bool): Whether to show the image. win_name (str): The window name. wait_time (int): Value of waitKey param. out_file (str or None): The filename of the output. """ assert isinstance(img, (np.ndarray, str)) assert utils.is_2dlist(text_quads) assert utils.is_2dlist(boundaries) assert utils.is_3dlist(char_quads) assert utils.is_2dlist(chars) assert utils.equal_len(text_quads, char_quads, boundaries) img = mmcv.imread(img) char_color = [mmcv.color_val('blue'), mmcv.color_val('green')] text_color = mmcv.color_val('red') text_inx = 0 for text_box, boundary, char_box, txt in zip(text_quads, boundaries, char_quads, chars): text_box = np.array(text_box) boundary = np.array(boundary) text_box = text_box.reshape(-1, 2).astype(np.int32) cv2.polylines(img, [text_box.reshape(-1, 1, 2)], True, color=text_color, thickness=thickness) if boundary.shape[0] > 0: cv2.polylines(img, [boundary.reshape(-1, 1, 2)], True, color=text_color, thickness=thickness) for b in char_box: b = np.array(b) c = char_color[text_inx % 2] b = b.astype(np.int32) cv2.polylines(img, [b.reshape(-1, 1, 2)], True, color=c, thickness=thickness) label_text = ''.join(txt) cv2.putText(img, label_text, (text_box[0, 0], text_box[0, 1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) text_inx = text_inx + 1 if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def imshow_pred_boundary(img, boundaries_with_scores, labels, score_thr=0, boundary_color='blue', text_color='blue', thickness=1, font_scale=0.5, show=True, win_name='', wait_time=0, out_file=None, show_score=False): """Draw boundaries and class labels (with scores) on an image. Args: img (str or ndarray): The image to be displayed. boundaries_with_scores (list[list[float]]): Boundaries with scores. labels (list[int]): Labels of boundaries. score_thr (float): Minimum score of boundaries to be shown. boundary_color (str or tuple or :obj:`Color`): Color of boundaries. text_color (str or tuple or :obj:`Color`): Color of texts. thickness (int): Thickness of lines. font_scale (float): Font scales of texts. show (bool): Whether to show the image. win_name (str): The window name. wait_time (int): Value of waitKey param. out_file (str or None): The filename of the output. show_score (bool): Whether to show text instance score. """ assert isinstance(img, (str, np.ndarray)) assert utils.is_2dlist(boundaries_with_scores) assert utils.is_type_list(labels, int) assert utils.equal_len(boundaries_with_scores, labels) if len(boundaries_with_scores) == 0: warnings.warn('0 text found in ' + out_file) return utils.valid_boundary(boundaries_with_scores[0]) img = mmcv.imread(img) scores = np.array([b[-1] for b in boundaries_with_scores]) inds = scores > score_thr boundaries = [boundaries_with_scores[i][:-1] for i in np.where(inds)[0]] scores = [scores[i] for i in np.where(inds)[0]] labels = [labels[i] for i in np.where(inds)[0]] boundary_color = mmcv.color_val(boundary_color) text_color = mmcv.color_val(text_color) font_scale = 0.5 for boundary, score, label in zip(boundaries, scores, labels): boundary_int = np.array(boundary).astype(np.int32) cv2.polylines(img, [boundary_int.reshape(-1, 1, 2)], True, color=boundary_color, thickness=thickness) if show_score: label_text = f'{score:.02f}' cv2.putText(img, label_text, (boundary_int[0], boundary_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def show_result(img, result, palette=None, win_name='', show=False, wait_time=0, out_file=None): """Draw `result` over `img`. Args: img (str or Tensor): The image to be displayed. result (Tensor): The semantic segmentation results to draw over `img`. palette (list[list[int]]] | np.ndarray | None): The palette of segmentation map. If None is given, random palette will be generated. Default: None win_name (str): The window name. wait_time (int): Value of waitKey param. Default: 0. show (bool): Whether to show the image. Default: False. out_file (str or None): The filename to write the image. Default: None. Returns: img (Tensor): Only if not `show` or `out_file` """ img = mmcv.imread(img) img = img.copy() seg = result[0] # if palette is None: # if self.PALETTE is None: # palette = np.random.randint( # 0, 255, size=(len(self.CLASSES), 3)) # else: # palette = self.PALETTE palette = np.array(palette) #assert palette.shape[0] == len(self.CLASSES) assert palette.shape[1] == 3 assert len(palette.shape) == 2 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 * 0.5 + color_seg * 0.5 img = img.astype(np.uint8) # if out_file specified, do not show image in window if out_file is not None: show = False if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) if not (show or out_file): warnings.warn('show==False and out_file is not specified, only ' 'result image will be returned') return img
def show_result(self, img, result, palette=None, win_name='', show=False, wait_time=0, out_file=None, opacity=0.5): """Draw `result` over `img`. Args: img (str or Tensor): The image to be displayed. result (Tensor): The semantic segmentation results to draw over `img`. palette (list[list[int]]] | np.ndarray | None): The palette of segmentation map. If None is given, random palette will be generated. Default: None win_name (str): The window name. wait_time (int): Value of waitKey param. Default: 0. show (bool): Whether to show the image. Default: False. out_file (str or None): The filename to write the image. Default: None. opacity(float): Opacity of painted segmentation map. Default 0.5. Must be in (0, 1] range. Returns: img (Tensor): Only if not `show` or `out_file` """ img = mmcv.imread(img) img = img.copy() seg = result[0] if palette is None: if self.PALETTE is None: # Get random state before set seed, # and restore random state later. # It will prevent loss of randomness, as the palette # may be different in each iteration if not specified. # See: https://github.com/open-mmlab/mmdetection/issues/5844 state = np.random.get_state() np.random.seed(42) # random palette palette = np.random.randint(0, 255, size=(len(self.CLASSES), 3)) np.random.set_state(state) else: palette = self.PALETTE palette = np.array(palette) assert palette.shape[0] == len(self.CLASSES) 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) # if out_file specified, do not show image in window if out_file is not None: show = False if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) if not (show or out_file): warnings.warn('show==False and out_file is not specified, only ' 'result image will be returned') return img