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.更加详细的解说可以看大佬的博客