python根据xml统计各类型个数

参考链接:

http://t.csdnimg.cn/KIICz

两种方法索引XML中关键字都可以。

# 根据xml统计类型个数
import os
import xml.dom.minidom
import xml.etree.ElementTree as ET

def count_class(AnnoPath, cls):
    Annolist = os.listdir(AnnoPath)
    total_count = 0
    for annotation in Annolist:
        filename = os.path.join(AnnoPath,annotation)
        dom = xml.dom.minidom.parse(filename) # 打开XML文件
        collection = dom.documentElement # 获取元素对象
        objectlist = collection.getElementsByTagName('name') # 获取标签名为ad的信息
        count = 0
        for ob in objectlist:
            if ob.firstChild.data == cls: #获取第一个节点数据,因为name标签只有一个数据,所以获取的就是某个类别
                count += 1
        total_count += count
    print(cls, ":" ,total_count)


def count_class_withsize(AnnoPath, cls, small, medium):
    Annolist = os.listdir(AnnoPath)
    total_count = 0
    total_large_count = 0
    total_medium_count = 0
    total_small_count = 0

    for annotation in Annolist:
        
        large_count = 0
        medium_count = 0
        small_count = 0
        

        filename = os.path.join(AnnoPath,annotation)
        tree = ET.parse(filename)
        root = tree.getroot()
        size = root.find('size')
        W = int(size.find('width').text)
        H = int(size.find('height').text)
        

        for obj in root.iter('object'):
        
            xml_cls = obj.find('name').text
            if cls  == xml_cls :
                
                xmlbox = obj.find('bndbox')
                box = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
                    float(xmlbox.find('ymax').text))

                ratio = cal_size((W, H), box)
                
                if 0 <= ratio <= small:
                    small_count += 1
                if small < ratio <= medium:
                    medium_count += 1
                if medium < ratio <= 1:
                    large_count +=1

        total_large_count += large_count
        total_medium_count += medium_count
        total_small_count += small_count
        total_count += small_count + medium_count + large_count
        
    print(cls, ":" , total_count)
    print(cls, "total_large_count:" , total_large_count)
    print(cls, "total_medium_count:" ,total_medium_count)
    print(cls, "total_small_count:" , total_small_count)
    
        

def cal_size(Size, box):
    size_area =  Size[0] * Size[1]
    box_area = (box[1] - box[0]) * (box[3] - box[2])
    ratio = box_area / size_area
    return ratio

if __name__ == '__main__':

    classes = ["1","2","3","4","5","6","7"]  
    AnnoPath = 'D:/folder/'

    # for cls in classes:
    #     count_class(AnnoPath, cls)
    
    # 类型数量 及 按大中小统计
    # 0.15 < large <= 1
    medium = 0.15 #  0.05 < medium <= 0.15
    small = 0.05 #  0 <= small <= 0.05
    for cls in classes:
        count_class_withsize(AnnoPath, cls, small, medium)
        print("\n")


  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种非常流行的编程语言,有许多常用的库可以帮助你进行各种任务。以下是一些常用的Python库: 1. **NumPy**:NumPy是Python的一个库,主要用于进行大型多维数组和矩阵的操作。它提供了一些函数,用于进行数学运算,线性代数运算,统计操作等。 2. **Pandas**:Pandas是一个用于数据操作和分析的库。它提供了一种类似于SQL的查询语言来访问数据,并且可以轻松地进行数据清洗和分析。 3. **Matplotlib**:Matplotlib是一个用于创建静态、动态、交互式图表的库。它可以帮助你创建各种类型的图表,包括折线图、柱状图、散点图等。 4. **Scikit-learn**:Scikit-learn是一个用于机器学习的库,提供了许多用于分类、回归、聚类等任务的算法。 5. **TensorFlow**:TensorFlow是一个用于深度学习的库,它提供了一种高级API来创建和训练神经网络。 6. **PyTorch**:PyTorch也是一个用于深度学习的库,它提供了一种更加灵活和易于使用的API来创建和训练神经网络。 7. **Flask**:Flask是一个轻量级的Web框架,可以帮助你快速构建Web应用程序。 8. **Django**:Django是一个更全面的Web框架,提供了许多内置的功能和工具,可以帮助你更轻松地构建大型Web应用程序。 9. **Requests**:Requests是一个用于发送HTTP请求的库,它可以帮助你从Web上获取数据或发送数据到Web上。 10. **BeautifulSoup**:BeautifulSoup是一个用于解析HTML和XML文档的库,可以帮助你从网页上提取数据。 以上是一些常用的Python库,它们在数据处理、数据分析、机器学习、Web开发等领域都有广泛的应用。还有很多其他的库和工具可供选择,具体选择哪个库取决于你的具体需求和技能水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值