华为云-文字识别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. 申請服務
使用文字識別服務之前,必須先申請并開通服務,服務申請步驟如下所示。
說明:
文字識別管理控制臺地址:https://console.huaweicloud.com/ocr/?agencyId=c59680fbb0d24af8a2da5416d19e91ad®ion=cn-north-1&locale=zh-cn#/ocr/management/main。
? ? ?3.?選擇要使用的服務,單擊“申請服務”,進行服務使用申請。
? ? ?4. 完成“申請服務”之后,即完成該服務的開通操作。
2.?獲取請求認證(用于調用api)
調用OCR的API有如下兩種認證方式,請任選其中一種進行認證鑒權。此處使用AK/SK方式(推薦)
- Token認證:通過Token認證調用請求,具體操作請參見文字識別API參考>認證鑒權>Token認證。
- AK/SK認證:通過AK/SK加密調用請求。AK/SK認證安全性更高,具體操作請參見文字識別API參考>認證鑒權>AK/SK認證。
? ? ? ?AK(Access Key ID):訪問密鑰ID。與私有訪問密鑰關聯的唯一標識符;訪問密鑰ID和私有訪問密鑰一起使用,對請求進行? ? 加密簽名。
? ? ? ?SK(Secret Access Key):與訪問密鑰ID結合使用的密鑰,對請求進行加密簽名,可標識發送方,并防止請求被修改。
? 獲取AK/SK步驟:
3. 調用API
配置JAVA SDK的AK/SK,地區,終端。如下圖:
? ? ? ? ?
請求參數說明:
| image | 與url二選一 | String | 圖像數據,base64編碼,要求base64編碼后大小不超過10M。圖像各邊的像素大小在15到8000之間。支持JPG/PNG/BMP/TIFF格式。 |
| url | 與image二選一 | String | 圖片的URL路徑,目前僅支持華為云上OBS提供的臨時授權或者匿名公開授權訪問的URL。 |
| side | 否 | String |
如果參數值為空或無該參數,系統自動識別,建議填寫,準確率更高。 |
響應參數說明:
| 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 Informationprivate AisAccess HttpService; //access to ocr service on cloudprivate 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 serviceString Region="cn-north-1"; //region name for the serviceString AK="xxx"; //AK from authenticationString SK="xxx"; //SK from authenticationtry {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參考》。
總結
以上是生活随笔為你收集整理的华为云-文字识别OCR-身份证识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 0.前端简历编写和面试前准备
- 下一篇: php 屏蔽浸膏,【佳学基因】副甲状腺浸