在使用SSD-tensorflow训练中需要传入自己的训练集与测试集的一些参数,其中包含各类名出现过的图片数,各类总框数,及总数。
今天写了一个计数的脚本如下,可用于索引XML文件中的目标数:
import re
import os
class_name =['0','1','2','3','4','5','6','7','8','9'] #自己的类名
# './VOC2007/test/Annotations'
annotation_folder = './VOCtest/test/Annotations' #改为自己xml标签文件夹的路径
list = os.listdir(annotation_folder)
current_number = []#存放类出现的次数
image_count=[]#存放类出现过的图片数
i = 0
while (i < len(class_name)):
cla = class_name[i]
total_number=0
image_num=0
for j in range(0, len(list)):
path = os.path.join(annotation_folder,list[j])
# print(path)
annotation_file = open(annotation_folder + '/' + os.path.basename(path)).read()
count = len(re.findall("<name>" + cla + "</name>", annotation_file)) #这里因为我的类名比较简单而且是数字,所以不能单单索引类名,必须把类名前后的符号加上,按自己的需求索引
if count >0:
image_num +=1
total_number += count
image_count.append(image_num)
current_number.append(total_number)
i += 1
#打印格式是按照SSD中格式设置的
for i in range(len(current_number)):
print('\"%d\" : (' %i,image_count[i],',',current_number[i],'),')
print('\"total\" : (',len(list),',',sum(current_number),'),')
结果如下: