Skip to content

Commit

Permalink
refactor: 重构模板图片读取路径
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Sep 8, 2023
1 parent 62d00f6 commit 2b30ff4
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 222 deletions.
Empty file added assets/resource/image/.gitkeep
Empty file.
Empty file.
2 changes: 0 additions & 2 deletions source/MaaFramework/Resource/ONNXResMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

MAA_RES_NS_BEGIN

ONNXResMgr::ONNXResMgr() {}

bool ONNXResMgr::lazy_load(const std::filesystem::path& path, bool is_base)
{
LogFunc << VAR(path) << VAR(is_base);
Expand Down
3 changes: 1 addition & 2 deletions source/MaaFramework/Resource/ONNXResMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ MAA_RES_NS_BEGIN
class ONNXResMgr : public NonCopyable
{
public:
// TODO: 这俩可以拆一下放两个 ResMgr 里
// TODO: 拆一下放两个 ResMgr 里?
inline static const std::filesystem::path kClassifierDir = "classify";
inline static const std::filesystem::path kDetectorDir = "detect";

public:
ONNXResMgr();
bool lazy_load(const std::filesystem::path& path, bool is_base);
void clear();

Expand Down
35 changes: 1 addition & 34 deletions source/MaaFramework/Resource/PipelineResMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ bool PipelineResMgr::load(const std::filesystem::path& path, bool is_base)
paths_.emplace_back(path);

bool loaded = load_all_json(path);
loaded &= load_template_images(path);
loaded &= check_all_next_list();

return loaded;
Expand All @@ -29,7 +28,6 @@ void PipelineResMgr::clear()

task_data_map_.clear();
paths_.clear();
template_mgr_.clear();
}

const TaskData& PipelineResMgr::get_task_data(const std::string& task_name)
Expand All @@ -41,15 +39,7 @@ const TaskData& PipelineResMgr::get_task_data(const std::string& task_name)
return empty;
}

auto& task_data = task_iter->second;
if (task_data.rec_type == Recognition::Type::TemplateMatch) {
auto& images = std::get<MAA_VISION_NS::TemplMatchingParam>(task_data.rec_param).template_images;
if (images.empty()) {
images = template_mgr_.get_template_images(task_name);
}
}

return task_data;
return task_iter->second;
}

bool PipelineResMgr::load_all_json(const std::filesystem::path& path)
Expand Down Expand Up @@ -108,26 +98,6 @@ bool PipelineResMgr::open_and_parse_file(const std::filesystem::path& path)
return true;
}

bool PipelineResMgr::load_template_images(const std::filesystem::path& path)
{
LogFunc << VAR(path);

template_mgr_.add_root(path);

for (const auto& [name, task_data] : task_data_map_) {
if (task_data.rec_type != Recognition::Type::TemplateMatch) {
continue;
}
const auto& templates = std::get<MAA_VISION_NS::TemplMatchingParam>(task_data.rec_param).template_paths;
bool ret = template_mgr_.lazy_load(name, templates);
if (!ret) {
LogError << "template_cfg_.lazy_load failed" << VAR(name) << VAR(path) << VAR(templates);
return false;
}
}
return true;
}

