VOCAnnotation.py:

# -*-coding:utf-8-*-
from lxml import etree
import os class VOCAnnotation(object):
def __init__(self, imageFileName, width, height):
annotation = etree.Element("annotation")
self.__newTextElement(annotation, "folder", "VOC2007")
self.__newTextElement(annotation, "filename", imageFileName) source = self.__newElement(annotation, "source")
self.__newTextElement(source, "database", "P&ID") size = self.__newElement(annotation, "size")
self.__newIntElement(size, "width", width)
self.__newIntElement(size, "height", height)
self.__newIntElement(size, "depth", 3) self.__newTextElement(annotation, "segmented", "0") self._annotation=annotation def __newElement(self, parent, name):
node = etree.SubElement(parent, name)
return node def __newTextElement(self, parent, name, text):
node = self.__newElement(parent, name)
node.text = text def __newIntElement(self, parent, name, num):
node = self.__newElement(parent, name)
node.text = "%d" % num def addBoundingBox(self,xmin,ymin,xmax,ymax,name):
object=self.__newElement(self._annotation,"object") self.__newTextElement(object,"name",name)
self.__newTextElement(object,"pose","Unspecified")
self.__newTextElement(object, "truncated", "0")
self.__newTextElement(object, "difficult", "0") bndbox = self.__newElement(object, "bndbox")
self.__newIntElement(bndbox, "xmin", xmin)
self.__newIntElement(bndbox, "ymin", ymin)
self.__newIntElement(bndbox, "xmax", xmax)
self.__newIntElement(bndbox, "ymax", ymax) def save(self, saveFileName):
tree = etree.ElementTree(self._annotation)
tree.write(saveFileName, pretty_print=True)

  

Test.py:

#-*-coding:utf-8-*-
import VOCAnnotation
voc=VOCAnnotation.VOCAnnotation("2.png",100,100)
voc.addBoundingBox(1,2,3,4,"a")
voc.save("test.xml")

  

python生成VOC2007的类库的更多相关文章

  1. Python生成PASCAL VOC格式的xml标注文件

    Python生成PASCAL VOC格式的xml标注文件 PASCAL VOC数据集的标注文件是xml格式的.对于py-faster-rcnn,通常以下示例的字段是合适的: <annotatio ...

  2. JavaScript 解析 Django Python 生成的 datetime 数据 时区问题解决

    JavaScript 解析 Django/Python 生成的 datetime 数据 当Web后台使用Django时,后台生成的时间数据类型就是Python类型的. 项目需要将几个时间存储到数据库中 ...

  3. 【python】【转】Python生成随机数的方法

    如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文 ...

  4. Python生成随机数的方法

    这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与 ...

  5. Python 生成的页面中文乱码问题

    第一 保证 程序源文件里的中文的编码格式,如我们把 源文件的编码设置成utf8的. reload(sys) sys.setdefaultencoding('utf-8') 第二, 告诉浏览器,我们须要 ...

  6. 如何使用python生成xml

    最近要用python生成一个xml格式的文件.因为有一些内容是中文,原来的xml文件中使用了CDATA 的部分. 而之前的python程序所用的库中没有 创建这个区域的方法.无奈研究了大半天. 最后用 ...

  7. python生成随机图形验证码

    使用python生成随机图片验证码,需要使用pillow模块 1.安装pillow模块 pip install pillow 2.pillow模块的基本使用 1.创建图片 from PIL impor ...

  8. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  9. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

随机推荐

  1. idea 提交拉取代码,解决冲突

    继上两篇文章,本篇重点.所用的都是项目实际操作 提交代码 新建文件提交代码 idea自动提醒你是否加入到本地缓存(点击add就是添加如果不添加提交不上去事后需要手动提交 ps:快捷键是ctrl+alt ...

  2. 解决asp.net web api时间datetime自动带上带上的T和毫秒的问题

    今天用asp.net web api写微信小程序的接口时遇到一个问题. 返回的model中的datetime类型的字段自动转换成了“2014-11-08T01:50:06:234”这样的字符串,带上的 ...

  3. Activiti业务键(businessKey)

    问题:如何让业务对象和对应的流程 关联? 发现ProcessInstance 有个方法getBusinessKey()可以得到一个businessKey. ProcessInstance 对应数据库中 ...

  4. C++头文件记得加#pragma once

    C++头文件记得加#pragma once不然可能会导致重定义类

  5. SecureCRT 64位 破解版和安装,以及解决乱码问题

    链接:https://pan.baidu.com/s/1q1DEmohK7ISNJ7UbJkN3jw 提取码:yea3 复制这段内容后打开百度网盘手机App,操作更方便哦 securecrt 破解版是 ...

  6. asp.net去除HTML标签

    public string NoHTML(string Htmlstring) //替换HTML标记 { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @ ...

  7. sql中on的连接条件与where的区别

    left join [表名] on [条件] where [条件] --on表示连接条件 --where表示对结果的过滤条件 两者不尽相同,使用时需注意 例如: select * from  tabl ...

  8. Config程序配置文件(configSections)操作实践及代码详注

    所有与配置文件相关的类:(粗体为一般情况下使用到的类,其它类功能可能在很复杂的情况下才使用到.) 1.ConfigurationManager,这个提供用于打开客户端应用程序集的Configurati ...

  9. Lydsy2017省队十连测

    5215: [Lydsy2017省队十连测]商店购物 可能FFT学傻了,第一反应是前面300*300背包,后面FFT... 实际上前面背包,后面组合数即可.只是这是一道卡常题,需要注意常数.. //A ...

  10. 洛谷P2526 【SHOI2001】小狗散步

    原题传送门 题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从 ...