华为云-文字识别OCR-身份证识别

什么是文字识别

文字识别(Optical Character Recognition,简称OCR)是指对图像文件的打印字符进行检测识别,将图像中的文字转换成可编辑的文本格式。

OCR以开放API(Application Programming Interface,应用程序编程接口)的方式提供给用户,用户通过实时访问和调用API获取推理结果,帮助用户自动采集关键数据,打造智能化业务系统,提升业务效率。

当前文字识别服务支持以下多种场景的文字识别功能:

  • 身份证识别

    自动识别身份证上的全部信息,支持身份证正反面识别,一次扫描即可识别身份证号码、姓名、地址等全部信息,在暗光、倾斜、过曝光、阴影等异常条件下均可准确识别身份证信息。

  • 驾驶证识别

    自动识别驾驶证正页上的全部信息,自动提取出姓名、性别、领证日期、准驾车型、有效期限等结构化信息,在暗光、倾斜、过曝光、防伪标志干扰、阴影等异常条件下均可准确识别驾驶证信息。

  • 行驶证识别

    自动识别行驶证正页上的全部信息,自动提取出号牌号码、车辆类型、所有人、使用性质、品牌型号、车辆识别代号、发动机号码、注册日期等结构化信息,在暗光、倾斜、过曝光、防伪标志干扰、阴影等异常条件下均可准确识别行驶证信息。

  • 增值税发票识别

    通过对增值税发票图片预处理、表格提取、文字提取、文字识别、结构化信息输出等一系列技术化手段,快速将增值税发票上的文字信息识别出来,用于后续的进一步处理,节省大量的人工录入成本。

  • 英文海关单据识别:可识别出英文海关单据图片上的文字内容和数字,智能提取为可编辑的文本。英文海关单据识别采用了自动定位分割算法、分布式计算框架、集成深度学习进行判断纠错,经过大规模图像文字训练,达到高精度的识别要求。
  • 通用表格识别

    提取表格内的文字和所在行列位置信息,适应不同格式的表格。同时也识别表格外部的文字区域。用于各种单据和报表的电子化,恢复结构化信息。

  • 通用文字识别

    提取图片内的文字及其对应位置信息,并能够根据文字在图片中的位置进行结构化整理工作。

  • 手写字母数字识别

    提取表格内的手写字母、数字和所在行列位置信息,适应不同格式的表格。同时也支持表格外部文字区域的手写字母数字识别。

  • 机动车销售发票识别

    自动识别机动车销售发票图片内的文本内容,并返回结构化字段信息,用于后续的进一步处理,节省大量的人工录入成本。

该文章主要说明身份证识别

应用场景

OCR文字识别可用于检测与识别多种类型的数字图像,提取其中的文本、数值、符号等字符,并返回结构化信息,方便用户自动采集关键数据,助力业务流程自动化、提升效率和降低人力成本。应用于用户认证识别、财务报销审核、金融保险、海关单据电子化等场景。

  • 用户认证识别

    降低用户实名认证成本,准确快速便捷。

  • 财务报销审核

    用于公司员工发票报销,票据自动识别,节省人工录入成本,提升效率。

  • 金融保险

    用户申请保险报销,需提供身份证,报销单,医疗单据等纸件材料。通过文字识别可实现信息的自动录入和审核校对,提升效率。

  • 海关单据电子化

    很多公司都存在海外业务,通用文字识别可实现海关单据数据自动结构化和电子化,提升效率和录入信息准确度。

使用限制

  • 只支持“华北-北京一”区域。
  • 只支持中国大陆汉族身份证的识别。(支持)
  • 只支持识别PNG、JPEG、BMP、TIFF格式的图片。
  • 图像各边的像素大小在15到8000px之间。
  • 图像中身份证区域有效占比超过25%,保证整张身份证内容及其边缘包含在图像内。
  • 支持图像中身份证任意角度的水平旋转。
  • 支持少量扭曲,扭曲后图像中的身份证长宽比与实际身份证相差不超过10%。
  • 能处理反光、暗光等干扰的图片但影响识别精度。
  • 目前只支持识别单张身份证的正面或者反面。
  • 目前不保证API调用的并发能力。

