mmdeploy/csrc/apis/python/text_recognizer.cpp
lzhangzz bb655af629
Add Python wrapper for SDK (#27)
* add python API for detector

* integrate detection

* add python segmentor

* add segmentation support

* add classifier, text-detector, text-recognizer and restorer

* integrate classifier

* integrate textdet, textrecog and restorer

* simplify

* add inst-seg

* fix inst-seg

* integrate inst-seg

* Moidfy _build_wrapper

* better pipeline substitution

* use registry for backend model creation

* build Python module according to C API targets

* minor fix

* move sdk data pipeline to backend_config

* remove debugging lines

* add docstring for SDKEnd2EndModel

* fix type hint

* fix lint

* fix lint

* insert build/lib to sys.path

Co-authored-by: SingleZombie <singlezombie@163.com>
2022-01-13 11:31:51 +08:00

65 lines
2.1 KiB
C++

// Copyright (c) OpenMMLab. All rights reserved.
#include "text_recognizer.h"
#include "common.h"
namespace mmdeploy {
class PyTextRecognizer {
public:
PyTextRecognizer(const char *model_path, const char *device_name, int device_id) {
auto status =
mmdeploy_text_recognizer_create_by_path(model_path, device_name, device_id, &handle_);
if (status != MM_SUCCESS) {
throw std::runtime_error("failed to create text_recognizer");
}
}
std::vector<std::tuple<std::string, std::vector<float>>> Apply(const std::vector<PyImage> &imgs) {
std::vector<mm_mat_t> mats;
mats.reserve(imgs.size());
for (const auto &img : imgs) {
auto mat = GetMat(img);
mats.push_back(mat);
}
mm_text_recognize_t *results{};
auto status = mmdeploy_text_recognizer_apply(handle_, mats.data(), (int)mats.size(), &results);
if (status != MM_SUCCESS) {
throw std::runtime_error("failed to apply text_recognizer, code: " + std::to_string(status));
}
auto output = std::vector<std::tuple<std::string, std::vector<float>>>{};
for (int i = 0; i < mats.size(); ++i) {
std::vector<float> score(results[i].score, results[i].score + results[i].length);
output.emplace_back(results[i].text, std::move(score));
}
mmdeploy_text_recognizer_release_result(results, (int)mats.size());
return output;
}
~PyTextRecognizer() {
mmdeploy_text_recognizer_destroy(handle_);
handle_ = {};
}
private:
mm_handle_t handle_{};
};
static void register_python_text_recognizer(py::module &m) {
py::class_<PyTextRecognizer>(m, "TextRecognizer")
.def(py::init([](const char *model_path, const char *device_name, int device_id) {
return std::make_unique<PyTextRecognizer>(model_path, device_name, device_id);
}))
.def("__call__", &PyTextRecognizer::Apply);
}
class PythonTextRecognizerRegisterer {
public:
PythonTextRecognizerRegisterer() {
gPythonBindings().emplace("text_recognizer", register_python_text_recognizer);
}
};
static PythonTextRecognizerRegisterer python_text_recognizer_registerer;
} // namespace mmdeploy