C++读取labelimg的标注xml文件内容

C++读取labelimg的标注xml文件内容

 该xml文件为labelImg.exe生成,通过C++获取到该文件中的标签名字与位置,然后保存到容器中供后续方便调用。

1、下载库tinyxmlhttps://sourceforge.net/projects/tinyxml/

 

2、将tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp这6个文件拷贝到工程下;

 

3、引入两个头文件,#include "tinystr.h",#include "tinyxml.h";

 1 #include <iostream>
 2 #include <opencv.hpp>
 3 #include <fstream>
 4 #include <vector>
 5 #include <map>
 6 #include <string>
 7 #include "tinystr.h"
 8 #include "tinyxml.h"
 9  
10 using namespace std;
11 using namespace cv;
12  
13 typedef vector<pair<std::string, cv::Rect>> vecNameLoc;
14 vecNameLoc m_boltCentRects, m_boltCheckRects;
15 vecNameLoc m_boardCheckRects, m_posTemplRects, m_angleTemplRects, m_maskTemplRects;
16 typedef map<std::string, cv::Rect> mapNameLoc;
17 mapNameLoc m_mapTemplRectsImg;
18 mapNameLoc m_mapTemplRectsMain;
19  
20 int readLabelFromXML(string path)
21 {
22     TiXmlDocument doc;//申明一个文档类型变量,用来存储读取的xml文档
23     if (!doc.LoadFile(path.c_str()))   //检测xml文档是否存在
24     {
25         cerr << doc.ErrorDesc() << endl;
26     }
27     TiXmlElement* annotation = doc.FirstChildElement();//指向xml文档的根元素
28     if (annotation == NULL)//检测根元素存在性     
29     {
30         cerr << "Failed to load file: No root element." << endl;
31         doc.Clear();
32         return -1;
33     }
34     TiXmlElement* object = annotation->FirstChildElement(); // xml的id 根节点下第一个子节点
35     object = object->NextSiblingElement("object");
36     while (object != NULL)
37     {
38         TiXmlElement* nameElement = object->FirstChildElement();
39         string name = nameElement->GetText();
40         TiXmlElement* bndbox = nameElement->NextSiblingElement("bndbox");
41         TiXmlElement* xmin = bndbox->FirstChildElement();
42         string xminValue = xmin->GetText();
43         TiXmlElement* ymin = xmin->NextSiblingElement();
44         string yminValue = ymin->GetText();
45         TiXmlElement* xmax = ymin->NextSiblingElement();
46         string xmaxValue = xmax->GetText();
47         TiXmlElement* ymax = xmax->NextSiblingElement();
48         string ymaxValue = ymax->GetText();
49         Rect rect(Point(stoi(xminValue), stoi(yminValue)), Point(stoi(xmaxValue), stoi(ymaxValue)));
50         m_mapTemplRectsImg[name] = rect;
51         m_mapTemplRectsMain[name] = rect;
52         if (name.find("pos") != string::npos && name.find("pos_net") == string::npos)
53         {
54             m_posTemplRects.push_back(std::pair<string, cv::Rect>(name, rect));
55         }
56         else if (name.find("angle") != string::npos || name.find("rot") != string::npos)
57         {
58             m_angleTemplRects.push_back(std::pair<string, cv::Rect>(name, rect));
59         }
60         else if (name.find("board") != string::npos || name.find("sim") != string::npos || name.find("plate") != string::npos)
61         {
62             m_boardCheckRects.push_back(std::pair<string, cv::Rect>(name, rect));
63         }
64         else if (name.find("bolt") != string::npos && name.find("checkbolt") == string::npos)
65         {
66             m_boltCentRects.push_back(std::pair<string, cv::Rect>(name, rect));
67         }
68         else if (name.find("checkbolt") != string::npos)
69         {
70             m_boltCheckRects.push_back(std::pair<string, cv::Rect>(name, rect));
71         }
72         else if (name.find("mask") != string::npos)
73         {
74             m_maskTemplRects.push_back(std::pair<string, cv::Rect>(name, rect));
75         }
76         else
77         {
78             if (name.find("power") != string::npos)
79             {
80                 continue;
81             }
82         }
83         object = object->NextSiblingElement("object");
84     }
85     //按名称排序
86     std::sort(m_boltCentRects.begin(), m_boltCentRects.end(), [](auto v1, auto v2) {return v1.first < v2.first; });
87     std::sort(m_boltCheckRects.begin(), m_boltCheckRects.end(), [](auto v1, auto v2) {return v1.first < v2.first; });
88     return 0;
89 }
90  
91 int main()
92 {
93     readLabelFromXML("test.xml");
94     return 0;
95 }
96  
97  

  1 <annotation>
  2     <folder>XXX</folder>
  3     <filename>XXX.png</filename>
  4     <path>D:/XXX.png</path>
  5     <source>
  6         <database>Unknown</database>
  7     </source>
  8     <size>
  9         <width>2309</width>
 10         <height>4647</height>
 11         <depth>1</depth>
 12     </size>
 13     <segmented>0</segmented>
 14     <object>
 15         <name>pos_rear_cam</name>
 16         <pose>Unspecified</pose>
 17         <truncated>0</truncated>
 18         <difficult>0</difficult>
 19         <bndbox>
 20             <xmin>190</xmin>
 21             <ymin>205</ymin>
 22             <xmax>572</xmax>
 23             <ymax>547</ymax>
 24         </bndbox>
 25     </object>
 26     <object>
 27         <name>pos_key_up</name>
 28         <pose>Unspecified</pose>
 29         <truncated>0</truncated>
 30         <difficult>0</difficult>
 31         <bndbox>
 32             <xmin>2087</xmin>
 33             <ymin>1011</ymin>
 34             <xmax>2168</xmax>
 35             <ymax>1356</ymax>
 36         </bndbox>
 37     </object>
 38     <object>
 39         <name>pos_key_down</name>
 40         <pose>Unspecified</pose>
 41         <truncated>0</truncated>
 42         <difficult>0</difficult>
 43         <bndbox>
 44             <xmin>2093</xmin>
 45             <ymin>1426</ymin>
 46             <xmax>2176</xmax>
 47             <ymax>1771</ymax>
 48         </bndbox>
 49     </object>
 50     <object>
 51         <name>pos_screen_b5</name>
 52         <pose>Unspecified</pose>
 53         <truncated>0</truncated>
 54         <difficult>0</difficult>
 55         <bndbox>
 56             <xmin>128</xmin>
 57             <ymin>3044</ymin>
 58             <xmax>178</xmax>
 59             <ymax>3136</ymax>
 60         </bndbox>
 61     </object>
 62     <object>
 63         <name>pos_screw_l_down</name>
 64         <pose>Unspecified</pose>
 65         <truncated>0</truncated>
 66         <difficult>0</difficult>
 67         <bndbox>
 68             <xmin>912</xmin>
 69             <ymin>4516</ymin>
 70             <xmax>953</xmax>
 71             <ymax>4594</ymax>
 72         </bndbox>
 73     </object>
 74     <object>
 75         <name>pos_lighting</name>
 76         <pose>Unspecified</pose>
 77         <truncated>0</truncated>
 78         <difficult>0</difficult>
 79         <bndbox>
 80             <xmin>997</xmin>
 81             <ymin>4525</ymin>
 82             <xmax>1282</xmax>
 83             <ymax>4597</ymax>
 84         </bndbox>
 85     </object>
 86     <object>
 87         <name>pos_motor</name>
 88         <pose>Unspecified</pose>
 89         <truncated>0</truncated>
 90         <difficult>0</difficult>
 91         <bndbox>
 92             <xmin>929</xmin>
 93             <ymin>3849</ymin>
 94             <xmax>2039</xmax>
 95             <ymax>4149</ymax>
 96         </bndbox>
 97     </object>
 98     <object>
 99         <name>board_main_1</name>
