1.简介
光学字符识别技术(Optical Character Recognition)简称OCR,是指用扫描仪或数码相机等光学设备, 获取文本资料的图像,然后对图像文件进行分析处理,获取上面的文字及版面信息的过程。相关的功能有文本识别,名片识别,银行卡识别,模板(行驶证、驾驶证、旅行证件、身份证、票据等)识别功能。
灵云 OCR SDK 支持本地和云端两种识别方式,支持多达12种语言的识别。除了单纯的文本识别,灵云 OCR SDK 还为证照(身份证、驾驶证、银行卡)和票据(如增值税发票)的识别提供简洁的开发接口。
本文档旨在讲解灵云SDK的相关知识和使用过程,帮助开发者快速地集成灵云 OCR 功能到开发者应用中。关于各服务接口更详细的说明,请参考灵云SDK开发手册。在集成过程中如有疑问,可登录灵云开发者论坛,查找答案或与其他开发者交流。
1.1名词解释
在 OCR 进行识别的过程中,需要对图片做一些特定处理,涉及到如下概念:
模板 捷通华声通过定义模板,将识别图像区域化,指导识别工作,从而实现一种核心引擎对若干种不同票据、证件及其他特殊纸质信息进行高速高准确率的OCR识别。
二值化 将灰度图像或彩色图像转换为二值图像的过程。二值图像,即常见的黑白图像。二值化用来减少网络数据传输量,现在只在文本识别中使用。
倾斜校正 通过图像处理技术,校正文本图像中倾斜现象的技术。能校正倾斜度在+/- 15度以内的图像。该技术只在文本识别中使用。
版面分析 划分文本图像的各栏(横排文本、竖排文本),并将每一栏切割为若干区域(文字区域、图像区域、表格区域等)的过程。该技术只在文本识别中使用。
1.2能力定义
灵云 OCR SDK 目前提供以下能力:
capkey | 功能 | 类别 |
---|---|---|
ocr.cloud | 云端文本识别,支持中文简繁体识别(包括英文等) | 文本识别 |
ocr.cloud.bizcard | 云端名片识别 | 文本识别 |
ocr.cloud.bankcard | 云端银行卡识别 | 文本识别 |
ocr.cloud.template | 云端模板识别,支持行驶证、驾驶证、身份证、票据 | 模板识别 |
ocr.loca | 本地文本识别,支持中文简繁体识别(包括英文等) | 文本识别 |
ocr.local.bizcard.v6 | 本地名片识别 | 文本识别 |
ocr.local.bankcard.v7 | 本地银行卡识别 | 文本识别 |
ocr.local.template.v6 | 本地模板识别,支持行驶证、驾驶证、旅行证件、身份证、票据 | 模板识别 |
1.3支持的图像类型
图像数据格式 OCR内部支持三种图像格式:
- 二值图像:每个像素点由1bit表示(0和1分别表示背景和文字)
- 256级灰度图像:每个像素点由1byte表示, 表示256级灰度
- 24位真彩色图像:每个像素点为RGB三种分量,每个分量由1byte表示。
同样幅面的图像,灰度图像的大小是二值图像的8倍,真彩色图像的大小是二值图像的24倍。 尽管通过压缩可使这一倍数缩小,但灰度和彩色图像仍然要比二值图像大很多。 当使用云端识别功能时,图像需要传输到云端,因此图像的大小显得比较关键。 我们建议在可以使用二值图像的时候,尽量直接使用二值图像。例如在使用扫描仪的时候, 可以将扫描图像的类型设为二值,而非灰度或彩色,这样可以减少上传的流量。 当一定要使用灰度图像时,我们也提供了配置项,可以让在本地端进行二值化工作,之后再传送到云端。
图像文件格式 灵云 OCR SDK 支持多种格式的文件,如 TIFF, BMP, JPG, PNG, GIF 等,并能够根据文件头自动判断文件格式。对于多页 TIFF 文件的情况,只处理第一页的内容。目前 Android 版 SDK 不支持 GIF 和 TIFF 格式的文件。
1.4 调用流程
2. 准备工作
2.1 创建应用
在集成OCR之前,开发者需要在灵云开发者社区创建相关应用,并保存appkey,developerkey等信息。同时选择需要使用的能力定义(Capkey),具体详见灵云开发者社区使用指南。
2.2 下载SDK
下载文字识别SDK并解压缩。
2.3 使用Android版SDK
Android版的SDK提供如下文件:
文件 | 说明 |
---|---|
armeabi | 存放灵云能力所需so文件的目录 |
hcicloud-5.0.jar | 提供灵云能力接口的jar包 |
使用时将这些文件复制到安卓工程的 libs 目录下,并把 jar 包添加到工程中。
2.3.1 库文件介绍
必选模块
- libcurl.so
- libhci_sys.so
- libhci_sys_jni.so
- libhci_ocr.so
- libhci_ocr_jni.so
- libstlport_shared.so
- libiRead_Function.so
- libjtz.so
- libopencv_core.so
- libopencv_imgproc.so
云端识别
- libhci_ocr_cloud_recog.so
本地文本识别
- libhci_ocr_local_recog.so
本地银行卡识别
- libBankCard.so
- libhci_ocr_local_recog_bankcard.so
本地名片识别
- libiRead_BizCard.so
- libhci_ocr_local_recog_bizcard.so
本地模板识别
- libiReadForm.so
- libhci_ocr_local_recog_template.so
2.3.2 添加用户权限
2.4 使用C++版SDK
必选模块
- hci_ocr.lib
- hci_sys.lib
- libhci_curl.dll
- hci_sys.dll
- hci_ocr.dll
- iRead_Function.dll
云端能力
- hci_ocr_cloud_recog.dll
- jtz.dll
本地能力
opencv
- opencv_core246.dll
- opencv_imgproc246.dll
- opencv_highgui246.dll
- opencv_nonfree246.dll
- opencv_flann246.dll
- opencv_features2d246.dll
- opencv_objdetect246.dll
- opencv_video246.dll
- opencv_gpu246.dll
- opencv_calib3d246.dll
mkl
- mkl_avx.dll
- mkl_avx2.dll
- mkl_core.dll
- mkl_intel_thread.dll
- mkl_p4.dll
- mkl_p4m.dll
- mkl_p4m3.dll
- mkl_p4p.dll
- mkl_rt.dll
- mkl_sequential.dll
- mkl_vml_avx.dll
- mkl_vml_avx2.dll
- mkl_vml_cmpt.dll
- mkl_vml_ia.dll
- mkl_vml_p4.dll
- mkl_vml_p4m.dll
- mkl_vml_p4m2.dll
- mkl_vml_p4m3.dll
- mkl_vml_p4p.dll
本地文本
- hci_ocr_local_recog.dll
本地模板
- hci_ocr_local_recog_template.dll
- iReadBarCodeApi.dll
- iReadForm.dll
本地名片
- hci_ocr_local_recog_bizcard.dll
- iRead_BizCard.dll
本地银行卡
- hci_ocr_local_recog_bankcard.dll
- iReadBankCard.dll
2.5 使用 iOS 版 SDK
必选模块
- libjtz_device_simulator_iOS5.1.1.a
- libcurl_device_simulator_iOS5.1.1.a
- libhci_sys_device_simulator_iOS5.1.1.a
- libhci_ocr_device_simulator_iOS5.1.1.a
云端能力
- libhci_ocr_cloud_recog_device_simulator_iOS5.1.1.a
本地文本
- libhci_ocr_local_recog_device_simulator_iOS5.1.1.a
本地模板
- libhci_ocr_local_recog_template_device_simulator_iOS5.1.1.a
本地名片
- libhci_ocr_local_recog_bizcard_device_simulator_iOS5.1.1.a
本地银行卡
- libhci_ocr_local_recog_bankcard_device_simulator_iOS5.1.1.a
适配器选择(只选其一)
- libhci_ocr_only_cloud_adapter_device_simulator_iOS5.1.1.a(只使用云端识别)
- libhci_ocr_only_cloud_adapter_device_simulator_iOS5.1.1.a(只使用云端识别)
- libhci_ocr_only_local_adapter_device_simulator_iOS5.1.1.a(只使用本地文本识别)
- libhci_ocr_only_local_template_adapter_device_simulator_iOS5.1.1.a (只使用模板识别)
- libhci_ocr_only_local_bizcard_adapter_device_simulator_iOS5.1.1.a (只使用名片识别)
- libhci_ocr_only_local_bankcard_adapter_device_simulator_iOS5.1.1.a (只使用银行卡识别)
- libhci_ocr_cloud_local_adapter_device_simulator_iOS5.1.1.a (既使用云端识别,又使用本地文本识别)
- libhci_ocr_cloud_local_bizcard_adapter_device_simulator_iOS5.1.1.a (既使用云端识别,又使用本地名片识别)
- libhci_ocr_bankcard_template_adapter_device_simulator_iOS5.1.1.a (既使用本地银行卡识别,又使用本地模板识别)
- libhci_ocr_cloud_local_template_bankcard_adapter_device_simulator_iOS5.1.1.a (既使用云端识别,又使用本地模板识别和本地银行卡识别)
如所提供的适配器不能满足您的需求,请联系我们
2.4 资源文件
如果需要使用本地能力时,请下载相应资源包并解压缩,资源文件版本请根据 2.2 节下载的SDK版本确定。
下载完成后,将资源文件放入指定的资源路径下,资源路径指定方式详见3.1节。
2.4.1 模板文件介绍
使用本地模板能力时下载的模板资源文件有以下几个文件夹,其使用场景如下:
- BL:营业执照
- DLC:驾驶证
- IDCard:身份证
- PCN:中国护照
- PID:旅行证件
- VAT:增值税票
- VLC:行驶证
3. OCR能力集成
3.1 系统模块初始化
在调用OCR能力之前,需要初始化灵云SDK的SYS系统模块。详见灵云SDK开发手册
Android代码
HciCloudSys.hciInit( "developerKey=01234567890, appKey=1234abcd, cloudUrl=api.hcicloud.com:8888, authPath=/storage/emulated/0/myApp/auth, logFileSize=500,logLevel=5, logFilePath=/storage/emulated/0/myApp/log, logFileCount=10", context);
C++代码
//SYS初始化 HCI_ERR_CODE err_code = HCI_ERR_NONE; //配置串是由"字段=值"的形式给出的一个字符串,多个字段之间以','隔开。字段名不分大小写。 string init_config = ""; init_config += "appKey=xxxxxx"; //灵云应用序号,开发者社区申请所得 init_config += ",developerKey=xxxxxxxxxxxxxxxx"; //灵云开发者密钥,开发者社区申请所得 init_config += ",cloudUrl=api.hcicloud.com:8888"; //灵云云服务的接口地址,开发者社区申请所得 init_config += ",authpath=./auth" //授权文件所在路径,保证可读写 init_config += ",logfilepath=./log"; //日志的路径,保证可读写 init_config += ",loglevel=5"; //日志的等级 init_config += ",logfilesize=512"; //日志文件的大小 //其他配置使用默认值,不再添加,如果想设置可以参考开发手册 err_code = hci_init( init_config.c_str() ); if( err_code != HCI_ERR_NONE ) { printf( "hci_init return (%d:%s)\n", err_code, hci_get_error_info(err_code) ); return -1; } printf( "hci_init success\n" );
iOS代码
HCI_ERR_CODE errCode = HCI_ERR_NONE; //appkey, developerKey, cloudUrl, authPath为必填项。其他可配置信息请查询灵云SDK开发手册hci_init函数说明。 NSString *appkey = @""; //开发者社区申请所得 NSString *developerKey = @""; //开发者社区申请所得 NSString *cloudUrl = @""; //开发者社区申请所得 NSString *authPath = @""; //授权文件所在路径,一般设置为沙盒Document路径 NSString *config = [NSString stringWithFormat:@"appkey=%@,developerKey=%@,cloudUrl=%@,authPath=%@", appkey, developerKey, cloudUrl, authPath]; errCode = hci_init(config.UTF8String); if (errCode != HCI_ERR_NONE && errCode != HCI_ERR_SYS_ALREADY_INIT) { NSLog(@"hci_init failed, error: %@", errCode); }else{ NSLog(@"hci_init success"); }
3.2 OCR 初始化
在系统模块初始化成功后,进行OCR模块初始化。
Android代码
OcrInitParam ocrInitParam = new OcrInitParam(); ocrInitParam.addParam(OcrInitParam.PARAM_KEY_INIT_CAP_KEYS, “ocr.local”); ocrInitParam.addParam(OcrInitParam.PARAM_KEY_DATA_PATH, "/storage/....."); int errCode = HciCloudOcr.hciOcrInit(ocrInitParam.getStringConfig()); if (errCode != HciErrorCode.HCI_ERR_NONE) { ShowMessage("hciOcrInit error:" + HciCloudSys.hciGetErrorInfo(errCode)); return; } else { ShowMessage("hciOcrInit Success"); }
C++代码
//OCR初始化 HCI_ERR_CODE err_code = HCI_ERR_NONE; string strInitConfig; strInitConfig += "dataPath=./data"; // 设置本地资源文件所在路径 strInitConfig += ",initCapkeys=ocr.local"; //strInitConfig +=",resPrefix=fk/"; err_code = hci_ocr_init(strInitConfig.c_str()); if (err_code != HCI_ERR_NONE) { printf("hci_ocr_init return (%d:%s) \n", err_code, hci_get_error_info(err_code)); return; } printf("hci_ocr_init success \n");
iOS代码
//initCapKeys为必填项,其他可配置信息请查询开发手册hci_ocr_init函数说明。 NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:Nil]; //本地识别所需本地资源所在路径 NSString *initCapKeys = @"ocr.local"; NSString *config = [NSString stringWithFormat:@"dataPath =%@, initCapKeys =%@", dataPath, initCapKeys]; HCI_ERR_CODE errCode = HCI_ERR_NONE; errCode = hci_ocr_init(config.UTF8String); if (errCode != HCI_ERR_NONE){ NSLog(@"hci_ocr_init failed, error:%@", errCode); eturn NO; } NSLog(@"hci_ocr_init success"); return YES;
3.3 加载模板
本地模板能力需要加载相应的模板文件,使用其他能力时请跳过此步骤。使用模板识别时需要在config中传入加载成功后的template id。
Android代码
OcrTemplateId templateId = new OcrTemplateId(); String loadConfig = ""; int errCode = HciCloudOcr.hciOcrLoadTemplate( loadConfig,“/Storage/emulated/0/myApp/templates/IDCard/iRead_IDCard_encode.xml", templateId); if (errCode != HciErrorCode.HCI_ERR_NONE) { ShowMessage("hciOcrLoadTemplate error:" + HciCloudSys.hciGetErrorInfo(errCode)); HciCloudOcr.hciOcrRelease(); return; } else { ShowMessage("hciOcrLoadTemplate Success"); } //本地需设置templateId recogConfig.addParam(OcrConfig.TemplateConfig.PARAM_KEY_TEMPLATE_ID, templateId.toString());
C++代码
//如果是本地模板识别则加载模板 int tempalte_id = -1; //此处加载身份证的模板,如果调用了边缘检测和清晰度检测,需使用iRead_IDCard_embeded_encode.xml,否则使用iRead_IDCard_encode.xml string template_file = data_path + "/templates/IDCard/iRead_IDCard_encode.xml"; err_code = hci_ocr_load_template(strConfig.c_str(), template_file.c_str(), &tempalte_id); if (err_code != HCI_ERR_NONE) { printf("hci_ocr_load_template failed(%d:%s) \n",err_code, hci_get_error_info(err_code)); hci_ocr_release(); return; } printf("hci_ocr_load_template success! \n"); OCR_RECOG_RESULT recog_result; char szRegConfig[40]; sprintf( szRegConfig, "templateId=%d", tempalte_id ); // 设置模板ID err_code = hci_ocr_recog( session_id, szRegConfig, layoutRegionList, layoutRegionCount, &recog_result );
iOS代码
//如果是本地模板识别则加载模板 int tempalteId = -1; //此处加载身份证的模板,如果调用了边缘检测和清晰度检测,需使用iRead_IDCard_embeded_encode.xml,否则使用iRead_IDCard_encode.xml NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:Nil]; //本地识别所需本地资源所在路径 NSString * templateFile = [NSString stringWithFormat:@"%@%@", dataPath,@"/templates/IDCard/iRead_IDCard_encode.xml"]; errCode = hci_ocr_load_template(strConfig.UTF8String, templateFile.UTF8String, & tempalteId); if (errCode != HCI_ERR_NONE) { NSLog(@"hci_ocr_load_template failed(%d:%@) ", errCode, hci_get_error_info(errCode)); hci_ocr_release(); return; } NSlog(@"hci_ocr_load_template success! "); OCR_RECOG_RESULT recog_result; NSString *szRegConfig = [NSString stringWithFormat:@"templateId =%d", tempalteId];// 设置模板ID errCode = hci_ocr_recog( session_id, szRegConfig.UTF8String, layoutRegionList, layoutRegionCount, &recog_result );
3.4 开启会话
OCR能力引擎通过会话来管理识别过程,可同时开启的最大会话数量由线程授权控制。请参考灵云SDK授权模块开发指南。
Android代码
Session session = new Session(); int errCode = HciCloudOcr.hciOcrSessionStart("capkey=ocr.local", session); if (HciErrorCode.HCI_ERR_NONE != errCode) { ShowMessage("hciOcrSessionStart error:" + HciCloudSys.hciGetErrorInfo(errCode)); return false; }
C++代码
HCI_ERR_CODE err_code = HCI_ERR_NONE; // 启动 OCR Session int session_id = -1; string session_config = "capkey=ocr.local"; err_code = hci_ocr_session_start(session_config.c_str(), &session_id); if (err_code != HCI_ERR_NONE) { printf("hci_ocr_session_start return(%d:%s)\n", err_code,hci_get_error_info(err_code)); return false; } printf( "hci_ocr_session_start success\n" );
iOS代码
HCI_ERR_CODE errCode = HCI_ERR_NONE; // 启动 OCR Session int sessionId = -1; NSString *sessionConfig = @"capkey=ocr.local"; errCode = hci_ocr_session_start(sessionConfig.UTF8String, &sessionId); if (errCode != HCI_ERR_NONE) { NSLog(@"hci_ocr_session_start return(%d:%@)", err_code,hci_get_error_info(err_code)); return NO; } NSLog( @"hci_ocr_session_start success" );
3.5 设置图片
此步骤用于设置需要进行识别的图片数据,可通过hci_ocr_set_image_buffer或 hci_ocr_set_image_file两种方式进行设置,此处以hci_ocr_set_image_buffer为例,可直接fread二进制方式读取图片文件,将读取到的buffer传入。
Android代码
byte[] data = getAssetFileData("BizCard.jpg"); if(null == data) { ShowMessage("Open image file " + imageFile + "error!"); return false; } errCode = HciCloudOcr.hciOcrSetImageBuffer(session, data); if (HciErrorCode.HCI_ERR_NONE != errCode) { ShowMessage("hciOcrSetImageBuffer error:" + HciCloudSys.hciGetErrorInfo(errCode)); HciCloudOcr.hciOcrSessionStop(session); return false; }
C++代码
// 设置图片 err_code = hci_ocr_set_image_buffer(session_id, image_data, image_data_len_); if (err_code != HCI_ERR_NONE) { printf("hci_ocr_set_image_by_decode_buffer failed(%d)\n", err_code); hci_ocr_session_stop(session_id); return; } printf("hci_ocr_set_image_by_decode_buffer success\n");
iOS代码
// 设置图片 errCode = hci_ocr_set_image_by_uiImage(sessionID, image); if (errCode != HCI_ERR_NONE) { NSLog(@"hci_ocr_set_image_by_uiImage failed(%d)", errCode); hci_ocr_session_stop(sessionID); return; } NSLog(@"hci_ocr_set_image_by_uiImage success");
3.6 倾斜校正
使用本地文本识别时,在识别前需要先进行倾斜校正,使用其他能力时请跳过此步骤。
Android代码
OcrRecogDeskewResult deskew_result = new OcrRecogDeskewResult(); errCode = HciCloudOcr.hciOcrDeskew(sessionid, "", deskew_result); if (HciErrorCode.HCI_ERR_NONE != errCode) { ShowMessage("hciOcrDeskew error:" + HciCloudSys.hciGetErrorInfo(errCode)); HciCloudOcr.hciOcrSessionStop(session); return false; }
C++代码
//倾斜校正 OCR_DESKEW_RESULT deskew_result; err_code = hci_ocr_deskew( session_id, NULL, &deskew_result ); if( err_code != HCI_ERR_NONE ) { printf("hci_ocr_deskew return(%d:%s)\n", err_code,hci_get_error_info(err_code)); hci_ocr_session_stop( session_id ); return false; } printf( "hci_ocr_deskew success\n" ); // 释放倾斜检测结果 hci_ocr_free_deskew_result(&deskew_result);
iOS代码
//倾斜校正 OCR_DESKEW_RESULT deskew_result; errCode = hci_ocr_deskew( sessionId, NULL, &deskew_result ); if( errCode != HCI_ERR_NONE ) { NSLog(@"hci_ocr_deskew return(%d:%@)", errCode,hci_get_error_info(errCode)); hci_ocr_session_stop( sessionId ); return false; } NSLog( @"hci_ocr_deskew success" ); // 释放倾斜检测结果 hci_ocr_free_deskew_result(&deskew_result);
3.7 版面分析
使用本地文本识别时,在识别前需要先进行版面分析,使用其他能力时请跳过此步骤。
Android代码
OcrRecogLayoutAnalysisResult ocrLayoutResult = new OcrRecogLayoutAnalysisResult(); // 添加数据 // 开始版面分析,分析配置参数传入空, 选择默认 errCode = HciCloudOcr.hciOcrLayoutAnalysis(session, null, ocrLayoutResult); if (errCode != HciErrorCode.HCI_ERR_NONE) { ShowMessage("hciOcrLayoutAnalysis error:" + HciCloudSys.hciGetErrorInfo(errCode)); HciCloudOcr.hciOcrSessionStop(session); return false; }
C++代码
// 进行版面分析 OCR_LAYOUT_RESULT layout_result; err_code = hci_ocr_layout_analysis( session_id, "", &layout_result ); if( err_code != HCI_ERR_NONE ) { printf("hci_ocr_layout_analysis return(%d:%s)\n", err_code,hci_get_error_info(err_code)); hci_ocr_session_stop( session_id ); return false; } printf( "hci_ocr_layout_analysis success\n" );
iOS代码
// 进行版面分析 OCR_LAYOUT_RESULT layout_result; errCode = hci_ocr_layout_analysis( sessionId, @"".UTF8String, &layout_result ); if( errCode != HCI_ERR_NONE ) { NSLog(@"hci_ocr_layout_analysis return(%d:%@)", errCode,hci_get_error_info(errCode)); hci_ocr_session_stop( errCode ); return NO; } NSLog( @"hci_ocr_layout_analysis success" );
3.8 识别
以上准备工作完成后,可通过此步骤进行识别并获取识别结果。
Android代码
ArrayList<OcrRecogRegion> ocrlayoutResultList = ocrLayoutResult.getRecogRegionList(); // 上一步中得到的版面分析结果 OcrRecogResult recogResult = new OcrRecogResult(); errCode = HciCloudOcr.hciOcrRecog(session, recogConfig.getStringConfig(), ocrlayoutResultList, recogResult); if (errCode == HciErrorCode.HCI_ERR_NONE) { ShowMessage("hciOcrRecog success"); ShowMessage("RecogResult: " ); ShowMessage(recogResult.getResultText()); }else{ ShowMessage("hciOcrRecog error:" + HciCloudSys.hciGetErrorInfo(errCode)); }
C++代码
// 进行识别 OCR_RECOG_RESULT recog_result; err_code = hci_ocr_recog( session_id, "templateid=2", layoutRegionList, layoutRegionCount, &recog_result ); if( err_code == HCI_ERR_NONE ) { printf( "hci_ocr_recog success\n" ); // 打印识别结果 PrintOcrResult( recog_result ); // 释放版面分析结果 hci_ocr_free_layout_result(&layout_result); // 释放识别结果 hci_ocr_free_recog_result(&recog_result); } else{ printf("hci_ocr_recog return(%d:%s)\n", err_code,hci_get_error_info(err_code)); }
iOS代码
// 进行识别 OCR_RECOG_RESULT recog_result; errCode = hci_ocr_recog( sessionId, @"templateid=2".UTF8String, layoutRegionList, layoutRegionCount, &recog_result ); if( errCode == HCI_ERR_NONE ) { NSLog( @"hci_ocr_recog success" ); // 打印识别结果 PrintOcrResult( recog_result ); // 释放版面分析结果 hci_ocr_free_layout_result(&layout_result); // 释放识别结果 hci_ocr_free_recog_result(&recog_result); } else{ NSLog(@"hci_ocr_recog return(%d:%@)", errCode,hci_get_error_info(errCode)); }
以识别名片为例,识别图片如下:
识别结果((recogResult.getResultText())如下:
<bizcard> <cell id="0" item="postalcode" name="邮政编码"> <result text="100085"/><bound left="527" top="421" right="639" bottom="449"/> </cell> <cell id="1" item="name" name="姓名"> <result text="许丹"/><bound left="196" top="421" right="377" bottom="493"/> </cell> <cell id="2" item="org" name="单位名称"><result text="手机产品中心"> <bound left="183" top="562" right="378" bottom="598"/> </cell> <cell id="3" item="title" name="称谓"> <result text="软件产品经理"/><bound left="181" top="606" right="376" bottom="642"/> </cell> <cell id="4" item="tel_cell" name="移动电话"> <result text="13601377553"/><bound left="516" top="542" right="701" bottom="570"/> </cell> <cell id="5" item="tel" name="电话"> <result text="010-62966688-8120"/><bound left="518" top="461" right="788" bottom="489"/> </cell> <cell id="6" item="fax" name="传真"> <result text="010-62966688-8100"/><bound left="841" top="692" right="1077" bottom="727"/> </cell> <cell id="9" item="unknown" name="其它"> <result text="MSN:carolyn7758@hotmail.com"/><bound left="462" top="620" right="864" bottom="651"/> </cell> <cell id="10" item="unknown" name="其它"> <result text="北海"/><bound left="737" top="225" right="918" bottom="247"/> </cell> <cell id="11" item="org" name="单位名称"> <result text="北京纽曼伟业科技有限公司"/><bound left="469" top="317" right="1006" bottom="363"/> </cell> <cell id="12" item="adr" name="地址"> <result text="北京市海淀区上地三街9号嘉华大厦D座505室"/><bound left="546" top="379" right="1094" bottom="409"/> </cell> </bizcard>
3.9 关闭会话和反初始化
识别完成后,需要关闭会话和反初始化来释放资源
Android代码
HciCloudOcr.hciOcrSessionStop(session); HciCloudOcr.hciOcrRelease(); HciCloudSys.hciRelease();
C++代码
// 终止 OCR Session err_code = hci_ocr_session_stop( session_id ); if( err_code != HCI_ERR_NONE ) { printf("hci_ocr_session_stop return(%d:%s)\n", err_code,hci_get_error_info(err_code)); } printf( "hci_ocr_session_stop success\n" ); //OCR反初始化 hci_ocr_release(); printf("hci_ocr_release\n"); //SYS反初始化 err_code = hci_release(); printf("hci_release\n");
iOS代码
// 终止 OCR Session errCode = hci_ocr_session_stop( sessionId ); if( errCode != HCI_ERR_NONE ) { NSLog(@"hci_ocr_session_stop return(%d:%s)", errCode,hci_get_error_info(errCode)); } NSLog( @"hci_ocr_session_stop success" ); //OCR反初始化 hci_ocr_release(); NSLog(@"hci_ocr_release"); //SYS反初始化 errCode = hci_release(); NSLog(@"hci_release");
3.10 识别截屏图像
因为通过屏幕截取得到的图像的分辨率低于拍摄或扫描的图像,所以在进行截屏图像识别时要做特殊设置才能正确识别。可以在识别、版面分析、倾斜校正的时候通过“imageType=screen”进行指定,配置项使用方法详见灵云SDK开发手册:
4. 常用配置
以下为 OCR 识别中常用配置,更多配置项请参考灵云SDK开发手册。
配置项 | 取值或示例 | 默认值 | 含义 | 详细信息 |
---|---|---|---|---|
localProcess | yes,no | yes | 是否进行本地预处理 | 将图片转换为引擎识别标准大小、上传时先进行压缩等 |
recogRange | gb, letter | all | 识别范围 | number: 数字 0 - 9, 10个 uppercase: 大写英文字母, A - Z, 26个 lowercase: 小写英文字母, a - z, 26个 letter: 大小写字母, 52个 alnum: 大小写字母和数字, 62个 gb: 中文简体汉字 big5: 中文繁体汉字 all: 所有字符集的合集,英文能力时不包含gb和big5 |
exportImage | yes,no | no | 是否导出图片数据 | 返回识别结果时,是否同时返回引擎处理后的图片数据 |
templateId | 1 | 无 | 本地识别使用的模板ID | 本地模板识别必选 本地识别时,本参数是由 hci_ocr_load_template()返回的ID 云端识别时,请使用domain参数指定欲使用的识别模板 |
domain | dlcard,vlcard,idcard | 无 | 指定云端模板识别欲使用的模板 | 云端模板识别必选,目前可以指定为dlcard(驾驶证)、vlcard(行驶证)、idcard(身份证) |
templateIndex | 0到对应模板文件中的最大模板索引号 | 无 | 模板文件中的模板索引号 | 必选,模板文件中可能有多个模板,比如身份证、票据等,本参数指定模板在模板文件中的索引号,目前每种模板能力使用各自的模板文件,此值传0即可,所有取值参考FAQ问题11 |
templatePageIndex | 0到对应模板中的最大页索引号 | 无 | 模板页索引号 | 必选,模板中可能存在多个模板页,比如身份证的正面和反面,本参数指定模板页在模板中的索引号,所有取值参考FAQ问题11 |
5. FAQ
Q:我的需求是我会将我手写的数据保存为图片,你们的OCR能否帮我识别图片里的手写数据?
A:手写用的是HWR能力,而不是OCR。OCR目前的识别范围不包含手写体。
Q:你们的OCR,是否支持对PDF的识别,我给你们一个PDF文件,能不能帮我们识别一下?
A:我们目前支持的图片格式,包括TIFF, BMP, JPG, PNG, GIF等,目前不支持PDF格式。多页TIFF类型的文件,只会处理第一页的内容。 Android平台,不支持GIF格式以及TIFF格式文件。
Q:你们的OCR文本识别效果怎么这么差?我随便截取了一段文本,都无法识别。
A:截屏和拍照图片,对应的参数是不一样的。imageType需要对应调整,normal为普通图像 ,screen为截屏图像。binarize参数也需要调整,global为全局二值化算法,主要针对通过扫描、截屏等获取的图像。adaptive为局部自适应二值化算法,主要针对通过拍照、摄像头等获取的受光照影响较大图像。
Q:你们的OCR识别,是否可以设置识别范围?
A:可以,通过recogRange参数设置,比如设置recogRange=gb, letter。缺失值是all。
Q:OCR需要识别的图片,都支持以哪些方式进行加载?
A:两种接口。hci_ocr_set_image_buffer,通过内存缓冲设置本会话处理的图像。hci_ocr_set_image_file,通过文件设置本会话处理的图像。
Q:OCR是否支持导出文件?都支持哪些导出文件格式?
A:云端多语种OCR支持,export参数可设置为none,xml,pdf,docx,rtf,xlsx,text,html,pptx。
Q:我用你们的SDK,识别身份证正面成功了,识别反面,返回403:HCI_ERR_OCR_ENGINE_FAILED,这个怎么解决?
A:模板识别,需要通过templatePageIndex参数设置模板页索引号,身份证识别正面此参数传0,反面传1,关于此配置具体细节,请参考Q11。
Q:我不仅希望返回文字识别结果,还希望返回各个字的坐标等信息,是否支持?
A:支持,通过detailResult参数设置,缺省为no,只返回识别结果的文本串。如果设为yes,则会返回详细的节点信息。
Q:你们的模板是否支持自己编辑?我想编辑一下你们的增值税发票的模板。
A:我们目前没有对外开放模板编辑器,如果需要定制模板,请联系我们的相关技术和商务。
Q:你们是否有拍照识别的示例程序?就是我在手机上,拍一张图,去识别相关信息。
A:有拍照器SDK,android和ios版本。默认支持识别银行卡和身份证。
Q:templateIndex和templatePageIndex取值设置及其含义。
A:
模板文件类型 | 取值及其含义 |
---|---|
iRead_IDCard.xml (二代身份证) |
templateIndex=0 ,templatePageIndex=0 :内部自动分类识别个人信息页或签发机关页 templateIndex=1 或0, templatePageIndex=1 :固定识别签发机关页 其他组合:非法 |
iRead_VAT.xml (增值税票) |
templateIndex=0 ,templatePageIndex=0 :识别14 个常用条目 templateIndex=0 ,templatePageIndex=1 :识别14 个常用条目+购销方 址电话+8 个详单+备注栏 其他组合:非法 |
iRead_DLC.xml (驾驶证) |
固定templateIndex=0,templatePageIndex=0 |
iRead_VLC.xml (行驶证) |
固定templateIndex=0,templatePageIndex=0 |
iRead_PCN.xml (中国护照) |
固定templateIndex=0,templatePageIndex=0 |
iRead_PID.xml (旅行证件) |
固定templateIndex=0,templatePageIndex=0 |
iRead_BL.xml (营业执照) |
templateIndex=0 ,templatePageIndex=0 :识别10 个常用条目 templateIndex=0 ,templatePageIndex=1 :识别10 个常用条目+编号+发证日期 其他组合:非法 |