标记格式转换xml->txt

代码

import xml.etree.ElementTree as ET
import os


def xml2txt(classes, xml_path, txt_path):
    file = open(xml_path)
    xml_text = file.read()
    root = ET.fromstring(xml_text)
    file.close()
    width = float(root.find('size').find('width').text)
    height = float(root.find('size').find('height').text)
    with open(txt_path, 'w') as file:
        pass

    for obj in root.iter('object'):
        name = obj.find('name').text
        if name not in classes:
            print(name)
            continue
        cls_id = classes.index(name)

        xmlbox = obj.find('bndbox')
        xmin = float(xmlbox.find('xmin').text)
        xmax = float(xmlbox.find('xmax').text)
        ymin = float(xmlbox.find('ymin').text)
        ymax = float(xmlbox.find('ymax').text)

        x = (xmin + xmax) / 2.0
        y = (ymin + ymax) / 2.0
        w = xmax - xmin
        h = ymax - ymin

        x /= width
        w /= width
        y /= height
        h /= height

        with open(txt_path, 'a') as file:
            file.write(str(cls_id) + ' ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')


if __name__ == '__main__':
    classes = ['smoke']
    inDir_path = 'Annotations'
    outDir_path = 'labels'
    for file_name in os.listdir(inDir_path):
        xml_path = inDir_path + '/' + file_name
        txt_path = outDir_path + '/' + file_name.replace('xml', 'txt')
        xml2txt(classes, xml_path, txt_path)

使用说明

我的目录如下:Annotations为原xml标注的文件夹,labels为新建的用于存放输出的空文件夹。

xml2txt函数定义了一个标注从xml格式到txt格式的转换,可根据需要在主函数自定义输入输出路径,然后调用xml2txt。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值