-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetection.h
More file actions
280 lines (227 loc) · 6.22 KB
/
detection.h
File metadata and controls
280 lines (227 loc) · 6.22 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#pragma once
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include "include/barcodeCreator.h"
#include <iostream>
#include <filesystem>
#include <string>
#include <fstream>
#include <opencv2/ml.hpp>
/////////////////////////////
/////////////////////////////
///////////////////////////// at ((row,col) = (hei,wid)
/////////////////////////////
/////////////////////////////
namespace fs = std::filesystem;
typedef cv::Point3_<uint8_t> Pixel;
using std::string;
using namespace cv;
int pr = 10; bool normA = false;
//Total : 1421 / 1756
const int N = 10;
std::unordered_map<string, int> categorues;
class barclassificator
{
public:
bc::Barcontainer classes[N * 2];
void addClass(bc::Barcontainer* cont, int classInd)
{
classes[classInd].addItem(cont->exractItem(0));
classes[classInd + N].addItem(cont->exractItem(1));
delete cont;
}
int check(bc::Baritem* bar0, bc::Baritem* bar255, int type)
{
float res = 0;
if (classes[type].count() == 0)
{
return -1;
}
int maxInd = type;
float maxP = res;
for (size_t i = 0; i < N; i++)
{
float ps = classes[i].compireBest(bar0) * 0.5 + classes[i + N].compireBest(bar255) * 0.5;
if (ps > maxP)
{
maxP = ps;
maxInd = i;
}
}
return type == maxInd ? 1 : 0;
}
~barclassificator()
{ }
};
using namespace bc;
using namespace std;
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
void split(string str, std::vector<string>& strings, char splitCh = ' ')
{
std::istringstream f(str);
string s;
while (getline(f, s, splitCh))
strings.push_back(s);
}
int gt(string s)
{
return atoi(s.c_str());
}
inline bool exists(const std::string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
void getSet(string path, barclassificator& data, char diff = '0', float* params = nullptr)
{
barcodeCreator bc;
int categoruesSize = 0;
string labelSubpath = "/labelTxt-v1.5/DOTA-v1.5_hbb/";
string coords = path + labelSubpath;
string subbaseIMG = "/images/";
string subpath = subbaseIMG;
int ispatr = 0;
if (exists(path + subbaseIMG + "part1"))
{
subpath = subbaseIMG + "part1/images/";
ispatr = 1;
}
std::vector<bc::barstruct> structure;
std::vector<bc::barstruct> constr;
constr.push_back(barstruct(ProcType::f0t255, ColorType::gray, ComponentType::Hole));
constr.push_back(barstruct(ProcType::f255t0, ColorType::gray, ComponentType::Hole));
using recursive_directory_iterator = fs::recursive_directory_iterator;
int k = 0;
for (const auto& dirEntry : recursive_directory_iterator(coords))
{
string coordsPath = dirEntry.path().string();
string imgpath = coordsPath;
replace(imgpath, labelSubpath, subpath);
replace(imgpath, "txt", "png");
if (!exists(imgpath))
{
ispatr += 1;
imgpath = coordsPath;
subpath = subbaseIMG + "part" + std::to_string(ispatr) + "/images/";
replace(imgpath, labelSubpath, subpath);
replace(imgpath, "txt", "png");
}
cv::Mat source = cv::imread(imgpath, cv::IMREAD_GRAYSCALE);
std::ifstream infile(coordsPath);
std::string line;
std::getline(infile, line);
std::getline(infile, line);
bool stop = false;
while (std::getline(infile, line))
{
std::vector<string> lids;
split(line, lids);
string s = (lids[8]);
if (lids[9][0] == diff)
continue;
auto r = categorues.find(s);
if (r == categorues.end())
continue;
int index = r->second;
int xend = atoi(lids[2].c_str());
int yend = atoi(lids[5].c_str());
if (yend >= source.rows || xend >= source.cols)
continue;
int x = atoi(lids[0].c_str());
int y = atoi(lids[1].c_str());
cv::Rect ds(x, y, xend - x, yend - y);
cv::Mat m = source(ds);
if (m.empty())
continue;
cv::resize(m, m, cv::Size(32, 32));
auto b = bc.createBarcode(m, constr);
b->preprocessBar(pr, normA);
addClass(b, index);
++k;
}
}
std::cout << k << endl;
}
void doMagickDOTA()
{
string s = "../analysis/datasets/DOTA/images";
barclassificator train;
string whiteList = "plane, ship, storage tank, tennis court, basketball court, bridge, small vehicle, helicopter, container crane";
std::vector<string> splited;
split(whiteList, splited, ',');
categorues.clear();
int l = 0;
for (auto& c : splited)
{
int soff = c[0] == ' ' ? 1 : 0;
for (size_t i = 1; i < c.length(); i++)
{
if (c[i] == ' ')
{
c[i] = '-';
}
}
categorues[c.substr(soff)] = l++;
}
int cN = splited.size();
assert(cN <= N);
string pathtrain = "../train";
string pathvalidation = "../validation";
getSet(pathtrain, train, '0');
barclassificator validation2;
getSet(pathvalidation, validation2, '0');
bc::Barcontainer testcont;
int correct = 0;
int total = 0;
int cTotal = 0, cCurrect = 0;
float params[][3] = {{1, 0, 0}, {0,0.5,0}, {0,0,0.5}, {1, 0.5, 0.0}, {1, 0.0, 0.5}, {1, 0.5, 0.5}};
for (size_t p = 0; p < sizeof(params) / (sizeof(int) * 3); p++)
{
barclassificator validation;
getSet(pathvalidation, validation, '0', params[p]);
cout << "Params: " << params[p][0] << " " << params[p][1] << " " << params[p][2] << endl;
for (size_t i = 0; i < cN; i++)
{
auto& barc0 = validation.classes[i];
auto& barc1 = validation.classes[N + i];
for (size_t j = 0; j < barc0.count(); j++)
{
auto* bar0 = barc0.get(j);
auto* bar1 = barc1.get(j);
if (train.check(bar0, bar1, i))
{
++correct;
}
++total;
}
cTotal += total;
cCurrect += correct;
std::cout << " done for " << splited[i] << ": " << correct << "/" << total << std::endl;
correct = 0;
total = 0;
}
std::cout << "\nTotal: " << cCurrect << "/" << cTotal << "| " << static_cast<float>(cCurrect) / cTotal << std::endl << std::endl;
cCurrect = 0;
cTotal = 0;
}
system("pause");
}
//
//Total : 1417 / 1756 | 0.806948
//Total : 1368 / 1756 | 0.779043
//Total : 1251 / 1756 | 0.712415
//Total : 1373 / 1756 | 0.781891
//Total : 1265 / 1756 | 0.720387
//Total : 1265 / 1756 | 0.720387
//Total: 28504/34789
//Total : 1417 / 1756 | 0.806948
//Total : 2838 / 3512 | 0.808087
//Total : 4259 / 5268 | 0.808466
//Total : 5676 / 7024 | 0.808087
//Total : 7093 / 8780 | 0.807859
//Total : 8510 / 10536 | 0.807707