本文共 2033 字,大约阅读时间需要 6 分钟。
近期在做一个关于图像识别的小项目时,经过大量资料查阅和实践,发现自己实现的图像识别功能还不够理想。经过一番调研,找到了一个可商用且识别率非常高的百度AI接口,值得推荐。
选择百度API主要有以下几点原因:
以下以动物识别为例,展示实际效果。
https://aip.baidubce.com/rest/2.0/image-classify/v1/animal示例返回结果:
{ "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": "闪电红白锦鲤"} ]} // 上传图片并获取识别结果private String plant(String filePath) { try { // 读取图片文件 byte[] imgData = FileUtil.readFileByBytes(filePath); // Base64编码 String imgStr = Base64Util.encode(imgData); // URL编码 String imgParam = URLEncoder.encode(imgStr, "UTF-8"); String param = "image=" + imgParam; // 获取AccessToken String accessToken = Token.getAuth(); // 发送HTTP POST请求 String result = HttpUtil.post(url, accessToken, param); System.out.println(result); return result; } catch (Exception e) { e.printStackTrace(); return "图片规格不符合"; }} // 使用Gson解析JSON结果Gson gson = new Gson();RecResult recresult = gson.fromJson(result, RecResult.class);Listlist = recresult.getResult();for (int i = 0; i < list.size(); i++) { double score = list.get(i).getScore(); String name = list.get(i).getName(); // 根据需求处理结果 // 例如:打印名称和相似度 System.out.println("名称: " + name + ", 相似度: " + score);}
为了正常使用图像识别功能,需要在AndroidManifest.xml中声明以下权限:
以上代码和文档已上传至相关平台,请根据需要进行集成和使用。
通过以上步骤,可以方便地实现百度API图像识别功能,识别率高且开发成本低,是开发者和企业的理想选择。
转载地址:http://zxcmz.baihongyu.com/