YOLOv7怎么改变检测框(box)的标签字体样式与大小,检测框颜色,修改字体宽度

您所在的位置:网站首页 vscode改字体大小代码 YOLOv7怎么改变检测框(box)的标签字体样式与大小,检测框颜色,修改字体宽度

YOLOv7怎么改变检测框(box)的标签字体样式与大小,检测框颜色,修改字体宽度

2023-06-21 16:13| 来源: 网络整理| 查看: 265

YOLOv7中detect.py所用的检测框字体为plots.py中的 plot_one_box函数

from utils.plots import plot_one_box

因此进入到utils文件夹下的plots.py文件中,找到plot_one_box

def plot_one_box(x, img, color=None, label=None, line_thickness=3): # Plots one bounding box on image img tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) if label: tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tf/3 , thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(img, label, (c1[0], c1[1] - 2), 0, 2, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)

修改标签背景框:

找到t_size = cv2.getTextSize(label, 0, fontScale=tl/3 , thickness=tl)[0],对这一行进行修改:fontscale=1.5和fontscale=3时效果为:

                                        

 thickness我改后发现没什么变化

 修改标签字体:

找到 cv2.putText(img, label, (c1[0], c1[1] - 2), 0, 2, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA),对这一行进行修改:

括号里的参数依次为:图片,添加的文字,左上角坐标,字体样式,字体大小,颜色,字体粗细

因此,想要修改字体大小,需要对第3个参数进行修改,

cv2.putText(img, label, (c1[0], c1[1] - 2), 0, 2, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)

一般来讲,字体大小数值和标签背景框的大小相差不大。

修改字体宽度:

一般你的标签看不清,都需要改这一项,找到

cv2.putText(img, label, (c1[0], c1[1] - 2), 0, 2, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)

可以直接tf改成你想要的数字,或者改tf参数

修改字体样式:cv2.putText(img, label, (c1[0], c1[1] - 2), 0, 2, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)

修改框的颜色:

因为YOLOv7颜色框每run一次exp就会随机分配一次,因此为了做不同网络对比,需要对每次run的检测框颜色进行定义。

修改颜色直接进detect.py文件中找到:

names = model.module.names if hasattr(model, 'module') else model.names colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]

把colors改为你想要的颜色,有多少个类别就选择多少颜色,也可以多列举几个颜色 ,比如6个类别时:

colors = [[0, 255, 0], [0, 0, 255], [255, 0, 0],[255, 150, 0], [255, 0, 150], [0, 150, 255], [150, 0, 255]]

本人也是近期接触YOLOv7,如果有什么更好的修改方法欢迎评论区互相讨论学习! 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3