xml文件操作

方式一

 import sysimport timeimport stringfrom 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_versioninterface_version_txt = '5'interface_version = etree.SubElement(data, 'interface_version')interface_version.text = interface_version_txt#2 sitesite_txt = 'www.xxx.com'site = etree.SubElement(data, 'site')site.text = site_txt#3 lastmodlastmod_txt = time.strftime('%Y-%m-%d', time.localtime())lastmod = etree.SubElement(data, 'lastmod')lastmod.text = lastmod_txt#5 appapp = 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 appidappid = 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 Documentdoc = 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 = 15x1 = center[0] - radiusy1 = center[1] - radiusx2 = center[0] + radiusy2 = center[1] + radiusxmin.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()

将数据集转换为VOC格式相关推荐

  1. IndexError: list index out of range coco数据集转换为voc格式出现的错误

    问题: 出现这个问题的地方: 解决办法: 成功!

  2. KITTI车辆检测数据集转VOC格式(亲测成功,附KITTI云盘连接以及完整格式转换代码)- KITTI车辆检测数据集看着一篇就够了!

    KIITI数据集转VOC格式 软件环境 一.准备数据集 1.1下载链接 1.2数据集使用方法 二.txt标注文件准换为VOC格式 2.1目前准换代码存在的问题 2.2博主提供的方法 2.3转换代码 2 ...

  3. 将widerface标注转换为VOC格式

    验证工具代码: https://github.com/wondervictor/WiderFace-Evaluation 将widerface标注转换为VOC格式 原文:https://blog.cs ...

  4. 将数据集转换为Excel格式的一个实现

    { 将数据集转换为Excel格式的一个实现 在做项目时,很多情况下,客户需要对我们保存在数据库中的数据再加工再利用, 如财务需要一份今年财务情况的电子报表,总经理需要今年销售情况的一个电子报表. 我们 ...

  5. 将fddb标注转换为VOC格式标注

    fddb是用来评估人脸检测性能事实上的标准,它给出了10折验证椭圆格式的标注,这和faster-rcnn以及SSD等的要求是不一样的,因此需要转换为VOC格式. 难点在于如何得到椭圆的外接矩形,我采用 ...

  6. mmdetection实战,训练扑克牌数据集(VOC格式)并测试计算mAP

    mmdetection实战,训练扑克牌数据集(VOC格式)并测试计算mAP 一.数据集准备 二.mmdetection的安装 三.修改相关文件 1. 修改class_names.py文件 2. 修改v ...

  7. 将车辆数据集kitti转换为VOC格式(车辆检测)

    Kitti数据集的下载只需要一个图片集(12G)和标注文件即可(data_object_image_2.zip.data_object_label_2.zip). 下载地址:http://datase ...

  8. 将Yolo格式标注文件转换为VOC格式

    这篇文章主要参考博客Yolo标准数据集格式转Voc数据集中的代码,对原博客代码进行一定修改.添加注释,此外还在后面添加了我自己写的一段关于对转换后的标注文件进行整理的脚本代码. Yolo标注的格式与V ...

  9. 将CityScapes数据集转换为COCO格式的实例分割数据集

    ##¥##2019年结束前最后两小时,写出本年度最后一篇博客,不是博主没地方去跨年,实在是Coding乐趣无穷ahhhh ##¥## COCO数据集是研究实例分割的通用数据集,但是针对道路环境的数据较 ...

最新文章

  1. Machine Learning | (8) Scikit-learn的分类器算法-随机森林(Random Forest)
  2. 2021重庆江北中学高考成绩查询,重庆江北中学校2021年排名
  3. SpannableString 设置一段文字中部分字体颜色
  4. pgsql 前10条_白沙湾南片区11条新建道路最新进度及建成时间,已建成一条!还有一条将通车...
  5. QT的QGLShaderProgram类的使用
  6. Native snappy library not available: this version of libhadoop was built without snappy support
  7. 三极管的“非教科书式”解析,什么正偏、反偏都统统滚蛋!
  8. python怎么做软件界面_python – 如何自定义桌面应用程序的标题栏和窗口
  9. 新记录 Core 2 Duo E8600 被超频到6.25GHz
  10. laravel 模型(2)
  11. linux fdisk 权限不够,Linux 文件系统结构、磁盘的管理
  12. 计算机病毒解析与防范结束语,2016年04月30日计算机病毒解析与防范题纲_向必青.doc...
  13. qthread run结束了算销毁吗_会计职称考试已结束,证书怎么领?
  14. java如何实现游戏暂停和恢复_Android:游戏循环暂停/恢复问题
  15. spring定时器时间表达式
  16. pgp各种弱类型绕过
  17. 计算机表格中的乘法怎么用,excel表格中怎么使用乘法公式
  18. SuperMap iObjects .NET开发ASP .NET网站入门
  19. C语言标准ANSI C、C语言的特点、C语言的关键字(32个)
  20. PDF文档免费转成Word文档,不限页数。

热门文章

  1. 视频字幕如何提取出来?这篇文章告诉你怎么提取
  2. B 站弹幕 protobuf 协议还原分析
  3. Graph Neural Networks: Graph Structure Learning
  4. 电极法测污水常规五参数(PH、电导率、溶解氧、温度、浊度)
  5. 2015百度校园招聘笔试
  6. 高级软件测试工程师的面试
  7. oracle闪回15分钟前,Oracle闪回技术(Flashback)
  8. 农产品质量溯源区块链方案
  9. Android端肤色检测
  10. 长江职业学院计算机应用技术代码,长江职业学院的学院代号和各个专业的代号...