使用服务

文字识别提供了Web化的服务管理平台,即管理控制台,以及基于HTTPS请求的API管理方式。

  • 您可以在管理控制台申请开通文字识别服务、查看服务的调用成功和失败次数。
  • 文字识别以开放API的方式提供给用户,用户可以将文字识别集成到第三方系统调用API。

具体流程如下:

1. 申请服务

使用文字识别服务之前,必须先申请并开通服务,服务申请步骤如下所示。

说明:

  1. 申请文字识别服务之前,必须先申请华为云账号。
  2. 用户首次使用文字识别服务时,需要先申请开通该服务。服务只需要开通一次即可,后面使用时无需再次申请。
  1. 登录华为云,单击右上角的“控制台”,进入“管理控制台”界面。
  2. 单击页面最上方的“服务列表”,选择“EI企业智能 > 文字识别”,进入“文字识别”界面。

文字识别管理控制台地址:https://console.huaweicloud.com/ocr/?agencyId=c59680fbb0d24af8a2da5416d19e91ad&region=cn-north-1&locale=zh-cn#/ocr/management/main

     3. 选择要使用的服务,单击“申请服务”,进行服务使用申请。

     4. 完成“申请服务”之后,即完成该服务的开通操作。

2. 获取请求认证(用于调用api)

调用OCR的API有如下两种认证方式,请任选其中一种进行认证鉴权。此处使用AK/SK方式(推荐)

       AK(Access Key ID):访问密钥ID。与私有访问密钥关联的唯一标识符;访问密钥ID和私有访问密钥一起使用,对请求进行    加密签名。

       SK(Secret Access Key):与访问密钥ID结合使用的密钥,对请求进行加密签名,可标识发送方,并防止请求被修改。

  获取AK/SK步骤:

  1. 登录管理控制台。
  2. 鼠标移动至用户名处,在下拉列表中单击“我的凭证”,进入“我的凭证”页面。
  3. 选择“管理访问密钥”页签,单击“新增访问秘钥”按钮。
  4. 输入当前用户的登录密码。通过邮箱或者手机进行验证,输入对应的验证码。
  5. 单击“确定”,下载认证账号的AK/SK,请妥善保管AK/SK信息。

3. 调用API

  1. 获取文字识别SDK。包下载地址https://developer.huaweicloud.com/sdk?OCR
  2. 获取OCR对应Endpoint:即调用API的请求地址,不同服务不同区域的终端节点不同。终端节点查询地址地区和终端节点
  3. 配置JAVA SDK的AK/SK,地区,终端。如下图:

         

请求参数说明:

名称

是否必选

类型

说明

image

与url二选一

String

图像数据,base64编码,要求base64编码后大小不超过10M。图像各边的像素大小在15到8000之间。支持JPG/PNG/BMP/TIFF格式。

url

与image二选一

String

图片的URL路径,目前仅支持华为云上OBS提供的临时授权或者匿名公开授权访问的URL。

side

String

  • front:身份证正面
  • back:身份证背面

如果参数值为空或无该参数,系统自动识别,建议填写,准确率更高。

响应参数说明:

名称

类型

说明

result

JSON

调用成功时表示调用结果。

调用失败时无此字段。

name

String

姓名。

sex

String

性别。

birth

String

出生日期。

ethnicity

String

民族。

address

String

地址。

number

String

身份证号。

issue

String

发证机关。

valid_from

String

有效起始日期。

valid_to

String

有效结束日期。

error_code

String

调用失败时的错误码,具体请参见错误码

调用成功时无此字段。

error_msg

String

调用失败时的错误信息。

调用成功时无此字段。

成功响应示例(正面)

{
  "result":{
      "name":"张三",
      "sex":"男",
      "ethnicity":"汉",
      "birth":"2000-03-06",
      "address":"XX省XX市XX区XX街道XX号",
      "number:"610XXXXXXXXXXXXXXX"
  }
}

