admin管理员组

文章数量:1530516

百度AI图片识别亲测成功详解

1、浏览器输入网址http://ai.baidu/或者百度一下搜索‘百度ai’点击第一个。

2、点击控制台,有百度账号的直接登录,没有的注册后登录。

3、点击左侧菜单“文字识别”—>点击“应用列表”—>点击“创建应用”—>填写相关信息后—>点“立即创建”—>获取“AppID、API Key、Secret Key”。

4、SDK下载,下载解压后将相应jar包添加到项目中;

5、点击“使用说明”查看帮助文档https://ai.baidu/ai-doc/OCR/1k3h7y3db

6、直接上代码,将如下代码直接复制到项目中运行看效果(注意需将第三点中获取的“AppID、API Key、Secret Key”填入代码中),根据项目实际情况结合帮助文档再进行调整即可。

 


import java.util.HashMap;

import org.json.JSONObject;
import org.springframework.stereotype.Component;
import com.baidu.aip.ocr.AipOcr;

@Component
public class Sample{

   // 设置APPID/AK/SK
   public static final String APP_ID = "第三点中获取的AppID";
   public static final String API_KEY = "第三点中获取的API Key";
   public static final String SECRET_KEY = "第三点中获取的Secret Key";

   public static void main(String[] arg){
       JSONObject res = sample();
       System.out.println(res.toString(2));
   }

   // 初始化用户对象
   public static AipOcr init() {
       // 初始化一个AipOcr
       AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
       // 可选:设置网络连接参数
       client.setConnectionTimeoutInMillis(2000);
       client.setSocketTimeoutInMillis(60000);

       return client;
   }

   public static JSONObject sample() {
       // 传入可选参数调用接口
       HashMap<String, String> options = new HashMap<String, String>();
       options.put("language_type", "CHN_ENG");
       options.put("detect_direction", "true");
       options.put("detect_language", "true");
       options.put("probability", "true");
       //初始化用户对象
       AipOcr client = init();

       // 参数为本地图片路径
       String path = "F:\\揣搜网.png";
       JSONObject res = client.basicGeneral(path, options);

       // 参数为本地图片二进制数组
       //String image = "F:\\揣搜网.png";
       //File file = new File(image);
       //byte[] fileByte = new byte[0];
       //try {
       //    fileByte = Files.readAllBytes(file.toPath());
       //} catch (IOException e) {
       //    e.printStackTrace();
       //}
       //JSONObject res = client.basicGeneral(fileByte, options);

       // 通用文字识别, 图片参数为远程url图片
       //String url = "https://timgsa.baidu/timg?" +
//        "image&quality=80&size=b9999_10000&sec=1600324362220&di=ffc9820b80235fcfa42c3c91b696ead4" +
//        "&imgtype=0&src=http%3A%2F%2Fcdn.duitang%" +
//        "2Fuploads%2Fitem%2F201512%2F29%2F20151229183756_SxVkw.jpeg";

       //JSONObject res = client.basicGeneralUrl(url, options);

       return res;
   }
}

 


7、识别效果。(注意免费是有次数限制的具体情况请查看“购买指南”)

 

转载至:http://www.chuaisou/index.html#/app/itw?rt=1&pn=I160031022Q521738

 

本文标签: 详解图片AI