栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

一般VOC数据集样本格式

        train
        0013.jpg
        
            1215
            1098
            1
        
        0
    
            AE1
            Unspecified
            0
            0
            
                322
                92
                832
                577
            
        

Code

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

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')

生成结果


	
		AE1
		
			0
			1
			2
			3
		
	
	test0.xml
	
		1024
		712
		3
	

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269402.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号