yolo|使输出的结果txt含目标的四个坐标信息及类别置信度

最近参加的智能船舶挑战赛对结果的格式要求:
包含目标边界框从左上角开始的顺时针标注点坐标,目标类别以及目标类别分数,并用空格分开。如下图所示:
在这里插入图片描述
故对yolov5的detect.py进行修改,定位到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
                        c1, c2 = (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3]))
                        line = (int(c1[0]),int(c1[1]),int(c2[0]),int(c1[1]),int(c2[0]),int(c2[1]),int(c1[0]),int(c2[1]),cls, 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')
                        with open(txt_path + '.txt') as f:
                            lines =f.readlines()
                        with open(txt_path + '.txt', 'w') as f:
                            for ll in lines:
                                ll_split =ll.strip().split()
                                ll_split[8]='ship'
                                f.write(ll_split[0]+' '+ll_split[1]+' '+ll_split[2]+' '+ll_split[3]+' '+ll_split[4]+' '+ll_split[5]+' '+ll_split[6]+' '+ll_split[7]+' '+ll_split[8]+' '+ll_split[9]+'\n')

                    if save_img or view_img:  # Add bbox to image
                        label = f'{
      
      names[int(cls)]} {
      
      conf:.2f}'
                        plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=1)

用yolov5的测试命令进行测试

python detect.py --weights runs/train/exp3/weights/best.pt --conf 0.25 --img-size 512 --source SAR_test1_images --save-txt --save-conf

结果如下:
每一张测试图像对应一个txt结果文件,结果文件名与图像名相同。txt内容如下:

275 68 338 68 338 121 275 121 ship 0.39502
251 69 335 69 335 157 251 157 ship 0.483398
186 46 249 46 249 93 186 93 ship 0.654785
144 3 205 3 205 52 144 52 ship 0.668945
286 143 349 143 349 185 286 185 ship 0.679199

猜你喜欢

转载自blog.csdn.net/weixin_44769034/article/details/127633565
今日推荐