zoukankan      html  css  js  c++  java
  • python生成VOC2007的类库

    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")
    

      

  • 相关阅读:
    java程序员裸机配置
    安装库
    自定义脚本模板
    Oracle数据库触发器简单案例
    Oracle数据库按正则切割字符串
    Oracle查询一张表的所有字段
    Oracle数据库系统表
    Oracle设置最大连接数
    Oracle博客参考教程
    区间dp [H
  • 原文地址:https://www.cnblogs.com/tangmiao/p/9914025.html
Copyright © 2011-2022 走看看