-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.cpp
161 lines (128 loc) · 3.72 KB
/
dictionary.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
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
#include "pch.h"
#include "dictionary.h"
namespace ocr
{
dictionary::dictionary(string path)
{
if (!load(path))
journal("Failed to load a dictionary at " + path, log_error);
}
dictionary::dictionary(map<char, vector<Mat>> _data)
{
// Calculate the heatmap size
uint rows = 0, cols = 0, images = 0;
for (auto& c : _data)
{
for (Mat image : c.second)
rows += image.rows, cols += image.cols;
images += (uint)c.second.size();
// Display images
Size image_size(std::min((int)c.second.size(), 30) * 32 + 1, (1 + (int)floor((c.second.size() - 1) / 30)) * 32 + 1);
Mat display_image(image_size, CV_32FC1);
for (int it = 0; it < c.second.size(); it++)
{
// Resize the image
Mat image = c.second[it];
resize(image, Size(32, 32));
// Copy the image to its proper position
int y = (int)floor(it / 30) * 32;
int x = 32 * (it - (int)floor(it / 30) * 30);
image.copyTo(display_image(Rect(x, y, image.size().width, image.size().height)));
}
blink_image(display_image, string(1, c.first));
}
// Make the signs
heatmap_size = Size(cols / images, rows / images);
for (auto sign_data : _data)
signs.push_back(sign(sign_data.first, sign_data.second, heatmap_size));
}
void dictionary::show_signs()
{
for (sign& s : signs)
s.show();
}
bool dictionary::save(string name, bool save_gradient)
{
// Check if the directory exists
string path = "dictionaries\\" + name + "\\";
if (!fs::exists(path))
fs::create_directory(path);
// Save gradient
if (save_gradient)
{
string gradient_path = path + "gradient.otsf";
gradient.save(gradient_path);
}
// Save signs
string signs_path = path + "signs.otsf";
ofstream out(signs_path.c_str());
if (!out.is_open())
CANNOT_CREATE_FILE_F(signs_path);
out << signs.size() << " " << heatmap_size.height << " " << heatmap_size.width << "\n";
for (size_t it = 0; it < signs.size(); it++)
signs[it].save(out);
out.close();
return true;
}
bool dictionary::load(string name)
{
// Check if the directory exists
string path = "dictionaries\\" + name + "\\";
if (!fs::exists(path))
{
journal("Dictionary directory at " + path + " does not exist", log_error);
return false;
}
// Load gradient
string gradient_path = path + "gradient.otsf";
if (!gradient.load(gradient_path))
return false;
// Load signs
string signs_path = path + "signs.otsf";
ifstream in(signs_path.c_str());
size_t it = 0; string buf;
if (!in.is_open()) MISSING_FILE_F(signs_path);
if (!file_read(in, buf)) MISSING_FILE_DATA_F(signs_path);
int sings_count = splstr<int>(buf, it);
heatmap_size = Size(splstr<int>(buf, it), splstr<int>(buf, it));
for (int s = 0; s < sings_count; s++)
signs.push_back(sign(in, heatmap_size));
in.close();
return true;
}
result dictionary::classify(Mat _image, Size _original_size)
{
vector<result> results;
for (sign& sign : signs)
results.push_back(sign.match(_image, _original_size, gradient));
return *max_element(results.begin(), results.end(), greater<result>());
}
pair<result, vector<Mat>> dictionary::ext_classify(Mat image, Size _original_size)
{
vector<Mat> images;
vector<result> results;
for (sign& s : signs)
{
pair<result, Mat> res = s.ext_match(image, _original_size, gradient);
results.push_back(res.first);
images.push_back(res.second);
}
sort(results.begin(), results.end(), greater<ocr::result>());
for (int it = 0; it < results.size() && it < 5; it++)
cout << results[it];
cout << "\n";
return make_pair(results.front(), images);
}
uint dictionary::size()
{
return (uint)signs.size();
}
Size dictionary::image_size()
{
return heatmap_size;
}
vector<sign> dictionary::get_signs()
{
return signs;
}
}