【自定义模板识别】居住证标注实例操作和接口调用
756665228 发布于2018-07-06 10:32 浏览:1776 回复:3
2
收藏
最后编辑于2018-10-25

看此贴。需要有一定的Java开发基础哦。自定义模板识别标注很简单。10分钟肯定能看明白

  • 创建模板选择要标注的图片 点击下一步

  • 给模板添加一个名称

  • 开始标注图片数据-框选参考字段

只选择文本内容不会变的内容哦

  • 开始标注图片数据-框选识别区

只框选会变的内容哦 记得在右边填写识别区内容名称哦

 

 

  • 最后点击保存,填写一些备注内容(不写也行哦) 点击试一试 上传一张图片检测识别效果

有木有发现。识别的超级准确呢。图片是作者本人的。

涂抹了一些比较重要的信息。文字基本是100%准确度识别哦

-------------------------------------------------------------------------------------------------------

  • 接口调用示例代码-返回String
import java.net.URLEncoder;

import com.alibaba.fastjson.JSON;
import com.xs.pojo.ocr.iocr.IOCRBean;
import com.xs.util.baidu.HttpUtil;
import com.xs.util.baidu.official.Base64Util;
import com.xs.util.baidu.official.FileUtil;

public class IOCRSample {
	public static void main(String[] args) throws Exception {
		//图片本地路径
		String filePath = "C:/Users/Administrator/Desktop/query.jpg";
		//根据apikey secretkey获取token
		String accessToken = "";
		//自定义模块templateSign 
		String templateSign = "";
		//返回字符串内容
		String result = getResidencePermitOCR(filePath, accessToken, templateSign);
		System.out.println(result);
	}
	/**
	 * 返回字符串结果
	 * @param filePath 图片文件路径
	 * @param accessToken 应用token
	 * @param templateSign 自定义模板id 
	 * @return String
	 * @throws Exception
	 */
	public static String getResidencePermitOCR(String filePath,String accessToken,String templateSign) throws Exception{
		//接口地址
		String iocrurl = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise";
		//获取图片的base64数据 先获取图片的数组数据再进行base64 编码
		byte[] imgaebinary = FileUtil.readFileByBytes(filePath);
		String imagebase64 = Base64Util.encode(imgaebinary);
		//拼接接口所需参数 image参数需要urlencode哦
		String param = "image="+URLEncoder.encode(imagebase64,"UTF-8")+"&templateSign="+templateSign;
		//开始请求接口
		String result = HttpUtil.post(iocrurl, accessToken, param);
		//输出接口返回的内容
		return result;	
	}

