构建自己的数据集,关于统计标签信息的问题

在构建自己的数据库时 ,往往需要对标签个数,类别进行统计。
已知数据集的某个图像的标签xml格式如下:

<annotation>
    <folder>dataset</folder>
    <filename>000.jpg</filename>
    <path>E:\dataset\000.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>4608</width>
        <height>3456</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>heating</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>2315</xmin>
            <ymin>1730</ymin>
            <xmax>2370</xmax>
            <ymax>1777</ymax>
        </bndbox>
    </object>
    <object>
        <name>flower stand</name>
        <pose>Unspecified</pose>
        <truncated>1</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1508</xmin>
            <ymin>2397</ymin>
            <xmax>2138</xmax>
            <ymax>3456</ymax>
        </bndbox>
    </object>
    <object>
        <name>flower stand</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>2005</xmin>
            <ymin>2135</ymin>
            <xmax>2295</xmax>
            <ymax>3006</ymax>
        </bndbox>
    </object>
    <object>
        <name>garbage can</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>2778</xmin>
            <ymin>1853</ymin>
            <xmax>2844</xmax>
            <ymax>1995</ymax>
        </bndbox>
    </object>
    <object>
        <name>storage room</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>3473</xmin>
            <ymin>1611</ymin>
            <xmax>3576</xmax>
            <ymax>2392</ymax>
        </bndbox>
    </object>
    <object>
        <name>storage room</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>3703</xmin>
            <ymin>1588</ymin>
            <xmax>3874</xmax>
            <ymax>2532</ymax>
        </bndbox>
    </object>
    <object>
        <name>storage room</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>4131</xmin>
            <ymin>1537</ymin>
            <xmax>4469</xmax>
            <ymax>2905</ymax>
        </bndbox>
    </object>
    <object>
        <name>laboratory</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>3032</xmin>
            <ymin>1247</ymin>
            <xmax>3142</xmax>
            <ymax>2139</ymax>
        </bndbox>
    </object>
    <object>
        <name>laboratory</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>2671</xmin>
            <ymin>1483</ymin>
            <xmax>2710</xmax>
            <ymax>1893</ymax>
        </bndbox>
    </object>
    <object>
        <name>laboratory</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>2043</xmin>
            <ymin>909</ymin>
            <xmax>2129</xmax>
            <ymax>2153</ymax>
        </bndbox>
    </object>
    <object>
        <name>laboratory</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>2577</xmin>
            <ymin>1596</ymin>
            <xmax>2631</xmax>
            <ymax>1877</ymax>
        </bndbox>
    </object>
    <object>
        <name>window</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>1</difficult>
        <bndbox>
            <xmin>2326</xmin>
            <ymin>1596</ymin>
            <xmax>2465</xmax>
            <ymax>1729</ymax>
        </bndbox>
    </object>
</annotation>


怎么能够统计有哪几个.xml文件或图像中有heating标签,并存入到txt文件中。
实现以下效果

img

期待王者回答,感谢

遍历一下xml文件,读取内容判断是否有heating,如果有的话写入heating.txt
举个例子:

import os
path = r'D/tags'
tags = os.listdir(path)
    with open('heating.txt', 'a') as f_s:
        with open(path + '/' + tags, 'r') as f:
            if 'heating' in f.readlines():
                f_s.write(tags.replace(".xml",".jpg" + "\n"))

转str然后非贪婪re一下?然后对于文件名取一下数字,with open write写入