Python 生成VOC格式的标签

常用目标检测模型基本都是读取的PASCAL VOC格式的标签,下面代码用于生成VOC格式的代码,根据需要修改即可:

from lxml import etree, objectify

def gen_txt(filename, h, w, c):
    E = objectify.ElementMaker(annotate=False)
    anno_tree = E.annotation(
        E.folder('VOC_OPEN_IMAGE'),
        E.filename(filename),
        E.source(
            E.database('The VOC2007 Database'),
            E.annotation('PASCAL VOC2007'),
            E.image('flickr'),
            E.flickrid("341012865")
        ),
        E.size(
            E.width(w),
            E.height(h),
            E.depth(c)
        ),
        E.segmented(0),
        E.object(
            E.name('1'),
            E.pose('left'),
            E.truncated('1'),
            E.difficult('0'),
            E.bndbox(
                E.xmin('0'),
                E.ymin('0'),
                E.xmax('0'),
                E.ymax('0')
            )
        ),
    )
    etree.ElementTree(anno_tree).write('ann/'+filename[:-4]+".xml", pretty_print=True)

你可能感兴趣的:(人工智能)