This repository was archived by the owner on Dec 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
380 lines (293 loc) · 12 KB
/
main.cpp
File metadata and controls
380 lines (293 loc) · 12 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <libgen.h>
#include <phdetection/svm.hpp>
#include <phdetection/bayes.hpp>
#include <phdetection/io.hpp>
#include <phdetection/core.hpp>
using namespace cv;
using namespace std;
using namespace cv::ml;
using namespace phd::io;
using namespace phd::ontologies;
int numberFirstSPCandidatesFound = 0;
Configuration config;
string config_folder = "/res/config";
string data_folder = "/res/features";
string results_folder = "/res/results";
string svm_folder = "/res/svm";
string nbayes_folder = "/res/bayes";
typedef struct ARGS {
string mode;
string target_type;
string target;
bool user_feedback;
string method;
string bayes_model;
string svm_model;
} ARGS;
void showHelper() {
cout << " Pothole Detection System Helper" << endl << endl;
cout << " -g == generate candidates inside the given folder" << endl << endl;
cout << "Example: -g -{i, d} {/path/to/image.abc, /path/to/images/} {-uf}" << endl << endl;
cout << "NOTE: The optional -uf parameter will enable user-driven feedback for each candidate found." << endl;
cout << "NOTE: This will create a features.json features file under ../res/features/ ."
<< endl << endl;
cout << " -t == train an SVM, Bayes or Multi Classifier using the generated data" << endl << endl;
cout << "Example: -t -ft /path/to/features.json "
<< "-{svm, bayes, multi} -b /path/to/bayes/model.yml -s /path/to/svm/model.yml" << endl << endl;
cout << " -c == classify the given image or set of images (folder path)" << endl << endl;
cout << "Example: -c -{i, d} {/path/to/image.abc, /path/to/images/} "
<< "-{svm, bayes, multi} -b /path/to/bayes/model.yml -s /path/to/svm/model.yml" << endl << endl;
}
/*
* This function show the candidate extracted and ask if is pothole (Y) or not (N)
* */
int askUserSupervisionBinaryClasses(const Features &candidateFeatures, const int scaleFactor = 5) {
Mat visual_image;
resize(candidateFeatures.candidate,
visual_image,
Size(candidateFeatures.candidate.cols * scaleFactor,
candidateFeatures.candidate.rows * scaleFactor));
imshow("Is pothole?", visual_image);
waitKey(1);
cout << "This candidate is a pothole? Y (for response yes) N (for no)" << endl;
int result = -1;
char response;
bool isResponseCorrect = false;
while(!isResponseCorrect) {
cin >> response;
if (response == 'P' || response == 'y') {
isResponseCorrect = true;
result = 1;
} else if (response == 'N' || response == 'n') {
result = -1;
isResponseCorrect = true;
} else {
cout << "Wrong response. Type (Y) for yes or (N) for no" << endl;
}
}
return result;
}
int askUserSupervisionMultiClasses(const Features &candidateFeatures, const int scaleFactor = 5) {
Mat visual_image;
resize(candidateFeatures.candidate,
visual_image,
Size(candidateFeatures.candidate.cols * scaleFactor, candidateFeatures.candidate.rows * scaleFactor));
imshow("Is pothole?", visual_image);
waitKey(1);
cout << "This candidate wich type of pothole is? "
"P (for response Pothole), C (for street crack), O (for out of road), S (for street/car/sidewalk)"
<< endl;
int result = -1;
char response;
bool isResponseCorrect = false;
while (!isResponseCorrect) {
cin >> response;
if (response == 'P' || response == 'p') {
isResponseCorrect = true;
result = ClassificationClasses::pothole;
} else if (response == 'C' || response == 'c') {
result = ClassificationClasses::asphaltCrack;
isResponseCorrect = true;
} else if (response == 'O' || response == 'o') {
result = ClassificationClasses::outOfRoad;
isResponseCorrect = true;
} else if (response == 'S' || response == 's') {
result = ClassificationClasses::streetSideWalkOrCar;
isResponseCorrect = true;
} else {
cout
<< "Wrong response. Type P (for response Pothole), C (for street crack), O (for out of road), S (for street/car/sidewalk)"
<< endl;
}
}
return result;
}
void _create_candidates_stub(
const std::string image,
const bool user_feedback,
const Configuration &config,
vector<Features> &features,
vector<string> &names) {
auto ft = phd::getFeatures(image, config);
for (auto f : ft) {
names.push_back(image);
if (user_feedback) {
//f._class = askUserSupervisionBinaryClasses(f);
f._class = askUserSupervisionMultiClasses(f);
cout << " Image " << image << " L" << f.label
<< " labeled as " << (f._class == 1 ? "pothole." : "not pothole.")
<< endl;
}
features.push_back(f);
}
}
void createCandidates(const ARGS args, const Configuration &config) {
vector<string> names;
vector<Features> features;
if (args.target_type == "-d") {
vector<string> fn = extractImagePath(args.target);
for (auto image : fn) {
cout << endl << "---------------" << image << endl;
_create_candidates_stub(image, args.user_feedback, config, features, names);
}
} else if (args.target_type == "-i") {
_create_candidates_stub(args.target, args.user_feedback, config, features, names);
}
saveFeaturesJSON(features, names, data_folder + "/features.json");
}
Mat _classifier_stub(const string &method, const string &bayes_model, const string &svm_model, const string &image,
const Configuration &config) {
cout << endl << "---------------" << image << endl;
auto features = phd::getFeatures(image, config);
cv::Mat labels;
try {
labels = phd::classify(method, svm_model, bayes_model, features);
} catch(phd::UndefinedMethod &ex) {
cerr << "ERROR: " << ex.what() << endl;
exit(-1);
}
cout << "LABELS: " << labels << endl;
for (int i = 0; i < features.size(); ++i) {
string folder = results_folder + "/neg/";
if (labels.at<int>(0, i) > 0 || labels.at<float>(0, i) > 0) {
folder = results_folder + "/pos/";
}
imwrite(
folder +
getName(set_format(image, "", false)) +
"_L" + to_string(features[i].label) +
"_" + to_string(features[i].id) +
".bmp",
features[i].candidate
);
}
return labels;
}
void classificationPhase(const ARGS args, const Configuration &config){
cout << "classify Method: " << args.method << endl;
portable_mkdir(results_folder.data());
portable_mkdir((results_folder + "/neg").data());
portable_mkdir((results_folder + "/pos").data());
/*--------------------------------- classify Phase ------------------------------*/
int numberOfCandidatesFound = 0;
if (args.target_type == "-d") { /// Whole folder
vector<string> fn = extractImagePath(args.target);
cout << "Number of image found in the directory: " << fn.size() << endl;
for (const auto &image : fn) {
numberOfCandidatesFound += _classifier_stub(args.method, args.bayes_model, args.svm_model, image, config).cols;
}
} else if (args.target_type == "-i") { /// Single Image
numberOfCandidatesFound += _classifier_stub(args.method, args.bayes_model, args.svm_model, args.target, config).cols;
} else {
cerr << "Unknown target type " << args.target_type << endl;
exit(-1);
}
cout << "Candidates at first segmentation found: " << numberFirstSPCandidatesFound << endl;
cout << "Candidates found: " << numberOfCandidatesFound << endl;
}
void trainingPhase(const ARGS args, const Configuration config){
cout << "Training Method: " << args.method << endl;
Mat labels(0, 0, CV_32SC1);
vector<Features> candidates;
loadFromJSON(args.target, candidates, labels);
/*--------------------------------- Training Phase ------------------------------*/
bool trainBoth = (args.method == "-multi");
if (trainBoth || args.method == "-svm") {
/***************************** SVM CLASSIFIER ********************************/
portable_mkdir(svm_folder.data());
phd::ml::svm::Training(candidates, labels, 1000, exp(-6), args.svm_model);
}
if (trainBoth || args.method == "-bayes") {
/***************************** Bayes CLASSIFIER ********************************/
portable_mkdir(nbayes_folder.data());
phd::ml::bayes::Training(candidates, labels, args.bayes_model);
}
}
ARGS parseCommandLineArguments(int argc, char*argv[]) {
auto mode = string(argv[1]);
auto target_type = string(argv[2]);
auto targets = string(argv[3]);
auto method = string();
auto bayes_model = string();
auto svm_model = string();
auto user_feedback = false;
if (mode == "-g") {
user_feedback = argc > 4 && string(argv[4]) == "-uf";
} else if (mode == "-c" || mode == "-t"){
method = string(argv[4]);
if (method == "-bayes") {
int idx = string(argv[5]) == "-b" ? 6 : 5;
bayes_model = string(argv[idx]);
} else if(method == "-svm") {
int idx = string(argv[5]) == "-s" ? 6 : 5;
svm_model = string(argv[6]);
} else if (method == "-multi" && argc > 7 && (
(string(argv[5]) == "-b" && string(argv[7]) == "-s") ||
(string(argv[7]) == "-b" && string(argv[5]) == "-s"))) {
if (string(argv[5]) == "-b") {
bayes_model = string(argv[6]);
svm_model = string(argv[8]);
} else {
bayes_model = string(argv[8]);
svm_model = string(argv[6]);
}
} else {
showHelper();
exit(-1);
}
} else {
showHelper();
exit(-1);
}
cout << "FOUND: " << endl
<< "Mode:" << mode << endl
<< "TT:" << target_type << endl
<< "Target:" << targets << endl
<< "feedback:" << user_feedback << endl
<< "Method:" << method << endl
<< "SVM:" << bayes_model << endl
<< "BAYES:" << svm_model << endl << endl;
return ARGS {mode, target_type, targets, user_feedback, method, bayes_model, svm_model};
}
int main(int argc, char *argv[]) {
// cout << phd::io::GetCurrentWorkingDir() << endl;
const string root = phd::io::getParentDirectory(string(dirname(argv[0])));
config_folder = root + config_folder;
data_folder = root + data_folder;
results_folder = root + results_folder;
svm_folder = root + svm_folder;
nbayes_folder = root + nbayes_folder;
cout << config_folder << endl;
cout << data_folder << endl;
cout << results_folder << endl;
cout << svm_folder << endl;
cout << nbayes_folder << endl;
// Save cpu tick count at the program start
double timeElapsed = (double) getTickCount();
if (argc < 3) {
showHelper();
return 0;
} else {
config = loadProgramConfiguration(config_folder + "/config.json");
printThresholds(config.primaryThresholds);
printThresholds(config.secondaryThresholds);
printOffsets(config.offsets);
const auto args = parseCommandLineArguments(argc, argv);
if (args.mode == "-g" && argc > 2) {
createCandidates(args, config);
} else if (args.mode == "-c" && argc > 6) {
classificationPhase(args, config);
} else if (args.mode == "-t" && argc >= 5) {
trainingPhase(args, config);
} else {
showHelper();
}
}
// Calculate and Print the execution time
timeElapsed = (static_cast<double>( getTickCount()) - timeElapsed) / getTickFrequency();
cout << "Times passed in seconds: " << timeElapsed << endl;
return 1;
}