-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_model.cpp
56 lines (44 loc) · 1.29 KB
/
train_model.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
#include <dirent.h>
#include <string.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <string>
#include <gflags/gflags.h>
#include "knearest.h"
using std::cerr;
using std::endl;
using std::string;
DEFINE_string(output, "", "Path to write trained model to");
void TrainDirectory(const string& path, const string& name,
KNearest* knearest) {
DIR* training_directory = opendir(path.c_str());
dirent* directory_info = nullptr;
while ((directory_info = readdir(training_directory))) {
if (directory_info->d_type != DT_REG) {
continue;
}
string file_path = path + "/";
file_path += directory_info->d_name;
cv::Mat image = cv::imread(file_path, 0);
if (image.data == nullptr) {
cerr << "Failed to load image: " << file_path << endl;
exit(1);
}
knearest->Learn(image, *path.rbegin());
}
closedir(training_directory);
}
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
KNearest knearest;
for (int i = 1; i < argc; ++i) {
string path(argv[i]);
size_t index = path.rfind('/');
cv::Mat image = cv::imread(path, 0);
knearest.Learn(image, path[index - 1]);
}
knearest.Train();
knearest.Save(FLAGS_output);
return 0;
}