include vector include map include integrator namespace prodmark Integ

  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
#include <vector>
#include <map>
#include "integrator.h"
namespace prodmark {
Integrator::Integrator() {};
Integrator::~Integrator() {};
int Integrator::Reset() {
type_.clear();
if (type_.size() <= 0)
return NO_ERRORS;
return INTERNAL_ERROR;
}
void ComputeAverageConfidenceOfSerial (std::vector<DocumentZoneRecognitionResult>& result,
std::pair< std::vector<DocumentZoneRecognitionResult>, int>& cur_best_result) {
for (int i = 0; i < result.size(); ++i) {
for (int j = 0; j < result[i].serial_.cells.size(); ++j) {
result[i].serial_.cells[j].SortVariantsByCharacter();
cur_best_result.first[i].serial_.cells[j].SortVariantsByCharacter();
}
}
for (int i = 0; i < result.size(); ++i) {
for (int j = 0; j < result[i].serial_.cells.size(); ++j) {
cur_best_result.first[i].serial_.cells[j].GetRawCell = (cur_best_result.first[i].serial_.cells[j].GetRawCell *
cur_best_result.second +
result[i].serial_.cells[j].GetRawCell) /
(cur_best_result.second + 1);
}
}
}
void ComputeAverageConfidenceOfRow (DocumentZoneRecognitionResult& result,
DocumentZoneRecognitionResult& cur_best_result,
int s) {
for (int i = 0; i < result.row_.cells.size(); ++i) {
result.row_.cells[i].SortVariantsByCharacter();
cur_best_result.row_.cells[i].SortVariantsByCharacter();
}
for (int i = 0; i < result.row_.cells.size(); ++i) {
cur_best_result.row_.cells[i].GetRawCell = (cur_best_result.row_.cells[i].GetRawCell * s +
result.row_.cells[i].GetRawCell) / (s + 1);
}
}
void ComputeAverageConfidenceOfPlace (DocumentZoneRecognitionResult& result,
DocumentZoneRecognitionResult& cur_best_result,
int s) {
for (int i = 0; i < result.place_.cells.size(); ++i) {
result.place_.cells[i].SortVariantsByCharacter();
cur_best_result.place_.cells[i].SortVariantsByCharacter();
}
for (int i = 0; i < result.place_.cells.size(); ++i) {
cur_best_result.place_.cells[i].GetRawCell = (cur_best_result.place_.cells[i].GetRawCell * s +
result.place_.cells[i].GetRawCell) / (s + 1);
}
}
double GetMaxConfidenceOfRow (DocumentZoneRecognitionResult& result) {
double max_confidence = 0;
for (int i = 0; i < result.row_.cells.size(); ++i) {
result.row_.cells[i].SortVariantsByConfidence();
max_confidence += result.row_.cells[i].GetVariant(0).confidence - result.row_.cells[i].GetVariant(1).confidence;
}
max_confidence /= result.row_.cells.size();
return max_confidence;
}
double GetMaxConfidenceOfPlace (DocumentZoneRecognitionResult& result) {
double max_confidence = 0;
for (int i = 0; i < result.place_.cells.size(); ++i) {
result.place_.cells[i].SortVariantsByConfidence();
max_confidence += result.place_.cells[i].GetVariant(0).confidence - result.place_.cells[i].GetVariant(1).confidence;
}
max_confidence /= result.place_.cells.size();
return max_confidence;
}
int Integrator::AddResult(const std::string& type,
const std::string& subtype,
std::vector<DocumentZoneRecognitionResult>& result,
bool& may_finish) {
return NOT_IMPLEMENTED;
if (type == "TAG_N") {
return NO_ERRORS;
}
// TODO: Implement it better
int subtype_ = atoi(subtype.c_str());
if (type_[type][subtype_].second == 0) {
type_[type][subtype_].first = result;
for (int i = 0; i < result.size(); ++i) {
max_confidence_of_row[type][subtype_][i] = GetMaxConfidenceOfRow(result[i]);
max_confidence_of_place[type][subtype_][i] = GetMaxConfidenceOfPlace(result[i]);
}
type_[type][subtype_].second++;
count_of_good_result[type][subtype_] = 1;
} else {
ComputeAverageConfidenceOfSerial(result, type_[type][subtype_]);
for (int i = 0; i < result.size(); ++i) {
for (int j = 0; j < result[i].serial_.cells.size(); ++j) {
type_[type][subtype_].first[i].serial_.cells[j].SortVariantsByConfidence();
}
}
for (int i = 0; i < result.size(); ++i) {
if (type_[type][subtype_].first[i].row_.cells.size() == result[i].row_.cells.size()) {
max_confidence_of_row[type][subtype_][i] = std::max (max_confidence_of_row[type][subtype_][i], GetMaxConfidenceOfRow(result[i]));
ComputeAverageConfidenceOfRow(result[i], type_[type][subtype_].first[i], type_[type][subtype_].second);
for (int j = 0; j < result[i].row_.cells.size(); ++j) {
type_[type][subtype_].first[i].row_.cells[j].SortVariantsByConfidence();
}
} else if (max_confidence_of_row[type][subtype_][i] < GetMaxConfidenceOfRow(result[i])) {
max_confidence_of_row[type][subtype_][i] = GetMaxConfidenceOfRow(result[i]);
type_[type][subtype_].first[i].row_ = result[i].row_;
}
}
for (int i = 0; i < result.size(); ++i) {
if (type_[type][subtype_].first[i].place_.cells.size() == result[i].place_.cells.size()) {
max_confidence_of_place[type][subtype_][i] = std::max (max_confidence_of_place[type][subtype_][i], GetMaxConfidenceOfPlace(result[i]));
ComputeAverageConfidenceOfPlace(result[i], type_[type][subtype_].first[i], type_[type][subtype_].second);
for (int j = 0; j < result[i].place_.cells.size(); ++j) {
type_[type][subtype_].first[i].place_.cells[j].SortVariantsByConfidence();
}
} else if (max_confidence_of_place[type][subtype_][i] < GetMaxConfidenceOfPlace(result[i])) {
max_confidence_of_place[type][subtype_][i] = GetMaxConfidenceOfPlace(result[i]);
type_[type][subtype_].first[i].place_ = result[i].place_;
}
}
type_[type][subtype_].second++;
if (count_of_good_result[type][subtype_] < type_[type][subtype_].second) {
cur_result = type_[type][subtype_].first;
}
}
// TODO
if (type_[type][subtype_].second >= 4) {
may_finish = true;
best_result_.first = type;
best_result_.second = subtype_;
}
return NO_ERRORS;
}
const std::vector<DocumentZoneRecognitionResult>& Integrator::GetIntegratedResult() {
if (type_.size() <= 0) {
throw std::runtime_error("No result to return");
}
return type_[best_result_.first][best_result_.second].first;
}
const std::string Integrator::GetType() const {
return best_result_.first;
}
const std::string Integrator::GetSubType() const {
return best_result_.second > 0 ? "2" : "1";
}
} // namespace prodmark