赞
踩
话不多说,直接上代码
- import os
- import os.path as osp
- import math
-
-
- BASEDIR = osp.dirname(osp.abspath(__file__))
- XMLDIR = osp.join(BASEDIR, 'D:\Bishe\mydata\original_image\data_xml')
- OUTDIR = osp.join(BASEDIR, 'D:\Bishe\mydata\original_image\data_txt3')
- if not osp.exists(OUTDIR):
- os.makedirs(OUTDIR)
- #pi=3.1415926
- xmlnames = [i for i in os.listdir(XMLDIR) if i.endswith('.xml')]
- # print(names)
- pi=3.1415926
- #转换成四点坐标
- def convert(cx,cy,w,h,a):
- if a>=pi:
- a-=pi
- #计算斜径半长
- l=math.sqrt(w**2+h**2)/2
- #计算初始矩形角度
- a0=math.atan(h/w)
- #旋转,计算旋转角
- #右上角点 ↗
- a1=a0+a
- x1=cx+l*math.cos(a1)
- y1=cy+l*math.sin(a1)
- #右下角点 ↘
- a2=-a0+a
- x2=cx+l*math.cos(a2)
- y2=cy+l*math.sin(a2)
- #左下角点 ↙
- a3=a1+pi
- x3=cx+l*math.cos(a3)
- y3=cy+l*math.sin(a3)
- #左上角点 ↖
- a4=a2+pi
- x4=cx+l*math.cos(a4)
- y4=cy+l*math.sin(a4)
- return [x1,y1,x2,y2,x3,y3,x4,y4]
-
- #点关于直线对称
- for xmlname in xmlnames:
- cx = []
- cy = []
- w = []
- h = []
- angle = []
-
- txtname = xmlname.split('.')[-2] + '.txt'
-
- with open(osp.join(OUTDIR, txtname), 'w') as fp:
- fp.write('')
- with open(osp.join(XMLDIR, xmlname), 'r') as fp:
- lines = fp.readlines()
- for line in lines:
- # print(line, end='')
- if line.strip().startswith('<width>'):
- img_width = eval(line.strip().strip('<width>').strip('</width>'))
- # print(img_width)
- if line.strip().startswith('<height>'):
- img_height = eval(line.strip().strip('<height>').strip('</height>'))
- # print(img_height)
- if line.strip().startswith('<depth>'):
- img_depth = eval(line.strip().strip('<depth>').strip('</depth>'))
- # print(img_depth)
- if line.strip().startswith('<cx>'):
- cx.append(eval(line.strip().strip('<cx>').strip('</cx>')))
- # print(cx)
- if line.strip().startswith('<cy>'):
- cy.append(eval(line.strip().strip('<cy>').strip('</cy>')))
- # print(cy)
- if line.strip().startswith('<w>'):
- w.append(eval(line.strip().strip('<w>').strip('</w>')))
- # print(w)
- if line.strip().startswith('<h>'):
- h.append(eval(line.strip().strip('<h>').strip('</h>')))
- # print(h)
- if line.strip().startswith('<angle>'):
- angle.append(eval(line.strip().strip('<angle>').strip('</angle>')))
- # print(angle)
- with open(osp.join(OUTDIR, txtname), 'a') as fp:
- fp.write("imagesource:GoogleEarth")
- fp.write('\n')
- fp.write("gsd:0.146343590398")
- fp.write('\n')
- for i in range(len(cx)):
- cls0 = 0.0
- cx_i = cx[i]
- cy_i = cy[i]
- w_i = w[i]
- h_i = h[i]
- a_i = angle[i]
-
-
- x0,y0,x1,y1,x2,y2,x3,y3=convert(cx_i,cy_i,w_i,h_i,a_i)
-
- put_str = ' '.join([str(x0), str(y0), str(x1), str(y1), str(x2), str(y2), str(x3), str(y3),str('bigship'),str(0)])
- #put_str = ' '.join([str(cx_i), str(cy_i), str(w_i), str(h_i), str(a_i)])
- with open(osp.join(OUTDIR, txtname), 'a') as fp:
- fp.write(put_str)
-
- fp.write('\n')
- print(xmlname, 'to', txtname, 'done.')
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。