U版YOLOv5
使用YOLOV5检测发现label字体太大,遮挡了小缺陷
1.detect.py
def run( ,line_thickness=3) 粗细,但是会有限制,有一个最小值,在utils/plots.py
第75行
self.font = check_font(font='Arial.Unicode.ttf' if is_chinese(example) else font,size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
2.在 plots.py 第96行,修改边框粗细
由于thickness不能取浮点数,我在这里改成了最小为1,
else: # cv2
p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
#cv2.rectangle(self.im, p1, p2, color, thickness=self.lw,lineType=cv2.LINE_AA)
cv2.rectangle(self.im, p1, p2, color, thickness=max(self.lw - 1, 1), lineType=cv2.LINE_AA)
3.修改label字体大小,在plots.py的第106行
原字体大小 为 s elf.lw/3, 我在这里改成了 self.lw/6
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)
cv2.putText(self.im, label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2), 0, self.lw / 6, txt_color,
thickness=tf, lineType=cv2.LINE_AA)
4.plots.py 第99行前后
修改label的粗细,和label 背景填充 的 宽和高
当label字体缩小时,如果字体还是原来这么粗,label会模糊
tf 为字体粗细,我直接改为了1
w,h 为填充宽和高, 由于上面label 大小 改为了self.lw/6, 这里也一样修改
if label:
#tf = max(self.lw - 1, 1) # font thickness
tf = max(self.lw - 100, 1)
#w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0] # text width, height
w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 6, thickness=tf)[0]