全部产品
云市场
云游戏

批量接口

更新时间:2020-05-21 10:58:11

介绍

批量接口支持一次提交多个输入(使用dataId字段进行区分),异步获取结果,dataId之间相互隔离。

已支持批量接口列表

批量接口 目标接口(Action) 名称
CreateSegmentBodyJob SegmentBody 批量人像抠图,在线调试

批量人像抠图(CreateSegmentBodyJob)

批量人像抠图是一次提交多张图片,异步获取结果,图片之间结果相互隔离。任务完成后(Completed=true)即可获取结果。

调用限制

  1. 输入遵循SegmentBody的输入限制;
  2. CreateSegmentBodyJob 调用量限制: 120次/分钟,每个Job最多支持50条数据;jobId不可重复,jobId内的dataId不可重复;
  3. jobId & dataId 输入限制:数字、字母、下划线(_)、减号(-),长度 1-64,必须包含数字或字母,推荐用UUID生成或数据库自增id。
  4. 任务超时时间:单位秒,范围[5,3600];
  5. 任务结果缓存有效期:最长1个小时。

操作步骤

  1. 创建任务: CreateSegmentBodyJob
  2. 查询状态: GetJobStatus
  3. 获取结果: GetJobResult
  4. 反序列化结果

代码示例

  1. package com.aliyun.ivpd.demo;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import com.aliyun.ivpd.demo.beans.*;
  6. import com.aliyuncs.AcsResponse;
  7. import com.aliyuncs.DefaultAcsClient;
  8. import com.aliyuncs.IAcsClient;
  9. import com.aliyuncs.RpcAcsRequest;
  10. import com.aliyuncs.http.ProtocolType;
  11. import com.aliyuncs.ivpd.model.v20190625.*;
  12. import com.aliyuncs.profile.DefaultProfile;
  13. import com.aliyuncs.regions.ProductDomain;
  14. import org.apache.http.util.Asserts;
  15. import java.text.DateFormat;
  16. import java.text.SimpleDateFormat;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.List;
  20. /**
  21. * doc: https://help.aliyun.com/document_detail/162597.html?spm=a2c4g.11174283.6.559.2db8519cdKrB6k
  22. */
  23. public class IVPDSdkDemo {
  24. static IAcsClient client = null;
  25. static String regionId = "cn-hangzhou";
  26. static ProductDomain preDomain = null;
  27. public static void main(String[] args) throws Exception {
  28. DefaultProfile profile = DefaultProfile.getProfile(regionId, Config.acccessKeyId, Config.accesssKeySecret);
  29. profile.getHttpClientConfig().setMaxRequests(100);
  30. profile.getHttpClientConfig().setMaxRequestsPerHost(50);
  31. client = new DefaultAcsClient(profile);
  32. testSegmentBodyJob();
  33. }
  34. public static void testSegmentBodyJob() throws Exception {
  35. Date date = new Date();
  36. DateFormat ds = new SimpleDateFormat("yyyMMdd_HHmmss");
  37. CreateSegmentBodyJobRequest req = new CreateSegmentBodyJobRequest();
  38. req.setJobId(ds.format(date));
  39. req.setTimeToLive(600);
  40. List<CreateSegmentBodyJobRequest.DataList> list = new ArrayList<>();
  41. int maxSize = 100;
  42. for (int i=0; i < maxSize; i++) {
  43. CreateSegmentBodyJobRequest.DataList d = new CreateSegmentBodyJobRequest.DataList();
  44. d.setDataId(String.format("%03d", i));
  45. d.setImageUrl(formatDemoUrl("SegmentBody/mayun.jpeg"));
  46. list.add(d);
  47. }
  48. req.setDataLists(list);
  49. System.out.println("-------- CreateSegmentBodyJob --------------");
  50. //1. 创建任务: CreateSegmentBodyJob
  51. CreateSegmentBodyJobResponse resp = getAcsResponse(req);
  52. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  53. String jobId = resp.getData().getJobId();
  54. System.out.println("-------- SegmentBodyJob--GetJobStatus --------------");
  55. GetJobStatusRequest statusRequest = new GetJobStatusRequest();
  56. statusRequest.setJobId(jobId);
  57. int tryTimes = 0, maxTryTimes = req.getTimeToLive();
  58. GetJobStatusResponse statusResp = null;
  59. //2. 查询状态: GetJobStatus
  60. do {
  61. statusResp = getAcsResponse(statusRequest);
  62. System.out.println(String.format("status => completed=%s progress=%f status=%s timeToLive=%d",
  63. statusResp.getData().getCompleted(), statusResp.getData().getProgress(), statusResp.getData().getStatus(), statusResp.getData().getTimeToLive()));
  64. Thread.sleep(1000);
  65. } while (tryTimes ++ < maxTryTimes && !statusResp.getData().getCompleted());
  66. System.out.println("-------- SegmentBodyJob--GetJobResult --------------");
  67. //3. 获取结果: GetJobResult
  68. GetJobResultRequest resultReq = new GetJobResultRequest();
  69. resultReq.setJobId(jobId);
  70. GetJobResultResponse resultResp = getAcsResponse(resultReq);
  71. printResponse(resultReq.getSysActionName(), resultResp.getRequestId(), resultResp.getCode(), resultResp.getMessage(), resultResp.getData());
  72. //4. 反序列化结果
  73. JSONObject json = (JSONObject) JSON.toJSON(resultResp.getData());
  74. CreateSegmentBodyJobResponse.Data jobRespData = json.toJavaObject(CreateSegmentBodyJobResponse.Data.class);
  75. StringBuilder bf = new StringBuilder();
  76. bf.append("BatchSize=").append(resultResp.getData().getBatchSize()).append("\t");
  77. bf.append("TotalUsedTime=").append(resultResp.getData().getTotalUsedTime()).append("\n");
  78. jobRespData.getResultList().sort((a1, a2) -> a1.getDataId().compareTo(a2.getDataId()));
  79. for (CreateSegmentBodyJobResponse.Data.Result result : jobRespData.getResultList()) {
  80. bf.append("dataId=").append(result.getDataId()).append("\t");
  81. bf.append("success=").append(result.getSuccess()).append("\t");
  82. bf.append("imageUrl= ").append(result.getSuccess()? result.getResultData().getImageUrl() : "").append("\n");
  83. }
  84. System.out.println("------------result DataList ------------------------------------------------------------------");
  85. System.out.println(bf.toString());
  86. }
  87. public static void printResponse(String action, String requestId, String code, String message, Object data) {
  88. System.out.print(" " + action + " ======================================\n");
  89. System.out.println("requestId: " + requestId);
  90. System.out.println("code: " + code);
  91. System.out.println(" msg: " + message);
  92. System.out.println("body.class: " + data == null ? null : data.getClass());
  93. System.out.println("body: " + JSON.toJSONString(data, SerializerFeature.PrettyFormat));
  94. Asserts.check("0".equals(code), "assert code == 0 req:");
  95. Asserts.check(null != data, "assert data != null");
  96. System.out.println("\n");
  97. }
  98. private static <R extends RpcAcsRequest<T>, T extends AcsResponse> T getAcsResponse(R req) throws Exception {
  99. if (preDomain != null) {
  100. req.setSysProductDomain(preDomain);
  101. }
  102. req.setSysProtocol(ProtocolType.HTTPS);
  103. return client.getAcsResponse(req);
  104. }
  105. private static String formatDemoUrl(String file) {
  106. return String.format("http://ivpd-%s.oss-%s.aliyuncs.com/ivpd-demo/images/%s", regionId, regionId, file);
  107. }
  108. }