JAVA调用阿里云图像识别接口实现图片垃圾分类
阿里云图像识别API,识别物品图片给出它属于的垃圾分类。看官方文档不太详细,这里给出一种实测可行方案,没考虑程序健壮性,还是建议优先参考官方文档:
官方教程文档:
.htm?spm=a2c4g.11186623.2.12.11bc5688Bee7Dq#doc-api-imagerecog-ClassifyingRubbish
阿里云视觉智能开放平台:
一、准备工作:
1.配置maven依赖:
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>imagerecog20190930</artifactId><version>1.0.1</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>tea-openapi</artifactId><version>0.0.7</version>
</dependency>
2.去视觉智能开放平台申请accessKey和accessKeySecret
二、JAVA实现示例
1.将本地图片上传获取图片url:
String file = "XX:\\aliClassifyRubish\\example.jpg"; //本地图片地址
InputStream inputStream = ViapiFileUtilAdvance.buildInputStream(file);
ViapiFileUtilAdvance fileUtils = ViapiFileUtilAdvance.getInstance(accessKey, accessKeySecret, regionId);
String ossTempFileUrl = fileUtils.upload(inputStream);
2.创建代理
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.imagerecog20190930.Client client = Classify.createClient(accessKey, accessKeySecret);//Classify为此类名
ClassifyingRubbishRequest classifyingRubbishRequest = new ClassifyingRubbishRequest().setImageURL(ossTempFileUrl);
其中
public static com.aliyun.imagerecog20190930.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {Config config = new Config()// 您的AccessKey ID.setAccessKeyId(accessKeyId)// 您的AccessKey Secret.setAccessKeySecret(accessKeySecret);// 访问的域名config.endpoint = "imagerecog-shanghai.aliyuncs";return new com.aliyun.imagerecog20190930.Client(config);
}
3.调用接口获取返回值
List list=client.classifyingRubbish(classifyingRubbishRequest).getBody().getData().getElements();
JSONArray jsonArray=(JSONArray) JSONArray.toJSON(list);
Map map=(Map) jsonArray.get(0);
String rubbish = (String) map.get("rubbish");
String category = (String) map.get("category");
float score = (float) map.get("categoryScore");
完整代码如下:
import com.alibaba.fastjson.JSONArray;
import com.aliyun.imagerecog20190930.models.*;
import com.aliyun.teaopenapi.models.*;
import java.io.InputStream;
import java.util.List;
import java.util.Map;public class Classify {/*** 使用AK&SK初始化账号Client* @param accessKeyId* @param accessKeySecret* @return Client* @throws Exception*/public static com.aliyun.imagerecog20190930.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {Config config = new Config()// 您的AccessKey ID.setAccessKeyId(accessKeyId)// 您的AccessKey Secret.setAccessKeySecret(accessKeySecret);// 访问的域名config.endpoint = "imagerecog-shanghai.aliyuncs";return new com.aliyun.imagerecog20190930.Client(config);}public static void main(String[] args_) throws Exception {String accessKey = "XXXXXXX"; //您的AccessKeyIDString accessKeySecret = "XXXXXXXXXXXX"; //您的AccessKeySecretString regionId = "cn-shanghai";String file = "XX:\\aliClassifyRubish\\example.jpg"; //或者本地上传//String file = ".jpg";InputStream inputStream = ViapiFileUtilAdvance.buildInputStream(file);ViapiFileUtilAdvance fileUtils = ViapiFileUtilAdvance.getInstance(accessKey, accessKeySecret, regionId);String ossTempFileUrl = fileUtils.upload(inputStream);System.out.println(ossTempFileUrl);java.util.List<String> args = java.util.Arrays.asList(args_);com.aliyun.imagerecog20190930.Client client = Classify.createClient(accessKey, accessKeySecret);ClassifyingRubbishRequest classifyingRubbishRequest = new ClassifyingRubbishRequest().setImageURL(ossTempFileUrl);// 获取结果List list=client.classifyingRubbish(classifyingRubbishRequest).getBody().getData().getElements();JSONArray jsonArray=(JSONArray) JSONArray.toJSON(list);Map map=(Map) jsonArray.get(0);String rubbish = (String) map.get("rubbish");String category = (String) map.get("category");float score = (float) map.get("categoryScore");System.out.println("rubbish:"+rubbish);System.out.println("category:"+category);System.out.println("score:"+ score);}}
运行结果:
JAVA调用阿里云图像识别接口实现图片垃圾分类
阿里云图像识别API,识别物品图片给出它属于的垃圾分类。看官方文档不太详细,这里给出一种实测可行方案,没考虑程序健壮性,还是建议优先参考官方文档:
官方教程文档:
.htm?spm=a2c4g.11186623.2.12.11bc5688Bee7Dq#doc-api-imagerecog-ClassifyingRubbish
阿里云视觉智能开放平台:
一、准备工作:
1.配置maven依赖:
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>imagerecog20190930</artifactId><version>1.0.1</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>tea-openapi</artifactId><version>0.0.7</version>
</dependency>
2.去视觉智能开放平台申请accessKey和accessKeySecret
二、JAVA实现示例
1.将本地图片上传获取图片url:
String file = "XX:\\aliClassifyRubish\\example.jpg"; //本地图片地址
InputStream inputStream = ViapiFileUtilAdvance.buildInputStream(file);
ViapiFileUtilAdvance fileUtils = ViapiFileUtilAdvance.getInstance(accessKey, accessKeySecret, regionId);
String ossTempFileUrl = fileUtils.upload(inputStream);
2.创建代理
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.imagerecog20190930.Client client = Classify.createClient(accessKey, accessKeySecret);//Classify为此类名
ClassifyingRubbishRequest classifyingRubbishRequest = new ClassifyingRubbishRequest().setImageURL(ossTempFileUrl);
其中
public static com.aliyun.imagerecog20190930.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {Config config = new Config()// 您的AccessKey ID.setAccessKeyId(accessKeyId)// 您的AccessKey Secret.setAccessKeySecret(accessKeySecret);// 访问的域名config.endpoint = "imagerecog-shanghai.aliyuncs";return new com.aliyun.imagerecog20190930.Client(config);
}
3.调用接口获取返回值
List list=client.classifyingRubbish(classifyingRubbishRequest).getBody().getData().getElements();
JSONArray jsonArray=(JSONArray) JSONArray.toJSON(list);
Map map=(Map) jsonArray.get(0);
String rubbish = (String) map.get("rubbish");
String category = (String) map.get("category");
float score = (float) map.get("categoryScore");
完整代码如下:
import com.alibaba.fastjson.JSONArray;
import com.aliyun.imagerecog20190930.models.*;
import com.aliyun.teaopenapi.models.*;
import java.io.InputStream;
import java.util.List;
import java.util.Map;public class Classify {/*** 使用AK&SK初始化账号Client* @param accessKeyId* @param accessKeySecret* @return Client* @throws Exception*/public static com.aliyun.imagerecog20190930.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {Config config = new Config()// 您的AccessKey ID.setAccessKeyId(accessKeyId)// 您的AccessKey Secret.setAccessKeySecret(accessKeySecret);// 访问的域名config.endpoint = "imagerecog-shanghai.aliyuncs";return new com.aliyun.imagerecog20190930.Client(config);}public static void main(String[] args_) throws Exception {String accessKey = "XXXXXXX"; //您的AccessKeyIDString accessKeySecret = "XXXXXXXXXXXX"; //您的AccessKeySecretString regionId = "cn-shanghai";String file = "XX:\\aliClassifyRubish\\example.jpg"; //或者本地上传//String file = ".jpg";InputStream inputStream = ViapiFileUtilAdvance.buildInputStream(file);ViapiFileUtilAdvance fileUtils = ViapiFileUtilAdvance.getInstance(accessKey, accessKeySecret, regionId);String ossTempFileUrl = fileUtils.upload(inputStream);System.out.println(ossTempFileUrl);java.util.List<String> args = java.util.Arrays.asList(args_);com.aliyun.imagerecog20190930.Client client = Classify.createClient(accessKey, accessKeySecret);ClassifyingRubbishRequest classifyingRubbishRequest = new ClassifyingRubbishRequest().setImageURL(ossTempFileUrl);// 获取结果List list=client.classifyingRubbish(classifyingRubbishRequest).getBody().getData().getElements();JSONArray jsonArray=(JSONArray) JSONArray.toJSON(list);Map map=(Map) jsonArray.get(0);String rubbish = (String) map.get("rubbish");String category = (String) map.get("category");float score = (float) map.get("categoryScore");System.out.println("rubbish:"+rubbish);System.out.println("category:"+category);System.out.println("score:"+ score);}}
运行结果:
发布评论