目标检测任务重,担心数据集中各标签类别不均衡,想统计XML标注文件中各标注类别的标签数量,可以使用以下脚本:文章来源:https://www.toymoban.com/news/detail-653250.html
import os
import glob
import xml.etree.ElementTree as et
def count_labels(source_dir):
file_list = glob.glob(os.path.join(source_dir,"*.xml"))
labels = {}
for file in file_list:
tree = et.ElementTree()
tree.parse(file)
objects = tree.findall(".//object")
for object in objects:
name = object.find(".//name")
label = name.text
if label in labels.keys():
labels[label] += 1
else:
labels[label] = 1
count_all = 0
print(source_dir)
for i in labels.keys():
print(i,labels[i])
count_all += labels[i]
print("all_labels : ", count_all)
print("all_images : ", len(file_list))
if __name__ == '__main__':
count_labels("/disk/test_xml")
文章来源地址https://www.toymoban.com/news/detail-653250.html
到了这里,关于统计XML标注文件中各标注类别的标签数量的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!