赞
踩
本案例是springboot + 百度AI 来实现 文字和图像识别
案例已经开源:点击打开链接
如有疑问加 QQ群联系我:278947305
案例如下 使用的是百度Ai API 所以使用时需要去百度注册得到apikey
AI文字识别 案例中使用两种方式 一种是本地上传 和网咯图片进行识别
AI图像识别 包括动物,植物,车型识别 只是用这三种来举例
请求日志
第一步搭建springboot环境
见之前的博客有(哈哈)这里主要介绍AI的使用、
从百度得到 API 地址
- /**
- * 文字解析
- */
- public static String OrcUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
-
- /**
- * 图像识别
- */
- //动物识别
- public static String classifyUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal";
- //植物识别
- public static String plantUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant";
- //车型识别
- public static String carUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/car";
调用时需要进行token验证,需要去百度ai获取
- /**
- * 获取API访问token 该token有一定的有效期,需要自行管理,当失效时需重新获取.
- * @param ak - 百度云官网获取的 API Key
- * @param sk
- * - 百度云官网获取的 Securet Key
- * @return assess_token 示例:
- * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
- */
- public static String getAuth(String ak, String sk) {
- // 获取token地址
- String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
- String getAccessTokenUrl = authHost
- // 1. grant_type为固定参数
- + "grant_type=client_credentials"
- // 2. 官网获取的 API Key
- + "&client_id=" + ak
- // 3. 官网获取的 Secret Key
- + "&client_secret=" + sk;
- try {
- URL realUrl = new URL(getAccessTokenUrl);
- // 打开和URL之间的连接
- HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
- connection.setRequestMethod("GET");
- connection.connect();
- // 获取所有响应头字段
- Map<String, List<String>> map = connection.getHeaderFields();
- // 遍历所有的响应头字段
- for (String key : map.keySet()) {
- System.err.println(key + "--->" + map.get(key));
- }
- // 定义 BufferedReader输入流来读取URL的响应
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String result = "";
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
- /**
- * 返回结果示例
- */
- System.err.println("result:" + result);
- JSONObject jsonObject = new JSONObject(result);
- String access_token = jsonObject.getString("access_token");
- return access_token;
- } catch (Exception e) {
- System.err.printf("获取token失败!");
- e.printStackTrace(System.err);
- }
- return null;
- }

使用网络图片来识别图片中的文字
- /**
- * 使用网络图片
- * @methodsDescription:
- * @methodName: rec2
- * @param file
- * @return
- * @author: singleton-zw
- * @return: R
- */
- @RequestMapping(value="/recognNet" ,method = {RequestMethod.GET,RequestMethod.POST})
- @ResponseBody
- public R rec2(@RequestParam("urlNet") String urlNet){
- if (urlNet.isEmpty()) {
- return R.ok().put("orc","url不能为空");
- }
- String relst = "";
- try {
- relst = URLEncoder.encode("url", "UTF-8") + "=" + URLEncoder.encode(urlNet, "UTF-8");
-
- /**
- * 线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
- */
- String accessToken = AccessToken.getAuth(orc.getOcrClientId(),orc.getOcrClientSecret());
- String result = HttpUtil.post(ConfigURL.OrcUrl, accessToken, relst);
- Result fromJson = GsonUtils.fromJson(result, Result.class);
- List<Result.Word> data = fromJson.getWords_result();
- String w = "";
- for (Result.Word word : data) {
- w += word.getWords()+"<br>";
- }
- return R.ok().put("orc", w);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return R.ok().put("orc", "失败");
- }

下一篇:
点击打开链接
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。