博客
关于我
Java调用百度API实现图像识别
阅读量:658 次
发布时间:2019-03-15

本文共 2347 字,大约阅读时间需要 7 分钟。

Java调用百度API实现图像识别

最近在做一个关于识别的小功能,翻阅了一堆资料,也实践自己去实现这个功能,最后识别的结果不是那么理想。这里介绍一个完全可以商用以及识别率超高的百度ai接口

1、为什么要用百度API来进行图像识别?

主要原因有三点:

1、识别率比自己做的要高、要好。

2、省去了收集数据集的时间。而且人家的数据集还更全面更牛逼。

3、直接调用接口,更加方便,不必去学习安卓ndk以及jni的使用,懂得安卓网络请求即可。

这里以动物识别为例,闲话就不多说了。先上个效果图

 

API文档参考地址:

首先先去百度AI官网 申请一个图像识别的应用获得API key 和Secret Key

1、获取接口url地址:

HTTP 方法:POST

请求URL: https://aip.baidubce.com/rest/2.0/image-classify/v1/animal

URL参数:

参数
access_token 通过API Key和Secret Key获取的access_token,参考“”
private String plant(String filePath) {        // 请求url        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal";        try {            // 本地文件路径            byte[] imgData = FileUtil.readFileByBytes(filePath);            String imgStr = Base64Util.encode(imgData);            String imgParam = URLEncoder.encode(imgStr, "UTF-8");            String param = "image=" + imgParam;            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。            String accessToken = Token.getAuth();            String result = HttpUtil.post(url, accessToken, param);            Log.i("lzw", result);            System.out.println(result);            return result;        } catch (Exception e) {            e.printStackTrace();            Log.i("lzw", "获取不到");            return "图片规格不符合";        }

2、Access_token获取

public static String getAuth() {        // 官网获取的 API Key 更新为你注册的        String clientId = "############";        // 官网获取的 Secret Key 更新为你注册的        String clientSecret = "*************";        return getAuth(clientId, clientSecret);    }

3、通过gson解析获得的数据

{"log_id": 686049192304682347, "result": [{"score": "0.55047", "name": "德国三色锦鲤"}, {"score": "0.0773791", "name": "锦鲤"}, {"score": "0.05252", "name": "大正三色锦鲤"}, {"score": "0.0337663", "name": "红白锦鲤"}, {"score": "0.0335902", "name": "日本锦鲤"}, {"score": "0.0248167", "name": "闪电红白锦鲤"}]}

Gson gson = new Gson();        RecResult recresult = gson.fromJson(result,RecResult.class);        Log.i("lzw","re_ok");        List
list = recresult.getResult(); List
> listmap = new ArrayList
>(); String str = ""; for(int i = 0;i
map = new HashMap
(); double s = score; String n = name; map.put("n","名称: "+n); map.put("s","相似度: "+s);}

4、权限

整个工程已上传到

你可能感兴趣的文章
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>