MMdet修改检测框字体大小、位置、颜色、填充框

1.修改填充框

       在mmdet/core/visulization/image.py下面,139行开始,写文字和填充框的代码,把填充框那部分注释掉

2.修改检测框字体大小、颜色

        找到mmdet/models/detectors/base.py文件,修改class BaseDetector()中的show_result()函数的输入参数

def show_result(self,
                    img,
                    result,
                    score_thr=0.3,
                    bbox_color='green',    # 检测框的颜色
                    text_color='green',    # 字体的颜色
                    thickness=2, # 检测框的粗细
                    font_size=1.5, # 字体大小
                    win_name='',
                    show=False,
                    wait_time=0,
                    out_file=None):

3.修改字体的粗细程度

       找到文件anaconda3/envs/open-mmlab/lib/python3.7/site-packages/mmcv/visualization/image.py。在mmcv.imshow_det_bboxes()函数调用cv2.putText函数将类别文字标注到图上,加上改变字体粗细的参数。

       cv2.putText(img, 
            label_text, 
            (bbox_int[0], bbox_int[1] - 2),
             cv2.FONT_HERSHEY_COMPLEX, 
             font_scale, 
             text_color,
             3    #加入改变字体粗细的参数)

4.设置跟随图片分辨率自动设置字体大小的

       我们可以根据输入图片的尺寸来给thickness和font_scale赋值,字体大小为np.floor(5e-4 * np.shape(img)[1] + 0.5)。检测框的粗细设置为max((np.shape(img)[0] + np.shape(img)[1]) // 1080, 1)。

def show_result(self,
                    img,
                    result,
                    score_thr=0.3,
                    bbox_color='green',
                    text_color='green',
                    #thickness=3,
                    #font_scale=0.8,
                    win_name='',
                    show=False,
                    wait_time=0,
                    out_file=None):
        img = mmcv.imread(img)
        font_scale=np.floor(5e-4 * np.shape(img)[1] + 0.5)
        thickness=thickness = max((np.shape(img)[0] + np.shape(img)[1]) // 1080, 1)
        img = img.copy()
        if isinstance(result, tuple):
            bbox_result, segm_result = result
            if isinstance(segm_result, tuple):
                segm_result = segm_result[0]  # ms rcnn
        else:
            bbox_result, segm_result = result, None
        bboxes = np.vstack(bbox_result)
        labels = [
            np.full(bbox.shape[0], i, dtype=np.int32)
            for i, bbox in enumerate(bbox_result)
        ]
        labels = np.concatenate(labels)
        # draw segmentation masks
        if segm_result is not None and len(labels) > 0:  # non empty
            segms = mmcv.concat_list(segm_result)
            inds = np.where(bboxes[:, -1] > score_thr)[0]
            np.random.seed(42)
            color_masks = [
                np.random.randint(0, 256, (1, 3), dtype=np.uint8)
                for _ in range(max(labels) + 1)
            ]
            for i in inds:
                i = int(i)
                color_mask = color_masks[labels[i]]
                sg = segms[i]
                if isinstance(sg, torch.Tensor):
                    sg = sg.detach().cpu().numpy()
                mask = sg.astype(bool)
                img[mask] = img[mask] * 0.5 + color_mask * 0.5
        # if out_file specified, do not show image in window
        if out_file is not None:
            show = False
        # draw bounding boxes
        mmcv.imshow_det_bboxes(
            img,
            bboxes,
            labels,
            class_names=self.CLASSES,
            score_thr=score_thr,
            bbox_color=bbox_color,
            text_color=text_color,
            thickness=thickness,
            font_scale=font_scale,
            win_name=win_name,
            show=show,
            wait_time=wait_time,
            out_file=out_file)

        if not (show or out_file):
            return img

5.更加详细的解说可以看大佬的博客

MMDetection V2.0 可视化参数修改_威风凛凛小程序员的博客-CSDN博客MMDetection V2.0 可视化参数修改改变检测框的颜色、粗细、文字大小改变文字粗细修改效果将MMDetection的检测结果在SAR图上可视化时,发现字体太小、太细,看不清。修改MMDtection中一些参数可以改变检测框的颜色、粗细、字体大小、字体粗细。改变检测框的颜色、粗细、文字大小找到mmdet/models/detectors/base.py文件,修改class BaseDetector()中的show_result()函数的输入参数 def show_result(selfhttps://blog.csdn.net/i013140225/article/details/109819366?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-109819366.pc_agg_new_rank&utm_term=mmdetection%E4%B8%AD%E6%A0%87%E6%B3%A8%E5%AD%97%E4%BD%93%E7%9A%84%E5%A4%A7%E5%B0%8F%E6%80%8E%E4%B9%88%E8%B0%83%E6%95%B4&spm=1000.2123.3001.4430

目标检测修改检测框大小、粗细_天涯小才的博客-CSDN博客_怎么调整目标检测框的大小https://blog.csdn.net/qq_40502460/article/details/117319923

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值