  • 返回的内容(很多很长)
{"data":{"ret":[{"charset":[{"rect":{"top":71,"left":300,"width":35,"height":44},"word":"X"},{"rect":{"top":71,"left":339,"width":31,"height":44},"word":"X"},{"rect":{"top":75,"left":372,"width":29,"height":36},"word":"帅"}],"word_name":"姓名","word":"XX帅"},{"charset":[{"rect":{"top":371,"left":302,"width":33,"height":50},"word":"山"},{"rect":{"top":371,"left":341,"width":34,"height":50},"word":"西"},{"rect":{"top":371,"left":382,"width":27,"height":50},"word":"省"},{"rect":{"top":371,"left":413,"width":34,"height":50},"word":"长"},{"rect":{"top":371,"left":452,"width":33,"height":50},"word":"治"},{"rect":{"top":371,"left":491,"width":27,"height":50},"word":"市"},{"rect":{"top":371,"left":523,"width":40,"height":50},"word":"郊"},{"rect":{"top":371,"left":570,"width":26,"height":50},"word":"区"}],"word_name":"户籍所在地","word":"山西省长治市郊区"},{"charset":[{"rect":{"top":142,"left":504,"width":37,"height":46},"word":"汉"}],"word_name":"民族","word":"汉"},{"charset":[{"rect":{"top":215,"left":293,"width":23,"height":45},"word":"0"},{"rect":{"top":214,"left":314,"width":23,"height":45},"word":"3"},{"rect":{"top":214,"left":339,"width":23,"height":45},"word":"1"},{"rect":{"top":214,"left":372,"width":22,"height":44},"word":"1"},{"rect":{"top":222,"left":407,"width":16,"height":31},"word":"1"},{"rect":{"top":221,"left":428,"width":15,"height":32},"word":"2"},{"rect":{"top":221,"left":447,"width":15,"height":32},"word":"0"},{"rect":{"top":221,"left":467,"width":16,"height":32},"word":"1"},{"rect":{"top":221,"left":482,"width":16,"height":31},"word":"1"},{"rect":{"top":221,"left":497,"width":15,"height":31},"word":"0"},{"rect":{"top":220,"left":523,"width":16,"height":32},"word":"1"},{"rect":{"top":220,"left":537,"width":16,"height":32},"word":"0"},{"rect":{"top":220,"left":563,"width":16,"height":32},"word":"5"},{"rect":{"top":220,"left":577,"width":16,"height":31},"word":"6"},{"rect":{"top":219,"left":598,"width":15,"height":32},"word":"0"},{"rect":{"top":219,"left":623,"width":16,"height":32},"word":"7"},{"rect":{"top":219,"left":637,"width":16,"height":32},"word":"4"}],"word_name":"证件编号","word":"03111201101056074"},{"charset":[{"rect":{"top":296,"left":298,"width":21,"height":39},"word":"1"},{"rect":{"top":296,"left":318,"width":20,"height":39},"word":"4"},{"rect":{"top":296,"left":339,"width":20,"height":39},"word":"0"},{"rect":{"top":296,"left":363,"width":19,"height":39},"word":"4"},{"rect":{"top":296,"left":383,"width":20,"height":39},"word":"1"},{"rect":{"top":299,"left":408,"width":17,"height":33},"word":"1"},{"rect":{"top":299,"left":430,"width":17,"height":33},"word":"1"},{"rect":{"top":299,"left":450,"width":17,"height":33},"word":"9"},{"rect":{"top":299,"left":472,"width":17,"height":33},"word":"9"},{"rect":{"top":299,"left":492,"width":17,"height":33},"word":"3"},{"rect":{"top":299,"left":514,"width":17,"height":33},"word":"0"},{"rect":{"top":299,"left":534,"width":17,"height":33},"word":"4"},{"rect":{"top":299,"left":556,"width":17,"height":33},"word":"1"},{"rect":{"top":299,"left":576,"width":17,"height":33},"word":"0"},{"rect":{"top":299,"left":598,"width":17,"height":33},"word":"4"},{"rect":{"top":299,"left":618,"width":17,"height":33},"word":"8"},{"rect":{"top":299,"left":640,"width":17,"height":33},"word":"5"},{"rect":{"top":299,"left":671,"width":17,"height":33},"word":"7"}],"word_name":"身份证号码","word":"X"},{"charset":[{"rect":{"top":142,"left":307,"width":34,"height":46},"word":"男"}],"word_name":"性别","word":"男"},{"charset":[{"rect":{"top":517,"left":297,"width":37,"height":48},"word":"武"},{"rect":{"top":516,"left":342,"width":37,"height":48},"word":"汉"},{"rect":{"top":516,"left":385,"width":34,"height":47},"word":"市"},{"rect":{"top":520,"left":425,"width":33,"height":41},"word":"江"},{"rect":{"top":519,"left":464,"width":33,"height":42},"word":"汉"},{"rect":{"top":518,"left":504,"width":33,"height":42},"word":"区"},{"rect":{"top":517,"left":556,"width":33,"height":42},"word":"金"},{"rect":{"top":517,"left":596,"width":33,"height":42},"word":"家"},{"rect":{"top":516,"left":636,"width":33,"height":42},"word":"墩"},{"rect":{"top":516,"left":675,"width":34,"height":41},"word":"责"},{"rect":{"top":515,"left":715,"width":33,"height":42},"word":"任"},{"rect":{"top":514,"left":754,"width":33,"height":42},"word":"区"},{"rect":{"top":514,"left":795,"width":33,"height":42},"word":"陈"},{"rect":{"top":513,"left":834,"width":33,"height":42},"word":"家"},{"rect":{"top":512,"left":873,"width":33,"height":42},"word":"墩"},{"rect":{"top":512,"left":914,"width":22,"height":42},"word":"8"},{"rect":{"top":512,"left":935,"width":21,"height":41},"word":"1"},{"rect":{"top":511,"left":954,"width":22,"height":42},"word":"8"},{"rect":{"top":511,"left":979,"width":33,"height":42},"word":"号"},{"rect":{"top":510,"left":1019,"width":22,"height":42},"word":"1"},{"rect":{"top":510,"left":1045,"width":33,"height":42},"word":"栋"},{"rect":{"top":509,"left":1086,"width":21,"height":42},"word":"1"},{"rect":{"top":559,"left":294,"width":38,"height":47},"word":"单"},{"rect":{"top":559,"left":349,"width":26,"height":47},"word":"元"},{"rect":{"top":559,"left":378,"width":23,"height":47},"word":"2"},{"rect":{"top":559,"left":402,"width":38,"height":47},"word":"楼"},{"rect":{"top":559,"left":446,"width":23,"height":47},"word":"2"},{"rect":{"top":559,"left":469,"width":33,"height":47},"word":"号"}],"word_name":"现居住地址","word":"武汉市江汉区金家墩责任区陈家墩818号1栋1单元2楼2号"},{"charset":[{"rect":{"top":600,"left":299,"width":20,"height":41},"word":"2"},{"rect":{"top":600,"left":319,"width":20,"height":39},"word":"0"},{"rect":{"top":601,"left":343,"width":17,"height":32},"word":"1"},{"rect":{"top":601,"left":359,"width":19,"height":32},"word":"2"},{"rect":{"top":596,"left":383,"width":30,"height":40},"word":"年"},{"rect":{"top":596,"left":422,"width":20,"height":38},"word":"1"},{"rect":{"top":596,"left":446,"width":20,"height":38},"word":"0"},{"rect":{"top":596,"left":470,"width":32,"height":39},"word":"月"},{"rect":{"top":596,"left":508,"width":21,"height":39},"word":"2"},{"rect":{"top":595,"left":532,"width":19,"height":39},"word":"8"},{"rect":{"top":595,"left":553,"width":31,"height":39},"word":"日"},{"rect":{"top":603,"left":662,"width":32,"height":39},"word":"至"},{"rect":{"top":596,"left":748,"width":17,"height":35},"word":"2"},{"rect":{"top":596,"left":769,"width":19,"height":34},"word":"0"},{"rect":{"top":596,"left":788,"width":17,"height":33},"word":"1"},{"rect":{"top":592,"left":809,"width":21,"height":39},"word":"3"},{"rect":{"top":593,"left":834,"width":34,"height":38},"word":"年"},{"rect":{"top":591,"left":875,"width":21,"height":41},"word":"1"},{"rect":{"top":590,"left":900,"width":19,"height":40},"word":"0"},{"rect":{"top":590,"left":922,"width":22,"height":40},"word":"月"},{"rect":{"top":590,"left":949,"width":21,"height":40},"word":"2"},{"rect":{"top":590,"left":974,"width":21,"height":39},"word":"8"},{"rect":{"top":588,"left":997,"width":34,"height":41},"word":"日"}],"word_name":"有效期限","word":"2012年10月28日至2013年10月28日"}],"templateSign":"464f20ca9a307b026d95bb42df7cb0b5","scores":1.0,"isStructured":true,"logId":"153084678412169"},"error_code":0,"error_msg":""}
  • 接口调用示例代码-返回JavaBean

需要用到第三方的jar-fastjson-通过pom添加或者自行下载jar文件引入项目中


