设置OCR配置 - SetOcrConfig
函数简介
设置OCR(光学字符识别)的配置参数。此函数可以通过JSON格式的配置字符串批量设置PP-OCRv5模型的各种参数,包括GPU设置、检测参数、识别参数、分类参数等。适用于需要批量配置OCR参数的场景。
接口名称
SetOcrConfig
DLL调用
int SetOcrConfig(long instance, string configStr)
参数说明
参数名 | 类型 | 说明 |
---|---|---|
instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
configStr | 字符串 | JSON格式的配置字符串,包含多个OCR配置参数 |
示例
// 基本GPU配置设置
string gpuConfig = "{\"OcrUseGpu\":true,\"OcrGpuId\":0,\"OcrGpuMem\":4000}";
int result = SetOcrConfig(ola, gpuConfig);
if (result == 1) {
printf("GPU配置设置成功\n");
} else {
printf("GPU配置设置失败\n");
}
// 检测模型配置
string detConfig = "{\"OcrDetModelDir\":\"./OCRv5_model/PP-OCRv5_mobile_det_infer/\",\"OcrDetDbThresh\":0.3,\"OcrDetDbBoxThresh\":0.6}";
int result = SetOcrConfig(ola, detConfig);
if (result == 1) {
printf("检测模型配置设置成功\n");
} else {
printf("检测模型配置设置失败\n");
}
// 识别模型配置
string recConfig = "{\"OcrRecModelDir\":\"./OCRv5_model/PP-OCRv5_mobile_rec_infer/\",\"OcrRecBatchNum\":6,\"OcrRecCharDictPath\":\"./ppocr/utils/ppocr_keys_v1.txt\"}";
int result = SetOcrConfig(ola, recConfig);
if (result == 1) {
printf("识别模型配置设置成功\n");
} else {
printf("识别模型配置设置失败\n");
}
// CPU配置设置
string cpuConfig = "{\"OcrCpuThreads\":8,\"OcrEnableMkldnn\":true,\"OcrPrecision\":\"int8\"}";
int result = SetOcrConfig(ola, cpuConfig);
if (result == 1) {
printf("CPU配置设置成功\n");
} else {
printf("CPU配置设置失败\n");
}
// 分类模型配置
string clsConfig = "{\"OcrUseAngleCls\":false,\"OcrClsModelDir\":\"\",\"OcrClsThresh\":0.9}";
int result = SetOcrConfig(ola, clsConfig);
if (result == 1) {
printf("分类模型配置设置成功\n");
} else {
printf("分类模型配置设置失败\n");
}
// 布局模型配置
string layoutConfig = "{\"OcrLayoutModelDir\":\"\",\"OcrLayoutScoreThreshold\":0.5,\"OcrLayoutNmsThreshold\":0.5}";
int result = SetOcrConfig(ola, layoutConfig);
if (result == 1) {
printf("布局模型配置设置成功\n");
} else {
printf("布局模型配置设置失败\n");
}
// 表格模型配置
string tableConfig = "{\"OcrTableModelDir\":\"\",\"OcrTableMaxLen\":488,\"OcrTableBatchNum\":1}";
int result = SetOcrConfig(ola, tableConfig);
if (result == 1) {
printf("表格模型配置设置成功\n");
} else {
printf("表格模型配置设置失败\n");
}
// 完整配置示例
string fullConfig = "{\"OcrUseGpu\":false,\"OcrCpuThreads\":8,\"OcrDetModelDir\":\"./OCRv5_model/PP-OCRv5_mobile_det_infer/\",\"OcrRecModelDir\":\"./OCRv5_model/PP-OCRv5_mobile_rec_infer/\",\"OcrDetDbThresh\":0.3,\"OcrRecBatchNum\":6}";
int result = SetOcrConfig(ola, fullConfig);
if (result == 1) {
printf("完整OCR配置设置成功\n");
} else {
printf("完整OCR配置设置失败\n");
}
// 性能优化配置
string perfConfig = "{\"OcrUseGpu\":true,\"OcrUseTensorrt\":true,\"OcrGpuId\":0,\"OcrGpuMem\":4000,\"OcrCpuThreads\":8,\"OcrEnableMkldnn\":true,\"OcrPrecision\":\"int8\"}";
int result = SetOcrConfig(ola, perfConfig);
if (result == 1) {
printf("性能优化配置设置成功\n");
} else {
printf("性能优化配置设置失败\n");
}
返回值
int: 返回设置结果
- 1: 设置成功
- 0: 设置失败
注意事项
- 配置字符串必须为有效的JSON格式
- 支持的配置参数包括:
GPU相关参数
- OcrUseGpu (bool): 是否使用GPU推理,false使用CPU,true使用GPU,默认false
- OcrUseTensorrt (bool): 是否使用TensorRT加速,默认false
- OcrGpuId (int): GPU设备ID,0表示第一个GPU,默认0
- OcrGpuMem (int): GPU内存大小(MB),默认4000
CPU相关参数
- OcrCpuThreads (int): CPU线程数,默认8
- OcrEnableMkldnn (bool): 是否启用MKL-DNN加速,默认true
推理相关参数
- OcrPrecision (string): 推理精度,可选fp32/fp16/int8,默认"int8"
- OcrBenchmark (bool): 是否启用性能基准测试,默认false
- OcrOutput (string): 基准测试日志保存路径,默认"./output/"
- OcrImageDir (string): 输入图像目录,默认""
- OcrType (string): 执行类型,ocr或structure,默认"ocr"
检测相关参数
- OcrDetModelDir (string): 检测模型路径,默认"./OCRv5_model/PP-OCRv5_mobile_det_infer/"
- OcrLimitType (string): 输入图像限制类型,max或min,默认"max"
- OcrLimitSideLen (int): 输入图像限制边长,默认960
- OcrDetDbThresh (double): 检测DB阈值,范围0.0-1.0,默认0.3
- OcrDetDbBoxThresh (double): 检测DB框阈值,范围0.0-1.0,默认0.6
- OcrDetDbUnclipRatio (double): 检测DB未裁剪比例,默认1.5
- OcrUseDilation (bool): 是否对输出图使用膨胀操作,默认false
- OcrDetDbScoreMode (string): 检测DB评分模式,fast或slow,默认"slow"
- OcrVisualize (bool): 是否显示检测结果,默认true
识别相关参数
- OcrRecModelDir (string): 识别模型路径,默认"./OCRv5_model/PP-OCRv5_mobile_rec_infer/"
- OcrRecBatchNum (int): 识别批处理数量,默认6
- OcrRecCharDictPath (string): 识别字符字典路径,默认"./ppocr/utils/ppocr_keys_v1.txt"
- OcrRecImgH (int): 识别图像高度,默认48
- OcrRecImgW (int): 识别图像宽度,默认320
分类相关参数
- OcrUseAngleCls (bool): 是否使用角度分类,默认false
- OcrClsModelDir (string): 分类模型路径,默认""
- OcrClsThresh (double): 分类阈值,范围0.0-1.0,默认0.9
- OcrClsBatchNum (int): 分类批处理数量,默认1
布局相关参数
- OcrLayoutModelDir (string): 布局模型路径,默认""
- OcrLayoutDictPath (string): 布局字典路径,默认"./ppocr/utils/dict/layout_dict/layout_publaynet_dict.txt"
- OcrLayoutScoreThreshold (double): 布局评分阈值,范围0.0-1.0,默认0.5
- OcrLayoutNmsThreshold (double): 布局NMS阈值,范围0.0-1.0,默认0.5
表格相关参数
- OcrTableModelDir (string): 表格结构模型路径,默认""
- OcrTableMaxLen (int): 表格最大长度,默认488
- OcrTableBatchNum (int): 表格批处理数量,默认1
- OcrMergeNoSpanStructure (bool): 是否合并无跨度结构,默认true
- OcrTableCharDictPath (string): 表格字符字典路径,默认"./ppocr/utils/dict/table_structure_dict_ch.txt"
前向相关参数
- OcrDet (bool): 是否使用检测,默认true
- OcrRec (bool): 是否使用识别,默认true
- OcrCls (bool): 是否使用分类,默认false
- OcrTable (bool): 是否使用表格结构,默认false
- OcrLayout (bool): 是否使用布局分析,默认false
注意事项
- 与 GetOcrConfig 和 SetOcrConfigByKey 函数配合使用
- 适用于OCR配置管理和性能优化场景