成功响应示例(背面)

{
    "result":{
        "issue": "XX省XX市XX区XX派出所", 
        "valid_from": "2004-08-26",
        "valid_to": "2034-08-26"
    }
}

失败响应示例

{
    "error_code": "AIS.0105",
    "error_msg": "Recognize Failed"
}

返回值

  • 正常  200
  • 失败

    返回值

    说明

    400

    • 语义有误,当前请求无法被服务器解析。除非进行修改,否则客户端不应重复提交这个请求。
    • 请求参数有误。

    401

    当前请求需要用户验证。

    403

    没有操作权限。

    404

    请求失败,在服务器上未找到请求所希望得到的资源。

    500

    服务器遇到了一个未曾预料的状况,导致无法完成对请求的处理。

完整的状态码列表请参见状态码

完整的错误码列表请参见错误码

代码示例:

HWOcrClientAKSK.java (传入AK/SK,地区(Endpoint),终端(Endpoint)调用识别接口的类

import com.alibaba.fastjson.JSONObject;
import com.huawei.ais.common.AuthInfo;
import com.huawei.ais.common.ProxyHostInfo;
import com.huawei.ais.sdk.AisAccess;
import com.huawei.ais.sdk.AisAccessWithProxy;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;

import java.io.File;
import java.io.IOException;


public class HWOcrClientAKSK {
	private AuthInfo HEC_AUTH;       //Authorization Information
	private AisAccess HttpService;   //access to ocr service on cloud
	private ProxyHostInfo PROXYINFO; //proxy host info (optional)
	/**
	 * Constructor for OcrClientAKSK without proxy
	 * @param Endpoint HTTPEndPoint for OCR Service
	 * @param Region The region info for OCR Service
	 * @param YourAK The Access Key from Authentication
	 * @param YourSK The Secret Key from Authentication
	 * */
	public HWOcrClientAKSK(String Endpoint,String Region,String YourAK,String YourSK) {
		if (Endpoint== null|Endpoint == ""|Region == null| Region == ""| YourAK == null| YourAK == ""| YourSK == null|
				YourSK == "")
					throw new IllegalArgumentException("the parameter for HWOcrClientAKSK's Constructor cannot be empty");		
		HEC_AUTH= new AuthInfo(Endpoint,Region,YourAK,YourSK);
		HttpService=new AisAccess(HEC_AUTH);
	}
	/**
	 * Constructor for OcrClientAKSK with proxy
	 * @param Endpoint HTTPEndPoint for OCR Service
	 * @param Region The region info for OCR Service
	 * @param YourAK The Access Key from Authentication
	 * @param YourSK The Secret Key from Authentication
	 * @param ProxyHost The URL for the proxy server
	 * @param ProxyPort The Port number for the proxy service
	 * @param ProxyUsrName The Loggin user for the proxy server if authentication is required
	 * @param ProxyPwd  The Loggin pwd for the proxy server if authentication is required
	 * */
	public HWOcrClientAKSK(String Endpoint,String Region,String YourAK,String YourSK,String ProxyHost,int ProxyPort,String ProxyUsrName,String ProxyPwd) {
		if (Endpoint== null|Endpoint == ""|Region == null| Region == ""| YourAK == null| YourAK == ""| YourSK == null|
				YourSK == ""|ProxyHost == null | ProxyHost == "" | ProxyUsrName == null |ProxyUsrName == ""| ProxyPwd == null | ProxyPwd == "")
					throw new IllegalArgumentException("the parameter for HWOcrClientAKSK's Constructor cannot be empty");				
		HEC_AUTH= new AuthInfo(Endpoint,Region,YourAK,YourSK);
		PROXYINFO= new ProxyHostInfo(ProxyHost,ProxyPort,ProxyUsrName,ProxyPwd);
		HttpService=new AisAccessWithProxy(HEC_AUTH,PROXYINFO);
	}
	/*
	 * Release the access service when the object is collected
	 * */
	protected void finalize(){
		HttpService.close();
	}
	/**
	 * Call the OCR API with a local picture file
	 * @param uri the uri for the http request to be called
	 * @param filepath the path for the picture file to be recognized
	 * */
	public HttpResponse requestOcrServiceBase64(String uri,String filepath) throws IOException {
	if(uri == null | uri == "" | filepath == null | filepath == "")
		throw new IllegalArgumentException("the parameter for requestOcrServiceBase64 cannot be empty");
		try {
			
			byte[] fileData = FileUtils.readFileToByteArray(new File(filepath));
			String fileBase64Str = Base64.encodeBase64String(fileData);

			JSONObject json = new JSONObject();
			json.put("image", fileBase64Str);

			//
			// 2.a (Optional) Specify whether the information is obtained from the front or back side of the ID card.
			// side Possible value: front, back, or unspecified. If the parameter is left unspecified or the parameter is not included, the algorithm automatically determine the side from which the information is obtained. You are advised to specify this parameter to improve the accuracy.
			//
			//json.put("side", "front");

			StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");

			// 3. Pass the URI and required parameters of ID Card OCR.
			// Pass the parameters in JSON objects and invoke the service using POST.
			HttpResponse response = HttpService.post(uri, stringEntity);
			return response;
			// 4. Check whether the service invocation is successful. If 200 is returned, the invocation succeeds. Otherwise, it fails.
			//ResponseProcessUtils.processResponseStatus(response);

			// 5. Process the character stream returned by the service.
			//ResponseProcessUtils.processResponse(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null; 
	}	

}

ResponseProcessUtils.java (工具类,用于处理结果的返回

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.apache.http.HttpResponse;

import com.huawei.ais.sdk.util.HttpClientUtils;

/**
 * Tool used to verify the information returned from service access.
 */
public class ResponseProcessUtils {

	/**
	 * Print the HTTP status code after the service access is complete.
	 *
	 * @param response Response object
	 */
	public static void processResponseStatus(HttpResponse response) {
		System.out.println(response.getStatusLine().getStatusCode());
	}

	/**
	 * Convert the service access result into a character stream, which is used for showing the JSON data.
	 *
	 * @param response Response object
	 * @throws UnsupportedOperationException
	 * @throws IOException
	 */
	public static void processResponse(HttpResponse response) throws UnsupportedOperationException, IOException {
		System.out.println(HttpClientUtils.convertStreamToString(response.getEntity().getContent()));
	}
	
	/**
	 *  Write the byte array to the file to support generation of binary files (for example, images).
	 * @param fileName File name
	 * @param data Data
	 * @throws IOException
	 */
	public static void writeBytesToFile(String fileName, byte[] data) throws IOException{

		FileChannel fc = null;
		try {
			ByteBuffer bb = ByteBuffer.wrap(data);
			fc = new FileOutputStream(fileName).getChannel();
			fc.write(bb);

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		finally {
			fc.close();
		}
	}
}

OCRDemo.java (调用身份识别示例方法)

import com.huawei.ais.demo.ocr.*;
import java.io.IOException;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.commons.io.IOUtils;
public class OCRDemo {
	public static void main(String[] args) throws URISyntaxException, UnsupportedOperationException, IOException{
		/*
		 * AK/SK demo code
		 * */
	String Httpendpoint="https://ocr.cn-north-1.myhuaweicloud.com"; //http endpoint for the service
		String Region="cn-north-1";   //region name for the service
		String AK="xxx";  //AK from authentication
		String SK="xxx"; //SK from authentication
		try {
			HWOcrClientAKSK ocrClient=new HWOcrClientAKSK(Httpendpoint,Region,AK,SK);
			HttpResponse response=ocrClient.requestOcrServiceBase64("/v1.0/ocr/id-card", "./data/id-card-demo.png");
			System.out.println(response);
			String content = IOUtils.toString(response.getEntity().getContent());
			System.out.println(content);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

OCR以API的方式提供服务,具体操作请参见《文字识别API参考》

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值