voc格式 从xml中得到所有类别对象的样本数

from __future__ import division
import os
from PIL import Image
import xml.dom.minidom
import numpy as np

AnnoPath = 'F:/code/transfor/VOC2007/Annotations/'

xmllist = os.listdir(AnnoPath)
num_plane =0
num_oil =0
for xmli in xmllist:
    xmlfile = AnnoPath + xmli[:-4] + '.xml'

    DomTree = xml.dom.minidom.parse(xmlfile)
    annotation = DomTree.documentElement

    filenamelist = annotation.getElementsByTagName('filename')  # [<DOM Element: filename at 0x381f788>]
    filename = filenamelist[0].childNodes[0].data
    objectlist = annotation.getElementsByTagName('object')

    i = 1
    for objects in objectlist:
        # print objects

        namelist = objects.getElementsByTagName('name')
        # print 'namelist:',namelist
        objectname = namelist[0].childNodes[0].data
        #print(objectname)
        if objectname=='plane':
            num_plane=num_plane+1
        else:num_oil = num_oil+1
print('num_plane=',num_plane,'num_oil=',num_oil)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容