通过固定格式的DataFrame来创建VOC格式XML
来源:互联网 发布:测量编程视频教学 编辑:程序博客网 时间:2024/06/05 22:44
通过固定格式的DataFrame来创建VOC格式XML
- 传入BBOX的DataFrame格式如下
- 直接把一整个dataframe传给ResultToXML.py中的函数divclass就可以了,生成的xml文件放在了ResultToXML.py所在目录中的result文件夹中(这个文件夹需要提前创建),一个图片的所有object都放在一起。
# -*- coding: utf-8 -*-from xml.dom.minidom import Documentimport osimport pandasdef divclass(dataframe): c=[] for a in dataframe['file']: c.append(a) c=list(set(c)) #得到去重的name for name in c: dataclass = [] for i in range(0,dataframe.__len__()): if dataframe.iloc[i]['file']==name: dataclass.append(dataframe.iloc[i]) # for j in range(0,dataclass.__len__()): data=pandas.DataFrame(dataclass) result_to_xml(data,name) def result_to_xml(dataframe,name): #传进来一个DataFrame #根据dataframe赋值 # name=dataframe.iloc[0]['file'] # 创建根节点 doc1 = Document() annotation = doc1.createElement('annotation') # 将根节点插入dom树 doc1.appendChild(annotation) folder = doc1.createElement('folder') # 文件夹 folder_text = doc1.createTextNode('VOC2017') folder.appendChild(folder_text) filename = doc1.createElement('filename') # 文件名 filename_text = doc1.createTextNode(str(name)) filename.appendChild(filename_text) # source=doc1.createElement('source') #资源 size = doc1.createElement('size') #大小 # owner=doc1.createElement('owner') # 拥有者 annotation.appendChild(folder) annotation.appendChild(filename) annotation.appendChild(size) #写入obj for i in range(0,dataframe.__len__()): classname=dataframe.iloc[i]['c'] possibility=dataframe.iloc[i]['prob'] xmin=dataframe.iloc[i]['left'] ymin=dataframe.iloc[i]['bottom'] xmax=dataframe.iloc[i]['right'] ymax=dataframe.iloc[i]['top'] obj = write_xml_object(classname,str(possibility), str(xmin), str(ymin), str(xmax), str(ymax)) # object annotation.appendChild(obj) # xml拼接完成 width = doc1.createElement('width') width_text = doc1.createTextNode('1024') width.appendChild(width_text) height = doc1.createElement('height') height_text = doc1.createTextNode('1024') height.appendChild(height_text) depth = doc1.createElement('depth') depth_text = doc1.createTextNode('1') depth.appendChild(depth_text) size.appendChild(width) size.appendChild(height) size.appendChild(depth) with open('result\\'+name.split('.')[0] + '.xml', 'w') as f: doc1.writexml(f, indent='\t', addindent='\t', newl='\n', encoding="utf-8")def write_xml_object(classname,prob,Xmin,Ymin,Xmax,Ymax): doc = Document() object = doc.createElement('object') doc.appendChild(object) name = doc.createElement('name') name_text = doc.createTextNode(classname) # 物体类别 name.appendChild(name_text) pose = doc.createElement('pose') pose_text = doc.createTextNode('Right') # 拍摄角度 pose.appendChild(pose_text) truncated = doc.createElement('truncated') truncated_text = doc.createTextNode('0') # 是否被截断 truncated.appendChild(truncated_text) difficult = doc.createElement('difficult') difficult_text = doc.createTextNode('0') # 目标是否完整 difficult.appendChild(difficult_text) bndbox = doc.createElement('bndbox') #bndbox possibility=doc.createElement('possibility')#类别概率 possibility_text=doc.createTextNode(str(prob)) possibility.appendChild(possibility_text) object.appendChild(name) object.appendChild(pose) object.appendChild(truncated) object.appendChild(difficult) object.appendChild(bndbox) object.appendChild(possibility) xmin = doc.createElement('xmin') xmin_text = doc.createTextNode(str(Xmin)) xmin.appendChild(xmin_text) ymin = doc.createElement('ymin') ymin_text = doc.createTextNode(Ymin) ymin.appendChild(ymin_text) xmax = doc.createElement('xmax') xmax_text = doc.createTextNode(Xmax) xmax.appendChild(xmax_text) ymax = doc.createElement('ymax') ymax_text = doc.createTextNode(Ymax) ymax.appendChild(ymax_text) bndbox.appendChild(xmin) bndbox.appendChild(ymin) bndbox.appendChild(xmax) bndbox.appendChild(ymax) return object
阅读全文
0 0
- 通过固定格式的DataFrame来创建VOC格式XML
- Python生成PASCAL VOC格式的xml标注文件
- VOC框图工具,自动生成需要的XML格式
- Python生成PASCAL VOC格式的xml标注文件
- Python生成PASCAL VOC格式的xml标注文件
- PASCAL Annotation转换为VOC xml格式数据
- 输出固定的格式
- struts2的struts.xml文件的固定格式
- struts2的struts.xml文件的固定格式
- java生成固定格式的XML文件,用于报表
- VOC数据集具体格式
- VOC数据集具体格式
- PASCAL VOC 数据集格式
- 使用rome创建RSS格式的xml
- java创建xml格式的字符串
- JS 通过固定格式读取文本内容
- RCNN系列实验的PASCAL VOC数据集格式设置
- YOLOv2训练自己的数据集(VOC格式)
- 使用gdal库拆分字符串
- 20. 递归和IO流(字节流和高效字节流)
- leetcode
- Java实现的软键盘并可以向Windows任意窗口发送中/英文信息
- 2017年浙江工业大学大学生程序设计迎新赛热身赛-E:递推(矩阵快速幂)
- 通过固定格式的DataFrame来创建VOC格式XML
- MS8413 光纤同轴解码 soundbar方案
- 最近了解到的知识点
- 搭建并配置splunk
- jquery 操作 checkbox 全选、反选、清除
- 扫盲一: 加密, 认证, 公钥,私钥,证书
- 安装realsense ros包时找不到librealsenseconfig.cmake 问题解决
- Python学习3:字符串操作
- caffe之solver.prototxt文件参数设置