forked from kly1997/head_shoulder-detection-by-yolov3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tongji.py
56 lines (43 loc) · 1.37 KB
/
tongji.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author : kly time:2019/4/13
# -*- coding:utf-8 -*-
from xml.dom.minidom import parse
import glob
import time
import json
def readXml(xml_file):
# 打开文件
doc = parse(xml_file)
return doc
def countTags(doc, labels, i, count):
# 解析XML,根据标签统计个数
for node in doc.getElementsByTagName("name"):
tags = node.firstChild.data
if tags == labels[i]:
count[i] += 1
# print(labels[i])
# print(count)
return count
if __name__ == '__main__':
# 标签池
labels = ['head_shoulder']
# labels = ['yes_withcloth','no_without']
xml_paths = glob.glob('C:\project\yolo\Test\Annotations//*.xml')
# xml数量
print('xml文件个数为:', len(xml_paths))
start_time = time.time()
# 初始化数目
count = [0] * len(labels)
# 统计所有xml文件
for i in range(len(xml_paths)):
for j in range(len(labels)):
count = countTags(readXml(xml_paths[i]), labels, j, count)
# 将标签与数目对应
label_counts = dict(zip(labels, count))
# 转成Json,方便以格式化输出
json_label_counts = json.dumps(label_counts, indent=1)
end_time = time.time()
used_time = end_time - start_time
print('各个标签数为:\n', json_label_counts)
print('统计所耗时间:%.2fs' % used_time)