Python根据指定⽂件⽣成XML的⽅法
因项⽬需要根据指定格式的⽂件⽣成XML标注⽂件,可以⽅便使⽤LabelImg打开进⾏编辑和查看。其原始⽂件默认使⽤逗号进⾏分隔,如下所⽰:
第1个值:原始图⽚中切图⼩⽂件,以AIpng_x,其中x代表原始图⽚的第⼏个切图⽂件
第2~5值:分别对应于ymin, xmin, ymax, xmax
第6个值:代表对应的标签标注
在⽣成XML⽂件时,需要对其进⾏汇总,即将属于同⼀个原始⽂件的切图⼩⽂件的标注汇总到⼀起,其实现代码如下所⽰:
import os
from Logger import MyLogger
from xml.dom.minidom import Document
from collections import defaultdict
import re
class OpeateXML:
def __init__(self, srcPath: str, targetPath: str, srcFileName: str):
self._srcPath = srcPath
self._targetPath = targetPath
self._srcFileName = srcFileName
def readSrcFileName(self, fileEncoding="utf8") -> dict:
data = defaultdict(list)
s = repile("\.AIpng_\d{1,}", re.IGNORECASE)
srcFileFullPath = os.path.join(self._srcPath, self._srcFileName)
try:
with open(srcFileFullPath, mode="r", encoding=fileEncoding, errors="ignore") as fr:
for content adlines():
data[s.sub(".AIpng",content.strip().split(",")[0])].append(content.strip())
except Exception as ex:
MyLogger().error(f"OperateXML:read file error:\n{ex}")
return {}
else:
# data.sort(key=lambda x: x.strip().split(",")[0])
return data
def createXML(self, data: dict, fileEncoding="utf8"):
if data:
try:
for k,v in data.items():
doc = Document()
# 创建根节点
rootNode = ateElement("annotation")
# 添加根节点
doc.appendChild(rootNode)
folder = ateElement("folder")
folderText = ateTextNode(self._targetPath)
folder.appendChild(folderText)
rootNode.appendChild(folder)
python处理xml文件filename = ateElement("filename")
filenameText = ateTextNode(k)
filename.appendChild(filenameText)
rootNode.appendChild(filename)
path = ateElement("path")
pathText = ateTextNode(os.path.join(self._targetPath,k))
path.appendChild(pathText)
rootNode.appendChild(path)
for i in v:
tmpData = i.strip().split(",")
if len(tmpData) == 6:
_, ymin, xmin, ymax, xmax, labelName = tmpData
objectObj = ateElement("object")
rootNode.appendChild(objectObj)
objectName = ateElement("name")
objectNameText = ateTextNode(labelName)
objectName.appendChild(objectNameText)
objectObj.appendChild(objectName)
objectBndBox = ateElement("bndbox")
objectObj.appendChild(objectBndBox)
objectBndBoxXmin = ateElement("xmin")
objectBndBoxYmin = ateElement("ymin")
objectBndBoxXmax = ateElement("xmax")
objectBndBoxYmax = ateElement("ymax")
objectBndBoxXminText = ateTextNode(xmin)
objectBndBoxYminText = ateTextNode(ymin)
objectBndBoxXmaxText = ateTextNode(xmax)
objectBndBoxYmaxText = ateTextNode(ymax)
objectBndBox.appendChild(objectBndBoxXmin)
objectBndBox.appendChild(objectBndBoxYmin)
objectBndBox.appendChild(objectBndBoxXmax)
objectBndBox.appendChild(objectBndBoxYmax)
objectBndBoxXmin.appendChild(objectBndBoxXminText)
objectBndBoxYmin.appendChild(objectBndBoxYminText)
objectBndBoxXmax.appendChild(objectBndBoxXmaxText)
objectBndBoxYmax.appendChild(objectBndBoxYmaxText)
objectObj.appendChild(objectBndBox)
else:
continue
# save xml
xmlName=os.path.splitext(k)[0]+".xml"
targetPath = os.path.join(self._targetPath, xmlName)
with open(targetPath, mode="w", encoding=fileEncoding) as fw:
doc.writexml(fw, indent="\t", newl="\n", addindent="\t", encoding=fileEncoding)
except Exception as ex:
MyLogger().error(f"OperateXML:Save xml error\n{ex}")
return
if __name__ == '__main__':
srcPath = r"C:\Users\Surpass\Documents\PycharmProjects\data\TEST-8\outs"
srcName = ""
targetPath = r"C:\Users\Surpass\Documents\PycharmProjects\data\TEST-8\outs\in_number"
operateXML = OpeateXML(srcPath, targetPath, srcName)
a = adSrcFileName()
最终⽣成的XML效果如下所⽰:
在LabelImg中的效果如下所⽰:
PS:这⾥再为⼤家提供⼏款关于xml操作的在线⼯具供⼤家参考使⽤:
对关于Python⽣成XML相关内容感兴趣的读者可查看本站专题:
以上就是Python根据指定⽂件⽣成XML的⽅法的详细内容,更多关于Python⽣成XML的资料请关注其它相关⽂章!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论