#include #include #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& result, std::pair< std::vector, 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& 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& 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