From 7fc46c160c1e0f5dc58878d15fcdd0df162e170a Mon Sep 17 00:00:00 2001 From: MistEO Date: Sun, 19 Dec 2021 21:32:31 +0800 Subject: [PATCH] =?UTF-8?q?fix.=E4=BF=AE=E5=A4=8DOCR=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=8C=BA=E5=9F=9F=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/tasks.json | 15 ++--- src/MeoAssistant/OcrPack.cpp | 32 ++++++---- src/MeoAssistant/ProcessTaskImageAnalyzer.cpp | 59 +++++++++---------- src/MeoAssistant/ProcessTaskImageAnalyzer.h | 2 +- 4 files changed, 56 insertions(+), 52 deletions(-) diff --git a/resource/tasks.json b/resource/tasks.json index ff44caa514..2e59ae4134 100644 --- a/resource/tasks.json +++ b/resource/tasks.json @@ -112,18 +112,15 @@ "PRTS": { "algorithm": "OcrDetect", "text": [ - "剩余可放置角色", - "接管作战", - "代理指挥", - "运行中" + "剩余可放置角色" ], "roi": [ - 300, - 530, - 980, - 190 + 900, + 500, + 380, + 150 + ], - "cache": false, "action": "doNothing", "rearDelay": 5000, "next": [ diff --git a/src/MeoAssistant/OcrPack.cpp b/src/MeoAssistant/OcrPack.cpp index 9d6b8c8c6b..d7274331ef 100644 --- a/src/MeoAssistant/OcrPack.cpp +++ b/src/MeoAssistant/OcrPack.cpp @@ -50,6 +50,7 @@ std::vector asst::OcrPack::recognize(const cv::Mat& image, const float scores[MaxBoxSize] = { 0 }; size_t size; + Log.trace("Without Det:", without_det); if (!without_det) { PaddleOcrSystem(m_ocr, buf.data(), buf.size(), false, boxes, strs, scores, &size, nullptr, nullptr); @@ -66,15 +67,17 @@ std::vector asst::OcrPack::recognize(const cv::Mat& image, const // the box rect like ↓ // 0 - 1 // 3 - 2 - int* box = boxes + i * 8; - int x_collect[4] = { *(box + 0), *(box + 2), *(box + 4), *(box + 6) }; - int y_collect[4] = { *(box + 1), *(box + 3), *(box + 5), *(box + 7) }; - int left = int(*std::min_element(x_collect, x_collect + 4)); - int right = int(*std::max_element(x_collect, x_collect + 4)); - int top = int(*std::min_element(y_collect, y_collect + 4)); - int bottom = int(*std::max_element(y_collect, y_collect + 4)); - - Rect rect(left, top, right - left, bottom - top); + Rect rect; + if (!without_det) { + int* box = boxes + i * 8; + int x_collect[4] = { *(box + 0), *(box + 2), *(box + 4), *(box + 6) }; + int y_collect[4] = { *(box + 1), *(box + 3), *(box + 5), *(box + 7) }; + int left = int(*std::min_element(x_collect, x_collect + 4)); + int right = int(*std::max_element(x_collect, x_collect + 4)); + int top = int(*std::min_element(y_collect, y_collect + 4)); + int bottom = int(*std::max_element(y_collect, y_collect + 4)); + rect = Rect(left, top, right - left, bottom - top); + } std::string text(*(strs + i)); float score = *(scores + i); @@ -97,9 +100,14 @@ std::vector asst::OcrPack::recognize(const cv::Mat& image, const std::vector asst::OcrPack::recognize(const cv::Mat& image, const asst::Rect& roi, const asst::TextRectProc& pred, bool without_det) { - auto rect_cor = [&roi, &pred](TextRect& tr) -> bool { - tr.rect.x += roi.x; - tr.rect.y += roi.y; + auto rect_cor = [&roi, &pred, &without_det](TextRect& tr) -> bool { + if (without_det) { + tr.rect = roi; + } + else { + tr.rect.x += roi.x; + tr.rect.y += roi.y; + } return pred(tr); }; Log.trace("OcrPack::recognize | roi : ", roi.to_string()); diff --git a/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp b/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp index 312d40becc..9a64c3fb8f 100644 --- a/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp +++ b/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp @@ -39,31 +39,31 @@ bool asst::ProcessTaskImageAnalyzer::ocr_analyze(std::shared_ptr task_ std::shared_ptr ocr_task_ptr = std::dynamic_pointer_cast(task_ptr); // 先尝试从缓存的结果里找 - for (const TextRect& tr : m_ocr_cache) { - TextRect temp = tr; - for (const auto& [regex, new_str] : ocr_task_ptr->replace_map) { - temp.text = std::regex_replace(temp.text, std::regex(regex), new_str); - } - for (const auto& text : ocr_task_ptr->text) { - bool flag = false; - if (ocr_task_ptr->need_full_match) { - if (temp.text == text) { - flag = true; - } - } - else if (temp.text.find(text) != std::string::npos) { - flag = true; - } - // 即使找到了,也得在ROI里才行,不然过滤掉 - if (flag && ocr_task_ptr->roi.include(tr.rect)) { - m_result = ocr_task_ptr; - m_result_rect = tr.rect; - ocr_task_ptr->region_of_appeared = m_result_rect; - Log.trace("ProcessTaskImageAnalyzer::ocr_analyze | found in cache", tr.to_string()); - return true; - } - } - } + //for (const TextRect& tr : m_ocr_cache) { + // TextRect temp = tr; + // for (const auto& [regex, new_str] : ocr_task_ptr->replace_map) { + // temp.text = std::regex_replace(temp.text, std::regex(regex), new_str); + // } + // for (const auto& text : ocr_task_ptr->text) { + // bool flag = false; + // if (ocr_task_ptr->need_full_match) { + // if (temp.text == text) { + // flag = true; + // } + // } + // else if (temp.text.find(text) != std::string::npos) { + // flag = true; + // } + // // 即使找到了,也得在ROI里才行,不然过滤掉 + // if (flag && ocr_task_ptr->roi.include(tr.rect)) { + // m_result = ocr_task_ptr; + // m_result_rect = tr.rect; + // ocr_task_ptr->region_of_appeared = m_result_rect; + // Log.trace("ProcessTaskImageAnalyzer::ocr_analyze | found in cache", tr.to_string()); + // return true; + // } + // } + //} if (!m_ocr_analyzer) { m_ocr_analyzer = std::make_unique(m_image); } @@ -71,14 +71,13 @@ bool asst::ProcessTaskImageAnalyzer::ocr_analyze(std::shared_ptr task_ bool ret = m_ocr_analyzer->analyze(); - const auto& ocr_result = m_ocr_analyzer->get_result(); if (ret) { + const auto& ocr_result = m_ocr_analyzer->get_result(); auto& res = ocr_result.front(); m_result = ocr_task_ptr; m_result_rect = res.rect; - ocr_task_ptr->region_of_appeared - = res.rect.center_zoom(1.5, m_image.cols, m_image.rows); // OCR库不扩大一点容易识别不到 - m_ocr_cache.insert(m_ocr_cache.end(), ocr_result.begin(), ocr_result.end()); + ocr_task_ptr->region_of_appeared = res.rect; + //m_ocr_cache.insert(m_ocr_cache.end(), ocr_result.begin(), ocr_result.end()); Log.trace("ProcessTaskImageAnalyzer::ocr_analyze | found", res.to_string()); } return ret; @@ -86,7 +85,7 @@ bool asst::ProcessTaskImageAnalyzer::ocr_analyze(std::shared_ptr task_ void asst::ProcessTaskImageAnalyzer::reset() noexcept { - m_ocr_cache.clear(); + //m_ocr_cache.clear(); m_ocr_analyzer = nullptr; m_match_analyzer = nullptr; } diff --git a/src/MeoAssistant/ProcessTaskImageAnalyzer.h b/src/MeoAssistant/ProcessTaskImageAnalyzer.h index 778877ce7f..1d6b75b8ce 100644 --- a/src/MeoAssistant/ProcessTaskImageAnalyzer.h +++ b/src/MeoAssistant/ProcessTaskImageAnalyzer.h @@ -55,6 +55,6 @@ namespace asst std::vector m_tasks_name; std::shared_ptr m_result; Rect m_result_rect; - std::vector m_ocr_cache; + //std::vector m_ocr_cache; }; }