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

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

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

2024-06-30 12:28:46| 来源: 网络整理| 查看: 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)

1. 修改标签背景框:

找到t_size = cv2.getTextSize(label, 0, fontScale=tl/3 , thickness=tl)[0],对这一行进行修改:

      fontscale=1.5                                                        fontscale=3

                                        

这里的fontscale(字体大小)指的是框里面应该放的字体大小,并不是真正改字体大小,下面的putText中才是。

2. 修改标签字体:

找到 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, 3, [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)

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

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

4. 修改字体样式:

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

5. 修改框的颜色:

修改颜色直接进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]

可见每次detect后颜色都是随机生成的,因此我们需要把每类标签的颜色固定。

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

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

6. 取消显示标签,只保留框:

注释掉以下几行即可

效果如下:

                 ​​​​​​​

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

7. 2024.6.10 有朋友私信想要新罗马体的预测标签 参考博客【YOLOv7】Yolov7不支持中文标签,中文乱码,中文显示问号解决方案_yolov8验证集图片显示中opencv不支持中文显示,使用plot打标签-CSDN博客

按照以下方式即可更换任意字体

步骤一: 下载你想要的字体,例如新罗马体Times-Roman免费字体下载 - 英文字体免费下载尽在字体家 (zitijia.com)

下载好后放在yolov7工作目录内(与detect.py同级目录)

步骤二:找到plots.py,将相应函数替换为:

def plot_one_box(x, img, color=None, label=None, line_thickness=3): # Plots one bounding box on image img 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])) tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) plot_one_box_PIL(tuple((float(x[0]), float(x[1] - 1), float(x[2]), float(x[3]))), img, color, label, line_thickness) # if label: # tf = max(tl - 1, 1) # font thickness # t_size = cv2.getTextSize(label, 0, fontScale=tl / 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, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) def plot_one_box_PIL(box, imgbase, color=None, label=None, line_thickness=None): rows, cols, channels = imgbase.shape img = Image.fromarray(imgbase) draw = ImageDraw.Draw(img) line_thickness = line_thickness or max(int(min(img.size) / 200), 2) draw.rectangle(box, width=line_thickness, outline=tuple(color)) # plot if label: fontsize = max(round(max(img.size) / 80), 6) font = ImageFont.truetype("Times.ttf", fontsize, encoding="utf-8") txt_width, txt_height = font.getsize(label) draw.rectangle([box[0], box[1] - txt_height + 0, box[0] + txt_width, box[1]], fill=tuple(color)) draw.text((box[0], box[1] - txt_height + 1), label, fill=(255, 255, 255), font=font) newimg = np.asarray(img) imgbase[0:rows, 0:cols] = newimg return newimg


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