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. 18.scrapy_maitian_analysis

    1_info.py # encoding: utf-8 import pandas as pd # 租房 基本信息 # 读取文件 df=dataframe df = pd.read_json(&quo ...

  2. stop slave->reset slave->start slave 复制从哪个位置开始?reset slave all呢?

    reset slave首先来看下当前master-slave情况 mysql> prompt \u@\h,\p:\d>\_ PROMPT set to '\u@\h,\p:\d>\_ ...

  3. Input:type属性

    1.button:定义可点击的按钮(通常与 JavaScript 一起使用来启动脚本). <input id="" type="button" name= ...

  4. Asp.Net中的HttpWebRequest类与HttpWebResponse类

    相关博文:https://www.cnblogs.com/xu-yi/p/10061342.html 相关博文:https://www.cnblogs.com/zoujinhua/p/11313396 ...

  5. JS流程控制语句 重复重复(for循环)语句结构: for(初始化变量;循环条件;循环迭代) { 循环语句 }

    重复重复(for循环) 很多事情不只是做一次,要重复做.如打印10份试卷,每次打印一份,重复这个动作,直到打印完成.这些事情,我们使用循环语句来完成,循环语句,就是重复执行一段代码. for语句结构: ...

  6. 【JZOJ1259】牛棚安排

    description Farmer John的N(1<=N<=1000)头奶牛分别居住在农场所拥有的B(1<=B<=20)个牛棚的某一个里.有些奶牛很喜欢她们当前住的牛棚,而 ...

  7. 树形dp——cf1029E

    题解给出的是带log的,,我自己写了个on的.. #include<bits/stdc++.h> #include<vector> using namespace std; # ...

  8. Windows中的Tree命令

    Windows中的Tree命令你会用吗? - 步行者的专栏 - CSDN博客 https://blog.csdn.net/hantiannan/article/details/7663893 (ven ...

  9. C++中无数据成员的类的对象占用内存大小

    结论: 对于没有数据成员的对象,其内存单元也不是0,c++用一个内存单元来表示这个实例对象的存在. 如果有了数据或虚函数(虚析构函数),则相应的内存替代1标记自己的存在. PS:以下代码均在win32 ...

  10. PAT甲级——A1003Emergency

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...