全部产品
云市场
云游戏

快速入门

更新时间:2019-10-30 15:35:35

本文介绍Java SDK快速入门流程。

  1. 创建AcsClient实例。
  1. DefaultProfile profile = DefaultProfile.getProfile(
  2. regionId, // 地域ID
  3. accessKeyId, // RAM账号的AccessKey ID
  4. accessKeySecret); // RAM账号Access Key Secret
  5. IAcsClient client = new DefaultAcsClient(profile);

2.创建request,并设置参数。

  1. ImagePredictRequest request = new ImagePredictRequest();

3.发起API请求并显示返回值。

  1. ImagePredictResponse response = client.getAcsResponse(request);
  2. System.out.println("Predict result: " + response.getImagePredict().getPredictResult());

4.完整代码

  1. import com.aliyuncs.exceptions.ServerException;
  2. import com.aliyuncs.profile.DefaultProfile;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.alibaba.fastjson.JSON;
  6. import com.aliyuncs.exceptions.ClientException;
  7. import com.aliyuncs.ivision.model.v20190308.*;
  8. public class QuickStart {
  9. private static String accessKeyId = "<AccessKeyId>";
  10. private static String accessKeySecret = "<AccessKeySecret>";
  11. private static String regionId = "<RegionId>";
  12. public static void main(String[] args) {
  13. // 创建DefaultAcsClient实例并初始化
  14. DefaultProfile profile = DefaultProfile.getProfile(
  15. regionId, // 地域ID
  16. accessKeyId, // RAM账号的AccessKey ID
  17. accessKeySecret); // RAM账号Access Key Secret
  18. IAcsClient client = new DefaultAcsClient(profile);
  19. // 创建API请求并设置参数;图片同步预测接口
  20. ImagePredictRequest request = new ImagePredictRequest();
  21. // 图片地址,存放在阿里云oss上, 如果是oss私有读权限,不需要输入鉴权信息
  22. request.setDataUrl("http://demo-bucket.oss-cn-shanghai.aliyuncs.com/predict/test.jgp");
  23. //模型ID,是否戴帽子口罩
  24. request.setModelId("59520769ac6349b08af7bce82bdf7492");
  25. // 发起请求并处理应答
  26. try {
  27. ImagePredictResponse response = client.getAcsResponse(request);
  28. System.out.println("Predict result: " + response.getImagePredict().getPredictResult());
  29. } catch (ServerException e) {
  30. e.printStackTrace();
  31. } catch (ClientException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }