当前位置:   article > 正文

AI识别(Springboot+AI)一_spring ai

spring ai

本案例是springboot + 百度AI 来实现 文字和图像识别

案例已经开源:点击打开链接

如有疑问加 QQ群联系我:278947305 

案例如下 使用的是百度Ai API 所以使用时需要去百度注册得到apikey

AI文字识别 案例中使用两种方式 一种是本地上传 和网咯图片进行识别


AI图像识别 包括动物,植物,车型识别 只是用这三种来举例


请求日志


第一步搭建springboot环境

见之前的博客有(哈哈)这里主要介绍AI的使用、

从百度得到 API 地址

  1. /**
  2. * 文字解析
  3. */
  4. public static String OrcUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
  5. /**
  6. * 图像识别
  7. */
  8. //动物识别
  9. public static String classifyUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal";
  10. //植物识别
  11. public static String plantUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant";
  12. //车型识别
  13. public static String carUrl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/car";

调用时需要进行token验证,需要去百度ai获取

  1. /**
  2. * 获取API访问token 该token有一定的有效期,需要自行管理,当失效时需重新获取.
  3. * @param ak - 百度云官网获取的 API Key
  4. * @param sk
  5. * - 百度云官网获取的 Securet Key
  6. * @return assess_token 示例:
  7. * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
  8. */
  9. public static String getAuth(String ak, String sk) {
  10. // 获取token地址
  11. String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
  12. String getAccessTokenUrl = authHost
  13. // 1. grant_type为固定参数
  14. + "grant_type=client_credentials"
  15. // 2. 官网获取的 API Key
  16. + "&client_id=" + ak
  17. // 3. 官网获取的 Secret Key
  18. + "&client_secret=" + sk;
  19. try {
  20. URL realUrl = new URL(getAccessTokenUrl);
  21. // 打开和URL之间的连接
  22. HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
  23. connection.setRequestMethod("GET");
  24. connection.connect();
  25. // 获取所有响应头字段
  26. Map<String, List<String>> map = connection.getHeaderFields();
  27. // 遍历所有的响应头字段
  28. for (String key : map.keySet()) {
  29. System.err.println(key + "--->" + map.get(key));
  30. }
  31. // 定义 BufferedReader输入流来读取URL的响应
  32. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  33. String result = "";
  34. String line;
  35. while ((line = in.readLine()) != null) {
  36. result += line;
  37. }
  38. /**
  39. * 返回结果示例
  40. */
  41. System.err.println("result:" + result);
  42. JSONObject jsonObject = new JSONObject(result);
  43. String access_token = jsonObject.getString("access_token");
  44. return access_token;
  45. } catch (Exception e) {
  46. System.err.printf("获取token失败!");
  47. e.printStackTrace(System.err);
  48. }
  49. return null;
  50. }

使用网络图片来识别图片中的文字

  1. /**
  2. * 使用网络图片
  3. * @methodsDescription:
  4. * @methodName: rec2
  5. * @param file
  6. * @return
  7. * @author: singleton-zw
  8. * @return: R
  9. */
  10. @RequestMapping(value="/recognNet" ,method = {RequestMethod.GET,RequestMethod.POST})
  11. @ResponseBody
  12. public R rec2(@RequestParam("urlNet") String urlNet){
  13. if (urlNet.isEmpty()) {
  14. return R.ok().put("orc","url不能为空");
  15. }
  16. String relst = "";
  17. try {
  18. relst = URLEncoder.encode("url", "UTF-8") + "=" + URLEncoder.encode(urlNet, "UTF-8");
  19. /**
  20. * 线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
  21. */
  22. String accessToken = AccessToken.getAuth(orc.getOcrClientId(),orc.getOcrClientSecret());
  23. String result = HttpUtil.post(ConfigURL.OrcUrl, accessToken, relst);
  24. Result fromJson = GsonUtils.fromJson(result, Result.class);
  25. List<Result.Word> data = fromJson.getWords_result();
  26. String w = "";
  27. for (Result.Word word : data) {
  28. w += word.getWords()+"<br>";
  29. }
  30. return R.ok().put("orc", w);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return R.ok().put("orc", "失败");
  35. }
下一篇: 点击打开链接
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/244034
推荐阅读
相关标签
  

闽ICP备14008679号