设置OCR配置键值 - SetOcrConfigByKey
函数简介
通过键值对的方式设置OCR(光学字符识别)的单个配置参数。此函数可以单独设置PP-OCRv5模型的特定参数,包括GPU设置、检测参数、识别参数、分类参数等。适用于需要精确控制单个OCR参数的场景。
接口名称
SetOcrConfigByKey
DLL调用
int SetOcrConfigByKey(long instance, string key, string value)
参数说明
参数名 | 类型 | 说明 |
---|---|---|
instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
key | 字符串 | 配置键名称,支持所有OCR配置参数 |
value | 字符串 | 配置值,根据参数类型设置相应的值 |
示例
// 设置GPU使用状态
int result = SetOcrConfigByKey(ola, "OcrUseGpu", "true");
if (result == 1) {
printf("GPU使用状态设置成功\n");
} else {
printf("GPU使用状态设置失败\n");
}
// 设置GPU设备ID
result = SetOcrConfigByKey(ola, "OcrGpuId", "0");
if (result == 1) {
printf("GPU设备ID设置成功\n");
} else {
printf("GPU设备ID设置失败\n");
}
// 设置GPU内存大小
result = SetOcrConfigByKey(ola, "OcrGpuMem", "4000");
if (result == 1) {
printf("GPU内存大小设置成功\n");
} else {
printf("GPU内存大小设置失败\n");
}
// 设置CPU线程数
result = SetOcrConfigByKey(ola, "OcrCpuThreads", "8");
if (result == 1) {
printf("CPU线程数设置成功\n");
} else {
printf("CPU线程数设置失败\n");
}
// 设置检测模型路径
result = SetOcrConfigByKey(ola, "OcrDetModelDir", "./OCRv5_model/PP-OCRv5_mobile_det_infer/");
if (result == 1) {
printf("检测模型路径设置成功\n");
} else {
printf("检测模型路径设置失败\n");
}
// 设置识别模型路径
result = SetOcrConfigByKey(ola, "OcrRecModelDir", "./OCRv5_model/PP-OCRv5_mobile_rec_infer/");
if (result == 1) {
printf("识别模型路径设置成功\n");
} else {
printf("识别模型路径设置失败\n");
}
// 设置检测DB阈值
result = SetOcrConfigByKey(ola, "OcrDetDbThresh", "0.3");
if (result == 1) {
printf("检测DB阈值设置成功\n");
} else {
printf("检测DB阈值设置失败\n");
}
// 设置检测DB框阈值
result = SetOcrConfigByKey(ola, "OcrDetDbBoxThresh", "0.6");
if (result == 1) {
printf("检测DB框阈值设置成功\n");
} else {
printf("检测DB框阈值设置失败\n");
}
// 设置识别批处理数量
result = SetOcrConfigByKey(ola, "OcrRecBatchNum", "6");
if (result == 1) {
printf("识别批处理数量设置成功\n");
} else {
printf("识别批处理数量设置失败\n");
}
// 设置推理精度
result = SetOcrConfigByKey(ola, "OcrPrecision", "int8");
if (result == 1) {
printf("推理精度设置成功\n");
} else {
printf("推理精度设置失败\n");
}
// 设置字符字典路径
result = SetOcrConfigByKey(ola, "OcrRecCharDictPath", "./ppocr/utils/ppocr_keys_v1.txt");
if (result == 1) {
printf("字符字典路径设置成功\n");
} else {
printf("字符字典路径设置失败\n");
}
// 设置分类阈值
result = SetOcrConfigByKey(ola, "OcrClsThresh", "0.9");
if (result == 1) {
printf("分类阈值设置成功\n");
} else {
printf("分类阈值设置失败\n");
}
// 设置是否使用角度分类
result = SetOcrConfigByKey(ola, "OcrUseAngleCls", "false");
if (result == 1) {
printf("角度分类设置成功\n");
} else {
printf("角度分类设置失败\n");
}
// 设置是否启用MKL-DNN加速
result = SetOcrConfigByKey(ola, "OcrEnableMkldnn", "true");
if (result == 1) {
printf("MKL-DNN加速设置成功\n");
} else {
printf("MKL-DNN加速设置失败\n");
}
// 设置是否使用TensorRT加速
result = SetOcrConfigByKey(ola, "OcrUseTensorrt", "false");
if (result == 1) {
printf("TensorRT加速设置成功\n");
} else {
printf("TensorRT加速设置失败\n");
}
// 设置输入图像限制边长
result = SetOcrConfigByKey(ola, "OcrLimitSideLen", "960");
if (result == 1) {
printf("输入图像限制边长设置成功\n");
} else {
printf("输入图像限制边长设置失败\n");
}
// 设置识别图像高度
result = SetOcrConfigByKey(ola, "OcrRecImgH", "48");
if (result == 1) {
printf("识别图像高度设置成功\n");
} else {
printf("识别图像高度设置失败\n");
}
// 设置识别图像宽度
result = SetOcrConfigByKey(ola, "OcrRecImgW", "320");
if (result == 1) {
printf("识别图像宽度设置成功\n");
} else {
printf("识别图像宽度设置失败\n");
}
返回值
int: 返回设置结果
- 1: 设置成功
- 0: 设置失败
注意事项
- 支持的配置参数包括:
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
注意事项
- 布尔值参数使用"true"或"false"字符串
- 数值参数使用字符串格式
- 路径参数使用字符串格式
- 配置修改后需要重新初始化OCR模型才能生效
- 与 GetOcrConfig 和 SetOcrConfig 函数配合使用
- 适用于精确控制单个OCR参数的场景