【目标检测】旋转框标注格式转换脚本 roLabelImgxml_to_royolo5txts

【目标检测】旋转框标注格式转换脚本

roLabelImgxml_to_royolo5txts.py
代码如下:

import math
import os
import os.path as osp
BASEDIR = osp.dirname(osp.abspath(__file__))
XMLDIR = osp.join(BASEDIR, 'overAlert_xmls')
OUTDIR = osp.join(BASEDIR, 'overAlert_txts')
if not osp.exists(OUTDIR):
    os.makedirs(OUTDIR)

xmlnames = [i for i in os.listdir(XMLDIR) if i.endswith('.xml')]
# print(names)

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)

    for i in range(len(cx)):
        cls0 = 0.0
        cx_i = cx[i] / img_width
        cy_i = cy[i] / img_height
        w_i = w[i] / img_width
        h_i = h[i] / img_height
        a_i = int(angle[i] * 180 / math.pi)

        if w_i < h_i:
            h_i, w_i = w_i, h_i
            a_i -= 90
            if a_i < 0:
                a_i += 180

        put_str = ' '.join([str(cls0), 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.')

OK

你可能感兴趣的:(python,目标检测)