-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnote
156 lines (146 loc) · 6.41 KB
/
note
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
rapidxml create Pascal xml
void CAIExamDlg::create_xml(const SheetDetail & sheet_detail)
{
rapidxml::xml_document<> doc;
rapidxml::xml_node<>* rot = doc.allocate_node(rapidxml::node_pi, doc.allocate_string("xml version='1.0' encoding='utf-8'"));
doc.append_node(rot);
// annotation 节点
rapidxml::xml_node<>* node = doc.allocate_node(rapidxml::node_element, "annotation", "information");
node->append_attribute(doc.allocate_attribute("verified", doc.allocate_string("no")));
doc.append_node(node);
// folder 节点
rapidxml::xml_node<>* node_folder = doc.allocate_node(rapidxml::node_element, "folder", doc.allocate_string(sheet_detail.str_dir_name.c_str()));
node->append_node(node_folder);
// filename 节点
rapidxml::xml_node<>* node_filename = doc.allocate_node(rapidxml::node_element, "filename", doc.allocate_string(sheet_detail.str_img_name.c_str()));
node->append_node(node_filename);
// path 节点
rapidxml::xml_node<>* node_path = doc.allocate_node(rapidxml::node_element, "path", doc.allocate_string(sheet_detail.str_img_path.c_str()));
node->append_node(node_path);
// source 节点
rapidxml::xml_node<>* node_source = doc.allocate_node(rapidxml::node_element, "source",NULL);
// source 子节点 database 节点
rapidxml::xml_node<>* node_database = doc.allocate_node(rapidxml::node_element, "database", "Unknown");
node_source->append_node(node_database);
node->append_node(node_source);
// size 节点
rapidxml::xml_node<>* node_size = doc.allocate_node(rapidxml::node_element, "size", NULL);
// size 子节点 width height depth 节点
rapidxml::xml_node<>* node_width = doc.allocate_node(rapidxml::node_element, "width", doc.allocate_string(to_string(sheet_detail.i_img_w).c_str()));
rapidxml::xml_node<>* node_height = doc.allocate_node(rapidxml::node_element, "height", doc.allocate_string(to_string(sheet_detail.i_img_h).c_str()));
rapidxml::xml_node<>* node_depth = doc.allocate_node(rapidxml::node_element, "depth", doc.allocate_string(to_string(sheet_detail.i_img_depth).c_str()));
node_size->append_node(node_width);
node_size->append_node(node_height);
node_size->append_node(node_depth);
node->append_node(node_size);
// segmented 节点
rapidxml::xml_node<>* node_segmented = doc.allocate_node(rapidxml::node_element, "segmented", "0");
node->append_node(node_segmented);
// 循环 多个 object 节点
for (int i = 0; i < sheet_detail.vec_boundboxes.size(); i++) {
BoundingBox bbx = sheet_detail.vec_boundboxes[i];
rapidxml::xml_node<>* node_object = doc.allocate_node(rapidxml::node_element, "object", NULL);
// 子节点
rapidxml::xml_node<>* node_name = doc.allocate_node(rapidxml::node_element, "name", doc.allocate_string(bbx.str_class_name.c_str()));
rapidxml::xml_node<>* node_pos = doc.allocate_node(rapidxml::node_element, "pos", "Unspecified");
rapidxml::xml_node<>* node_truncated = doc.allocate_node(rapidxml::node_element, "truncated", "0");
rapidxml::xml_node<>* node_difficult = doc.allocate_node(rapidxml::node_element, "difficult", "0");
rapidxml::xml_node<>* node_score = doc.allocate_node(rapidxml::node_element, "score", doc.allocate_string(to_string(bbx.score).c_str()));
// bndbox 节点
rapidxml::xml_node<>* node_bndbox = doc.allocate_node(rapidxml::node_element, "bndbox", NULL);
rapidxml::xml_node<>* node_xmin = doc.allocate_node(rapidxml::node_element, "xmin", doc.allocate_string(to_string(bbx.x0).c_str()));
rapidxml::xml_node<>* node_ymin = doc.allocate_node(rapidxml::node_element, "ymin", doc.allocate_string(to_string(bbx.x1).c_str()));
rapidxml::xml_node<>* node_xmax = doc.allocate_node(rapidxml::node_element, "xmax", doc.allocate_string(to_string(bbx.y0).c_str()));
rapidxml::xml_node<>* node_ymax = doc.allocate_node(rapidxml::node_element, "ymax", doc.allocate_string(to_string(bbx.y1).c_str()));
node_bndbox->append_node(node_xmin);
node_bndbox->append_node(node_ymin);
node_bndbox->append_node(node_xmax);
node_bndbox->append_node(node_ymax);
// 加入 object 节点
node_object->append_node(node_name);
node_object->append_node(node_pos);
node_object->append_node(node_truncated);
node_object->append_node(node_difficult);
node_object->append_node(node_bndbox);
node_object->append_node(node_score);
// 加入根节点
node->append_node(node_object);
}
std::string text;
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout << text << std::endl;
std::ofstream out(sheet_detail.str_xml_path);
out << doc;
return;
}
// parse xml
void CAIExamDlg::parse_xml(SheetDetail & sheet_detail)
{
rapidxml::file<> fdoc(sheet_detail.str_xml_path.c_str());
rapidxml::xml_document<> doc;
doc.parse<0>(fdoc.data());
// 取得根节点
rapidxml::xml_node<> * node = doc.first_node();// annotation 节点不解析信息 忽略
// 取得 annotation 节点下面的子节点列表
node = node->first_node();
string node_name; node_name.clear();
string node_val; node_val.clear();
while (node) {
node_name = node->name();
if ("size" == node_name) {
rapidxml::xml_node<> * node_size = node->first_node();
while (node_size){
node_name = node_size->name();
if ("width" == node_name) {
sheet_detail.i_img_w = atoi(node_size->value());
}
else if ("height" == node_name) {
sheet_detail.i_img_h = atoi(node_size->value());
}
else if ("depth" == node_name) {
sheet_detail.i_img_depth = atoi(node_size->value());
}
else
;
node_size = node_size->next_sibling();
}
}
else if("object" == node_name){
BoundingBox bbx;
rapidxml::xml_node<> * node_obj = node->first_node();
while (node_obj) {
node_name = node_obj->name();
if ("name" == node_name) {
bbx.str_class_name = node_obj->value();
}
else if ("score" == node_name) {
bbx.score = atof(node_obj->value());
}
else if ("bndbox" == node_name) {
rapidxml::xml_node<> * node_rect = node_obj->first_node();
while (node_rect) {
node_name = node_rect->name();
if ("xmin" == node_name) {
bbx.x0 = atoi(node_rect->value());
}
else if ("ymin" == node_name) {
bbx.y0 = atoi(node_rect->value());
}
else if ("xmax" == node_name) {
bbx.x1 = atoi(node_rect->value());
}
else if ("ymax" == node_name) {
bbx.y1 = atoi(node_rect->value());
}
node_rect = node_rect->next_sibling();
}
}
else
;
node_obj = node_obj->next_sibling();
}
sheet_detail.vec_boundboxes.push_back(bbx);
}
node = node->next_sibling();
}
}