将数据集转换为VOC格式

xml文件操作

方式一

 import sys
 import time
 import string
  
 from lxml import etree
 
 
 #设置默认字符集为UTF8 不然有些时候转码会出问题
 default_encoding = 'utf-8'
 if sys.getdefaultencoding() != default_encoding:
     reload(sys)
     sys.setdefaultencoding(default_encoding)
 
 def create_xml():
 
     data = etree.Element("data")
     #1 interface_version
     interface_version_txt = '5'
     interface_version = etree.SubElement(data, 'interface_version')
     interface_version.text = interface_version_txt
     #2 site
     site_txt = 'www.xxx.com'
     site = etree.SubElement(data, 'site')
     site.text = site_txt
     #3 lastmod
     lastmod_txt = time.strftime('%Y-%m-%d', time.localtime())
     lastmod = etree.SubElement(data, 'lastmod')
     lastmod.text = lastmod_txt
     #5 app
     app = etree.SubElement(data, 'app')
     #6 title 
     title_txt = u'%s' % '真心话大冒险'
     title_txt = etree.CDATA(title_txt)
     title = etree.SubElement(app, 'title')
     title.text = title_txt
     #7 appid
     appid = etree.SubElement(app, 'appid')
     appid.text = '%s' % '222'
 
     dataxml = etree.tostring(data, pretty_print=True, encoding="UTF-8", method="xml", xml_declaration=True, standalone=None)
     print dataxml

方式二

from xml.dom.minidom import Document

    doc = Document()  # 创建DOM文档对象

    bookstore = doc.createElement('bookstore')  # 创建根元素

    doc.appendChild(bookstore)

    ############book:Python处理XML之Minidom################

    book = doc.createElement('book')

    book.setAttribute('genre', 'XML')

    bookstore.appendChild(book)

    title = doc.createElement('title')

    title_text = doc.createTextNode('Python处理XML之Minidom')  # 元素内容写入

    title.appendChild(title_text)

    book.appendChild(title)

    author = doc.createElement('author')

    book.appendChild(author)

    author_first_name = doc.createElement('first-name')

    author_last_name = doc.createElement('last-name')

    author_first_name_text = doc.createTextNode('张')

    author_last_name_text = doc.createTextNode('三')

    author.appendChild(author_first_name)

    author.appendChild(author_last_name)

    author_first_name.appendChild(author_first_name_text)

    author_last_name.appendChild(author_last_name_text)

    book.appendChild(author)

    price = doc.createElement('price')

    price_text = doc.createTextNode('28')

    price.appendChild(price_text)

    book.appendChild(price)

    f = open('bookstore.xml','w')

    f.write(doc.toprettyxml(indent = '\t'))
    # doc.writexml(f, addindent='\t', newl='\n', encoding='utf-8')
    f.close()

json 转 voc 数据集xml格式

这里主要采用上述方式二进行操作:

def make_voc(format="train"):
    label_path = "xxx"+format+"\labels\\"
    Annotations_path = "xxx\VOCdevkit\VOC2007\Annotations\\"
    txt_path = "xxx\VOCdevkit\VOC2007\ImageSets\Main\\"
    txt_file = open(txt_path+format+".txt","w")

    for file in os.listdir(label_path):
        txt_file.write(file[0:-5]+"\n")
        xml_file = Annotations_path + file[0:-5] + ".xml"
        doc = Document()
        annotation = doc.createElement("annotation")
        doc.appendChild(annotation)

        folder = doc.createElement("folder")
        folder_text = doc.createTextNode("VOC2007")
        folder.appendChild(folder_text)

        filename = doc.createElement("filename")
        filename_text = doc.createTextNode(file[0:-5] + ".png")
        filename.appendChild(filename_text)

        size = doc.createElement("size")
        annotation.appendChild(folder)
        annotation.appendChild(filename)
        annotation.appendChild(size)


        with open(label_path + file, 'r') as f:
            json_file = json.load(f)
            points = json_file["shapes"]
            width = json_file["imageWidth"]
            height = json_file["imageHeight"]

            size_w = doc.createElement("width")
            size.appendChild(size_w)
            size_w_text = doc.createTextNode(str(width))
            size_w.appendChild(size_w_text)

            size_h = doc.createElement("height")
            size.appendChild(size_h)
            size_h_text = doc.createTextNode(str(height))
            size_h.appendChild(size_h_text)

            depth = doc.createElement("depth")
            size.appendChild(depth)
            depth_text = doc.createTextNode("1")
            depth.appendChild(depth_text)

            for one_point_label in points:
                one_object = doc.createElement("object")
                annotation.appendChild(one_object)

                object_name = doc.createElement("name")
                object_name_text = doc.createTextNode("lgd")
                object_name.appendChild(object_name_text)
                one_object.appendChild(object_name)

                pose = doc.createElement("pose")
                pose.appendChild(doc.createTextNode("center"))
                truncated = doc.createElement("truncated")
                truncated.appendChild(doc.createTextNode("0"))
                difficult = doc.createElement("difficult")
                difficult.appendChild(doc.createTextNode("0"))
                one_object.appendChild(pose)
                one_object.appendChild(truncated)
                one_object.appendChild(difficult)


                bndbox = doc.createElement("bndbox")
                xmin = doc.createElement("xmin")
                ymin = doc.createElement("ymin")
                xmax = doc.createElement("xmax")
                ymax = doc.createElement("ymax")
                one_object.appendChild(bndbox)
                bndbox.appendChild(xmin)
                bndbox.appendChild(ymin)
                bndbox.appendChild(xmax)
                bndbox.appendChild(ymax)

                center = list(map(int, one_point_label["points"][0]))
                radius = 15
                x1 = center[0] - radius
                y1 = center[1] - radius
                x2 = center[0] + radius
                y2 = center[1] + radius

                xmin.appendChild(doc.createTextNode(str(x1)))
                ymin.appendChild(doc.createTextNode(str(y1)))
                xmax.appendChild(doc.createTextNode(str(x2)))
                ymax.appendChild(doc.createTextNode(str(y2)))

        f = open(xml_file, 'w')

        f.write(doc.toprettyxml(indent='\t'))
        # doc.writexml(f, addindent='\t', newl='\n', encoding='utf-8')
        f.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AICVer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值