当前位置:   article > 正文

【目标检测】旋转框标注格式转换脚本 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('<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)

    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.')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

OK

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/722316
推荐阅读
相关标签
  

闽ICP备14008679号