【百度大脑CV主题月征稿计划】定额发票识别
小雨青青润无声 发布于2019-09 浏览:2638 回复:0
1
收藏

百度定额发票识别,对各类定额发票的代码、号码、金额进行识别

 

功能接入:
进入百度AI管理界面:https://console.bce.baidu.com/
进入界面后点击“创建应用”, 输入应用信息及需要的AI功能即可。

百度现在提供每天一定量的免费调用额度,足够大家测试使用了。


获取Access Token
调用定额发票识别功能前需要先调用鉴权API,确保安全。
具体说明请查阅:http://ai.baidu.com/docs#/Auth/top

请求URL数据格式
向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:
grant_type: 必须参数,固定为client_credentials;
client_id: 必须参数,应用的API Key;
client_secret: 必须参数,应用的Secret Key;
例如:
https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&


定额发票识别AP调用
定额发票识别的API说明链接为:https://ai.baidu.com/docs#/OCR-API-QuotaInvoice/top

接口描述
对各类定额发票的代码、号码、金额进行识别

请求说明
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/quota_invoice

具体参数:
image string 是 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式

返回参数

字段 是否必选 类型 说明
log_id 是 uint64 唯一的log id,用于问题定位
words_result_num 是 int 识别结果数,表示words_result的元素个数
words_result 是 array() 识别结果数组
invoice_code 否 string 发票代码
invoice_number 否 string 发票号码
invoice_rate 否 string 金额

 

返回示例
{
"log_id": 2480896295,
"words_result_num": 3,
"words_result": {
"invoice_code": "132081490320",
"invoice_number": "01275486",
"invoice_rate": "伍拾元整"
}
}


详细代码(C#):

public static class AccessToken
{
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "你获得的ID";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "你获得的KEY";

public static String getAccessToken() {
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List> paraList = new List>();
paraList.Add(new KeyValuePair("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair("client_id", clientId));
paraList.Add(new KeyValuePair("client_secret", clientSecret));

HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
JObject jobject = (JObject)JsonConvert.DeserializeObject(result);
string token = jobject["access_token"].ToString();
return token;
//return result;
}
public class Program
{
// 定额发票识别
public static string quotaInvoice(String imagefilename)
{
string token = AccessToken.getAccessToken();
Console.WriteLine(token);
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/quota_invoice?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
string base64 = getFileBase64(imagefilename);
String str = "image=" + HttpUtility.UrlEncode(base64);
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("结果:");
Console.WriteLine(result);
return result;
}
public static String getFileBase64(String fileName) {
FileStream filestream = new FileStream(fileName, FileMode.Open);
byte[] arr = new byte[filestream.Length];
filestream.Read(arr, 0, (int)filestream.Length);
string baser64 = Convert.ToBase64String(arr);
filestream.Close();
return baser64;
}
public static String saveBase64File(string base64Str, string outPath)
{
var contents = Convert.FromBase64String(base64Str);
using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
fs.Write(contents, 0, contents.Length);
fs.Flush();
}
return "finished";
}
}

功能测试(所有测试图片都来自网络):
定额发票图片:


返回结果:
{"log_id": 3289818712431853756, "words_result_num": 4, "words_result": {"invoice_number": "63809943", "avg_score": "0.999922", "invoice_rate": "伍拾元整", "invoice_code": "111001676064"}}

 

测试结论及建议
测试后发现效果不错,调用速度很快,有非常好的应用前景,会大大促进办公自动化。

收藏
点赞
1
个赞
TOP
切换版块