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")
手机扫一扫
移动阅读更方便
你可能感兴趣的文章