百度云文字识别 (AIPOcr)

接下来写点关于百度云 文字识别的 笔记吧.


在这里我根据返回参数 写了几个对应的实体类来接收,





写的一个java类继承文档提供的关于文字识别操作类

public class AIPOcrJava extends AipOcr {
    //设置APPID/AK/SK
    public static final String APP_ID = "*********";
    public static final String API_KEY = "*******************";
    public static final String SECRET_KEY = "************************";

    public AIPOcrJava(){super(APP_ID, API_KEY, SECRET_KEY);
     this.setConnectionTimeoutInMillis(60000);
     this.setSocketTimeoutInMillis(20000);
        System.setProperty("aip.log4j.conf", "classpath/log4j.properties");
    }

    public AIPOcrJava(String aipId, String aipKey, String secretKey) {
       super(aipId,aipKey,secretKey);
        this.setConnectionTimeoutInMillis(60000);
        this.setSocketTimeoutInMillis(60000);
        System.setProperty("aip.log4j.conf", "classpath/log4j.properties")

1,通用文字识别

public class AipOcrTest {

    //通用文字识别
  public static  String basicGeneral (String file){
       HashMap<String,String> options = new HashMap<>(4);

           options.put("language_type", "CHN_ENG");
          options.put("detect_direction", "true"); // 检测图片朝上
          options.put("detect_language", "true");  // 检测语言,默认是不检查
          options.put("probability", "true");   //是否返回识别结果中每一行的置信度
      AIPOcrJava aipOcrJava = new AIPOcrJava();
     org.json.JSONObject jsonObject = aipOcrJava.basicGeneral(file, options);
      return jsonObject.toString();
  }

// String result = basicGeneral("D:\\BaiDuYun\\webImageTest.jpg");
 //{"log_id":4384474417080234428,"wordsResult":[
 // {"probability":{"average":0.968971,"min":0.955538,"variance":1.8E-4},"words":"甜蜜"},
 // {"probability":{"average":0.871825,"min":0.744326,"variance":0.016256},"words":"酿造"}],
 // "words_result_num":2,"language":-1,"direction":0}

2,通用文字识别高精度版

public static  JSONObject basicAccurateGeneral (String file) {
    HashMap<String,String> options = new HashMap<>(4);

    options.put("language_type", "CHN_ENG");
    options.put("detect_direction", "true"); // 检测图片朝上
    options.put("detect_language", "true");  // 检测语言,默认是不检查
    options.put("probability", "true");   //是否返回识别结果中每一行的置信度
    AIPOcrJava aipOcrJava = new AIPOcrJava();
   // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = FileCopyUtils.copyToByteArray(new FileInputStream(file));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return   aipOcrJava.basicAccurateGeneral(bytes, options);

}

   //高精度版
 // JSONObject result = basicAccurateGeneral("D:\\BaiDuYun\\imageTest2.jpeg");
//    String s = /result.toString();
    //  {"log_id":4759662113465573053,"wordsResult":[
    // {"probability":{"average":0.985876,"min":0.940408,"variance":4.62E-4},"words":"当我爱一个人"},
    // {"probability":{"average":0.999704,"min":0.99927,"variance":0},"words":"会很热烈又会很淡漠"},
    // {"probability":{"average":0.996163,"min":0.964984,"variance":7.3E-5},"words":"会好好的爱也会口无遮拦的去说违心的话"},
    // {"probability":{"average":0.993361,"min":0.960509,"variance":1.84E-4},"words":"如果我会毁了爱也会被爱毁掉"},
    // {"probability":{"average":0.989808,"min":0.951065,"variance":3.72E-4},"words":"但是我一定很爱那个人"},
    // {"probability":{"average":0.985335,"min":0.878724,"variance":0.001031},"words":"爱情对我来说是爱到他人看不穿"}],
    // "words_result_num":6,"language":-1,"direction":0}

3,

//通用文字识别(含位置信息版)
public static JSONObject general (String file) {
    HashMap<String,String> options = new HashMap<>();

    options.put("language_type", "CHN_ENG");
    options.put("detect_direction", "true"); // 检测图片朝上
    options.put("vertexes_location", "true");
    options.put("recognize_granularity", "big");
    options.put("detect_language", "true");  // 检测语言,默认是不检查
    options.put("probability", "true");   //是否返回识别结果中每一行的置信度
    AIPOcrJava aipOcrJava = new AIPOcrJava();
    // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = FileCopyUtils.copyToByteArray(new FileInputStream(file));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return aipOcrJava.general(bytes, options);
}
 
 
 //JSONObject jsonObject = general("D:\\BaiDuYun\\wordTest.jpg");
 // {"log_id":6464113204717508848,"words_result":[
 // {"probability":{"average":0.955421,"min":0.81863,"variance":0.004858},"words":"想拉你一把"},
 // {"probability":{"average":0.887704,"min":0.364053,"variance":0.054941},"words":"你的手在里"},
 // {"probability":{"average":0.722126,"min":0.599519,"variance":0.015032},"words":"33"},
 // {"probability":{"average":0.764071,"min":0.764071,"variance":0},"words":"i"}],"words_result_num":4,"language":-1,"direction":0}
 //String str = jsonObject.toString();
// ResultBean resultBean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(str),ResultBean.class);

4
 // 含位置信息的 高精度版本
// 通用文字识别(含生僻字版) 繁体字
public static JSONObject enhancedGeneral (String file) {
     HashMap<String,String> options = new HashMap<>(6);

    options.put("language_type", "CHN_ENG");
    options.put("detect_direction", "true");
    options.put("detect_language", "true");
    options.put("probability", "true");
     AIPOcrJava aipOcrJava = new AIPOcrJava();
     // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
     byte[] bytes = new byte[0];
     try {
         bytes = Util.readFileByBytes(file);
     } catch (IOException e) {
         e.printStackTrace();
     }
     JSONObject jsonObject = aipOcrJava.enhancedGeneral(bytes, options);
  //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

     return jsonObject;
  //生僻字
// JSONObject result = enhancedGeneral("D:\\BaiDuYun\\fantizi.jpg");


// 身份证识别
public static JSONObject idCard (String file,String side) {
    HashMap<String,String> options = new HashMap<>(6);

    options.put("detect_direction", "true");
    options.put("detect_risk", "true");//是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,默认不开启,即:false。可选值:true-开启;false-不开启
    AIPOcrJava aipOcrJava = new AIPOcrJava();
    // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = Util.readFileByBytes(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject jsonObject = aipOcrJava.idcard(bytes, side,options);
    //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

    return jsonObject;
}
 
 
//身份证 正面 有一个 image-status
normal-识别正常
reversed_side-未摆正身份证
non_idcard-上传的图片中不包含身份证
blurred-身份证模糊
over_exposure-身份证关键字段反光或过曝
unknown-未知状态
// JSONObject front = idCard("D:\\BaiDuYun\\idCrdFront.jpg", "front"); // System.out.println(front.toString()); //{"log_id":3309234557215895357,"words_result": // {"姓名":{"words":"***","location":{"top":444,"left":138,"width":172,"height":63}}, // "民族":{"words":"汉","location":{"top":574,"left":419,"width":36,"height":44}}, // "住址":{"words":"****************","location":{"top":788,"left":125,"width":568,"height":130}}, // "公民身份号码":{"words":"************00122","location":{"top":1045,"left":361,"width":707,"height":76}}, // "出生":{"words":"******8","location":{"top":0,"left":0,"width":0,"height":0}}, // "性别":{"words":"男","location":{"top":0,"left":0,"width":0,"height":0}}},"words_result_num":6,"image_status":"normal","direction":0}
 //身份 背面  传的参数 "back"
 //JSONObject back = idCard("D:\\BaiDuYun\\idCrdBack.jpg","back");
// System.out.println(back.toString());
 //{"log_id":1473111127994119341,"words_result":
 // {"失效日期":{"words":"20220502","location":{"top":1029,"left":732,"width":190,"height":42}},
 // "签发机关":{"words":"***县公安局","location":{"top":945,"left":504,"width":236,"height":45}},
 // "签发日期":{"words":"20120502","location":{"top":1037,"left":508,"width":198,"height":42}}},
 // "words_result_num":3,"image_status":"normal","direction":0}
// 银行卡识别
public static JSONObject bankCard (String file) {
    HashMap<String,String> options = new HashMap<>(6);

    AIPOcrJava aipOcrJava = new AIPOcrJava();
    // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = Util.readFileByBytes(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject jsonObject = aipOcrJava.bankcard(bytes,options);
    //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

    return jsonObject;
}
 
 
  //银行卡识别
//  JSONObject result = bankCard("D:\\BaiDuYun\\bankCard.jpg");
 // System.out.println(result.toString());
  //+bank_card_type    number 是  银行卡类型,0:不能识别; 1: 借记卡; 2: 信用卡
  //{"result":{"bank_card_number":"621**********13","bank_card_type":1,"bank_name":"邮储银行"},"log_id":3218655891356708921}

//驾驶证识别
public static JSONObject drivingLicense (String file) {
    HashMap<String,String> options = new HashMap<>(6);
    options.put("detect_direction", "false");  //不检查朝上

    AIPOcrJava aipOcrJava = new AIPOcrJava();
    // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = Util.readFileByBytes(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject jsonObject = aipOcrJava.drivingLicense(bytes,options);
    //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

    return jsonObject;
}
 
 
//驾照识别
 // JSONObject result = drivingLicense("D:\\BaiDuYun\\drivingLicense.jpg");
//  System.out.println(result.toString());
  //{"log_id":2986902282992381075,"words_result":
  // {"姓名":{"words":"***"},"至":{"words":"20211124"},
  // "证号":{"words":"5********"},
  // "出生日期":{"words":"19921004"},
  // "住址":{"words":"**********号"},
  // "国籍":{"words":"中国"},
  // "初次领证日期":{"words":"20151124"},
  // "准驾车型":{"words":"C1"},
  // "有效期限":{"words":"20151124"},
  // "性别":{"words":"女"}},"words_result_num":10,"direction":-1}
//行驶证 识别
public static JSONObject vehicleLicense (String file) {
    HashMap<String,String> options = new HashMap<>(6);
    options.put("detect_direction", "false");  //不检查朝上
    options.put("accuracy", "normal");
  //  normal 使用快速服务,1200ms左右时延;缺省或其它值使用高精度服务,1600ms左右时延

    AIPOcrJava aipOcrJava = new AIPOcrJava();
    // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = Util.readFileByBytes(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject jsonObject = aipOcrJava.vehicleLicense(bytes,options);
    //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

    return jsonObject;
}
 
 
//没得车,所以没得行驶证............,未测试.....

 
 
//车牌 识别
public static JSONObject plateLicense (String file) {
    HashMap<String,String> options = new HashMap<>(6);
    options.put("multi_detect", "true");
    //是否检测多张车牌,默认为false,当置为true的时候可以对一张图片内的多张车牌进行识别

    AIPOcrJava aipOcrJava = new AIPOcrJava();
    // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = Util.readFileByBytes(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject jsonObject = aipOcrJava.plateLicense(bytes,options);
    //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

    return jsonObject;
}
// 营业执照识别 businessLicense
public static JSONObject businessLicense (String file) {
    HashMap<String,String> options = new HashMap<>(6);

    AIPOcrJava aipOcrJava = new AIPOcrJava();
    // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
    byte[] bytes = new byte[0];
    try {
        bytes = Util.readFileByBytes(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject jsonObject = aipOcrJava.businessLicense(bytes,options);
    //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

    return jsonObject;
}
 
 
 //车牌识别
// JSONObject result = plateLicense("D:\\BaiDuYun\\plateLicense.jpg");
 //System.out.println(result.toString());
 //{"log_id":5545684252572090954,"words_result":[
 // {"number":"粤A69L59","
 // vertexes_location":[{"x":44,"y":106},{"x":243,"y":107},{"x":243,"y":172},{"x":44,"y":170}],
 // "color":"blue",
 // "probability":[1,0.9999933242797852,0.9999986886978149,0.9999988079071045,0.9999998807907104,0.9999427795410156,0.9997766613960266]}]}

 
 
//执照识别  businessLicense
 JSONObject result = plateLicense("D:\\BaiDuYun\\businessLicense.jpg");
   System.out.println(result.toString());
 //自定义模版文字识别
// 自定义模版文字识别,是针对百度官方没有推出相应的模版,但是当用户需要对某一类卡证/票据(如房产证、军官证、火车票等)进行结构化的提取内容时
 // ,可以使用该产品快速制作模版,进行识别。
 public static JSONObject custom (String file) {
     HashMap<String,String> options = new HashMap<>(6);
     String templateSign = "Nsdax2424asaAS791823112"; //通用模板的 编号

     AIPOcrJava aipOcrJava = new AIPOcrJava();
     // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
     byte[] bytes = new byte[0];
     try {
         bytes = Util.readFileByBytes(file);
     } catch (IOException e) {
         e.printStackTrace();
     }
     JSONObject jsonObject = aipOcrJava.custom(bytes,templateSign,options);
     //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

     return jsonObject;
 }
百度的语音其实都跟这些差不多,可以自己试试了.

猜你喜欢

转载自blog.csdn.net/fzq_javaee/article/details/80490398