统计xml不同种类样本个数

import xml.etree.ElementTree as ET
import os

TRAIN_STATISTICS = {
    'none': [0, 0],
    '1': [0, 0],
    '2': [0, 0]
}

all_filepath = []
for dirpath, dirname, filepath in os.walk(r"C:\Users\ADMIN\Desktop\xml_to_voc\datasets\Annotations"):
    for one_file in filepath:
        one_file_path = os.path.join(dirpath, one_file)
        all_filepath.append(one_file_path)

def _process_image(path):
    for i in path:
        # Read the XML annotation file.
        # 3. 构造xml文件路径
        filename = i
        # 4. xml文件解析
        tree = ET.parse(filename)
        # a. 得到xml文件对应的根节点
        root = tree.getroot()
        # 有几种类别就设置几种FLAG
        FLAG1 = False
        FLAG2 = False
        for obj in root.findall('object'):
            label = obj.find('name').text
            TRAIN_STATISTICS[label][1] += 1
            # 有几种类别就设置几种FLAG
            if label == "1":
                FLAG1 = True
            if label == "2":
                FLAG2 = True

        if FLAG1 == True:
            TRAIN_STATISTICS["1"][0] += 1
        if FLAG2 == True:
            TRAIN_STATISTICS["2"][0] += 1

_process_image(all_filepath)
print(TRAIN_STATISTICS)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值