    com.alibaba
    fastjson
    1.2.47
  • 调用示例代码
package com.xs.ocr;

import java.net.URLEncoder;

import com.alibaba.fastjson.JSON;
import com.xs.pojo.ocr.iocr.IOCRBean;
import com.xs.util.baidu.HttpUtil;
import com.xs.util.baidu.official.Base64Util;
import com.xs.util.baidu.official.FileUtil;

public class IOCRSample {
	public static void main(String[] args) throws Exception {
		//图片本地路径
		String filePath = "C:/Users/Administrator/Desktop/query.jpg";
		//根据apikey secretkey获取token
		String accessToken = "";
		//自定义模块templateSign 
		String templateSign = "";
		//返回指定的对象属性内容
		IOCRBean iocrBean = getResidencePermitOCRBean(filePath, accessToken, templateSign);
		for (int i = 0; i < iocrBean.getData().getRet().size(); i++) {
			System.out.println(iocrBean.getData().getRet().get(i).getWord_name()+":"+iocrBean.getData().getRet().get(i).getWord());
		}
	}
	/**
	 * 返回对象
	 * @param filePath 图片文件路径
	 * @param accessToken 应用token
	 * @param templateSign 自定义模板id 
	 * @return IOCRBean
	 * @throws Exception
	 */
	public static IOCRBean getResidencePermitOCRBean(String filePath,String accessToken,String templateSign) throws Exception{
		//接口地址
		String iocrurl = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise";
		//获取图片的base64数据 先获取图片的数组数据再进行base64 编码
		byte[] imgaebinary = FileUtil.readFileByBytes(filePath);
		String imagebase64 = Base64Util.encode(imgaebinary);
		//拼接接口所需参数 image参数需要urlencode哦
		String param = "image="+URLEncoder.encode(imagebase64,"UTF-8")+"&templateSign="+templateSign;
		//开始请求接口
		String result = HttpUtil.post(iocrurl, accessToken, param);
		//输出接口返回的内容
		IOCRBean bean = JSON.parseObject(result,IOCRBean.class);
		return bean;
	}

