-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
193 lines (165 loc) · 7 KB
/
main.cpp
File metadata and controls
193 lines (165 loc) · 7 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
/**
* @file main.cpp
* @brief -
* @author Tobias Egger, Daniel Giritzer
* @copyright
* MIT License
* Copyright (c) 2021 Tobias Egger, Daniel Giritzer
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <iostream>
#include "File/LabelDataType.h"
#include "Processor/DummyProcessor.h"
#include "Loader/Loader.h"
#include "LeNet.h"
#include "ImgShow.h"
template<typename T, typename T2>
auto predict(const ILoader::lw_label_data_ptr_t &d, const std::shared_ptr<LeNet<T, T2>> &lnet) {
if (lnet == nullptr) {
throw std::invalid_argument("Nullptr in Line: " + std::to_string(__LINE__) + " of File: " + __FILE__);
}
if (d == nullptr) {
throw std::invalid_argument("Nullptr in Line: " + std::to_string(__LINE__) + " of File: " + __FILE__);
}
auto p = lnet->predict(d);
auto s = lnet->resolve(p);
return s;
}
template<typename T, typename T2>
void verify(const ILoader::lw_label_data_ptr_vect_t &data, const std::shared_ptr<LeNet<T, T2>> &lnet) {
if (data.empty()) {
throw std::logic_error("data Empty in Line: " + std::to_string(__LINE__) + " of File: " + __FILE__);
}
if (lnet == nullptr) {
throw std::invalid_argument("Nullptr in Line: " + std::to_string(__LINE__) + " of File: " + __FILE__);
}
fl_message_title("Use console?");
bool use_console = fl_choice("Do you want to print the result to console rather than using a GUI?", "No", "Yes", 0);
#ifdef _WIN32
if(!use_console)
FreeConsole();
#endif
for (auto &i : data) {
if (i == nullptr) {
throw std::invalid_argument("Nullptr in Line: " + std::to_string(__LINE__) + " of File: " + __FILE__);
}
auto p = predict(i, lnet);
std::stringstream strstr;
strstr << "#############################################" << std::endl;
strstr << "File #" << i->getPath().string() << std::endl;
if (p == std::nullopt) {
strstr << "Prediction #" << "NaN" << std::endl;
} else {
strstr << "Prediction #" << p.value() << std::endl;
}
strstr << "#############################################" << std::endl;
if(use_console){
std::cout << strstr.str();
}
else{
ImgShow a(i->getData(), "Picture", ImgShow::rgb, false);
fl_message_title("Result");
fl_message(strstr.str().c_str());
}
}
}
auto load(const ILoader::lw_label_path_ptr_vect_t &paths) {
if (paths.empty()) {
throw std::logic_error("data Empty in Line: " + std::to_string(__LINE__) + " of File: " + __FILE__);
}
auto loader = std::make_shared<Loader>();
auto dummy_processor = std::make_shared<DummyProcessor>();
loader->setPathsWithLabels(paths);
loader->load();
loader->commit();
loader->process(dummy_processor);
return loader->getData();
}
template<typename T, typename T2>
void validate(const std::shared_ptr<LeNet<T, T2>> &lnet) {
if (lnet == nullptr) {
throw std::invalid_argument("Nullptr in Line: " + std::to_string(__LINE__) + " of File: " + __FILE__);
}
auto validation_all = ILoader::lw_label_path_ptr_vect_t();
validation_all.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/All/"),
"All"));
validation_all.push_back(std::make_shared<LabelPathType>(
std::filesystem::path(
"../pic/Combinations/"),
"Combination"));
validation_all.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/Other/"),
"Other"));
verify(load(validation_all), lnet);
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
try {
// wm icon
auto icon = std::make_shared<Fl_RGB_Image>(icon_data, 128, 128, ImgShow::fl_imgtype::rgba, 0);
((Fl_Double_Window*)fl_message_icon()->parent())->icon(icon.get());
auto pathsWithLabels = ILoader::lw_label_path_ptr_vect_t();
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/0-Normal/"),
"Normal"));
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/1-NoHat/"),
"NoHat"));
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/2-NoFace/"),
"NoFace"));
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/3-NoLeg/"),
"NoLeg"));
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path(
"../pic/4-NoBodyPrint/"),
"NoBodyPrint"));
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/5-NoHand/"),
"NoHand"));
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/6-NoHead/"),
"NoHead"));
pathsWithLabels.push_back(std::make_shared<LabelPathType>(
std::filesystem::path("../pic/7-NoArm/"),
"NoArm"));
auto f = load(pathsWithLabels);
auto lnet = std::make_shared<LeNet<dlib::bgr_pixel, unsigned char>>(f, "../Model/indie_sync_file.sync");
lnet->makeLabelFromData();
lnet->train();
lnet->saveNet("../Model/indie_file_model.mod");
lnet->readNet("../Model/indie_file_model.mod");
validate(lnet);
} catch (const std::invalid_argument &exe) {
std::cerr << exe.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::logic_error &exe) {
std::cerr << exe.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception &exe) {
std::cerr << exe.what() << std::endl;
return EXIT_FAILURE;
}
catch (...) {
std::cerr << "Unknown Error Occured" << std::endl;
return EXIT_FAILURE;
}
return(Fl::run());
}