YOLO修改检测结果图片中标签的格式

格式修改项

  1. 标签透明度
  2. 标签与BBX的相对位置

修改代码

代码文件:ultralytics/yolo/utils/plotting.py

// 修改前的代码如下
class Annotator:
    # YOLOv8 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
    def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
    // 此处省略中间代码
    def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
    // 此处省略中间代码
                outside = p1[1] - h >= 3
                p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
                cv2.rectangle(self.im, p1, p2, color, -1, cv2.LINE_AA)  # filled
                cv2.putText(self.im,
                            label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2),
                            0,
                            self.lw / 3,
                            txt_color,
                            thickness=tf,
                            lineType=cv2.LINE_AA)
// 修改后的代码如下
class Annotator:
    # YOLOv8 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
    def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc', alpha=0.4):
    // 此处省略中间代码
    self.alpha = alpha
    // 此处省略中间代码
    def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
    // 此处省略中间代码
                text_topleft_x = p1[0]  # text左上角点x坐标
                text_topleft_y = int(box[3]) + 3  # text左上角点y坐标
                overlay = self.im.copy()  # 复制图层,用于设置标签透明度
                cv2.rectangle(overlay, (text_topleft_x, text_topleft_y),
                              (text_topleft_x + w, text_topleft_y + h), color, -1, cv2.LINE_AA)  # 画框,filled
                cv2.addWeighted(overlay, self.alpha, self.im, 1 - self.alpha, 0, self.im)  # 设置标签透明度
                cv2.putText(self.im,
                            label, (text_topleft_x, text_topleft_y + h + 2),
                            0,
                            self.lw / 3,
                            txt_color,
                            thickness=tf,
                            lineType=cv2.LINE_AA)  # 写text
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值