先说一下Yolov5的格式:
检测文本输出格式也是标注格式:1、类 2、检测框中心点x值 3、中心点y值 4、宽 5、高 6、置信度 (2,3,4,5均为归一化后的值)
如果是视频会把每一帧都建立一个TXT。
MOT17格式:
1、帧 2、对象ID 3、左上角点的X 4、左上角Y 5、宽 6、高 7、置信度 8、9、10分别为xyz,2D用不着。
总共要改两处地方(注释掉的为原来的代码):
# 设置保存框坐标txt文件的路径
#txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # img.txt
#MOT17格式
txt_path = str(save_dir / 'labels' / p.stem)
# if save_txt: # Write to file
# # 将xyxy(左上角+右下角)格式转为xywh(中心点+宽长)格式,并除上w,h做归一化,转化为列表再保存
# xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
# line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh) # label format
# with open(txt_path + '.txt', 'a') as f:
# f.write(('%g ' * len(line)).rstrip() % line + '\n')
if save_txt: #重写成mot17格式的输出
xywh = xyxy2xywh(torch.tensor(xyxy).view(1, 4))
line = (frame,-1, xyxy[0],xyxy[1],xywh[:,2],xywh[:,3],conf,-1,-1,-1)
with open(txt_path + '.txt', 'a') as f:
f.write(('%g,' *(len(line)-1)+'%g').rstrip() % line + '\n')
顺便一提 gt格式: