关于自定义模板文字识别如何使用可以看官方的教程贴哦 http://ai.baidu.com/forum/topic/show/497372
注意一:获取templateSign http://ai.baidu.com/iocr#/templatelist
注意二:获取access_token 在控制台复制apikey secretkey获取哦。不太清楚可以看小帅的帖子哦
注意三:接口属于测试阶段,大家要注意自己是否有接口权限哦,没有去文字识别群找pm申请哦
-------------------以下是代码哦-------------------
自定义模板文字识别-JavaAPI示例代码
package cn.xsshome.iocr;
import java.net.URLEncoder;
import cn.xsshome.test.Base64Util;
import cn.xsshome.test.FileUtil;
import cn.xsshome.test.HttpUtil;
/**
* 非官方示例代码
* @author 小帅
* 自定义模版文字识别-JavaAPI调用方式
*/
public class IOCRTest {
public static void main(String[] args) throws Exception {
//根据apikey secretkey获取token
String accessToken = "";
//接口地址
String iocrurl = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise";
//自定义模块templateSign
String templateSign = "";
//获取图片的base64数据 先获取图片的数组数据再进行base64 编码
byte[] imgaebinary = FileUtil.readFileByBytes("/Users/xiaoshuai/Downloads/icon.png");
String imagebase64 = Base64Util.encode(imgaebinary);
//拼接接口所需参数 image参数需要urlencode哦
String param = "image="+URLEncoder.encode(imagebase64,"UTF-8")+"&templateSign="+templateSign;
//开始请求接口
String result = HttpUtil.post(iocrurl, accessToken, param);
//输出接口返回的内容
System.out.println(result);
}
}
自定义模板文字识别-返回接口内容-未能识别
{
"data": {
"isStructured": false,
"logId": "151589630267357"
},
"error_code": 0,
"error_msg": ""
}
自定义模板文字识别-返回接口内容-识别成功 内容太多 就不格式化代码了
{"data":{"ret":[{"rect":{"top":34,"left":99,"width":463,"height":50},"charset":[{"rect":{"top":43,"left":111,"width":25,"height":32},"word":"北"},{"rect":{"top":43,"left":141,"width":26,"height":32},"word":"京"},{"rect":{"top":43,"left":173,"width":26,"height":32},"word":"市"},{"rect":{"top":43,"left":206,"width":25,"height":32},"word":"国"},{"rect":{"top":35,"left":238,"width":27,"height":49},"word":"家"},{"rect":{"top":35,"left":302,"width":26,"height":49},"word":"务"},{"rect":{"top":35,"left":332,"width":33,"height":49},"word":"局"},{"rect":{"top":35,"left":365,"width":40,"height":49},"word":"通"},{"rect":{"top":35,"left":429,"width":29,"height":49},"word":"定"},{"rect":{"top":43,"left":461,"width":26,"height":31},"word":"额"},{"rect":{"top":43,"left":501,"width":25,"height":31},"word":"发"},{"rect":{"top":43,"left":532,"width":25,"height":31},"word":"票"}],"word":"北京市国家务局通定额发票"},{"rect":{"top":83,"left":226,"width":53,"height":23},"charset":[{"rect":{"top":83,"left":227,"width":19,"height":23},"word":"北"},{"rect":{"top":83,"left":263,"width":12,"height":23},"word":"市"}],"word":"北市"},{"rect":{"top":83,"left":308,"width":133,"height":19},"charset":[{"rect":{"top":83,"left":309,"width":16,"height":19},"word":"通"},{"rect":{"top":83,"left":326,"width":16,"height":19},"word":"一"},{"rect":{"top":83,"left":344,"width":15,"height":19},"word":"卡"},{"rect":{"top":83,"left":361,"width":15,"height":19},"word":"通"},{"rect":{"top":83,"left":378,"width":11,"height":19},"word":"有"},{"rect":{"top":83,"left":391,"width":16,"height":19},"word":"限"},{"rect":{"top":83,"left":428,"width":11,"height":19},"word":"司"}],"word":"通一卡通有限司"},{"rect":{"top":99,"left":288,"width":66,"height":44},"charset":[{"rect":{"top":109,"left":324,"width":22,"height":27},"word":"禁"}],"word":"禁"},{"rect":{"top":160,"left":161,"width":304,"height":28},"charset":[{"rect":{"top":167,"left":168,"width":14,"height":18},"word":"发"},{"rect":{"top":167,"left":185,"width":14,"height":18},"word":"票"},{"rect":{"top":167,"left":203,"width":16,"height":18},"word":"代"},{"rect":{"top":167,"left":225,"width":16,"height":18},"word":"码"},{"rect":{"top":163,"left":267,"width":11,"height":22},"word":"1"},{"rect":{"top":163,"left":284,"width":11,"height":22},"word":"1"},{"rect":{"top":163,"left":301,"width":11,"height":22},"word":"1"},{"rect":{"top":163,"left":315,"width":11,"height":22},"word":"0"},{"rect":{"top":163,"left":331,"width":11,"height":22},"word":"0"},{"rect":{"top":163,"left":348,"width":11,"height":22},"word":"1"},{"rect":{"top":163,"left":365,"width":11,"height":22},"word":"7"},{"rect":{"top":163,"left":385,"width":11,"height":22},"word":"7"},{"rect":{"top":163,"left":398,"width":11,"height":22},"word":"7"},{"rect":{"top":163,"left":412,"width":11,"height":21},"word":"0"},{"rect":{"top":163,"left":431,"width":11,"height":21},"word":"1"},{"rect":{"top":163,"left":445,"width":11,"height":21},"word":"3"}],"word":"发票代码111001777013"},{"rect":{"top":194,"left":161,"width":242,"height":29},"charset":[{"rect":{"top":201,"left":168,"width":15,"height":19},"word":"发"},{"rect":{"top":201,"left":186,"width":15,"height":19},"word":"票"},{"rect":{"top":201,"left":204,"width":15,"height":19},"word":"号"},{"rect":{"top":201,"left":222,"width":16,"height":19},"word":"码"},{"rect":{"top":197,"left":270,"width":11,"height":22},"word":"7"},{"rect":{"top":197,"left":287,"width":11,"height":22},"word":"9"},{"rect":{"top":197,"left":301,"width":12,"height":22},"word":"0"},{"rect":{"top":197,"left":322,"width":12,"height":22},"word":"4"},{"rect":{"top":197,"left":336,"width":12,"height":22},"word":"7"},{"rect":{"top":197,"left":350,"width":11,"height":22},"word":"5"},{"rect":{"top":197,"left":371,"width":11,"height":22},"word":"1"},{"rect":{"top":197,"left":384,"width":12,"height":22},"word":"1"}],"word":"发票号码79047511"},{"rect":{"top":235,"left":224,"width":43,"height":23},"charset":[{"rect":{"top":238,"left":230,"width":15,"height":18},"word":"密"},{"rect":{"top":238,"left":247,"width":15,"height":18},"word":"码"}],"word":"密码"},{"rect":{"top":223,"left":269,"width":330,"height":51},"charset":[{"rect":{"top":223,"left":349,"width":41,"height":51},"word":"■"},{"rect":{"top":223,"left":523,"width":18,"height":51},"word":"一"},{"rect":{"top":223,"left":546,"width":26,"height":51},"word":"卡"},{"rect":{"top":223,"left":570,"width":29,"height":51},"word":"逾"}],"word":"■一卡逾"},{"rect":{"top":283,"left":254,"width":283,"height":70},"charset":[{"rect":{"top":283,"left":266,"width":38,"height":70},"word":"伍"},{"rect":{"top":283,"left":311,"width":38,"height":70},"word":"拾"},{"rect":{"top":283,"left":355,"width":17,"height":70},"word":"元"},{"rect":{"top":283,"left":379,"width":57,"height":70},"word":"整"}],"word":"伍拾元整"},{"rect":{"top":297,"left":522,"width":113,"height":32},"charset":[{"rect":{"top":300,"left":528,"width":13,"height":26},"word":"8"},{"rect":{"top":300,"left":540,"width":14,"height":26},"word":"0"},{"rect":{"top":300,"left":557,"width":14,"height":26},"word":"1"},{"rect":{"top":300,"left":570,"width":14,"height":26},"word":"5"},{"rect":{"top":300,"left":578,"width":14,"height":26},"word":"3"},{"rect":{"top":300,"left":587,"width":13,"height":26},"word":"8"},{"rect":{"top":300,"left":599,"width":15,"height":26},"word":"1"},{"rect":{"top":300,"left":615,"width":16,"height":26},"word":"H"}],"word":"8015381H"},{"rect":{"top":344,"left":317,"width":51,"height":20},"charset":[{"rect":{"top":347,"left":321,"width":12,"height":14},"word":"¥"},{"rect":{"top":347,"left":334,"width":7,"height":14},"word":"5"},{"rect":{"top":347,"left":341,"width":7,"height":14},"word":"0"},{"rect":{"top":347,"left":345,"width":8,"height":14},"word":"."},{"rect":{"top":347,"left":351,"width":7,"height":14},"word":"0"},{"rect":{"top":347,"left":363,"width":5,"height":14},"word":"0"}],"word":"¥50.00"},{"rect":{"top":319,"left":464,"width":190,"height":51},"charset":[{"rect":{"top":325,"left":479,"width":25,"height":45},"word":"发"},{"rect":{"top":325,"left":507,"width":26,"height":45},"word":"票"},{"rect":{"top":325,"left":536,"width":25,"height":45},"word":"专"},{"rect":{"top":325,"left":562,"width":25,"height":45},"word":"用"},{"rect":{"top":325,"left":591,"width":36,"height":45},"word":"章"}],"word":"发票专用章"},{"rect":{"top":372,"left":470,"width":14,"height":13},"charset":[{"rect":{"top":373,"left":475,"width":6,"height":10},"word":"7"}],"word":"7"},{"rect":{"top":378,"left":143,"width":459,"height":32},"charset":[{"rect":{"top":391,"left":149,"width":14,"height":17},"word":"东"},{"rect":{"top":391,"left":166,"width":14,"height":17},"word":"印"},{"rect":{"top":391,"left":179,"width":9,"height":17},"word":"2"},{"rect":{"top":391,"left":186,"width":9,"height":17},"word":"0"},{"rect":{"top":390,"left":194,"width":9,"height":17},"word":"1"},{"rect":{"top":390,"left":203,"width":9,"height":17},"word":"7"},{"rect":{"top":390,"left":213,"width":9,"height":17},"word":"年"},{"rect":{"top":390,"left":225,"width":9,"height":17},"word":"0"},{"rect":{"top":390,"left":235,"width":9,"height":17},"word":"6"},{"rect":{"top":390,"left":245,"width":9,"height":17},"word":"月"},{"rect":{"top":390,"left":256,"width":13,"height":17},"word":"印"},{"rect":{"top":391,"left":271,"width":8,"height":16},"word":"8"},{"rect":{"top":391,"left":279,"width":8,"height":16},"word":"4"},{"rect":{"top":391,"left":288,"width":9,"height":16},"word":"0"},{"rect":{"top":391,"left":298,"width":9,"height":16},"word":"0"},{"rect":{"top":391,"left":309,"width":9,"height":16},"word":"0"},{"rect":{"top":390,"left":318,"width":8,"height":17},"word":"0"},{"rect":{"top":390,"left":324,"width":11,"height":17},"word":"本"},{"rect":{"top":390,"left":352,"width":8,"height":17},"word":"5"},{"rect":{"top":390,"left":361,"width":8,"height":17},"word":"0"},{"rect":{"top":390,"left":369,"width":8,"height":17},"word":"”"},{"rect":{"top":378,"left":376,"width":16,"height":32},"word":"5"},{"rect":{"top":378,"left":401,"width":17,"height":32},"word":"0"},{"rect":{"top":378,"left":442,"width":23,"height":32},"word":"0"},{"rect":{"top":378,"left":500,"width":27,"height":32},"word":"0"},{"rect":{"top":378,"left":547,"width":16,"height":32},"word":"0"},{"rect":{"top":378,"left":573,"width":29,"height":32},"word":"3"}],"word":"东印2017年06月印840000本50”500003"}],"isStructured":false,"logId":"151589711325206"},"error_code":0,"error_msg":""}
目前测试结果发现的问题:
1.接口文档不完善,data包含的数据未做具体说明
2.isStructured字段也没有做具体说明
以上就是小帅通过Java-API方式调用自定义模板文字识别示例代码。
其他语言的参考官网文档其他接口的示例代码即可。
切记accesstoken是用APIKey 和 SecretKey获取的哦。鉴权有2种方式。第二种过于复杂。
切记accesstoken是拼接在接口地址后面哦
https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise?access_token=24.xxxxxxx