本文是基于训练好的yolov5模型进行更改,即更改detect.py中的代码
信息储存位置
改变输出内容,我们就要找到结果生成的代码。阅读代码,找到 251 行
(# Write results ) 为结果生成代码。
# Write results
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) /gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # labelformat
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
if save_img or save_crop or view_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
annotator.box_label(xyxy, label, color=colors(c, True)) #文本生成
if save_crop:
save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
不难看出,annoator.box_lable为相关函数。label储存了文本信息
更改代码
更改:
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
即可
如加入‘良好’
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f} {'良好'}')