全部产品

Java

更新时间:2020-03-13 22:27:15

本文介绍了如何使用阿里云IVPD服务的Java SDK,具体包括SDK的获取和安装方法以及SDK代码示例。

获取地址

您需要引入两个SDK,包括aliyun-java-sdk-core(阿里云核心SDK)和aliyun-java-sdk-ivpd(智能视觉生产SDK)。每个SDK都提供了Maven Repository、Central Repository、GitHub的获取方式,您可以选择合适的方式获取SDK。

安装说明

方法1:使用Maven(推荐)

如果您使用Maven管理Java项目,可以通过在pom.xml文件中添加Maven依赖

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-ivpd</artifactId>
  4. <version>1.0.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>aliyun-java-sdk-core</artifactId>
  9. <version>4.4.9</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.alibaba</groupId>
  13. <artifactId>fastjson</artifactId>
  14. <version>1.2.60</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.httpcomponents</groupId>
  18. <artifactId>httpclient</artifactId>
  19. <version>4.5.3</version>
  20. </dependency>

说明: version的值以SDK获取地址中的最新版本为准。

方法2:在集成开发环境(IDE)中导入jar文件

  • Eclipse安装
    • 将下载的aliyun-java-sdk-xxx.jar文件复制到您的项目文件夹中。
    • 在Eclipse中打开您的项目,右键单击该项目,单击Properties。
    • 在弹出的对话框中,单击Java Build Path > Libraries > Add JARs添加下载的JAR文件。
    • 单击Apply and Close。
  • IntelliJ 安装
    • 将下载的aliyun-java-sdk-xxx.jar文件复制到您的项目文件夹中。
    • 在IntelliJ中打开您的项目,在菜单栏中单击File > Project > Structure。
    • 单击Apply,然后单击OK。

