該xml文件爲labelImg.exe生成,通過C++獲取到該文件中的標籤名字與位置,然後保存到容器中供後續方便調用。
1、下載庫tinyxml;
2、將tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp這6個文件拷貝到工程下;
3、引入兩個頭文件,#include "tinystr.h",#include "tinyxml.h";
#include <iostream>
#include <opencv.hpp>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include "tinystr.h"
#include "tinyxml.h"
using namespace std;
using namespace cv;
typedef vector<pair<std::string, cv::Rect>> vecNameLoc;
vecNameLoc m_boltCentRects, m_boltCheckRects;
vecNameLoc m_boardCheckRects, m_posTemplRects, m_angleTemplRects, m_maskTemplRects;
typedef map<std::string, cv::Rect> mapNameLoc;
mapNameLoc m_mapTemplRectsImg;
mapNameLoc m_mapTemplRectsMain;
int readLabelFromXML(string path)
{
TiXmlDocument doc;//申明一個文檔類型變量,用來存儲讀取的xml文檔
if (!doc.LoadFile(path.c_str())) //檢測xml文檔是否存在
{
cerr << doc.ErrorDesc() << endl;
}
TiXmlElement* annotation = doc.FirstChildElement();//指向xml文檔的根元素
if (annotation == NULL)//檢測根元素存在性
{
cerr << "Failed to load file: No root element." << endl;
doc.Clear();
return -1;
}
TiXmlElement* object = annotation->FirstChildElement(); // xml的id 根節點下第一個子節點
object = object->NextSiblingElement("object");
while (object != NULL)
{
TiXmlElement* nameElement = object->FirstChildElement();
string name = nameElement->GetText();
TiXmlElement* bndbox = nameElement->NextSiblingElement("bndbox");
TiXmlElement* xmin = bndbox->FirstChildElement();
string xminValue = xmin->GetText();
TiXmlElement* ymin = xmin->NextSiblingElement();
string yminValue = ymin->GetText();
TiXmlElement* xmax = ymin->NextSiblingElement();
string xmaxValue = xmax->GetText();
TiXmlElement* ymax = xmax->NextSiblingElement();
string ymaxValue = ymax->GetText();
Rect rect(Point(stoi(xminValue), stoi(yminValue)), Point(stoi(xmaxValue), stoi(ymaxValue)));
m_mapTemplRectsImg[name] = rect;
m_mapTemplRectsMain[name] = rect;
if (name.find("pos") != string::npos && name.find("pos_net") == string::npos)
{
m_posTemplRects.push_back(std::pair<string, cv::Rect>(name, rect));
}
else if (name.find("angle") != string::npos || name.find("rot") != string::npos)
{
m_angleTemplRects.push_back(std::pair<string, cv::Rect>(name, rect));
}
else if (name.find("board") != string::npos || name.find("sim") != string::npos || name.find("plate") != string::npos)
{
m_boardCheckRects.push_back(std::pair<string, cv::Rect>(name, rect));
}
else if (name.find("bolt") != string::npos && name.find("checkbolt") == string::npos)
{
m_boltCentRects.push_back(std::pair<string, cv::Rect>(name, rect));
}
else if (name.find("checkbolt") != string::npos)
{
m_boltCheckRects.push_back(std::pair<string, cv::Rect>(name, rect));
}
else if (name.find("mask") != string::npos)
{
m_maskTemplRects.push_back(std::pair<string, cv::Rect>(name, rect));
}
else
{
if (name.find("power") != string::npos)
{
continue;
}
}
object = object->NextSiblingElement("object");
}
//按名稱排序
std::sort(m_boltCentRects.begin(), m_boltCentRects.end(), [](auto v1, auto v2) {return v1.first < v2.first; });
std::sort(m_boltCheckRects.begin(), m_boltCheckRects.end(), [](auto v1, auto v2) {return v1.first < v2.first; });
return 0;
}
int main()
{
readLabelFromXML("test.xml");
return 0;
}
<annotation>
<folder>XXX</folder>
<filename>XXX.png</filename>
<path>D:/XXX.png</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>2309</width>
<height>4647</height>
<depth>1</depth>
</size>
<segmented>0</segmented>
<object>
<name>pos_rear_cam</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>190</xmin>
<ymin>205</ymin>
<xmax>572</xmax>
<ymax>547</ymax>
</bndbox>
</object>
<object>
<name>pos_key_up</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>2087</xmin>
<ymin>1011</ymin>
<xmax>2168</xmax>
<ymax>1356</ymax>
</bndbox>
</object>
<object>
<name>pos_key_down</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>2093</xmin>
<ymin>1426</ymin>
<xmax>2176</xmax>
<ymax>1771</ymax>
</bndbox>
</object>
<object>
<name>pos_screen_b5</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>128</xmin>
<ymin>3044</ymin>
<xmax>178</xmax>
<ymax>3136</ymax>
</bndbox>
</object>
<object>
<name>pos_screw_l_down</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>912</xmin>
<ymin>4516</ymin>
<xmax>953</xmax>
<ymax>4594</ymax>
</bndbox>
</object>
<object>
<name>pos_lighting</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>997</xmin>
<ymin>4525</ymin>
<xmax>1282</xmax>
<ymax>4597</ymax>
</bndbox>
</object>
<object>
<name>pos_motor</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>929</xmin>
<ymin>3849</ymin>
<xmax>2039</xmax>
<ymax>4149</ymax>
</bndbox>
</object>
<object>
<name>board_main_1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>218</xmin>
<ymin>752</ymin>
<xmax>777</xmax>
<ymax>1383</ymax>
</bndbox>
</object>
<object>
<name>board_main_3</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>197</xmin>
<ymin>2339</ymin>
<xmax>467</xmax>
<ymax>2784</ymax>
</bndbox>
</object>
<object>
<name>pos_mic</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>594</xmin>
<ymin>281</ymin>
<xmax>667</xmax>
<ymax>399</ymax>
</bndbox>
</object>
<object>
<name>pos_checknet_l</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>377</xmin>
<ymin>4537</ymin>
<xmax>803</xmax>
<ymax>4605</ymax>
</bndbox>
</object>
<object>
<name>pos_checknet_r</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>1483</xmin>
<ymin>4534</ymin>
<xmax>1913</xmax>
<ymax>4598</ymax>
</bndbox>
</object>
<object>
<name>bolt_b1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>1893</xmin>
<ymin>609</ymin>
<xmax>1928</xmax>
<ymax>641</ymax>
</bndbox>
</object>
<object>
<name>checkbolt_b2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>1321</xmin>
<ymin>577</ymin>
<xmax>1420</xmax>
<ymax>671</ymax>
</bndbox>
</object>
<object>
<name>checkbolt_b3</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>921</xmin>
<ymin>579</ymin>
<xmax>1019</xmax>
<ymax>674</ymax>
</bndbox>
</object>
<object>
<name>checkbolt_b4</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>181</xmin>
<ymin>609</ymin>
<xmax>273</xmax>
<ymax>699</ymax>
</bndbox>
</object>
</annotation>