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')  # []
    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)

你可能感兴趣的:(voc格式 从xml中得到所有类别对象的样本数)