feat: 实验性增加 OCR 字符等价类支持

This commit is contained in:
Horror Proton
2023-02-01 19:56:44 +08:00
committed by Yifan Liu
parent 38ceba64db
commit 094f40d9b8
2 changed files with 57 additions and 5 deletions

View File

@@ -15,6 +15,11 @@ bool asst::OcrImageAnalyzer::analyze()
std::vector<TextRectProc> preds_vec;
preds_vec.emplace_back([](TextRect& tr) -> bool {
tr.text = equivalent_class_preprocess(tr.text);
return true;
});
if (!m_replace.empty()) {
TextRectProc text_replace = [&](TextRect& tr) -> bool {
for (const auto& [regex, new_str] : m_replace) {
@@ -93,19 +98,25 @@ void asst::OcrImageAnalyzer::set_use_cache(bool is_use) noexcept
void asst::OcrImageAnalyzer::set_required(std::vector<std::string> required) noexcept
{
ranges::for_each(required, [](std::string& str) { str = equivalent_class_preprocess(str); });
m_required = std::move(required);
}
void asst::OcrImageAnalyzer::set_replace(std::unordered_map<std::string, std::string> replace) noexcept
void asst::OcrImageAnalyzer::set_replace(const std::unordered_map<std::string, std::string>& replace) noexcept
{
m_replace = std::move(replace);
m_replace = {};
for (auto&& [key, val] : replace) {
auto new_key = equivalent_class_preprocess(key);
auto new_val = equivalent_class_preprocess(val);
m_replace[new_key] = new_val;
}
}
void asst::OcrImageAnalyzer::set_task_info(OcrTaskInfo task_info) noexcept
{
m_required = std::move(task_info.text);
set_required(std::move(task_info.text));
m_full_match = task_info.full_match;
m_replace = std::move(task_info.replace_map);
set_replace(task_info.replace_map);
m_use_cache = task_info.cache;
m_use_char_model = task_info.is_ascii;
@@ -124,6 +135,44 @@ std::vector<asst::TextRect>& asst::OcrImageAnalyzer::get_result() noexcept
return m_ocr_result;
}
std::string asst::OcrImageAnalyzer::equivalent_class_preprocess(const std::string& in)
{
using equivalent_class = std::vector<std::string>;
static const std::vector<equivalent_class> classes {
// hiragana
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
// katakana
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
{ "", "" },
}; // TODO: store this into a file
auto result = in;
for (const auto& cls : classes) {
// replace the elements in group into the first one
ranges::for_each(cls.begin() + 1, cls.end(),
[&](const std::string& elem) { utils::string_replace_all_in_place(result, elem, cls.front()); });
}
return result;
}
void asst::OcrImageAnalyzer::set_task_info(std::shared_ptr<TaskInfo> task_ptr)
{
set_task_info(*std::dynamic_pointer_cast<OcrTaskInfo>(task_ptr));

View File

@@ -25,7 +25,7 @@ namespace asst
virtual void sort_result_by_required(); // 按传入的需求数组排序,传入的在前面结果接在前面
void set_required(std::vector<std::string> required) noexcept;
void set_replace(std::unordered_map<std::string, std::string> replace) noexcept;
void set_replace(const std::unordered_map<std::string, std::string>& replace) noexcept;
virtual void set_task_info(std::shared_ptr<TaskInfo> task_ptr);
virtual void set_task_info(const std::string& task_name);
@@ -38,6 +38,9 @@ namespace asst
virtual const std::vector<TextRect>& get_result() const noexcept;
virtual std::vector<TextRect>& get_result() noexcept;
// normalize similar characters into the same one
static std::string equivalent_class_preprocess(const std::string& in);
protected:
virtual void set_task_info(OcrTaskInfo task_info) noexcept;