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)
统计xml不同种类样本个数
最新推荐文章于 2022-08-16 15:32:57 发布