feat: support GPU OCR

This commit is contained in:
MistEO
2023-10-11 17:44:18 +08:00
parent e32c580c76
commit 4f1bf344c4
4 changed files with 29 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
#include <meojson/json.hpp>
#include "Config/GeneralConfig.h"
#include "Config/Miscellaneous/OcrPack.h"
#include "Config/ResourceLoader.h"
#include "Controller/Controller.h"
#include "Status.h"
@@ -34,6 +35,24 @@ using namespace asst;
bool ::AsstExtAPI::set_static_option(StaticOptionKey key, const std::string& value)
{
Log.info(__FUNCTION__, "| key", static_cast<int>(key), "value", value);
switch (key) {
case StaticOptionKey::CpuOCR: {
WordOcr::get_instance().use_cpu();
CharOcr::get_instance().use_cpu();
return true;
} break;
case StaticOptionKey::GpuOCR: {
int device_id = std::stoi(value);
WordOcr::get_instance().use_gpu(device_id);
CharOcr::get_instance().use_gpu(device_id);
return true;
} break;
default:
Log.error(__FUNCTION__, "| unknown key:", static_cast<int>(key));
break;
}
return false;
}

View File

@@ -27,6 +27,8 @@ namespace asst
enum class StaticOptionKey
{
Invalid = 0,
CpuOCR = 1, // use CPU to OCR, no value. It does not support switching after the resource is loaded.
GpuOCR = 2, // use GPU to OCR, value is gpu_id int to string. It does not support switching after the resource is loaded.
};
enum class InstanceOptionKey

View File

@@ -131,6 +131,9 @@ bool asst::OcrPack::check_and_load()
fastdeploy::RuntimeOption option;
option.UseOrtBackend();
if (m_gpu_id) {
option.UseGpu(*m_gpu_id);
}
auto det_model = asst::utils::read_file<std::string>(m_det_model_path);
option.SetModelBuffer(det_model.data(), det_model.size(), nullptr, 0, fastdeploy::ModelFormat::ONNX);

View File

@@ -4,6 +4,7 @@
#include "Config/AbstractResource.h"
#include <vector>
#include <optional>
namespace cv
{
@@ -38,6 +39,8 @@ namespace asst
virtual ~OcrPack() override;
virtual bool load(const std::filesystem::path& path) override;
void use_cpu() { m_gpu_id = std::nullopt; }
void use_gpu(int gpu_id) { m_gpu_id = gpu_id; }
ResultsVec recognize(const cv::Mat& image, bool without_det = false);
@@ -53,6 +56,8 @@ namespace asst
std::filesystem::path m_det_model_path;
std::filesystem::path m_rec_model_path;
std::filesystem::path m_rec_label_path;
std::optional<int> m_gpu_id = std::nullopt;
};
class WordOcr final : public SingletonHolder<WordOcr>, public OcrPack