mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-19 10:32:19 +08:00
Merge pull request #3795 from MaaAssistantArknights/feat/ocr_eq_class
feat: OCR字符等价类支持
This commit is contained in:
28
src/MaaCore/Config/Miscellaneous/OcrConfig.cpp
Normal file
28
src/MaaCore/Config/Miscellaneous/OcrConfig.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "OcrConfig.h"
|
||||
|
||||
#include <meojson/json.hpp>
|
||||
|
||||
#include "Utils/Logger.hpp"
|
||||
|
||||
std::string asst::OcrConfig::process_equivalence_class(const std::string& str)
|
||||
{
|
||||
std::string result = str;
|
||||
for (const auto& eq_class : m_eq_classes) {
|
||||
ranges::for_each(eq_class.begin() + 1, eq_class.end(), [&](const std::string& elem) {
|
||||
utils::string_replace_all_in_place(result, elem, eq_class.front());
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool asst::OcrConfig::parse(const json::value& json)
|
||||
{
|
||||
for (const json::value& eq_class : json.at("equivalence_classes").as_array()) {
|
||||
equivalence_class eq_class_tmp;
|
||||
for (const json::value& eq_element : eq_class.as_array()) {
|
||||
eq_class_tmp.emplace_back(eq_element.as_string());
|
||||
}
|
||||
m_eq_classes.emplace_back(std::move(eq_class_tmp));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
31
src/MaaCore/Config/Miscellaneous/OcrConfig.h
Normal file
31
src/MaaCore/Config/Miscellaneous/OcrConfig.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "Config/AbstractConfig.h"
|
||||
|
||||
#include "Utils/Ranges.hpp"
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "Common/AsstTypes.h"
|
||||
|
||||
namespace asst
|
||||
{
|
||||
class OcrConfig final : public SingletonHolder<OcrConfig>, public AbstractConfig
|
||||
{
|
||||
public:
|
||||
virtual ~OcrConfig() override = default;
|
||||
|
||||
std::string process_equivalence_class(const std::string& str);
|
||||
|
||||
protected:
|
||||
virtual bool parse(const json::value& json) override;
|
||||
|
||||
using equivalence_class = std::vector<std::string>;
|
||||
|
||||
std::vector<equivalence_class> m_eq_classes;
|
||||
};
|
||||
|
||||
} // namespace asst
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "Miscellaneous/RecruitConfig.h"
|
||||
#include "Miscellaneous/StageDropsConfig.h"
|
||||
#include "Miscellaneous/TilePack.h"
|
||||
#include "Miscellaneous/OcrConfig.h"
|
||||
#include "Roguelike/RoguelikeCopilotConfig.h"
|
||||
#include "Roguelike/RoguelikeRecruitConfig.h"
|
||||
#include "Roguelike/RoguelikeShoppingConfig.h"
|
||||
@@ -64,6 +65,7 @@ bool asst::ResourceLoader::load(const std::filesystem::path& path)
|
||||
LoadResourceAndCheckRet(RoguelikeRecruitConfig, "roguelike"_p / "recruitment.json"_p);
|
||||
LoadResourceAndCheckRet(RoguelikeShoppingConfig, "roguelike"_p / "shopping.json"_p);
|
||||
LoadResourceAndCheckRet(BattleDataConfig, "battle_data.json"_p);
|
||||
LoadResourceAndCheckRet(OcrConfig, "ocr_config.json"_p);
|
||||
|
||||
/* load resource with json and template files*/
|
||||
LoadResourceWithTemplAndCheckRet(TaskData, "tasks.json"_p, "template"_p);
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
<ClInclude Include="Common\AsstTypes.h" />
|
||||
<ClInclude Include="Common\AsstVersion.h" />
|
||||
<ClInclude Include="Config\Miscellaneous\AvatarCacheManager.h" />
|
||||
<ClInclude Include="Config\Miscellaneous\OcrConfig.h" />
|
||||
<ClInclude Include="Config\Miscellaneous\SSSCopilotConfig.h" />
|
||||
<ClInclude Include="Controller.h" />
|
||||
<ClInclude Include="InstHelper.h" />
|
||||
@@ -159,6 +160,7 @@
|
||||
<ClCompile Include="Assistant.cpp" />
|
||||
<ClCompile Include="AsstCaller.cpp" />
|
||||
<ClCompile Include="Config\Miscellaneous\AvatarCacheManager.cpp" />
|
||||
<ClCompile Include="Config\Miscellaneous\OcrConfig.cpp" />
|
||||
<ClCompile Include="Config\Miscellaneous\SSSCopilotConfig.cpp" />
|
||||
<ClCompile Include="Controller.cpp" />
|
||||
<ClCompile Include="InstHelper.cpp" />
|
||||
|
||||
@@ -507,6 +507,9 @@
|
||||
<ClInclude Include="Task\Reclamation\ReclamationControlTask.h">
|
||||
<Filter>源文件\Task\Reclamation</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Config\Miscellaneous\OcrConfig.h">
|
||||
<Filter>源文件\Config\Miscellaneous</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Vision\AbstractImageAnalyzer.cpp">
|
||||
@@ -836,5 +839,8 @@
|
||||
<ClCompile Include="Task\Reclamation\ReclamationControlTask.cpp">
|
||||
<Filter>源文件\Task\Reclamation</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Config\Miscellaneous\OcrConfig.cpp">
|
||||
<Filter>源文件\Config\Miscellaneous</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Config/Miscellaneous/OcrPack.h"
|
||||
#include "Config/Miscellaneous/OcrConfig.h"
|
||||
#include "Config/TaskData.h"
|
||||
#include "Utils/Logger.hpp"
|
||||
|
||||
@@ -15,6 +16,11 @@ bool asst::OcrImageAnalyzer::analyze()
|
||||
|
||||
std::vector<TextRectProc> preds_vec;
|
||||
|
||||
preds_vec.emplace_back([](TextRect& tr) -> bool {
|
||||
tr.text = OcrConfig::get_instance().process_equivalence_class(tr.text);
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!m_replace.empty()) {
|
||||
TextRectProc text_replace = [&](TextRect& tr) -> bool {
|
||||
for (const auto& [regex, new_str] : m_replace) {
|
||||
@@ -93,19 +99,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 = OcrConfig::get_instance().process_equivalence_class(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 = OcrConfig::get_instance().process_equivalence_class(key);
|
||||
auto new_val = OcrConfig::get_instance().process_equivalence_class(val);
|
||||
m_replace.emplace(std::move(new_key), std::move(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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user