数据标注:将xml 格式转换txt 并且没有标注class类文件

import xml.etree.ElementTree as ET
import os
import shutil
import random

def convert_annotation(cls, cls_id, image_id):
    in_root = root_path + '/%s.xml' %(image_id)
    out_root = root_path + './../convert/%s/%s.txt' % (cls, image_id)
    in_file = open(in_root)
    out_file = open(out_root, 'w')

    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    size_width = int(size.find('width').text)
    size_height = int(size.find('height').text)
    for obj in root.iter('object'):
        xmlbox = obj.find('bndbox')
        b = [float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text)]
        if b[1] > size_width:
            b[1] = size_width
        if b[3] > size_height:
            b[3] = size_height
        txt_data=[((b[0]+b[1])/2.0-1)/size_width,((b[2]+b[3])/2.0-1)/size_height,(b[1]-b[0])/size_width,(b[3]-b[2])/size_height]
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in txt_data]) + '\n')

path = './'
classes=['chairs', 'keyboards', 'mouses', 'people', 'phones']#标签名
for num, cls in enumerate(classes):
    root_path = os.path.abspath(path + cls)
    img_path = os.path.join(root_path, '')
    imglist = [file for file in os.listdir(img_path) if file.endswith('.xml')]
    print(imglist)
    for img_id in imglist:
        img_id=img_id[:-4]
        convert_annotation(cls, num, img_id)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高高呀~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值