C++ 调用示例
入门例示:快速入门
请先下载好依赖库。清单如下:
- Lkinfer.dll(64位程序使用)
- Lkinfer32.dll(32位程序使用)
Lkinfer.lib
(64位 C++ 程序使用)Lkinfer32.lib
(32位 C++ 程序使用)
请根据您的应用选择相应依赖库。
提示
此文档作为 C++ 有些基础的同学参考。
导入依赖
- 添加链接库:
Lkinfer.lib
或者Lkinfer32.lib
- 添加头文件:
export_dll.h
:
C++
// 头文件
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#ifdef MATHLIBRARY_EXPORTS
#define EXPORT_DLL_API __declspec(dllexport)
#else
#define EXPORT_DLL_API __declspec(dllimport)
#endif
// 头文件
struct OcrParam {
int padding = 50; // 图像外接白框,用于提升识别率,文字框没有正确框住所有文字时,增加此值。默认50。
int max_size = 1024; // 按图像长边进行总体缩放,大图增加识别耗时但精度更高,小图减小耗时但精度降低,maxSize为0表示不缩放
float sim = 0.5f;// 文字置信度,推理的相似度,默认0.5
float box_thresh = 0.3f;// 用于控制检测到的文本框(bounding box)是否被保留的阈值参数,默认 0.3
float un_clip_ratio = 1.6f;// 单个文字框大小倍率,越大时单个文字框越大,默认1.6 [1.0-2.0(常见默认值1.5-1.8)]
int use_angle_cls = 0; // 是否启用文本方向分类器。关闭:0,启用:1。默认:0(关闭)
int most_angle = 0; // 控制方向分类的投票策略,决定是否采用多数角度作为最终结果。1或0。0:保持自己的预测角度。1:选择频率最高的角度作为全局方向 默认:0
};
EXPORT_DLL_API int existsYolov8Name(const char* name);
EXPORT_DLL_API int loadYolov8Model(const char* name, const char* modelPath);
EXPORT_DLL_API int reLoadYolov8Model(const char* name, const char* modelPath);
EXPORT_DLL_API const char* getYolov8Info(const char* name);
EXPORT_DLL_API const char* getAllYolov8Name();
EXPORT_DLL_API int setYolov8Sim(const char* name, float sim);
EXPORT_DLL_API int setYolov8Iou(const char* name, float iou);
EXPORT_DLL_API const char* yolov8Detect(const char* name, const char* imgPath);
EXPORT_DLL_API const char* yolov8DetectDraw(const char* name, const char* imgPath, const char* outPath);
EXPORT_DLL_API const char* version();
EXPORT_DLL_API const char* getLastError();
EXPORT_DLL_API int clearCache();
EXPORT_DLL_API int initOcrv4(int nThreads);
EXPORT_DLL_API const char* ocrv4Detect(const char* img_path, OcrParam* param_);
EXPORT_DLL_API const char* ocrv4DetectParam(
const char* img_path, float sim, int padding,
int max_size, float box_thresh, float un_clip_ratio,
int use_angle_cls, int most_angle
);
EXPORT_DLL_API int log_off();
EXPORT_DLL_API const char* utf8ToGBK(const char* u8);
#ifdef __cplusplus
}
#endif
demo 代码
下面使用控制台调用演示:
OCR 文字识别
c++
#include <Windows.h>
#include <iostream>
#include "export_dll.h"
int main() {
// 加载模型,返回值为 1 表示加载成功,0 表示加载失败
int res = initOcrv4(3);// 3 表示识别所使用的线程数,建议设置为 3 或 cpu核心数,根据电脑配置决定
if (res == 1)
{
MessageBoxA(NULL, "ocr 初始化完成,生命周期中只需要初始化一次!", "Message from C++", MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBoxA(NULL, "ocr 初始化失败:", "Message from C++", MB_OK | MB_ICONINFORMATION);
}
std::cout << "---------------------接口入参调用---------------------" << std::endl;
const char* result = ocrv4DetectParam("C:/Users/Administrator/Desktop/22.png", 0.5f, 50, 1024, 0.3f, 1.6f, 0, 0);
// window下console默认gbk,result是utf-8,如果想控制台不乱码,则需要转换一下
const char* gbk_result = utf8ToGBK(result);
std::cout << "ocrv4DetectParam:" << gbk_result << std::endl << std::endl;
std::cout << "---------------------结构体入参调用---------------------" << std::endl;
// 使用结构体
OcrParam param;
param.sim = 0.7f;
const char* result_structure = ocrv4Detect("C:/Users/Administrator/Desktop/22.png", ¶m);
// window下console默认gbk,result是utf-8,如果想控制台不乱码,则需要转换一下
const char* gbk_result_structure = utf8ToGBK(result);
std::cout << "ocrv4Detect:" << gbk_result_structure << std::endl;
return 0;
}
yolov8 目标检测
c++
#include <Windows.h>
#include <iostream>
#include "export_dll.h"
int main() {
int res = loadYolov8Model("C++", "C:\\Users\\Administrator\\Desktop\\yolo\\yolov8n.onnx");
if (res == 1)
{
MessageBoxA(NULL, "加载模型成功", "Message from C++", MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBoxA(NULL, "加载模型失败", "Message from C++", MB_OK | MB_ICONINFORMATION);
}
const char* result = yolov8Detect("C++", "C:\\Users\\Administrator\\Desktop\\yolo\\bus.jpg");
std::cout << "yolov8Detect:" << result << std::endl;
}