目标检测修改检测框大小、粗细

修改mmdetection参数

如果你的网络是用mmdetection写的,可视化预测结果时,发现框的线条太细,当输入图片太大时会看不清标注的框。
在这里插入图片描述

我们可以通过修改mmdtection中的一些参数来改变检测框的颜色、粗细以及字体的大小。
在这里插入图片描述

改变检测框的颜色、粗细、文字大小

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

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

改变文字粗细

如果要修改文字的粗细,首先找到文件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	#加入字体粗细参数)

但是用调整mmdtection的参数后检测框的线条粗细和字体大小也就固定了,无法随着输入图片的大小进行自动更改,如果我们输入一张分辨率很高的图片,即使我们把thickness设为3如果不放大了看还是会看不清。

这时候我们可以根据输入图片的尺寸来给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

如果你的代码不是用mmdetection框架写的,那么只要找到show_result部分的代码,进行相应的修改即可。
之前使用yolov4的代码的时候,检测出来的效果长这个样子。
在这里插入图片描述
标签文本底下加了个填充框,使得在检测小物体的时候有些小框会被遮住。究其原因,是因为在yolo.py里面的detect_image函数里加了draw.rectangle函数,只要把这个函数注释掉就可以了,但是这样一来标签文本全是黑色,我们可以找到标注标签的函数,将便签颜色改成跟框一样的颜色。

# draw.rectangle(
#            [tuple(text_origin), tuple(text_origin + label_size)],
#            fill=self.colors[self.class_names.index(predicted_class)])
# draw.text(text_origin, str(label,'UTF-8'), fill=(0, 0, 0), font=font)
draw.text(text_origin, str(label,'UTF-8'), fill=self.colors[self.class_names.index(predicted_class)], font=font)

对于框的大小、粗细,以及标签的字体,都可以在对应位置进行更改。

font = ImageFont.truetype(font='model_data/simhei.ttf',size=np.floor(3e-2 * np.shape(image)[1] + 0.5).astype('int32'))
thickness = max((np.shape(image)[0] + np.shape(image)[1]) // self.model_image_size[0], 1)

在这里插入图片描述

版权声明:本文为CSDN博主「天涯小才」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40502460/article/details/117319923

天涯小才

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

YOLO区域目标检测

基于YOLO的区域检测 最近在研究YOLO的项目,也研究一些大神关于区域检测的代码,发现比较稀缺,几番周折之后,研究了一下,搞了一套暂且能凑合用的,先码上&#

SSD详细版

SSD在VGGNet基础上又增加几个卷基层,然后用3*3的卷积核在不同尺度上进行分类和回归。SSD的创新点:数据增强、VGGNet卷积块儿、PriorBox与多层特征图、正负样本选取与损失函数的设计。SSD的优点&#