bool PipelineResMgr::check_all_next_list() const
{
LogFunc;
Expand Down Expand Up @@ -425,9 +395,6 @@ bool PipelineResMgr::parse_templ_matching_param(const json::value& input, MAA_VI
LogError << "templates is empty" << VAR(input);
return false;
}
if (output.template_paths == default_value.template_paths) {
output.template_images = default_value.template_images;
}

if (!get_and_check_value_or_array(input, "threshold", output.thresholds, default_value.thresholds)) {
LogError << "failed to get_and_check_value_or_array threshold" << VAR(input);
Expand Down
4 changes: 0 additions & 4 deletions source/MaaFramework/Resource/PipelineResMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "Conf/Conf.h"
#include "PipelineTypes.h"
#include "TemplateResMgr.h"

MAA_RES_NS_BEGIN

Expand All @@ -26,7 +25,6 @@ class PipelineResMgr : public NonCopyable

const std::vector<std::filesystem::path>& get_paths() const { return paths_; }
const TaskDataMap& get_task_data_map() const { return task_data_map_; }
const TemplateResMgr& get_template_mgr() const { return template_mgr_; }

public:
static bool parse_config(const json::value& input, TaskDataMap& output, const TaskDataMap& default_value);
Expand Down Expand Up @@ -74,14 +72,12 @@ class PipelineResMgr : public NonCopyable
private:
bool load_all_json(const std::filesystem::path& path);
bool open_and_parse_file(const std::filesystem::path& path);
bool load_template_images(const std::filesystem::path& path);
bool check_all_next_list() const;
bool check_next_list(const TaskData::NextList& next_list) const;

private:
std::vector<std::filesystem::path> paths_;
TaskDataMap task_data_map_;
TemplateResMgr template_mgr_;
};

MAA_RES_NS_END
1 change: 1 addition & 0 deletions source/MaaFramework/Resource/ResourceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ bool ResourceMgr::load(const std::filesystem::path& path)
bool ret = pipeline_res_.load(path / "pipeline"_path, is_base);
ret &= ocr_res_.lazy_load(path / "model"_path / "ocr"_path, is_base);
ret &= onnx_res_.lazy_load(path / "model"_path, is_base);
ret &= template_res_.lazy_load(path / "image"_path, is_base);

LogInfo << VAR(path) << VAR(ret);

Expand Down
3 changes: 3 additions & 0 deletions source/MaaFramework/Resource/ResourceMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ResourceMgr : public MaaResourceAPI
auto& ocr_res() { return ocr_res_; }
const auto& onnx_res() const { return onnx_res_; }
auto& onnx_res() { return onnx_res_; }
const auto& template_res() const { return template_res_; }
auto& template_res() { return template_res_; }

private:
bool run_load(typename AsyncRunner<std::filesystem::path>::Id id, std::filesystem::path path);
Expand All @@ -44,6 +46,7 @@ class ResourceMgr : public MaaResourceAPI
PipelineResMgr pipeline_res_;
OCRResMgr ocr_res_;
ONNXResMgr onnx_res_;
TemplateResMgr template_res_;

private:
std::vector<std::filesystem::path> paths_;
Expand Down
106 changes: 28 additions & 78 deletions source/MaaFramework/Resource/TemplateResMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,15 @@

MAA_RES_NS_BEGIN

void TemplateResMgr::add_root(const std::filesystem::path& root)
bool TemplateResMgr::lazy_load(const std::filesystem::path& path, bool is_base)
{
roots_.emplace_back(root);
}

void TemplateResMgr::set_roots(const std::vector<std::filesystem::path>& roots)
{
roots_ = roots;
}

bool TemplateResMgr::lazy_load(const std::string& name, const std::vector<std::string>& filenames)
{
LogDebug << VAR(name) << VAR(filenames);

template_filenames_.insert_or_assign(name, filenames);
template_cache_.erase(name);
LogFunc << VAR(path) << VAR(is_base);

#ifdef MAA_DEBUG
const auto& images = get_template_images(name);
if (images.empty() || MAA_RNS::ranges::any_of(images, [](const auto& image) -> bool { return image.empty(); })) {
LogError << "image is empty" << VAR(name) << VAR(images);
return false;
if (is_base) {
clear();
}
#endif

roots_.emplace_back(path);
return true;
}

Expand All @@ -38,77 +22,43 @@ void TemplateResMgr::clear()
LogFunc;

roots_.clear();
template_filenames_.clear();
template_cache_.clear();
template_bank_.clear();
images_.clear();
}

const std::vector<cv::Mat>& TemplateResMgr::get_template_images(const std::string& name) const
std::shared_ptr<TemplateResMgr::Image> TemplateResMgr::image(const std::string& name) const
{
if (auto templ_iter = template_cache_.find(name); templ_iter != template_cache_.end()) {
return templ_iter->second;
if (auto iter = images_.find(name); iter != images_.end()) {
return iter->second;
}

return load_images(name);
auto img = load(name);
if (img) {
images_.emplace(name, img);
}

return img;
}

const std::vector<cv::Mat>& TemplateResMgr::load_images(const std::string& name) const
std::shared_ptr<TemplateResMgr::Image> TemplateResMgr::load(const std::string& name) const
{
LogFunc << VAR(name);

auto path_iter = template_filenames_.find(name);
if (path_iter == template_filenames_.end()) {
LogError << "Invalid template name" << VAR(name);
static std::vector<cv::Mat> empty;
return empty;
}
LogFunc << VAR(name) << VAR(roots_);

std::vector<cv::Mat> images;
for (const auto& filename : path_iter->second) {
cv::Mat templ_mat;
for (const auto& root : roots_ | MAA_RNS::views::reverse) {
auto path = root / MAA_NS::path(filename);
templ_mat = load_single_image(path);
if (!templ_mat.empty()) {
break;
}
}
if (templ_mat.empty()) {
LogError << "template image is empty" << VAR(name) << VAR(filename) << VAR(roots_);
#ifdef MAA_DEBUG
static std::vector<cv::Mat> empty;
return empty;
#else
for (const auto& root : roots_ | MAA_RNS::views::reverse) {
auto path = root / MAA_NS::path(name);
if (!std::filesystem::exists(path)) {
continue;
#endif
}
images.emplace_back(std::move(templ_mat));
}

if (images.empty()) {
LogError << "template list is empty" << VAR(name) << VAR(path_iter->second) << VAR(roots_);
static std::vector<cv::Mat> empty;
return empty;
}

return template_cache_.emplace(name, std::move(images)).first->second;
}

cv::Mat TemplateResMgr::load_single_image(const std::filesystem::path& path) const
{
if (auto bank_iter = template_bank_.find(path); bank_iter != template_bank_.end()) {
LogDebug << "Withdraw image" << VAR(path);
return bank_iter->second;
}
else if (std::filesystem::exists(path)) {
LogDebug << "Read image" << VAR(path);
cv::Mat temp = MAA_NS::imread(path);
template_bank_.emplace(path, temp);
return temp;
LogDebug << VAR(path);
cv::Mat image = MAA_NS::imread(path);
if (image.empty()) {
LogError << "Failed to load image: " << path;
return nullptr;
}
return std::make_shared<Image>(std::move(image));
}

LogDebug << "Image not found" << VAR(path);
return {};
return nullptr;
}

MAA_RES_NS_END
19 changes: 8 additions & 11 deletions source/MaaFramework/Resource/TemplateResMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ MAA_RES_NS_BEGIN
class TemplateResMgr : public NonCopyable
{
public:
void add_root(const std::filesystem::path& root);
void set_roots(const std::vector<std::filesystem::path>& roots);
using Image = cv::Mat;

bool lazy_load(const std::filesystem::path& path, bool is_base);

bool lazy_load(const std::string& name, const std::vector<std::string>& filenames);
void clear();

public:
const std::vector<cv::Mat>& get_template_images(const std::string& name) const;
const std::vector<cv::Mat>& load_images(const std::string& name) const;
cv::Mat load_single_image(const std::filesystem::path& path) const;
std::shared_ptr<Image> image(const std::string& name) const;

private:
// for lazy load
std::vector<std::filesystem::path> roots_; // 类似环境变量,一级一级找,优先级高的在后面
std::map<std::string, std::vector<std::string>> template_filenames_;
std::shared_ptr<Image> load(const std::string& name) const;

std::vector<std::filesystem::path> roots_;

mutable std::map<std::string, std::vector<cv::Mat>> template_cache_;
mutable std::map<std::filesystem::path, cv::Mat> template_bank_;
mutable std::map<std::string, std::shared_ptr<Image>> images_;
};

MAA_RES_NS_END
30 changes: 28 additions & 2 deletions source/MaaFramework/Task/Recognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,26 @@ std::optional<Recognizer::Result> Recognizer::template_match(const cv::Mat& imag
{
using namespace MAA_VISION_NS;

if (!resource()) {
LogError << "Resource not binded";
return std::nullopt;
}

Matcher matcher(inst_, image);
matcher.set_name(name);
matcher.set_param(param);
matcher.set_cache(cache);
matcher.set_name(name);

std::vector<std::shared_ptr<cv::Mat>> templates;
for (const auto& path : param.template_paths) {
auto templ = resource()->template_res().image(path);
if (!templ) {
LogWarn << "Template not found:" << path;
continue;
}
templates.emplace_back(std::move(templ));
}
matcher.set_templates(std::move(templates));

auto ret = matcher.analyze();
if (ret.empty()) {
Expand All @@ -105,10 +121,20 @@ std::optional<Recognizer::Result> Recognizer::ocr(const cv::Mat& image, const MA
{
using namespace MAA_VISION_NS;

if (!resource()) {
LogError << "Resource not binded";
return std::nullopt;
}

OCRer ocrer(inst_, image);
ocrer.set_name(name);
ocrer.set_param(param);
ocrer.set_cache(cache);
ocrer.set_name(name);

auto det_session = resource()->ocr_res().deter(param.model);
auto rec_session = resource()->ocr_res().recer(param.model);
auto ocr_session = resource()->ocr_res().ocrer(param.model);
ocrer.set_session(std::move(det_session), std::move(rec_session), std::move(ocr_session));

auto ret = ocrer.analyze();
if (ret.empty()) {
Expand Down
Loading

0 comments on commit 2b30ff4

Please sign in to comment.