  • 使用fastjson转JavaBean提取必要的参数内容(隐藏了部分真实个人资料)
姓名:XX帅
户籍所在地:xxxxxxx区
民族:汉
证件编号:xxxxxxxx74
身份证号码:xxxxxxx857
性别:男
现居住地址:武汉市江汉区金家墩责任区陈家墩818号1栋1单元2楼2号
有效期限:2012年10月28日至2013年10月28日
  • 用到的JavaBean(方便读取识别结果内容)set/get方法省略请自行添加
package com.xs.pojo.ocr.iocr;

import java.util.List;
/**
 * 自定义模板文字识别Javabean 
 * 只针对于templateSign参数测试过 classifierId并不一定完全适用哦
 * @author 小帅丶
 */
public class IOCRBean {
    private Data data;//识别返回的结果
    private int error_code;//0代表成功,如果有错误码返回可以参考官方错误码列表排查问题
    private String error_msg;//如果error_code具体的失败信息,可以官方错误码列表排查问题
    
    public static class Data {
        private List ret;//识别出来的字段数组,每一个单元里包含以下几个元素
        private String templateSign;//使用的模板ID
        private int scores;//分类器的得分,直接选择模板ID识别时为1
        private boolean isStructured;//表示是否结构话成功,true为成功,false为失败
        private String logId;//调用的日志id
    }
    
    public static class Ret {
        private List charset;//单个字的数组,每个元素包含一个word 和 rect
        private String word_name;//表示字段的名字
        private String word;//识别的字符串
    }
    public static class Charset {
        private Rect rect;//字符串或单字所在矩形框
        private String word;//识别的单字
    }
    public static class Rect {
        private int top;//表示定位位置的长方形左上顶点的垂直坐标
        private int left;//表示定位位置的长方形左上顶点的水平坐标
        private int width;//表示定位位置的长方形的宽度
        private int height;//表示定位位置的长方形的高度
    }
}

是不是很简单 很好用呢?目前小帅丶做了武汉和北京居住证正面识别哦

期待模板可以供所有开发者使用就好了。大家就不用自己再标注了

收藏
点赞
2
个赞
共3条回复 最后由追星逐月的家回复于2018-10-25 23:19
#4追星逐月的家回复于2018-10-25 23:19:46

好详细,贴吧需要这么详细的帖子

0
#3756665228回复于2018-07-08 09:58:13
#2 秒1818回复
膜拜,这个已经特别详细了,按照教程可以直接操作

目前想法做分类器(已经包含了北京和武汉居住证)。想法是把全国所有的居住证都放进去。然后这个分类器能公开使用就好了。这样大家就没必要再去自己做分类器训练了。

1
#2秒1818回复于2018-07-07 23:47:55

膜拜,这个已经特别详细了,按照教程可以直接操作

0
TOP
切换版块