示例

  1. package com.aliyun.aic.demo;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. import com.aliyuncs.*;
  5. import com.aliyuncs.http.ProtocolType;
  6. import com.aliyuncs.ivpd.model.v20190625.*;
  7. import com.aliyuncs.profile.DefaultProfile;
  8. import org.apache.http.util.Asserts;
  9. import java.util.*;
  10. /**
  11. * doc: https://help.aliyun.com/document_detail/133958.html?spm=a2c4g.11174283.6.559.2db8519cdKrB6k
  12. */
  13. public class IVPDSdkDemo {
  14. static IAcsClient client = null;
  15. static String regionId = "cn-beijing";
  16. public static void main(String[] args) throws Exception {
  17. DefaultProfile profile = DefaultProfile.getProfile(regionId, Config.acccessKeyId, Config.accesssKeySecret);
  18. profile.getHttpClientConfig().setMaxRequests(100);
  19. profile.getHttpClientConfig().setMaxRequestsPerHost(50);
  20. client = new DefaultAcsClient(profile);
  21. testSegmentImage();//通用分割
  22. testSegmentBody();//人像分割
  23. testDetectImageElements();//元素检测
  24. testRecolorImage();//色彩迁移/拓色
  25. testRecognizeImageColor();//色板识别
  26. testRecognizeImageStyle();//风格识别
  27. testExtendImageStyle();//风格迁移
  28. testChangeImageSize();//尺寸变换
  29. testMakeSuperResolutionImage();//超分辨率
  30. }
  31. public static void testChangeImageSize() throws Exception {
  32. System.out.println("-------- 尺寸变换 --------------");
  33. ChangeImageSizeRequest req = new ChangeImageSizeRequest();
  34. req.setUrl(formatDemoUrl("ChangeImageSize/change-image-size-src.png"));
  35. req.setWidth(800);
  36. req.setHeight(600);
  37. ChangeImageSizeResponse resp = getAcsResponse(req);
  38. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  39. }
  40. public static void testExtendImageStyle() throws Exception {
  41. ExtendImageStyleRequest req = new ExtendImageStyleRequest();
  42. System.out.println("-------- 图像风格迁移 --------------");
  43. String majorUrl = formatDemoUrl("ExtendImageStyle/majorUrl.jpeg");
  44. String styleUrl = formatDemoUrl("ExtendImageStyle/styleUrl.jpeg");
  45. req.setMajorUrl(majorUrl);
  46. req.setStyleUrl(styleUrl);
  47. ExtendImageStyleResponse resp = getAcsResponse(req);
  48. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  49. }
  50. public static void testMakeSuperResolutionImage() throws Exception {
  51. MakeSuperResolutionImageRequest req = new MakeSuperResolutionImageRequest();
  52. System.out.print("-------- 清晰化/超分辨 ----");
  53. req.setUrl(formatDemoUrl("MakeSuperResolution/sup-dog.png"));
  54. MakeSuperResolutionImageResponse resp = getAcsResponse(req);
  55. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  56. }
  57. public static void testRecognizeImageColor() throws Exception {
  58. RecognizeImageColorRequest req = new RecognizeImageColorRequest();
  59. System.out.print("-------- 色板识别 ----");
  60. req.setUrl(formatDemoUrl("RecognizeImageColor/RecognizeImageColor.png"));
  61. RecognizeImageColorResponse resp = getAcsResponse(req);
  62. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  63. }
  64. public static void testRecognizeImageStyle() throws Exception {
  65. RecognizeImageStyleRequest req = new RecognizeImageStyleRequest();
  66. System.out.print("-------- 风格识别 ----");
  67. req.setUrl(formatDemoUrl("RecognizeImageStyle/technology.png"));
  68. RecognizeImageStyleResponse resp = getAcsResponse(req);
  69. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  70. }
  71. public static void testRecolorImage() throws Exception {
  72. RecolorImageRequest req = new RecolorImageRequest();
  73. System.out.print("-------- 拓色 ----");
  74. req.setUrl(formatDemoUrl("RecolorImage/recolor-1-src.png"));
  75. req.setColorCount(3);
  76. //(必填)拓色模式,默认AUTO,取值范围[AUTO:自动拓色, TEMPLATE:色板拓色, REF_PIC:参考图拓色]
  77. req.setMode("AUTO");
  78. RecolorImageResponse resp = null;
  79. //AUTO
  80. System.out.println("自动拓色");
  81. resp = getAcsResponse(req);
  82. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  83. //RefUrl TODO
  84. System.out.println("参考图拓色");
  85. req.setMode("REF_PIC");
  86. req.setRefUrl(formatDemoUrl("RecolorImage/recolor-refurl-src.png"));
  87. resp = getAcsResponse(req);
  88. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  89. //ColorTemplate TODO
  90. System.out.println("色板拓色");
  91. req.setMode("TEMPLATE");
  92. req.setUrl(formatDemoUrl("RecolorImage/recolor-2-src.png"));
  93. req.setUrl(formatDemoUrl("RecolorImage/recolor-refurl-src.png"));
  94. List<RecolorImageRequest.ColorTemplate> colorTemplateList = new ArrayList<>();
  95. RecolorImageRequest.ColorTemplate c1 = new RecolorImageRequest.ColorTemplate();
  96. RecolorImageRequest.ColorTemplate c2 = new RecolorImageRequest.ColorTemplate();
  97. RecolorImageRequest.ColorTemplate c3 = new RecolorImageRequest.ColorTemplate();
  98. c1.setColor("056A6B");
  99. c2.setColor("FF0000");//16711680
  100. c3.setColor("00FF00");//65280
  101. colorTemplateList.add(c1);
  102. colorTemplateList.add(c2);
  103. colorTemplateList.add(c3);
  104. req.setColorTemplates(colorTemplateList);
  105. resp = getAcsResponse(req);
  106. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  107. }
  108. public static void testDetectImageElements() throws Exception {
  109. DetectImageElementsRequest req = new DetectImageElementsRequest();
  110. System.out.print("-------- 元素检测 -----");
  111. req.setUrl(formatDemoUrl("DetectImageElements/detect-elements-src.png"));
  112. DetectImageElementsResponse resp = getAcsResponse(req);
  113. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  114. }
  115. public static SegmentImageResponse testSegmentImage() throws Exception {
  116. SegmentImageRequest req = new SegmentImageRequest();
  117. System.out.print("-------- 通用分割 -----");
  118. req.setUrl(formatDemoUrl("SegmentImage/segment-image-src.jpg"));
  119. SegmentImageResponse resp = getAcsResponse(req);
  120. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  121. return resp;
  122. }
  123. public static SegmentBodyResponse testSegmentBody() throws Exception {
  124. SegmentBodyRequest req = new SegmentBodyRequest();
  125. System.out.print("-------- 人像分割 -----");
  126. req.setImageUrl(formatDemoUrl("SegmentBody/mayun.jpeg"));
  127. SegmentBodyResponse resp = getAcsResponse(req);
  128. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  129. return resp;
  130. }
  131. public static void printResponse(String action, String requestId, String code, String message, Object data) {
  132. System.out.print(" " + action + " ======================================\n");
  133. System.out.println("requestId: " + requestId);
  134. System.out.println("code: " + code);
  135. System.out.println(" msg: " + message);
  136. System.out.println("body.class: " + data == null ? null : data.getClass());
  137. System.out.println("body: " + JSON.toJSONString(data, SerializerFeature.PrettyFormat));
  138. Asserts.check("0".equals(code), "assert code == 0 req:");
  139. Asserts.check(null != data, "assert data != null");
  140. System.out.println("\n");
  141. }
  142. private static <R extends RpcAcsRequest<T>, T extends AcsResponse> T getAcsResponse(R req) throws Exception {
  143. req.setSysProtocol(ProtocolType.HTTPS);
  144. return client.getAcsResponse(req);
  145. }
  146. private static String formatDemoUrl(String file) {
  147. return String.format("http://ivpd-%s.oss-%s.aliyuncs.com/ivpd-demo/images/%s", regionId, regionId, file);
  148. }
  149. }