x_center,y_center,width,height格式转DOTA格式

本文介绍如何使用 Python 对 YOLOv5 的标注文件进行转换,将原始的标注格式转换为适用于特定任务的新格式。通过读取指定路径下的文本文件,解析并重新计算边界框坐标,最后将转换后的数据保存到新的文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import os
path_name="/media/liuyanxin/7bb102f1-967f-40cb-8069-704229851f24/heaven/tianzhi_2_pretrain/"
filenames = os.listdir(path_name+'ship_dataset_v0')
save_path="/media/liuyanxin/7bb102f1-967f-40cb-8069-704229851f24/heaven/tianzhi_2_pretrain/labels/"
for filename in filenames:
    if(filename.endswith(".txt")):
        org_filename=str(path_name+"ship_dataset_v0/"+filename)
        with open(org_filename,'r') as f:
            for lines in f:
                category,x_center,y_center,width,height=lines.split(" ")[0:5]
                x_center=float(x_center)
                y_center = float(y_center)
                width = float(width)
                height = float(height)

                x1=256*(x_center-width/2)
                y1=256*(y_center+height/2)
                x2=256*(x_center+width/2)
                y2=256*(y_center+height/2)
                x3=256*(x_center-width/2)
                y3=256*(y_center-height/2)
                x4=256*(x_center+width/2)
                y4=256*(y_center-height/2)
                category=category
                save_txt_path=os.path.join(save_path, filename)
                print(save_txt_path)
                with open(save_txt_path, 'a+') as f:
                    f.write(str(x1))
                    f.write(' ')
                    f.write(str(y1))
                    f.write(' ')
                    f.write(str(x2))
                    f.write(' ')
                    f.write(str(y2))
                    f.write(' ')
                    f.write(str(x3))
                    f.write(' ')
                    f.write(str(y3))
                    f.write(' ')
                    f.write(str(x4))
                    f.write(' ')
                    f.write(str(y4))
                    f.write(' ')
                    f.write(str(category))
                    #f.write(' ')
                    #f.write()
                    f.write('\n')





转之前的格式

0 0.162109375 0.46875 0.06640625 0.0703125
0 0.5234375 0.462890625 0.0546875 0.12109375
0 0.4296875 0.92578125 0.0859375 0.0625
0 0.630859375 0.21484375 0.06640625 0.125
0 0.4140625 0.154296875 0.0625 0.12890625
0 0.861328125 0.67578125 0.05078125 0.0703125
转之后的格式
33.0 129.0 50.0 129.0 33.0 111.0 50.0 111.0 0
127.0 134.0 141.0 134.0 127.0 103.0 141.0 103.0 0
99.0 245.0 121.0 245.0 99.0 229.0 121.0 229.0 0
153.0 71.0 170.0 71.0 153.0 39.0 170.0 39.0 0
98.0 56.0 114.0 56.0 98.0 23.0 114.0 23.0 0
214.0 182.0 227.0 182.0 214.0 164.0 227.0 164.0 0

图像256*256

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值