格式修改项
- 标签透明度
- 标签与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