使用python生成VOC类型数据集样本

voc和coco是常用的数据集,它们有各自的dom结构,本文介绍使用python实现一个voc样本生成类,用于批量将非voc格式样本转化为voc格式

一般VOC数据集样本格式

<annotation>
        <folder>train</folder>
        <filename>0013.jpg</filename>
        <size>
            <width>1215</width>
            <height>1098</height>
            <depth>1</depth>
        </size>
        <segmented>0</segmented>
    <object>
            <name>AE1</name>
            <pose>Unspecified</pose>
            <truncated>0</truncated>
            <difficult>0</difficult>
            <bndbox>
                <xmin>322</xmin>
                <ymin>92</ymin>
                <xmax>832</xmax>
                <ymax>577</ymax>
            </bndbox>
        </object>
</annotation>

Code

该类目前适配PaddleDetection VOC格式,部分VOC格式字段有需要可自行添加
paddlevoc格式

from xml.dom import minidom


class VOC_Sample_Generator:

    def __init__(self):
        self.dom = minidom.Document()
        self.root_node = self.dom.createElement('annotation')
        self.object_node = self.dom.createElement('object')
        self.root_node.appendChild(self.object_node)

    def add_filename(self, filename):
        text = self.dom.createTextNode(filename)
        filename_node = self.dom.createElement('filename')
        filename_node.appendChild(text)
        self.root_node.appendChild(filename_node)

    def add_class(self, class_name):
        text = self.dom.createTextNode(class_name)
        name_node = self.dom.createElement('name')
        name_node.appendChild(text)
        self.object_node.appendChild(name_node)

    def add_size(self, width, height, depth):
        text = self.dom.createTextNode(str(width))
        width_node = self.dom.createElement('width')
        width_node.appendChild(text)

        text = self.dom.createTextNode(str(height))
        height_node = self.dom.createElement('height')
        height_node.appendChild(text)

        text = self.dom.createTextNode(str(depth))
        depth_node = self.dom.createElement('depth')
        depth_node.appendChild(text)

        size_node = self.dom.createElement('size')

        size_node.appendChild(width_node)
        size_node.appendChild(height_node)
        size_node.appendChild(depth_node)
        self.root_node.appendChild(size_node)


    def add_bndbox(self, xmin, ymin, xmax, ymax):
        text = self.dom.createTextNode(str(xmin))
        xmin_node = self.dom.createElement('xmin')
        xmin_node.appendChild(text)

        text = self.dom.createTextNode(str(ymin))
        ymin_node = self.dom.createElement('ymin')
        ymin_node.appendChild(text)

        text = self.dom.createTextNode(str(xmax))
        xmax_node = self.dom.createElement('xmax')
        xmax_node.appendChild(text)

        text = self.dom.createTextNode(str(ymax))
        ymax_node = self.dom.createElement('ymax')
        ymax_node.appendChild(text)

        bndbox_node = self.dom.createElement('bndbox')

        bndbox_node.appendChild(xmin_node)
        bndbox_node.appendChild(ymin_node)
        bndbox_node.appendChild(xmax_node)
        bndbox_node.appendChild(ymax_node)
        self.object_node.appendChild(bndbox_node)

    def build(self, path):
        self.dom.appendChild(self.root_node)
        with open(path, 'w') as f:
            self.dom.writexml(f, indent='', addindent='\t', newl='\n', encoding='UTF-8')
        f.close()

# 测试
if __name__ == "__main__":
    for i in range(2):
        voc = VOC_Sample_Generator()
        voc.add_filename('test' + str(i) + '.xml')
        voc.add_size(1024, 712, 3)
        voc.add_class('AE1')
        voc.add_bndbox(0, 1, 2, 3)
        voc.build('.\\test' + str(i) + '.xml')

生成结果

<?xml version="1.0" encoding="UTF-8"?>
<annotation>
	<object>
		<name>AE1</name>
		<bndbox>
			<xmin>0</xmin>
			<ymin>1</ymin>
			<xmax>2</xmax>
			<ymax>3</ymax>
		</bndbox>
	</object>
	<filename>test0.xml</filename>
	<size>
		<width>1024</width>
		<height>712</height>
		<depth>3</depth>
	</size>
</annotation>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alex-Leung

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

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

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

打赏作者

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

抵扣说明:

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

余额充值