def __init__(self, name: str, input_buffer: str, output_buffer: Union[str, List[str]], enable_key: Optional[Union[str, int]] = None, enable: bool = True, show_bbox: bool = True, show_keypoint: bool = True, must_have_keypoint: bool = False, kpt_thr: float = 0.3, radius: int = 4, thickness: int = 2, bbox_color: Optional[Union[str, Tuple, Dict]] = None): super().__init__(name=name, input_buffer=input_buffer, output_buffer=output_buffer, enable_key=enable_key, enable=enable) self.kpt_thr = kpt_thr self.radius = radius self.thickness = thickness self.show_bbox = show_bbox self.show_keypoint = show_keypoint self.must_have_keypoint = must_have_keypoint if bbox_color is None: self.bbox_color = self.default_bbox_color elif isinstance(bbox_color, dict): self.bbox_color = {k: color_val(v) for k, v in bbox_color.items()} else: self.bbox_color = color_val(bbox_color)
def draw_projected_box3d(image, qs, color=(255, 0, 255), middle_color=None, bottom_color=None, thickness=2): """ Draw 3d bounding box in image qs: (8,2), projected 3d points array of vertices for the 3d box in following order: 1 -------- 0 /| /| 2 -------- 3 . | | | | . 5 -------- 4 |/ |/ 6 -------- 7 """ # Ref: https://github.com/charlesq34/frustum-pointnets/blob/master/kitti/kitti_util.py qs = qs.astype(np.int32) color = mmcv.color_val(color) # top color colors = colormap(rgb=False, maximum=255) for k in range(0, 4): # Ref: http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html # use LINE_AA for opencv3 # CV_AA for opencv2? # bottom: blue i, j = k + 4, (k + 1) % 4 + 4 if bottom_color is None: _bottom_color = tuple(int(_c) for _c in colors[k % len(colors)]) else: _bottom_color = tuple( int(_c) for _c in mmcv.color_val(bottom_color)) cv2.line(image, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), _bottom_color, thickness, cv2.LINE_AA) # middle: colormap i, j = k, k + 4 if middle_color is None: _middle_color = tuple(int(_c) for _c in colors[k % len(colors)]) else: _middle_color = tuple( int(_c) for _c in mmcv.color_val(middle_color)) cv2.line(image, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), _middle_color, thickness, cv2.LINE_AA) # top: pink/red i, j = k, (k + 1) % 4 cv2.line(image, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness, cv2.LINE_AA) # # method 2 # draw pillars in blue color------------------- # for i, j in zip(range(4), range(4, 8)): # image = cv2.line(image, tuple(qs[i]), tuple(qs[j]), (255), thickness) # # draw bottom layer in red color # image = cv2.drawContours(image, [qs[4:]], -1, (0, 0, 255), thickness) # # draw top layer in red color # image = cv2.drawContours(image, [qs[:4]], -1, (0, 255, 0), thickness) # --------------------------- return image
def __init__(self, name: str, input_buffer: str, output_buffer: Union[str, List[str]], enable_key: Optional[Union[str, int]] = None, enable: bool = False, x_offset=20, y_offset=20, y_delta=15, text_color='black', background_color=(255, 183, 0), text_scale=0.4, ignore_items: Optional[List[str]] = None): super().__init__(name=name, enable_key=enable_key, enable=enable) self.x_offset = x_offset self.y_offset = y_offset self.y_delta = y_delta self.text_color = color_val(text_color) self.background_color = color_val(background_color) self.text_scale = text_scale if ignore_items is None: self.ignore_items = self._default_ignore_items else: self.ignore_items = ignore_items self.register_input_buffer(input_buffer, 'input', trigger=True) self.register_output_buffer(output_buffer)
def __init__(self, name: str, input_buffer: str, output_buffer: Union[str, List[str]], enable_key: Optional[Union[str, int]] = None, enable: bool = True, content_lines: Optional[List[str]] = None, x_offset: int = 20, y_offset: int = 20, y_delta: int = 15, text_color: Union[str, Tuple[int, int, int]] = 'black', background_color: Union[str, Tuple[int, int, int]] = (255, 183, 0), text_scale: float = 0.4): super().__init__(name=name, input_buffer=input_buffer, output_buffer=output_buffer, enable_key=enable_key, enable=enable) self.x_offset = x_offset self.y_offset = y_offset self.y_delta = y_delta self.text_color = color_val(text_color) self.background_color = color_val(background_color) self.text_scale = text_scale if content_lines: self.content_lines = content_lines else: self.content_lines = self.default_content_lines
def det_bboxes(img, bboxes, labels, score_thr=0, bbox_color='green', text_color='green', thickness=2, font_scale=0.5, show=True, win_name='', wait_time=0, out_file=None): assert bboxes.ndim == 2 assert labels.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] bbox_color = mmcv.color_val(bbox_color) text_color = mmcv.color_val(text_color) ret_str = '' for bbox, label in zip(bboxes, labels): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness) label_text = str(label) dataset_name = 'sign' class_names = get_classes(dataset_name) label_text = class_names[ label] if class_names is not None else 'cls {}'.format(label) w = bbox_int[2] - bbox_int[0] h = bbox_int[3] - bbox_int[1] ss = ' ' + str(left_top[0]) + ',' + str( left_top[1]) + ',' + str(w) + ',' + str(h) if len(bbox) > 4: cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, font_scale, text_color) ret_str += ss if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return ret_str
def imshow_tracklets(img, bboxes, labels=None, ids=None, thickness=2, font_scale=0.4, show=False, win_name='', color=None, out_file=None): assert bboxes.ndim == 2 assert labels.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 if isinstance(img, str): img = imread(img) i = 0 for bbox, label in zip(bboxes, labels): x1, y1, x2, y2, _ = bbox.astype(np.int32) if ids is not None: if color is None: bbox_color = random_color(ids[i]) bbox_color = [int(255 * _c) for _c in bbox_color][::-1] else: bbox_color = mmcv.color_val(color) img[y1:y1 + 12, x1:x1 + 20, :] = bbox_color cv2.putText(img, str(ids[i]), (x1, y1 + 10), cv2.FONT_HERSHEY_COMPLEX, font_scale, color=color_val('black')) else: if color is None: bbox_color = color_val('green') else: bbox_color = mmcv.color_val(color) cv2.rectangle(img, (x1, y1), (x2, y2), bbox_color, thickness=thickness) if bbox[-1] < 0: bbox[-1] = np.nan label_text = '{:.02f}'.format(bbox[-1]) img[y1 - 12:y1, x1:x1 + 30, :] = bbox_color cv2.putText(img, label_text, (x1, y1 - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, color=color_val('black')) i += 1 if show: imshow(img, win_name) if out_file is not None: imwrite(img, out_file) return img
def show_result_in_Chinese(img, result, class_names, score_thr=0.3, out_file=None, thickness=1, bbox_color='green', text_color='green'): assert isinstance(class_names, (tuple, list)) img = mmcv.imread(img) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) assert bboxes.ndim == 2 assert labels.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] bbox_color = color_val(bbox_color) text_color = color_val(text_color) test_bboxes = nms(bboxes, 0.5) new_bboxes = [bboxes[i] for i in test_bboxes[1]] new_labels = [labels[i] for i in test_bboxes[1]] for bbox, label in zip(new_bboxes, new_labels): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) cv2.rectangle( img, left_top, right_bottom, bbox_color, thickness=thickness) label_text = class_names[ label] if class_names is not None else 'cls {}'.format(label) if len(bbox) > 4: label_text += '|{:.02f}'.format(bbox[-1]) img = write_text_to_image(img, label_text, (bbox_int[0], bbox_int[1] - 2), text_color) if out_file is not None: imwrite(img, out_file)
def imshow_det_bboxes(self, img, bboxes, labels, class_names=None, score_thr=0, bbox_color='green', text_color='green', thickness=2, font_scale=2, show=True, win_name='', wait_time=0, out_file=None): assert bboxes.ndim == 2 assert labels.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 img = mmcv.imread(img) if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] bbox_color = mmcv.color_val(bbox_color) text_color = mmcv.color_val(text_color) for bbox, label in zip(bboxes, labels): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness) label_text = class_names[ label] if class_names is not None else 'cls {}'.format(label) if len(bbox) > 4: label_text += '|{:.02f}'.format(bbox[-1]) cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color, thickness) if show: plt.figure(figsize=(20, 20)) #plt.axis('off') plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.show() if out_file is not None: mmcv.imwrite(img, out_file)
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 get_img(self,imgs,fl_indexs): """流式处理图像 Example :imgs = video[0:3] :fl_indexs = [0,2] :imgs = Process().get_img(imgs,fl_indexs) Args: imgs ([List]): 图像数组, fl_indexs ([List]): 图像数组中每个图像对应视频中的第几帧 Returns: [List]: 经过跟踪算法填充后的图像数组 """ ffbbs,lfbbs = self._get_bboxs_from_imgs(imgs) (fhat_bbox,fhat_bbox_pro),(fperson_bbox,fperson_bbox_pro) = ffbbs (lhat_bbox,lhat_bbox_pro),(lperson_bbox,lperson_bbox_pro) = lfbbs flhat_bboxs = [fhat_bbox,lhat_bbox] flpsn_bboxs = [fperson_bbox,lperson_bbox] # 通过第一帧探测到的bboxs和最后一帧探测到的bboxs,结合跟踪算法推断出中间帧数的bboxs信息 psn_bboxs_ids = track(flpsn_bboxs,self.psn_tracker) hat_bboxs_ids = track(flhat_bboxs,self.hat_tracker) # 跟踪推断中间的帧的bboxs psn_bboxs_index,all_psn_bbox_ids = fill(psn_bboxs_ids,fl_indexs) hat_bboxs_index,all_hat_bbox_ids = fill(hat_bboxs_ids,fl_indexs) # 将探测到的目标数据传入打包为objects psn_objects = [ (all_psn_bbox_ids[bindex],all_psn_bbox_ids[bindex],psn_bboxs_index[bindex]) for bindex in range(len(psn_bboxs_index)-1)] hat_objects = [ (all_hat_bbox_ids[bindex],all_hat_bbox_ids[bindex],hat_bboxs_index[bindex]) for bindex in range(len(hat_bboxs_index)-1)] # 绘制目标探测图像 origin_frames = draw_label1( imgs, all_psn_bbox_ids, lperson_bbox_pro, "no wear helmet", color_val(self.person_color), color_val(self.person_color)) origin_frames = draw_label1( imgs, all_hat_bbox_ids, lhat_bbox_pro, 'wear helmet', color_val(self.hat_color), color_val(self.hat_color)) return origin_frames,psn_objects,hat_objects
def get_palette(palette, num_classes=None): """Get palette from various inputs. Args: palette (list[tuple]/str/tuple/:obj:`Color`): palette inputs Returns: list[tuple[int]]: A list of color tuples. """ if isinstance(palette, list): return palette elif isinstance(palette, tuple): assert isinstance(num_classes, int) return [palette] * num_classes elif palette == 'coco': return mmdet.datasets.CocoDataset.PALETTE elif palette == 'voc': return mmdet.datasets.VOCDataset.PALETTE elif palette == 'citys': return mmdet.datasets.CityscapesDataset.PALETTE elif palette == 'random' or palette is None: assert isinstance(num_classes, int) state = np.random.get_state() # random color np.random.seed(42) palette = np.random.randint(0, 256, size=(num_classes, 3)) np.random.set_state(state) return [tuple(c) for c in palette] elif mmcv.is_str(palette): assert isinstance(num_classes, int) return [mmcv.color_val(palette)[::-1]] * num_classes else: raise TypeError(f'Invalid type for palette: {type(palette)}')
def show_result(self, img, result, text_color='green', font_scale=0.5, row_width=20, show=False, win_name='', wait_time=0, out_file=None): """Draw `result` over `img`. Args: img (str or Tensor): The image to be displayed. result (Tensor): The classification results to draw over `img`. text_color (str or tuple or :obj:`Color`): Color of texts. font_scale (float): Font scales of texts. row_width (int): width between each row of results on the image. show (bool): Whether to show the image. Default: False. win_name (str): The window name. wait_time (int): Value of waitKey param. Default: 0. 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() # write results on left-top of the image x, y = 0, row_width text_color = color_val(text_color) for k, v in result.items(): if isinstance(v, float): v = f'{v:.2f}' label_text = f'{k}: {v}' cv2.putText(img, label_text, (x, y), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) y += row_width # 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 vis_image_mask_cv2(img, mask, color=None): # import pycocotools.mask as cocomask if color is None: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) else: color_mask = np.array(color_val(color), dtype=np.uint8) # print(color_mask, type(color_mask)) mask = mask.astype(np.bool) img_show = img.copy() img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 return img_show
def color_val_matplotlib(color): """Convert various input in BGR order to normalized RGB matplotlib color tuples, Args: color (:obj:`Color`/str/tuple/int/ndarray): Color inputs Returns: tuple[float]: A tuple of 3 normalized floats indicating RGB channels. """ color = mmcv.color_val(color) color = [color / 255 for color in color[::-1]] return tuple(color)
def vis_image_bboxes_cv2( img, bboxes, labels=None, font_scale=0.5, text_color="green", font_thickness=2, box_thickness=2, box_color=_GREEN, draw_center=False, ): """ bboxes: xyxy """ if isinstance(bboxes, list): bboxes = np.array(bboxes) text_color = color_val(text_color) box_color = tuple(int(_c) for _c in color_val(box_color)) img_show = img.copy() for i, bbox in enumerate(bboxes): bbox = bbox.astype(np.int32) x1, y1, x2, y2 = bbox[:4] cv2.rectangle(img_show, (x1, y1), (x2, y2), box_color, thickness=box_thickness) if draw_center: center = (int((x1 + x2) / 2), int((y1 + y2) / 2)) img_show = cv2.circle(img_show, center, radius=box_thickness, color=box_color, thickness=-1) if labels is not None: label_text = labels[i] cv2.putText(img_show, label_text, (x1, y1 - 2), cv2.FONT_HERSHEY_SIMPLEX, font_scale, text_color, font_thickness) return img_show
def vis_bbox_opencv(img, bbox, thick=1, fmt='xywh', bbox_color='green'): """Visualizes a bounding box.""" bbox = np.array(bbox + 0.5).astype(np.int) if fmt == 'xywh': (x1, y1, w, h) = bbox x1, y1 = int(x1), int(y1) x2, y2 = int(x1 + w - 1), int(y1 + h - 1) else: x1, y1, x2, y2 = bbox _img = img.copy() bbox_color = color_val(bbox_color) cv2.rectangle(_img, (x1, y1), (x2, y2), bbox_color, thickness=thick) return _img
def imshow_bboxes(img, bboxes, colors="green", top_k=-1, thickness=1, show=True, win_name="", wait_time=0, out_file=None): """Draw bboxes on an image. Args: img (str or ndarray): The image to be displayed. bboxes (list or ndarray): A list of ndarray of shape (k, 4). colors (list[str or tuple or Color]): A list of colors. top_k (int): Plot the first k bboxes only if set positive. thickness (int): Thickness of lines. show (bool): Whether to show the image. win_name (str): The window name. wait_time (int): Value of waitKey param. out_file (str, optional): The filename to write the image. """ img = imread(img) if isinstance(bboxes, np.ndarray): bboxes = [bboxes] if not isinstance(colors, list): colors = [colors for _ in range(len(bboxes))] colors = [color_val(c) for c in colors] assert len(bboxes) == len(colors) for i, _bboxes in enumerate(bboxes): _bboxes = _bboxes.astype(np.int32) if top_k <= 0: _top_k = _bboxes.shape[0] else: _top_k = min(top_k, _bboxes.shape[0]) for j in range(_top_k): left_top = (_bboxes[j, 0], _bboxes[j, 1]) right_bottom = (_bboxes[j, 2], _bboxes[j, 3]) cv2.rectangle(img, left_top, right_bottom, colors[i], thickness=thickness) if show: imshow(img, win_name, wait_time) if out_file is not None: imwrite(img, out_file)
def get_palette(palette, num_classes): """Get palette from various inputs. Args: palette (list[tuple] | str | tuple | :obj:`Color`): palette inputs. num_classes (int): the number of classes. Returns: list[tuple[int]]: A list of color tuples. """ assert isinstance(num_classes, int) if isinstance(palette, list): dataset_palette = palette elif isinstance(palette, tuple): dataset_palette = [palette] * num_classes elif palette == 'random' or palette is None: state = np.random.get_state() # random color np.random.seed(42) palette = np.random.randint(0, 256, size=(num_classes, 3)) np.random.set_state(state) dataset_palette = [tuple(c) for c in palette] elif palette == 'coco': from mmdet.datasets import CocoDataset, CocoPanopticDataset dataset_palette = CocoDataset.PALETTE if len(dataset_palette) < num_classes: dataset_palette = CocoPanopticDataset.PALETTE elif palette == 'citys': from mmdet.datasets import CityscapesDataset dataset_palette = CityscapesDataset.PALETTE elif palette == 'voc': from mmdet.datasets import VOCDataset dataset_palette = VOCDataset.PALETTE elif palette == 'wod': from mmdet.datasets import WaymoOpenDataset dataset_palette = WaymoOpenDataset.PALETTE elif mmcv.is_str(palette): dataset_palette = [mmcv.color_val(palette)[::-1]] * num_classes else: raise TypeError(f'Invalid type for palette: {type(palette)}') assert len(dataset_palette) >= num_classes, \ 'The length of palette should not be less than `num_classes`.' return dataset_palette
def vis_image_bboxes_cv2(img, bboxes, labels=None, font_scale=0.5, text_color="green", font_thickness=2, box_thickness=2): ''' bboxes: xyxy ''' if isinstance(bboxes, list): bboxes = np.array(bboxes) text_color = color_val(text_color) img_show = img.copy() for i, bbox in enumerate(bboxes): bbox = bbox.astype(np.int32) x1, y1, x2, y2 = bbox[:4] cv2.rectangle(img_show, (x1, y1), (x2, y2), _GREEN, thickness=box_thickness) if labels is not None: label_text = labels[i] cv2.putText(img_show, label_text, (x1, y1-2), cv2.FONT_HERSHEY_SIMPLEX, font_scale, text_color, font_thickness) return img_show
def vis_image_mask_plt(im, mask, dpi=200, color=None, outfile=None, show=True): if color is None: color_list = colormap(rgb=True) / 255 mask_color_id = 0 color_mask = color_list[mask_color_id % len(color_list), 0:3] else: color_mask = color_val(color) # cmap = plt.get_cmap('rainbow') fig = plt.figure(frameon=False) fig.set_size_inches(im.shape[1] / dpi, im.shape[0] / dpi) ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0]) ax.axis("off") fig.add_axes(ax) ax.imshow(im[:, :, [2, 1, 0]]) # show mask img = np.ones(im.shape) w_ratio = 0.4 for c in range(3): color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio for c in range(3): img[:, :, c] = color_mask[c] e = mask _, contour, hier = cv2.findContours(e.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) for c in contour: polygon = Polygon(c.reshape((-1, 2)), fill=True, facecolor=color_mask, edgecolor="w", linewidth=1.2, alpha=0.5) ax.add_patch(polygon) if outfile is not None: mkdir_p(os.path.dirname(outfile)) fig.savefig(outfile, dpi=dpi) plt.close("all") if show: plt.show()
def vis_image_mask_bbox_cv2(img, masks, bboxes=None, labels=None, font_scale=0.5, text_color="green", font_thickness=2, box_thickness=1): """ bboxes: xyxy """ # import pycocotools.mask as cocomask text_color = color_val(text_color) img_show = img.copy() for i, mask in enumerate(masks): color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = mask.astype(np.bool) img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 if bboxes is None: x1, y1, x2, y2 = mask2bbox_xyxy(mask) else: bbox = bboxes[i].astype(np.int32) x1, y1, x2, y2 = bbox[:4] cv2.rectangle(img_show, (x1, y1), (x2, y2), _GREEN, thickness=box_thickness) if labels is not None: label_text = labels[i] cv2.putText( img_show, label_text, (x1, max(y1 - 2, 5)), cv2.FONT_HERSHEY_SIMPLEX, font_scale, text_color, font_thickness, ) return img_show
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_in_Chinese(img, resultrg, resultab, rg_class_names, ab_class_names, score_thr=0.3, out_file=None, thickness=1, bbox_color='green', text_color='green'): assert isinstance(rg_class_names, (tuple, list)) assert isinstance(ab_class_names, (tuple, list)) img = mmcv.imread(img) if isinstance(resultrg, tuple): rg_bbox_result, segm_result = resultrg else: rg_bbox_result, segm_result = resultrg, None if isinstance(resultab, tuple): ab_bbox_result, segm_result = resultab else: ab_bbox_result, segm_result = resultab, None print(rg_bbox_result) print(ab_bbox_result) # bbox_result = np.vstack(ab_bbox_result) bboxes_rg = np.vstack(rg_bbox_result) bboxes_ab = np.vstack(ab_bbox_result) # draw segmentation masks # if segm_result is not None: # segms = mmcv.concat_list(segm_result) # inds = np.where(bboxes[:, -1] > score_thr)[0] # for i in inds: # color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) # mask = maskUtils.decode(segms[i]).astype(np.bool) # img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels_rg = [ np.full(bbox.shape[0], rg_class_names[i]) for i, bbox in enumerate(rg_bbox_result) ] labels_rg = np.concatenate(labels_rg) labels_ab = [ np.full(bbox.shape[0], ab_class_names[i]) for i, bbox in enumerate(ab_bbox_result) ] labels_ab = np.concatenate(labels_ab) assert bboxes_rg.ndim == 2 assert labels_rg.ndim == 1 assert bboxes_rg.shape[0] == labels_rg.shape[0] assert bboxes_rg.shape[1] == 4 or bboxes_rg.shape[1] == 5 assert bboxes_ab.ndim == 2 assert labels_ab.ndim == 1 assert bboxes_ab.shape[0] == labels_ab.shape[0] assert bboxes_ab.shape[1] == 4 or bboxes_ab.shape[1] == 5 bboxes = np.vstack((rg_bbox_result + ab_bbox_result)) labels = np.hstack((labels_rg, labels_ab)) if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] bbox_color = color_val(bbox_color) text_color = color_val(text_color) test_bboxes = py_cpu_nms(bboxes, labels, 0.5) new_bboxes = [bboxes[i] for i in test_bboxes] new_labels = [labels[i] for i in test_bboxes] # test_bboxes = nms(bboxes, 0.5) # new_bboxes = [bboxes[i] for i in test_bboxes[1]] # new_labels = [labels[i] for i in test_bboxes[1]] for bbox, label in zip(new_bboxes, new_labels): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness) label_text = label if len(bbox) > 4: label_text += '|{:.02f}'.format(bbox[-1]) img = write_text_to_image(img, label_text, (bbox_int[0], bbox_int[1] - 2), text_color) if out_file is not None: imwrite(img, out_file)
def imshow_det_bboxes(img, bboxes, labels, segms=None, class_names=None, score_thr=0, bbox_color='green', text_color='green', mask_color=None, thickness=2, font_size=13, win_name='', show=True, wait_time=0, out_file=None, visual_label_threshold=-1, visual_label_offset=0): """Draw bboxes and class labels (with scores) on an image. Args: img (str or ndarray): The image to be displayed. bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or (n, 5). labels (ndarray): Labels of bboxes. segms (ndarray or None): Masks, shaped (n,h,w) or None class_names (list[str]): Names of each classes. score_thr (float): Minimum score of bboxes to be shown. Default: 0 bbox_color (str or tuple(int) or :obj:`Color`):Color of bbox lines. The tuple of color should be in BGR order. Default: 'green' text_color (str or tuple(int) or :obj:`Color`):Color of texts. The tuple of color should be in BGR order. Default: 'green' mask_color (str or tuple(int) or :obj:`Color`, optional): Color of masks. The tuple of color should be in BGR order. Default: None thickness (int): Thickness of lines. Default: 2 font_size (int): Font size of texts. Default: 13 show (bool): Whether to show the image. Default: True win_name (str): The window name. Default: '' wait_time (float): Value of waitKey param. Default: 0. out_file (str, optional): The filename to write the image. Default: None visual_label_threshold(int, optional): Bbox showed label limit. visual_label_offset(int, optional): Bbox showed label offset. Returns: ndarray: The image with bboxes drawn on it. """ assert bboxes.ndim == 2, \ f' bboxes ndim should be 2, but its ndim is {bboxes.ndim}.' assert labels.ndim == 1, \ f' labels ndim should be 1, but its ndim is {labels.ndim}.' assert bboxes.shape[0] == labels.shape[0], \ 'bboxes.shape[0] and labels.shape[0] should have the same length.' assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5, \ f' bboxes.shape[1] should be 4 or 5, but its {bboxes.shape[1]}.' img = mmcv.imread(img).astype(np.uint8) if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] if segms is not None: segms = segms[inds, ...] mask_colors = [] if labels.shape[0] > 0: if mask_color is None: # random color np.random.seed(42) mask_colors = [ np.random.randint(0, 256, (1, 3), dtype=np.uint8) for _ in range(max(labels) + 1) ] else: # specify color mask_colors = [ np.array(mmcv.color_val(mask_color)[::-1], dtype=np.uint8) ] * (max(labels) + 1) bbox_color = color_val_matplotlib(bbox_color) text_color = color_val_matplotlib(text_color) img = mmcv.bgr2rgb(img) width, height = img.shape[1], img.shape[0] img = np.ascontiguousarray(img) fig = plt.figure(win_name, frameon=False) plt.title(win_name) canvas = fig.canvas dpi = fig.get_dpi() # add a small EPS to avoid precision lost due to matplotlib's truncation # (https://github.com/matplotlib/matplotlib/issues/15363) fig.set_size_inches((width + EPS) / dpi, (height + EPS) / dpi) # remove white edges by set subplot margin plt.subplots_adjust(left=0, right=1, bottom=0, top=1) ax = plt.gca() ax.axis('off') polygons = [] color = [] for i, (bbox, label) in enumerate(zip(bboxes, labels)): if visual_label_threshold < 0 or visual_label_offset <= label <= visual_label_threshold: bbox_int = bbox.astype(np.int32) poly = [[bbox_int[0], bbox_int[1]], [bbox_int[0], bbox_int[3]], [bbox_int[2], bbox_int[3]], [bbox_int[2], bbox_int[1]]] np_poly = np.array(poly).reshape((4, 2)) polygons.append(Polygon(np_poly)) color.append(bbox_color) label_text = class_names[ label] if class_names is not None else f'class {label}' if len(bbox) > 4: label_text += f'|{bbox[-1]:.02f}' ax.text(bbox_int[0], bbox_int[1], f'{label_text}', bbox={ 'facecolor': 'black', 'alpha': 0.8, 'pad': 0.7, 'edgecolor': 'none' }, color=text_color, fontsize=font_size, verticalalignment='top', horizontalalignment='left') if segms is not None: color_mask = mask_colors[labels[i]] mask = segms[i].astype(bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 plt.imshow(img) p = PatchCollection(polygons, facecolor='none', edgecolors=color, linewidths=thickness) ax.add_collection(p) stream, _ = canvas.print_to_buffer() buffer = np.frombuffer(stream, dtype='uint8') img_rgba = buffer.reshape(height, width, 4) rgb, alpha = np.split(img_rgba, [3], axis=2) img = rgb.astype('uint8') img = mmcv.rgb2bgr(img) if show: # We do not use cv2 for display because in some cases, opencv will # conflict with Qt, it will output a warning: Current thread # is not the object's thread. You can refer to # https://github.com/opencv/opencv-python/issues/46 for details if wait_time == 0: plt.show() else: plt.show(block=False) plt.pause(wait_time) if out_file is not None: mmcv.imwrite(img, out_file) plt.close() return img
def imshow_det_bboxes(img, bboxes, labels, segms=None, class_names=None, score_thr=0, bbox_color='green', text_color='green', mask_color=None, #thickness=2, thickness=0, # clw modify font_scale=0.5, #font_size=13, font_size=6, win_name='', #fig_size=(15, 10), fig_size=(3, 2), show=True, wait_time=0, out_file=None): """Draw bboxes and class labels (with scores) on an image. Args: img (str or ndarray): The image to be displayed. bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or (n, 5). labels (ndarray): Labels of bboxes. segms (ndarray or None): Masks, shaped (n,h,w) or None class_names (list[str]): Names of each classes. score_thr (float): Minimum score of bboxes to be shown. Default: 0 bbox_color (str or tuple(int) or :obj:`Color`):Color of bbox lines. The tuple of color should be in BGR order. Default: 'green' text_color (str or tuple(int) or :obj:`Color`):Color of texts. The tuple of color should be in BGR order. Default: 'green' mask_color (None or str or tuple(int) or :obj:`Color`): Color of masks. The tuple of color should be in BGR order. Default: None thickness (int): Thickness of lines. Default: 2 font_scale (float): Font scales of texts. Default: 0.5 font_size (int): Font size of texts. Default: 13 show (bool): Whether to show the image. Default: True win_name (str): The window name. Default: '' fig_size (tuple): Figure size of the pyplot figure. Default: (15, 10) wait_time (float): Value of waitKey param. Default: 0. out_file (str or None): The filename to write the image. Default: None Returns: ndarray: The image with bboxes drawn on it. """ warnings.warn('"font_scale" will be deprecated in v2.9.0,' 'Please use "font_size"') assert bboxes.ndim == 2, \ f' bboxes ndim should be 2, but its ndim is {bboxes.ndim}.' assert labels.ndim == 1, \ f' labels ndim should be 1, but its ndim is {labels.ndim}.' assert bboxes.shape[0] == labels.shape[0], \ 'bboxes.shape[0] and labels.shape[0] should have the same length.' assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5,\ f' bboxes.shape[1] should be 4 or 5, but its {bboxes.shape[1]}.' img = mmcv.imread(img).copy() if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] if segms is not None: segms = segms[inds, ...] mask_colors = [] if labels.shape[0] > 0: if mask_color is None: # random color np.random.seed(42) mask_colors = [ np.random.randint(0, 256, (1, 3), dtype=np.uint8) for _ in range(max(labels) + 1) ] else: # specify color mask_colors = [ np.array(mmcv.color_val(mask_color)[::-1], dtype=np.uint8) ] * ( max(labels) + 1) bbox_color = color_val_matplotlib(bbox_color) text_color = color_val_matplotlib(text_color) img = mmcv.bgr2rgb(img) img = np.ascontiguousarray(img) plt.figure(win_name, figsize=fig_size) plt.title(win_name) plt.axis('off') ax = plt.gca() polygons = [] color = [] for i, (bbox, label) in enumerate(zip(bboxes, labels)): bbox_int = bbox.astype(np.int32) poly = [[bbox_int[0], bbox_int[1]], [bbox_int[0], bbox_int[3]], [bbox_int[2], bbox_int[3]], [bbox_int[2], bbox_int[1]]] np_poly = np.array(poly).reshape((4, 2)) polygons.append(Polygon(np_poly)) color.append(bbox_color) label_text = class_names[ label] if class_names is not None else f'class {label}' if len(bbox) > 4: label_text += f'|{bbox[-1]:.02f}' ax.text( bbox_int[0], bbox_int[1], f'{label_text}', bbox={ 'facecolor': 'black', #'alpha': 0.8, 'alpha': 0, # clw modify #'pad': 0.7, 'pad': 0, # clw modify 'edgecolor': 'none' }, color=text_color, fontsize=font_size, verticalalignment='top', horizontalalignment='left') if segms is not None: color_mask = mask_colors[labels[i]] mask = segms[i].astype(bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 plt.imshow(img) p = PatchCollection( polygons, facecolor='none', edgecolors=color, linewidths=thickness) ax.add_collection(p) if out_file is not None: dir_name = osp.abspath(osp.dirname(out_file)) mmcv.mkdir_or_exist(dir_name) plt.savefig(out_file) #plt.savefig(out_file, dpi=600) # clw modify: too slow if not show: plt.close() if show: if wait_time == 0: plt.show() else: plt.show(block=False) plt.pause(wait_time) plt.close() return mmcv.rgb2bgr(img)
def test_color(): assert mmcv.color_val(mmcv.Color.blue) == (255, 0, 0) assert mmcv.color_val('green') == (0, 255, 0) assert mmcv.color_val((1, 2, 3)) == (1, 2, 3) assert mmcv.color_val(100) == (100, 100, 100) assert mmcv.color_val(np.zeros(3, dtype=np.int)) == (0, 0, 0) with pytest.raises(TypeError): mmcv.color_val([255, 255, 255]) with pytest.raises(TypeError): mmcv.color_val(1.0) with pytest.raises(AssertionError): mmcv.color_val((0, 0, 500))
def imshow_det_bboxes(img, bboxes, labels, class_names=None, score_thr=0, bbox_color='green', text_color='green', thickness=2, font_scale=2, show=True, win_name='', wait_time=0, out_file=None): """Draw bboxes and class labels (with scores) on an image. Args: img (str or ndarray): The image to be displayed. bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or (n, 5). labels (ndarray): Labels of bboxes. class_names (list[str]): Names of each classes. score_thr (float): Minimum score of bboxes to be shown. bbox_color (str or tuple or :obj:`Color`): Color of bbox lines. 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 to write the image. """ assert bboxes.ndim == 2 assert labels.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 img = mmcv.imread(img) if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] bbox_color = mmcv.color_val(bbox_color) text_color = mmcv.color_val(text_color) for bbox, label in zip(bboxes, labels): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness) label_text = class_names[ label] if class_names is not None else 'cls {}'.format(label) if len(bbox) > 4: label_text += '|{:.02f}'.format(bbox[-1]) cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color, thickness) if show: plt.figure(figsize=(20, 20)) plt.axis('off') plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.show() if out_file is not None: imwrite(img, out_file)
def imshow_bboxes(img, bboxes, labels=None, colors='green', text_color='white', thickness=1, font_scale=0.5, show=True, win_name='', wait_time=0, out_file=None): """Draw bboxes with labels (optional) on an image. This is a wrapper of mmcv.imshow_bboxes. Args: img (str or ndarray): The image to be displayed. bboxes (ndarray): ndarray of shape (k, 4), each row is a bbox in format [x1, y1, x2, y2]. labels (str or list[str], optional): labels of each bbox. colors (list[str or tuple or :obj:`Color`]): A list of colors. 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, optional): The filename to write the image. Returns: ndarray: The image with bboxes drawn on it. """ # adapt to mmcv.imshow_bboxes input format bboxes = np.split(bboxes, bboxes.shape[0], axis=0) if bboxes.shape[0] > 0 else [] if not isinstance(colors, list): colors = [colors for _ in range(len(bboxes))] colors = [mmcv.color_val(c) for c in colors] assert len(bboxes) == len(colors) img = mmcv.imshow_bboxes(img, bboxes, colors, top_k=-1, thickness=thickness, show=False, out_file=None) if labels is not None: if not isinstance(labels, list): labels = [labels for _ in range(len(bboxes))] assert len(labels) == len(bboxes) for bbox, label, color in zip(bboxes, labels, colors): bbox_int = bbox[0, :4].astype(np.int32) # roughly estimate the proper font size text_size, text_baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_DUPLEX, font_scale, thickness) text_x1 = bbox_int[0] text_y1 = max(0, bbox_int[1] - text_size[1] - text_baseline) text_x2 = bbox_int[0] + text_size[0] text_y2 = text_y1 + text_size[1] + text_baseline cv2.rectangle(img, (text_x1, text_y1), (text_x2, text_y2), color, cv2.FILLED) cv2.putText(img, label, (text_x1, text_y2 - text_baseline), cv2.FONT_HERSHEY_DUPLEX, font_scale, mmcv.color_val(text_color), thickness) if show: mmcv.imshow(img, win_name, wait_time) if out_file is not None: mmcv.imwrite(img, out_file) return img
def imshow_det_bboxes_poses( img, bboxes, labels, class_names=None, score_thr=0, bbox_color="green", text_color="green", thickness=1, font_scale=0.5, show=True, win_name="", wait_time=0, out_file=None, poses=None, corners_3d=None, dataste_name=None, renderer=None, K=None, vis_tool="matplotlib", ): """Draw bboxes and class labels (with scores) on an image. Render the contours of poses to image. (or the 3d bounding box) Args: img (str or ndarray): The image to be displayed. bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or (n, 5). labels (ndarray): Labels of bboxes. 0-based class_names (list[str]): Names of each classes. score_thr (float): Minimum score of bboxes to be shown. bbox_color (str or tuple or :obj:`Color`): Color of bbox lines. 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 to write the image. ------ poses: corners_3d: dict of 3d corners(un-transformed), key is cls_name dataset_name: camera intrinsic parameter renderer: K: camera intrinsic """ # logger.info('poses: {}'.format(poses)) assert bboxes.ndim == 2 assert labels.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 img = imread(img) if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] bbox_color = color_val(bbox_color) text_color = color_val(text_color) for bbox, label in zip(bboxes, labels): # pose if poses is not None: if poses[label]: pose = poses[label][0] # TODO: handle multiple poses bgr, depth = renderer.render(label, pose[:, :3], pose[:, 3], r_type="mat") # img = img - bgr pose_mask = np.zeros(depth.shape) pose_mask[depth != 0] = 1 edges_3 = mask_utils.get_edge(pose_mask, bw=3) edges_3[:, :, [0, 1]] = 0 # red img[edges_3 != 0] = 255 cls_name = class_names[label] corners_2d, _ = misc_6d.points_to_2D(corners_3d[cls_name], pose[:, :3], pose[:, 3], K) img = misc_6d.draw_projected_box3d(img, corners_2d, thickness=thickness) bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness) label_text = class_names[label] if class_names is not None else "cls {}".format(label) if len(bbox) > 4: label_text += "|{:.02f}".format(bbox[-1]) cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) if show: if vis_tool == "matplotlib": fig = plt.figure(frameon=False, figsize=(8, 6), dpi=100) tmp = fig.add_subplot(1, 1, 1) tmp.set_title("{}".format(win_name)) plt.axis("off") plt.imshow(img[:, :, [2, 1, 0]]) plt.show() else: # use 'mmcv' imshow(img, win_name, wait_time) if out_file is not None: imwrite(img, out_file) return img