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

      

  • 相关阅读:
    linux系统调整磁盘分区
    Linux命令sort和uniq 的基本使用
    路由的执行优先级
    Linux命令xargs的使用
    wget命令的使用
    Linux环境下错误码及意义总结
    Linux下使用ip netns命令进行网口的隔离和配置ip地址
    MongoDB初始化创建管理员账户登录
    Python字符串的截取原理,下标的位置图示
    Python操作MongoDB查询时处理ObjectId
  • 原文地址:https://www.cnblogs.com/tangmiao/p/9914025.html
Copyright © 2020-2023  润新知