概述
继续上一篇
1,打开CMAKE3.23.3
第二个输出目录,如果没有相应文件夹创建一个
这里时CPU版本所以不要添加CUDA目录
依次点击configure、Generate、Open Project
2,生成dll
2.1,1、打开ppocr的项目属性并更改两个地方从exe改到dll
2.2.2、添加一个头文件ppocr.h
1,新建筛选器,命名为 头文件
2,右键筛选器,添加-》新建项
3,复制代码
#pragma once
///c+ +
#pragma once
#ifndef IMAGE_API
#define IMAGE_API
extern "C"
{
// 图像推理
__declspec(dllexport) char* ImageProcess(char* image_dir);
}
#endif
///
2.3.3、先注释掉main.cpp中的所有代码
2.4.4、添加以下代码
#include <string>
#include <iostream>
#include <vector>
#include <include/paddleocr.h>
#include <include/args.h>
#include "ppocr.h"
//#include <include/ppocr.h>
using namespace PaddleOCR;
PPOCR ocr; //PPOCR的对象
//处理图片的函数
char* ImageProcess(char* image_dir)
{
std::vector<cv::String> cv_all_img_names;
cv::glob(image_dir, cv_all_img_names);
if (cv_all_img_names.size() > 1)
{
return "只支持单独图片识别!";
}
std::vector<std::vector<OCRPredictResult>> ocr_results =
ocr.ocr(cv_all_img_names, FLAGS_det, FLAGS_rec, FLAGS_cls);
auto ocr_result = ocr_results[0];
std::string ret_str;
for (int i = 0; i < ocr_result.size(); i++)
{
if (ocr_result[i].score != -1.0) {
ret_str.append(ocr_result[i].text + "n");
}
}
return const_cast<char*>(ret_str.c_str());
}
2.5.5、在args.cpp下更改一下默认参数,这里我将模型文件改成了和dll同目录下,这里也可以改成参数传入dll。
#include <gflags/gflags.h>
// common args
DEFINE_bool(use_gpu, false, "Infering with GPU or CPU.");
DEFINE_bool(use_tensorrt, false, "Whether use tensorrt.");
DEFINE_int32(gpu_id, 0, "Device id of GPU to execute.");
DEFINE_int32(gpu_mem, 4000, "GPU id when infering with GPU.");
DEFINE_int32(cpu_threads, 10, "Num of threads with CPU.");
DEFINE_bool(enable_mkldnn, false, "Whether use mkldnn with CPU.");
DEFINE_string(precision, "fp32", "Precision be one of fp32/fp16/int8");
DEFINE_bool(benchmark, false, "Whether use benchmark.");
DEFINE_string(output, "./output/", "Save benchmark log path.");
DEFINE_string(image_dir, "", "Dir of input image.");
DEFINE_string(
type, "ocr",
"Perform ocr or structure, the value is selected in ['ocr','structure'].");
// detection related
DEFINE_string(det_model_dir, "./models/ch_PP-OCRv3_det_infer", "Path of det inference model.");
DEFINE_int32(max_side_len, 960, "max_side_len of input image.");
DEFINE_double(det_db_thresh, 0.3, "Threshold of det_db_thresh.");
DEFINE_double(det_db_box_thresh, 0.6, "Threshold of det_db_box_thresh.");
DEFINE_double(det_db_unclip_ratio, 1.5, "Threshold of det_db_unclip_ratio.");
DEFINE_bool(use_dilation, false, "Whether use the dilation on output map.");
DEFINE_string(det_db_score_mode, "slow", "Whether use polygon score.");
DEFINE_bool(visualize, true, "Whether show the detection results.");
// classification related
DEFINE_bool(use_angle_cls, false, "Whether use use_angle_cls.");
DEFINE_string(cls_model_dir, "", "Path of cls inference model.");
DEFINE_double(cls_thresh, 0.9, "Threshold of cls_thresh.");
DEFINE_int32(cls_batch_num, 1, "cls_batch_num.");
// recognition related
DEFINE_string(rec_model_dir, "./models/ch_PP-OCRv3_rec_infer", "Path of rec inference model.");
DEFINE_int32(rec_batch_num, 6, "rec_batch_num.");
DEFINE_string(rec_char_dict_path, "./models/ppocr_keys_v1.txt",
"Path of dictionary.");
DEFINE_int32(rec_img_h, 48, "rec image height");
DEFINE_int32(rec_img_w, 320, "rec image width");
// ocr forward related
DEFINE_bool(det, true, "Whether use det in forward.");
DEFINE_bool(rec, true, "Whether use rec in forward.");
DEFINE_bool(cls, false, "Whether use cls in forward.");
2.6.6、生成项目,一切顺利的话会得到一个ppocr.dll
3,测试DLL
3.1.编写一个测试的exe,把模型文件和DLL文件放在项目目录
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
system("chcp 65001");
HINSTANCE hDll = LoadLibrary(L"ppocr.dll");// 加载DLL库文件,DLL名称和路径用自己的
if (hDll == NULL)
{
std::cout << "load dll fail n";
return -1;
}
const auto ImageProcess{ reinterpret_cast<char* (*)(const char* image_dir)>(GetProcAddress(hDll, "ImageProcess")) };
for (int xxd=0; xxd < 10; xxd++) {
char* result2 = ImageProcess("./images/12.png");
std::cout << result2;
cout << "---------------------------------------------------------------" << endl;
}
}
最后
以上就是痴情小松鼠为你收集整理的Win10 + C++ + Paddle编译成DLL C++调用OCR文字识别的全部内容,希望文章能够帮你解决Win10 + C++ + Paddle编译成DLL C++调用OCR文字识别所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复