Skip to content

Commit

Permalink
fix: OCR REC using CPU for CoreML
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 29, 2024
1 parent 5857661 commit f502540
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
24 changes: 17 additions & 7 deletions source/MaaFramework/Resource/OCRResMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,43 @@ OCRResMgr::OCRResMgr()
{
LogFunc;

option_.UseOrtBackend();
det_option_.UseOrtBackend();
rec_option_.UseOrtBackend();
}

void OCRResMgr::use_cpu()
{
LogInfo;

option_.UseCpu();
det_option_.UseCpu();
rec_option_.UseCpu();
}

void OCRResMgr::use_cuda(int device_id)
{
LogInfo << VAR(device_id);

option_.UseCuda(device_id);
det_option_.UseCuda(device_id);
rec_option_.UseCuda(device_id);
}

void OCRResMgr::use_directml(int device_id)
{
LogInfo << VAR(device_id);
option_.UseDirectML(device_id);

det_option_.UseDirectML(device_id);
rec_option_.UseDirectML(device_id);
}

void OCRResMgr::use_coreml(uint32_t coreml_flag)
{
LogInfo << VAR(coreml_flag);
option_.UseCoreML(coreml_flag);

det_option_.UseCoreML(coreml_flag);

LogWarn << "OCR REC with CoreML is very poor. I don’t know the reason yet. Roll back to using CPU for REC. (DET still uses CoreML)";
//rec_option_.UseCoreML(coreml_flag);
rec_option_.UseCpu();
}

bool OCRResMgr::lazy_load(const std::filesystem::path& path, bool is_base)
Expand Down Expand Up @@ -123,7 +133,7 @@ std::shared_ptr<fastdeploy::vision::ocr::DBDetector> OCRResMgr::load_deter(const
LogDebug << VAR(model_path);

auto det =
std::make_shared<fastdeploy::vision::ocr::DBDetector>(path_to_utf8_string(model_path), std::string(), option_, fastdeploy::ModelFormat::ONNX);
std::make_shared<fastdeploy::vision::ocr::DBDetector>(path_to_utf8_string(model_path), std::string(), det_option_, fastdeploy::ModelFormat::ONNX);
if (!det || !det->Initialized()) {
LogError << "Failed to load DBDetector:" << VAR(name) << VAR(det) << VAR(det->Initialized());
return nullptr;
Expand Down Expand Up @@ -153,7 +163,7 @@ std::shared_ptr<fastdeploy::vision::ocr::Recognizer> OCRResMgr::load_recer(const
path_to_utf8_string(model_path),
std::string(),
path_to_utf8_string(label_path),
option_,
rec_option_,
fastdeploy::ModelFormat::ONNX);
if (!rec || !rec->Initialized()) {
LogError << "Failed to load Recognizer:" << VAR(name) << VAR(rec) << VAR(rec->Initialized());
Expand Down
3 changes: 2 additions & 1 deletion source/MaaFramework/Resource/OCRResMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class OCRResMgr : public NonCopyable

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

fastdeploy::RuntimeOption option_;
fastdeploy::RuntimeOption det_option_;
fastdeploy::RuntimeOption rec_option_;

std::unordered_map<std::string, std::shared_ptr<fastdeploy::vision::ocr::DBDetector>> deters_;
std::unordered_map<std::string, std::shared_ptr<fastdeploy::vision::ocr::Recognizer>> recers_;
Expand Down

0 comments on commit f502540

Please sign in to comment.