rolabelimg和labelimg标注完后voc格式转dota格式,xml转txt

话不多说,直接上代码

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(''):
            img_width = eval(line.strip().strip('').strip(''))
            # print(img_width)
        if line.strip().startswith(''):
            img_height = eval(line.strip().strip('').strip(''))
            # print(img_height)
        if line.strip().startswith(''):
            img_depth = eval(line.strip().strip('').strip(''))
            # print(img_depth)
        if line.strip().startswith(''):
            cx.append(eval(line.strip().strip('').strip('')))
            # print(cx)
        if line.strip().startswith(''):
            cy.append(eval(line.strip().strip('').strip('')))
            # print(cy)
        if line.strip().startswith(''):
            w.append(eval(line.strip().strip('').strip('')))
            # print(w)
        if line.strip().startswith(''):
            h.append(eval(line.strip().strip('').strip('')))
            # print(h)
        if line.strip().startswith(''):
            angle.append(eval(line.strip().strip('').strip('')))
            # 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.')

你可能感兴趣的:(python)