voc bbox的xml数据转换成txt格式

代码

# -*- coding: utf-8 -*-

try:
    import xml.etree.cElementTree as ET 
except ImportError:
    import xml.etree.ElementTree as ET
import os

def GetAnnotBoxLoc(AnotPath):
    tree = ET.ElementTree(file=AnotPath)  
    root = tree.getroot()
    ObjSize=root.findall('size')
    assert(len(ObjSize)==1)
    fwidth =  float(ObjSize[0].find("width").text)
    fheight = float(ObjSize[0].find("height").text)
    ObjectSet=root.findall('object')
    ObjBndBoxSet={} 
    for Object in ObjectSet:
        ObjName=Object.find('name').text
        BndBox=Object.find('bndbox')
        x1 = float(BndBox.find('xmin').text)#-1 
        y1 = float(BndBox.find('ymin').text)#-1
        x2 = float(BndBox.find('xmax').text)#-1
        y2 = float(BndBox.find('ymax').text)#-1
        BndBoxLoc=[x1/fwidth,y1/fheight,x2/fwidth,y2/fheight]
        if ObjBndBoxSet.has_key(ObjName):
        	ObjBndBoxSet[ObjName].append(BndBoxLoc)
        else:
        	ObjBndBoxSet[ObjName]=[BndBoxLoc]
    return ObjBndBoxSet

def ConvertXml2Txt(fileInput,fileOutput,name2Lable):
    objBndBoxSet = GetAnnotBoxLoc(fileInput) 
    fp = open(fileOutput,"w+")
    for objk in objBndBoxSet:
        label = name2Lable[objk]
        objv = objBndBoxSet[objk]
        for v in objv:
            fp.writelines("%d %.6f %.6f %.6f %.6f\n"%(label,v[0],v[1],v[2],v[3]))
    fp.close()

# main
if __name__== '__main__':

    path = r"../voc/Annotations"
    name2Lable = {"aeroplane": 1,"bicycle": 2,"bird": 3,"boat": 4,"bottle": 5,"bus": 6,"car": 7,"cat": 8,"chair": 9,"cow": 10,
                "diningtable": 11,"dog": 12,"horse": 13,"motorbike":14,"person": 15,"pottedplant": 16,"sheep": 17,"sofa": 18,"train": 19,"tvmonitor": 20}

    for root,dirs,files in os.walk(path):
        for file in files:
            splitSeg = file.split('.')
            if splitSeg[len(splitSeg)-1] == "xml":
                fileInput = os.path.join(root,file)
                fileOutput = os.path.join(root,file.replace('xml','txt'))
                print("Process %s"%(fileInput))
                ConvertXml2Txt(fileInput,fileOutput,name2Lable)
                

输出

//2007_000346.txt
15 0.274000 0.208000 0.994000 1.000000
15 0.178000 0.538667 0.258000 0.658667
15 0.144000 0.557333 0.222000 0.690667
5 0.248000 0.285333 0.460000 0.914667

参考
核心代码参考 https://blog.csdn.net/qq_38109843/article/details/85323777

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值