forked from lewes6369/TensorRT-Yolov3
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dataReader.cpp
executable file
·134 lines (112 loc) · 3.61 KB
/
dataReader.cpp
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
#include "dataReader.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
namespace Tn
{
list<string> readFileList(const string& fileName)
{
ifstream file(fileName);
if(!file.is_open())
{
cout << "read file list error,please check file :" << fileName << endl;
exit(-1);
}
string strLine;
list<string> files;
while( getline(file,strLine) )
files.push_back(strLine);
file.close();
return files;
}
list<Source> readLabelFileList(const string& fileName)
{
ifstream file(fileName);
if(!file.is_open())
{
cout << "read file list error,please check file :" << fileName << endl;
exit(-1);
}
string strLine;
list<Source> result;
while(!file.eof())
{
Source data;
file >> data.fileName >> data.label;
result.emplace_back(data);
}
return result;
}
vector<string> split(const string& str, char delim)
{
stringstream ss(str);
string token;
vector<string> container;
while (getline(ss, token, delim)) {
container.push_back(token);
}
return container;
}
// vector<string> split(string str, string pat)
// {
// vector<string> bufStr;
// while (true)
// {
// int index = str.find(pat);
// string subStr = str.substr(0, index);
// if (!subStr.empty())
// bufStr.push_back(subStr);
// str.erase(0, index + pat.size());
// if (index == -1)
// break;
// }
// return bufStr;
// }
std::tuple<std::list<std::string>, std::list<std::vector<Bbox>>> readObjectLabelFileList(const string& fileName)
{
list<string> fileList;
list<vector<Bbox>> bBoxes;
ifstream file(fileName);
if(!file.is_open())
{
cout << "read file list error,please check file :" << fileName << endl;
exit(-1);
}
string strLine;
while( getline(file,strLine) )
{
vector<string> line=split(strLine, '\n');
if(line.size() < 1)
continue;
vector<string> strs=split(line[0], ' ');
int idx = 0;
string dataName=strs[idx++];
int trueBoxCount = (strs.size() - 1)/2;
vector<Bbox> truthboxes;
truthboxes.reserve(trueBoxCount);
for (int i = 0 ;i < trueBoxCount ;++i)
{
//class
string classId = strs[idx++];
//bbox Length
int length = strs[idx].length();
//remove bracket [ ]
string bbox = strs[idx++].substr(1,length-2);
vector<string> strs_txt = split(bbox, ',');
Bbox truthbox;
truthbox.classId = stoi(classId);
truthbox.left = stof(strs_txt[0]);
truthbox.top = stof(strs_txt[1]);
truthbox.right = truthbox.left + stof(strs_txt[2]);
truthbox.bot = truthbox.top + stof(strs_txt[3]);
truthboxes.push_back(truthbox);
}
fileList.emplace_back(dataName);
bBoxes.emplace_back(truthboxes);
}
file.close();
return make_tuple(move(fileList),move(bBoxes));
}
}