100         <pose>Unspecified</pose>
101         <truncated>0</truncated>
102         <difficult>0</difficult>
103         <bndbox>
104             <xmin>218</xmin>
105             <ymin>752</ymin>
106             <xmax>777</xmax>
107             <ymax>1383</ymax>
108         </bndbox>
109     </object>
110     <object>
111         <name>board_main_3</name>
112         <pose>Unspecified</pose>
113         <truncated>0</truncated>
114         <difficult>0</difficult>
115         <bndbox>
116             <xmin>197</xmin>
117             <ymin>2339</ymin>
118             <xmax>467</xmax>
119             <ymax>2784</ymax>
120         </bndbox>
121     </object>
122     <object>
123         <name>pos_mic</name>
124         <pose>Unspecified</pose>
125         <truncated>0</truncated>
126         <difficult>0</difficult>
127         <bndbox>
128             <xmin>594</xmin>
129             <ymin>281</ymin>
130             <xmax>667</xmax>
131             <ymax>399</ymax>
132         </bndbox>
133     </object>
134     <object>
135         <name>pos_checknet_l</name>
136         <pose>Unspecified</pose>
137         <truncated>0</truncated>
138         <difficult>0</difficult>
139         <bndbox>
140             <xmin>377</xmin>
141             <ymin>4537</ymin>
142             <xmax>803</xmax>
143             <ymax>4605</ymax>
144         </bndbox>
145     </object>
146     <object>
147         <name>pos_checknet_r</name>
148         <pose>Unspecified</pose>
149         <truncated>0</truncated>
150         <difficult>0</difficult>
151         <bndbox>
152             <xmin>1483</xmin>
153             <ymin>4534</ymin>
154             <xmax>1913</xmax>
155             <ymax>4598</ymax>
156         </bndbox>
157     </object>
158     <object>
159         <name>bolt_b1</name>
160         <pose>Unspecified</pose>
161         <truncated>0</truncated>
162         <difficult>0</difficult>
163         <bndbox>
164             <xmin>1893</xmin>
165             <ymin>609</ymin>
166             <xmax>1928</xmax>
167             <ymax>641</ymax>
168         </bndbox>
169     </object>
170     <object>
171         <name>checkbolt_b2</name>
172         <pose>Unspecified</pose>
173         <truncated>0</truncated>
174         <difficult>0</difficult>
175         <bndbox>
176             <xmin>1321</xmin>
177             <ymin>577</ymin>
178             <xmax>1420</xmax>
179             <ymax>671</ymax>
180         </bndbox>
181     </object>
182     <object>
183         <name>checkbolt_b3</name>
184         <pose>Unspecified</pose>
185         <truncated>0</truncated>
186         <difficult>0</difficult>
187         <bndbox>
188             <xmin>921</xmin>
189             <ymin>579</ymin>
190             <xmax>1019</xmax>
191             <ymax>674</ymax>
192         </bndbox>
193     </object>
194     <object>
195         <name>checkbolt_b4</name>
196         <pose>Unspecified</pose>
197         <truncated>0</truncated>
198         <difficult>0</difficult>
199         <bndbox>
200             <xmin>181</xmin>
201             <ymin>609</ymin>
202             <xmax>273</xmax>
203             <ymax>699</ymax>
204         </bndbox>
205     </object>
206  
207 </annotation>

 

posted @ 2024-11-12 00:32  量子与太极  阅读(6)  评论(0编辑  收藏  举报