1 #ifndef _NNDEPLOY_DETECT_RECOGNIZER_OCR_OCR_H_
2 #define _NNDEPLOY_DETECT_RECOGNIZER_OCR_OCR_H_
48 bool normalize_ =
true;
49 int rec_batch_size_ = 6;
50 std::vector<int> rec_image_shape_ = {3, 48, 320};
52 float scale_[3] = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
53 float mean_[3] = {0.5f, 0.5f, 0.5f};
54 float std_[3] = {0.5f, 0.5f, 0.5f};
65 rapidjson::Value &json,
66 rapidjson::Document::AllocatorType &allocator)
override {
68 json.AddMember(
"src_pixel_type_",
69 rapidjson::Value(src_pixel_type_str.c_str(), allocator),
72 json.AddMember(
"dst_pixel_type_",
73 rapidjson::Value(dst_pixel_type_str.c_str(), allocator),
76 json.AddMember(
"interp_type_",
77 rapidjson::Value(interp_type_str.c_str(), allocator),
80 json.AddMember(
"data_type_",
81 rapidjson::Value(data_type_str.c_str(), allocator),
84 json.AddMember(
"data_format_",
85 rapidjson::Value(data_format_str.c_str(), allocator),
87 json.AddMember(
"h_", h_, allocator);
88 json.AddMember(
"w_", w_, allocator);
89 json.AddMember(
"rec_batch_size_", rec_batch_size_, allocator);
90 rapidjson::Value rec_image_shape(rapidjson::kArrayType);
91 for (
int i = 0; i < 3; i++) {
92 rec_image_shape.PushBack(rec_image_shape_[i], allocator);
94 json.AddMember(
"rec_image_shape_", rec_image_shape, allocator);
95 json.AddMember(
"normalize_", normalize_, allocator);
97 rapidjson::Value scale_array(rapidjson::kArrayType);
98 rapidjson::Value mean_array(rapidjson::kArrayType);
99 rapidjson::Value std_array(rapidjson::kArrayType);
100 for (
int i = 0; i < 3; i++) {
101 scale_array.PushBack(scale_[i], allocator);
102 mean_array.PushBack(mean_[i], allocator);
103 std_array.PushBack(std_[i], allocator);
105 json.AddMember(
"scale_", scale_array, allocator);
106 json.AddMember(
"mean_", mean_array, allocator);
107 json.AddMember(
"std_", std_array, allocator);
110 json.AddMember(
"border_type_",
111 rapidjson::Value(border_type_str.c_str(), allocator),
113 json.AddMember(
"top_", top_, allocator);
114 json.AddMember(
"bottom_", bottom_, allocator);
115 json.AddMember(
"left_", left_, allocator);
116 json.AddMember(
"right_", right_, allocator);
118 rapidjson::Value border_val_array(rapidjson::kArrayType);
119 for (
int i = 0; i < 4; i++) {
120 border_val_array.PushBack(border_val_.
val_[i], allocator);
122 json.AddMember(
"border_val_", border_val_array, allocator);
129 if (json.HasMember(
"src_pixel_type_") &&
130 json[
"src_pixel_type_"].IsString()) {
134 if (json.HasMember(
"dst_pixel_type_") &&
135 json[
"dst_pixel_type_"].IsString()) {
139 if (json.HasMember(
"interp_type_") && json[
"interp_type_"].IsString()) {
142 if (json.HasMember(
"data_type_") && json[
"data_type_"].IsString()) {
145 if (json.HasMember(
"data_format_") && json[
"data_format_"].IsString()) {
148 if (json.HasMember(
"h_") && json[
"h_"].IsInt()) {
149 h_ = json[
"h_"].GetInt();
151 if (json.HasMember(
"w_") && json[
"w_"].IsInt()) {
152 w_ = json[
"w_"].GetInt();
155 if (json.HasMember(
"rec_batch_size_") && json[
"rec_batch_size_"].IsInt()) {
156 rec_batch_size_ = json[
"rec_batch_size_"].GetInt();
158 if (json.HasMember(
"rec_image_shape_") &&
159 json[
"rec_image_shape_"].IsArray()) {
160 const rapidjson::Value &rec_image_shape_array = json[
"rec_image_shape_"];
161 for (
int i = 0; i < 3 && i < rec_image_shape_array.Size(); i++) {
162 if (rec_image_shape_array[i].IsInt()) {
163 rec_image_shape_[i] = rec_image_shape_array[i].GetInt();
168 if (json.HasMember(
"normalize_") && json[
"normalize_"].IsBool()) {
169 normalize_ = json[
"normalize_"].GetBool();
172 if (json.HasMember(
"scale_") && json[
"scale_"].IsArray()) {
173 const rapidjson::Value &scale_array = json[
"scale_"];
174 for (
int i = 0; i < 3 && i < scale_array.Size(); i++) {
175 if (scale_array[i].IsFloat()) {
176 scale_[i] = scale_array[i].GetFloat();
180 if (json.HasMember(
"mean_") && json[
"mean_"].IsArray()) {
181 const rapidjson::Value &mean_array = json[
"mean_"];
182 for (
int i = 0; i < 3 && i < mean_array.Size(); i++) {
183 if (mean_array[i].IsFloat()) {
184 mean_[i] = mean_array[i].GetFloat();
188 if (json.HasMember(
"std_") && json[
"std_"].IsArray()) {
189 const rapidjson::Value &std_array = json[
"std_"];
190 for (
int i = 0; i < 3 && i < std_array.Size(); i++) {
191 if (std_array[i].IsFloat()) {
192 std_[i] = std_array[i].GetFloat();
197 if (json.HasMember(
"border_type_") && json[
"border_type_"].IsString()) {
200 if (json.HasMember(
"top_") && json[
"top_"].IsInt()) {
201 top_ = json[
"top_"].GetInt();
203 if (json.HasMember(
"bottom_") && json[
"bottom_"].IsInt()) {
204 bottom_ = json[
"bottom_"].GetInt();
206 if (json.HasMember(
"left_") && json[
"left_"].IsInt()) {
207 left_ = json[
"left_"].GetInt();
209 if (json.HasMember(
"right_") && json[
"right_"].IsInt()) {
210 right_ = json[
"right_"].GetInt();
213 if (json.HasMember(
"border_val_") && json[
"border_val_"].IsArray()) {
214 const rapidjson::Value &border_val_array = json[
"border_val_"];
215 for (
int i = 0; i < 4 && i < border_val_array.Size(); i++) {
216 if (border_val_array[i].IsFloat()) {
217 border_val_.
val_[i] = border_val_array[i].GetFloat();
229 key_ =
"nndeploy::ocr::RecognizerPreProcess";
231 "ocr recognizer preprocess cv::Mat to "
232 "device::Tensor[resize->pad->normalize->transpose]";
233 param_ = std::make_shared<RecognizerPreProcessParam>();
234 this->setInputTypeInfo<OCRResult>();
235 this->setOutputTypeInfo<device::Tensor>();
238 std::vector<dag::Edge *> outputs)
239 : dag::Node(name, inputs, outputs) {
240 key_ =
"nndeploy::ocr::RecognizerPreProcess";
242 "ocr recognizer preprocess cv::Mat to "
243 "device::Tensor[resize->pad->normalize->transpose]";
244 param_ = std::make_shared<RecognizerPreProcessParam>();
245 this->setInputTypeInfo<OCRResult>();
246 this->setOutputTypeInfo<device::Tensor>();
256 double rec_thresh_ = 0.2;
261 rapidjson::Document::AllocatorType &allocator);
269 key_ =
"nndeploy::ocr::RecognizerPostProcess";
270 desc_ =
"PPOcrRecv3/v4/v5 postprocess[device::Tensor->DetectResult]";
271 param_ = std::make_shared<RecognizerPostParam>();
272 this->setInputTypeInfo<device::Tensor>();
273 this->setOutputTypeInfo<OCRResult>();
276 std::vector<dag::Edge *> inputs,
277 std::vector<dag::Edge *> outputs)
278 : dag::Node(name, inputs, outputs) {
279 key_ =
"nndeploy::ocr::RecognizerPostProcess";
280 desc_ =
"PPOcrRecv3/v4/v5 postprocess[device::Tensor->DetectResult]";
281 param_ = std::make_shared<RecognizerPostParam>();
282 this->setInputTypeInfo<device::Tensor>();
283 this->setOutputTypeInfo<OCRResult>();
293 key_ =
"nndeploy::ocr::RecognizerGraph";
296 "graph[cv::Mat->preprocess->infer->postprocess->OcrResult]";
297 this->setInputTypeInfo<OCRResult>();
298 this->setOutputTypeInfo<OCRResult>();
300 this->createNode<RecognizerPreProcess>(
"preprocess"));
302 dynamic_cast<infer::Infer *
>(this->createNode<infer::Infer>(
"infer"));
304 this->createNode<RecognizerPostProcess>(
"postprocess"));
308 std::vector<dag::Edge *> outputs)
309 : dag::Graph(name, inputs, outputs) {
310 key_ =
"nndeploy::ocr::RecognizerGraph";
313 "graph[cv::Mat->preprocess->infer->postprocess->DetectResult]";
314 this->setInputTypeInfo<OCRResult>();
315 this->setOutputTypeInfo<OCRResult>();
317 this->createNode<RecognizerPreProcess>(
"preprocess"));
319 dynamic_cast<infer::Infer *
>(this->createNode<infer::Infer>(
"infer"));
321 this->createNode<RecognizerPostProcess>(
"postprocess"));
345 this->setNodeDesc(pre_, pre_desc);
346 this->setNodeDesc(infer_, infer_desc);
347 this->setNodeDesc(post_, post_desc);
348 this->defaultParam();
349 base::Status status = infer_->setInferenceType(inference_type);
359 base::Status status = infer_->setInferenceType(inference_type);
368 std::vector<std::string> &model_value) {
371 param->model_type_ = model_type;
372 param->is_path_ = is_path;
373 param->model_value_ = model_value;
378 if (character_path.empty()) {
405 std::vector<dag::Edge *>
forward(std::vector<dag::Edge *> inputs) {
406 inputs = (*pre_)(inputs);
407 inputs = (*infer_)(inputs);
408 std::vector<dag::Edge *> outputs = (*post_)(inputs);
virtual base::Status deserialize(rapidjson::Value &json)
virtual std::string serialize()
Template class for a 4-element vector. Scalar_ and Scalar can be used just as typical 4-element vecto...
Directed Acyclic Graph Node.
InferenceParam is the base class of all inference param.
base::DeviceType device_type_
RecognizerGraph(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
virtual ~RecognizerGraph()
base::Status make(const dag::NodeDesc &pre_desc, const dag::NodeDesc &infer_desc, base::InferenceType inference_type, const dag::NodeDesc &post_desc)
base::Status setCharacterPath(const std::string &character_path)
virtual base::Status defaultParam()
Set default parameters.
base::Status setInferParam(base::DeviceType device_type, base::ModelType model_type, bool is_path, std::vector< std::string > &model_value)
RecognizerGraph(const std::string &name)
base::Status setVersion(int version)
base::Status setRecThresh(float threshold)
base::Status setInferenceType(base::InferenceType inference_type)
std::vector< dag::Edge * > forward(std::vector< dag::Edge * > inputs)
base::Status setSrcPixelType(base::PixelType pixel_type)
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
std::string character_path_
virtual base::Status deserialize(rapidjson::Value &json)
RecognizerPostProcess(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
PostProcessor util_post_processor_
virtual ~RecognizerPostProcess()
virtual base::Status run()
Run node (pure virtual function)
RecognizerPostProcess(const std::string &name)
virtual base::Status deserialize(rapidjson::Value &json) override
std::vector< int > rec_image_shape_
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) override
base::PixelType dst_pixel_type_
base::InterpType interp_type_
base::PixelType src_pixel_type_
RecognizerPreProcess(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
virtual base::Status run()
Run node (pure virtual function)
RecognizerPreProcess(const std::string &name)
virtual ~RecognizerPreProcess()
#define NNDEPLOY_LOGE(fmt,...)
#define NNDEPLOY_CC_API
api
std::string dataFormatToString(DataFormat data_format)
PixelType stringToPixelType(const std::string &pixel_type_str)
@ kStatusCodeErrorInvalidParam
BorderType stringToBorderType(const std::string &border_type_str)
DataType stringToDataType(const std::string &str)
DataFormat stringToDataFormat(const std::string &str)
std::string borderTypeToString(BorderType border_type)
std::string dataTypeToString(DataType data_type)
DataType dataTypeOf< float >()
std::string pixelTypeToString(PixelType pixel_type)
std::string interpTypeToString(InterpType interp_type)
InterpType stringToInterpType(const std::string &interp_type_str)