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