当前位置:   article > 正文

YOLOv5-2.0如何更改标签颜色和字体大小_yolov5怎么调整标签大小

yolov5怎么调整标签大小

在yolov5-2.0项目文件下找到utils目录,打开utils.py文件,找到“plot_one_box()”函数,大概是在第940行位置。

内容如下:

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

1、修改标签背景框,即找到下面代码,修改参数“ fontScale”,如“ fontScale=3=5”。

t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]

2、修改标签字体格式,即找到下面代码 进行修改,参数“img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf”依次代表“图片添加的文字左上角坐标字体样式字体大小颜色字体粗细”,参数的值可以是0、1、2、3、4、5等等。

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

字体样式如下:

3、取消检测标签,只保留检测框,注释掉若干行代码即可。

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

4、修改随机颜色,可见每次detect后颜色都是随机生成的,因此我们需要把每类标签的颜色固定。把colors改为你想要的颜色,有多少个类别就选择多少颜色,也可以多列举几个颜色 :

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

修改后:

  1. colors = [[0, 255, 0], [0, 0, 255], [255, 0, 0],[255, 150, 0],
  2. [255, 0, 150], [0, 150, 255], [150, 0, 255]]

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/666963
推荐阅读
相关标签
  

闽ICP备14008679号