python生成VOC2007的xml代码
来源:互联网 发布:php post 源代码 编辑:程序博客网 时间:2024/05/29 17:08
之前在网上下了一份matlab版本的代码,在遇到中文路径写入时,使用gVim打开出现了乱码,弄了一下午不知道怎么回事,后来发现好像是gVim环境配置没有写支持'utf-8'导致,纠结了一下午。。。
后来索性自己用python写了一个,代码粗糙,欢迎大家提意见
# !/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'xiaobanderui'from xml.dom import minidomimport numpy as npimport cv2imgpath = u'e:/VOC2007/JPEGImages/' # 图片存放路径txtpath = u'e:/data.txt' # 保存标注信息的文件,每一行的内容为[filename objectname xmin ymin xmax ymax]xml_path = u'e:/VOC2007/Annotations/'lastname = u'begin'foldername = u'VOC2007'num = 0 # 总样本数valid_num = 0 # 有效样本数cv2.namedWindow('img', cv2.WINDOW_NORMAL)with open(txtpath, 'r') as fr: for item in fr.readlines(): num += 1 print 'processing %d' % num item = item.strip().decode('gbk') str_item = item.split(' ') img =cv2.imread((imgpath + str_item[0]).encode('gbk'), cv2.IMREAD_UNCHANGED) ht. wd. cn = img.shape xmin = int(float(str_item[2])) xmax = int(float(str_item[3])) ymin = int(float(str_item[4])) ymax = int(float(str_item[5])) cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2) cv2.imshow('img', img) cv2.waitKey() # 过滤掉标注信息错误的样本 # 框的位置越界 if xmin >= xmax or ymin >= ymax or xmin < 0 or ymin < 0 or xmax < 0 or ymax < 0 or xmin >= wd or xmax >= wd or ymin >= ht or ymax >= ht continue # 框的位置不对,比如翻转时 oldx1 =xmin oldx2 = xmax newxmin = wd - oldx2 - 1 newxmax = wd - oldx1 - 1 if newxmin >= newxmax continue valid_num += 1 if str_item[0] == last_name: object_node = doc.createElem('object') rootNode = doc.appendChild(object_node) node = doc.createElem('name') node_text = doc.createTextNode(str_item[1]) node.appendChild(node_text) object_ode = doc.appendChild(node) ''' 其他类似添加,不再赘述 ''' else: if vars().has_key('rootNode'): fw_xml = open(xml_path + file_full_name[0] + '.xml', 'w') fw_xml.write(doc.toprettyxml(encoding='utf-8')) fw_xml.close() node = doc.createElem('name') node_text = doc.createTextNode(str_item[1]) node.appendChild(node_text) object_ode = doc.appendChild(node) fw_xml = open(xml_path + file_full_name[0] + '.xml', 'w') fw_xml.write(doc.toprettyxml(encoding='utf-8')) fw_xml.close()
1 1
- python生成VOC2007的xml代码
- txt版本的标签转换成PASCAL VOC2007 格式的XML标签
- 生成xml文档的函数代码
- VOC2007数据集的制作
- Faster RCNN 训练自己的数据集(Matlab,python版本)及制作VOC2007格式数据集
- 用faster-rcnn训练自己的数据集(VOC2007格式,python版)
- 用faster-rcnn训练自己的数据集(VOC2007格式,python版)
- 代码生成XML
- 生成XMl文档代码
- 代码生成XML
- 用python生成xml
- Python XML生成
- python生成xml文件
- python 生成xml文件
- python 生成xml
- 利用 Python 做简单的代码生成
- python写的代码生成工具
- python 代码生成邮件
- mybatis中常用的标签
- python读取文件并上传数据库
- 洛谷 P1629 邮递员送信
- log4j配置
- HDU 3622 Bomb Game(2-SAT)
- python生成VOC2007的xml代码
- Duplicate files copied in APK META-INF/notice.txt错误的解决办法
- MyBatis Like模糊查询参考解决策略
- std::ostringstream用法
- linux ffmpeg开发环境搭建(基于ubuntu14.04和ffmpeg3.2)
- SQL中变量用String.Format
- [生存志] 第35节 后羿弯弓射九日
- 【职坐标】高级特性之内部类简介
- C#预处理器指令 用法