fix.修复OCR缓存区域错误的问题

This commit is contained in:
MistEO
2021-12-19 21:32:31 +08:00
parent 01e4dba10b
commit df4f09eaaa
4 changed files with 56 additions and 52 deletions

View File

@@ -112,18 +112,15 @@
"PRTS": {
"algorithm": "OcrDetect",
"text": [
"剩余可放置角色",
"接管作战",
"代理指挥",
"运行中"
"剩余可放置角色"
],
"roi": [
300,
530,
980,
190
900,
500,
380,
150
],
"cache": false,
"action": "doNothing",
"rearDelay": 5000,
"next": [

View File

@@ -50,6 +50,7 @@ std::vector<asst::TextRect> 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::TextRect> 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::TextRect> asst::OcrPack::recognize(const cv::Mat& image, const
std::vector<asst::TextRect> 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());

View File

@@ -39,31 +39,31 @@ bool asst::ProcessTaskImageAnalyzer::ocr_analyze(std::shared_ptr<TaskInfo> task_
std::shared_ptr<OcrTaskInfo> ocr_task_ptr = std::dynamic_pointer_cast<OcrTaskInfo>(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<OcrImageAnalyzer>(m_image);
}
@@ -71,14 +71,13 @@ bool asst::ProcessTaskImageAnalyzer::ocr_analyze(std::shared_ptr<TaskInfo> 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<TaskInfo> task_
void asst::ProcessTaskImageAnalyzer::reset() noexcept
{
m_ocr_cache.clear();
//m_ocr_cache.clear();
m_ocr_analyzer = nullptr;
m_match_analyzer = nullptr;
}

View File

@@ -55,6 +55,6 @@ namespace asst
std::vector<std::string> m_tasks_name;
std::shared_ptr<TaskInfo> m_result;
Rect m_result_rect;
std::vector<TextRect> m_ocr_cache;
//std::vector<TextRect> m_ocr_cache;
};
}