18 changed files with 977 additions and 16 deletions
@ -0,0 +1,97 @@ |
|||||
|
package com.epmet.resi.group.dto.scanapicallback; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 语音检测callBack入参 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/10 10:47 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceScanCallBackContentFormDTO implements Serializable { |
||||
|
|
||||
|
/** |
||||
|
* 错误码,和HTTP状态码一致。 |
||||
|
* 200:表示检测成功。 |
||||
|
* 280:表示处理中,需要继续轮询。 |
||||
|
* 其他:表示任务失败。 |
||||
|
* 更多信息,请参见公共错误码。 |
||||
|
* |
||||
|
* 说明 只有返回280表示任务还在进行,需要继续轮询该任务的检测结果。返回其他值均表示该语音检测任务已结束。如果实际上该语音检测没有结束,而是因为网络异常等原因异常结束,您可以重新提交语音异步检测任务。 |
||||
|
*/ |
||||
|
private Integer code; |
||||
|
|
||||
|
/** |
||||
|
* 请求参数的响应信息。 |
||||
|
*/ |
||||
|
private String msg; |
||||
|
|
||||
|
/** |
||||
|
* 检测对象对应的数据ID。 |
||||
|
* 说明 如果在检测请求参数中传入了dataId,则此处返回对应的dataId。 |
||||
|
*/ |
||||
|
private String dataId; |
||||
|
|
||||
|
/** |
||||
|
* 该检测任务的ID。 |
||||
|
*/ |
||||
|
private String taskId; |
||||
|
|
||||
|
/** |
||||
|
* 检测对象的URL。 |
||||
|
*/ |
||||
|
private String url; |
||||
|
|
||||
|
/** |
||||
|
* 检测成功(code=200)时,返回的检测结果。该结果包含一个或多个元素,每个元素是个结构体,对应一个场景。关于每个元素的结构描述,请参见result。 |
||||
|
*/ |
||||
|
private List<ResultDTO> results; |
||||
|
|
||||
|
|
||||
|
@Data |
||||
|
private static class ResultDTO{ |
||||
|
/** |
||||
|
* 检测结果的分类。取值: |
||||
|
* normal:正常文本 |
||||
|
* spam:含垃圾信息 |
||||
|
* ad:广告 |
||||
|
* politics:涉政 |
||||
|
* terrorism:暴恐 |
||||
|
* abuse:辱骂 |
||||
|
* porn:色情 |
||||
|
* flood:灌水 |
||||
|
* contraband:违禁 |
||||
|
* meaningless:无意义 |
||||
|
* customized:自定义(例如命中自定义关键词) |
||||
|
*/ |
||||
|
private String label; |
||||
|
|
||||
|
/** |
||||
|
* 检测场景,和调用请求中的场景对应。唯一取值:antispam。 |
||||
|
*/ |
||||
|
private String scene; |
||||
|
|
||||
|
/** |
||||
|
* 建议您执行的后续操作。取值: |
||||
|
* pass:结果正常,无需进行其余操作。 |
||||
|
* review:结果不确定,需要进行人工审核。 |
||||
|
* block:结果违规,建议直接删除或者限制公开。 |
||||
|
*/ |
||||
|
private String suggestion; |
||||
|
|
||||
|
/** |
||||
|
* 置信度分数,取值范围:0(表示置信度最低)~100(表示置信度最高)。 |
||||
|
* 如果suggestion为pass,则置信度越高,表示内容正常的可能性越高;如果suggestion为review或block,则置信度越高,表示内容违规的可能性越高。 |
||||
|
* 注意 该值仅作为参考,强烈建议您不要在业务中使用。建议您参考suggestion和label(或者部分接口返回的sublabel)结果用于内容违规判定。 |
||||
|
*/ |
||||
|
private Float rate; |
||||
|
//下面还有个details 语音对应的文本详情。每一句文本对应一个元素,包含一个或者多个元素。关于每个元素的结构描述,请参见detail。
|
||||
|
//暂时不解析了。
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
package com.epmet.openapi.scan.common.enu; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 语音异步检测场景 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 10:12 |
||||
|
*/ |
||||
|
public enum VoiceSceneEnum { |
||||
|
ANTISPAM("antispam", "检测场景,取值:antispam"); |
||||
|
|
||||
|
private String code; |
||||
|
private String desc; |
||||
|
|
||||
|
VoiceSceneEnum(String code, String desc) { |
||||
|
this.code = code; |
||||
|
this.desc = desc; |
||||
|
} |
||||
|
|
||||
|
public static List<String> getVoiceSceneList() { |
||||
|
List<String> result = new ArrayList<>(); |
||||
|
VoiceSceneEnum[] values = VoiceSceneEnum.values(); |
||||
|
for (VoiceSceneEnum v : values) { |
||||
|
result.add(v.getCode()); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public String getCode() { |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
public void setCode(String code) { |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
public String getDesc() { |
||||
|
return desc; |
||||
|
} |
||||
|
|
||||
|
public void setDesc(String desc) { |
||||
|
this.desc = desc; |
||||
|
} |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
/** |
||||
|
* Copyright 2018 人人开源 https://www.renren.io
|
||||
|
* <p> |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU General Public License as published by |
||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||
|
* (at your option) any later version. |
||||
|
* <p> |
||||
|
* This program is distributed in the hope that it will be useful, |
||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
* GNU General Public License for more details. |
||||
|
* <p> |
||||
|
* You should have received a copy of the GNU General Public License |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
*/ |
||||
|
|
||||
|
package com.epmet.openapi.scan.redis; |
||||
|
|
||||
|
import com.epmet.commons.tools.redis.RedisUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* 用户居民端注册信息表 用户在居民端完善的个人信息 |
||||
|
* |
||||
|
* @author generator generator@elink-cn.com |
||||
|
* @since v1.0.0 2020-03-30 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class ScanRedis { |
||||
|
@Autowired |
||||
|
private RedisUtils redisUtils; |
||||
|
|
||||
|
public void test(){ |
||||
|
redisUtils.set("yinzuomei", "尹作梅测试用",RedisUtils.MINUTE_THIRTY_EXPIRE); |
||||
|
} |
||||
|
|
||||
|
public void setVoiceScanKey(String taskId, String seed) { |
||||
|
redisUtils.set(taskId, seed,RedisUtils.HOUR_FOUR_EXPIRE); |
||||
|
} |
||||
|
} |
@ -0,0 +1,63 @@ |
|||||
|
package com.epmet.openapi.scan.support.param; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.Valid; |
||||
|
import javax.validation.constraints.NotEmpty; |
||||
|
import java.io.Serializable; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 音频审查 入参 |
||||
|
* 参考文档:https://help.aliyun.com/document_detail/89630.html?spm=a2c4g.11186623.2.12.51a32dbfW6AdqV#reference-bcf-3nk-z2b
|
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 9:07 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceAsyncScanParam implements Serializable { |
||||
|
private static final long serialVersionUID = 3408043673247901184L; |
||||
|
|
||||
|
/** |
||||
|
* 不必填 |
||||
|
* 该字段用于标识您的业务场景。您可以通过内容安全控制台创建业务场景(具体操作,请参见自定义机审标准),或者提交工单联系我们帮助您创建业务场景。 |
||||
|
*/ |
||||
|
private String bizType; |
||||
|
|
||||
|
/** |
||||
|
* 必填 |
||||
|
* 检测场景,取值:antispam。 |
||||
|
*/ |
||||
|
private List<String> scenes; |
||||
|
|
||||
|
/** |
||||
|
* 不必填 |
||||
|
* 是否为语音流(例如直播流)检测。取值: |
||||
|
* true:表示语音流检测。 |
||||
|
* false(默认):表示音频文件检测。 |
||||
|
*/ |
||||
|
private Boolean live; |
||||
|
|
||||
|
/** |
||||
|
* 不必填 |
||||
|
* 是否为近线检测模式。 取值: |
||||
|
* true:表示近线检测模式。近线检测模式下,您提交的任务不保证能够实时处理,但是可以排队并在24小时内开始检测。 |
||||
|
* false(默认):表示实时检测模式。对于超过了并发路数限制的检测请求会直接拒绝。 |
||||
|
* 说明 该参数仅适用于音频文件检测,不适用于语音流检测。 |
||||
|
*/ |
||||
|
private Boolean offline; |
||||
|
|
||||
|
/** |
||||
|
* 异步检测结果回调地址,执行异步审查内容时 必填 |
||||
|
*/ |
||||
|
private String callback; |
||||
|
|
||||
|
/** |
||||
|
* 随机字符串,该值用于回调通知请求中的签名,使用callback时 必填 |
||||
|
*/ |
||||
|
private String seed; |
||||
|
|
||||
|
@Valid |
||||
|
@NotEmpty(message = "任务列表不能为空") |
||||
|
private List<VoiceTask> tasks; |
||||
|
|
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
package com.epmet.openapi.scan.support.param; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
/** |
||||
|
* 语音异步检测 对象 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 10:16 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceTask implements Serializable { |
||||
|
/** |
||||
|
* 不必填 |
||||
|
* 要检测的数据id 非必填 |
||||
|
* 检测对象对应的数据ID。 |
||||
|
* 由大小写英文字母、数字、下划线(_)、短划线(-)、英文句号(.)组成,不超过128个字符,可以用于唯一标识您的业务数据。 |
||||
|
* */ |
||||
|
@NotBlank(message = "dataId不能为空") |
||||
|
private String dataId; |
||||
|
|
||||
|
/** |
||||
|
* 必填 |
||||
|
* 需要检测的音频文件或语音流的下载地址。 |
||||
|
*/ |
||||
|
@NotBlank(message = "音频URL不能为空") |
||||
|
private String url; |
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
package com.epmet.openapi.scan.support.result; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
/** |
||||
|
* 语音异步检测结果查询结果返参 -data-result-detail详情 |
||||
|
* 语音对应的文本详情。每一句文本对应一个元素,包含一个或者多个元素。关于每个元素的结构描述 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 11:11 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceAsyncScanDetailDTO implements Serializable { |
||||
|
private static final long serialVersionUID = -2664219492371705160L; |
||||
|
/** |
||||
|
* 句子开始的时间,单位:秒。 |
||||
|
*/ |
||||
|
private Integer startTime; |
||||
|
|
||||
|
/** |
||||
|
* 句子结束的时间,单位:秒。 |
||||
|
*/ |
||||
|
private Integer endTime; |
||||
|
|
||||
|
/** |
||||
|
* 语音转换成文本的结果。 |
||||
|
*/ |
||||
|
private String text; |
||||
|
|
||||
|
/** |
||||
|
* 检测结果的分类。取值: |
||||
|
* normal:正常文本 |
||||
|
* spam:含垃圾信息 |
||||
|
* ad:广告 |
||||
|
* politics:涉政 |
||||
|
* terrorism:暴恐 |
||||
|
* abuse:辱骂 |
||||
|
* porn:色情 |
||||
|
* flood:灌水 |
||||
|
* contraband:违禁 |
||||
|
* meaningless:无意义 |
||||
|
* customized:自定义(例如命中自定义关键词) |
||||
|
*/ |
||||
|
private String label; |
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
package com.epmet.openapi.scan.support.result; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 语音异步检测结果查询结果返参 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 10:56 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceAsyncScanResult implements Serializable { |
||||
|
private static final long serialVersionUID = 8702129292884498023L; |
||||
|
/** |
||||
|
* 错误码,和HTTP状态码一致。 |
||||
|
* 200:表示检测成功。 |
||||
|
* 280:表示处理中,需要继续轮询。 |
||||
|
* 其他:表示任务失败。 |
||||
|
* 更多信息,请参见公共错误码。 |
||||
|
*/ |
||||
|
private String code; |
||||
|
/** |
||||
|
* 错误描述信息。 |
||||
|
*/ |
||||
|
private String msg; |
||||
|
/** |
||||
|
* 检测对象对应的数据ID。 |
||||
|
*/ |
||||
|
private String dataId; |
||||
|
|
||||
|
/** |
||||
|
* 检测任务的ID |
||||
|
*/ |
||||
|
private String taskId; |
||||
|
|
||||
|
/** |
||||
|
* 暂时没用,所以返回忽略 |
||||
|
*/ |
||||
|
@JsonIgnore |
||||
|
private String url; |
||||
|
|
||||
|
/** |
||||
|
* 检测成功(code=200)时,返回的检测结果。该结果包含一个或多个元素,每个元素是个结构体,对应一个场景。关于每个元素的结构描述,请参见result。 |
||||
|
*/ |
||||
|
private List<VoiceAsyncScanResultDTO> results; |
||||
|
|
||||
|
} |
@ -0,0 +1,57 @@ |
|||||
|
package com.epmet.openapi.scan.support.result; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 语音异步检测结果查询结果返参 -data-result详情 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 11:07 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceAsyncScanResultDTO implements Serializable { |
||||
|
private static final long serialVersionUID = 4602226363259983753L; |
||||
|
/** |
||||
|
* 检测场景,和调用请求中的场景对应。取值:antispam。 |
||||
|
*/ |
||||
|
private String scene; |
||||
|
|
||||
|
/** |
||||
|
* 检测结果的分类。取值: |
||||
|
* normal:正常文本 |
||||
|
* spam:含垃圾信息 |
||||
|
* ad:广告 |
||||
|
* politics:涉政 |
||||
|
* terrorism:暴恐 |
||||
|
* abuse:辱骂 |
||||
|
* porn:色情 |
||||
|
* flood:灌水 |
||||
|
* contraband:违禁 |
||||
|
* meaningless:无意义 |
||||
|
* customized:自定义(例如命中自定义关键词) |
||||
|
*/ |
||||
|
private String label; |
||||
|
|
||||
|
/** |
||||
|
* 建议您执行的后续操作。取值: |
||||
|
* pass:结果正常,无需进行其余操作。 |
||||
|
* review:结果不确定,需要进行人工审核。 |
||||
|
* block:结果违规,建议直接删除或者限制公开。 |
||||
|
*/ |
||||
|
private String suggestion; |
||||
|
|
||||
|
/** |
||||
|
* 置信度分数,取值范围:0(表示置信度最低)~100(表示置信度最高)。 |
||||
|
* 如果suggestion为pass,则置信度越高,表示内容正常的可能性越高;如果suggestion为review或block,则置信度越高,表示内容违规的可能性越高。 |
||||
|
* 注意 该值仅作为参考,强烈建议您不要在业务中使用。建议您参考suggestion和label(或者部分接口返回的sublabel)结果用于内容违规判定。 |
||||
|
*/ |
||||
|
private Float rate; |
||||
|
|
||||
|
/** |
||||
|
* 语音对应的文本详情。每一句文本对应一个元素,包含一个或者多个元素。关于每个元素的结构描述, |
||||
|
*/ |
||||
|
private List<VoiceAsyncScanDetailDTO> details; |
||||
|
} |
@ -0,0 +1,40 @@ |
|||||
|
package com.epmet.openapi.scan.support.result; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
/** |
||||
|
* 描述一下 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 16:38 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceAsyncScanTaskDataDTO implements Serializable { |
||||
|
/** |
||||
|
* 错误码,和HTTP状态码一致。 |
||||
|
* 更多信息,请参见公共错误码。 |
||||
|
*/ |
||||
|
private String code; |
||||
|
/** |
||||
|
* 错误描述信息。 |
||||
|
*/ |
||||
|
private String msg; |
||||
|
/** |
||||
|
* 检测对象对应的数据ID。 |
||||
|
*/ |
||||
|
private String dataId; |
||||
|
|
||||
|
/** |
||||
|
* 检测任务的ID |
||||
|
*/ |
||||
|
private String taskId; |
||||
|
|
||||
|
/** |
||||
|
* 暂时没用,所以返回忽略 |
||||
|
*/ |
||||
|
@JsonIgnore |
||||
|
private String url; |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
package com.epmet.openapi.scan.support.result; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 音频审查返参,用于接收taskId |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/9 9:23 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class VoiceAsyncScanTaskResult implements Serializable { |
||||
|
private static final long serialVersionUID = 8702129292884498023L; |
||||
|
/** |
||||
|
* 随机字符串,该值用于回调通知请求中的签名。 |
||||
|
*/ |
||||
|
private String seed; |
||||
|
private List<VoiceAsyncScanTaskDataDTO> taskList; |
||||
|
} |
@ -0,0 +1,90 @@ |
|||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.aliyuncs.DefaultAcsClient; |
||||
|
import com.aliyuncs.IAcsClient; |
||||
|
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest; |
||||
|
import com.aliyuncs.http.FormatType; |
||||
|
import com.aliyuncs.http.HttpResponse; |
||||
|
import com.aliyuncs.profile.DefaultProfile; |
||||
|
import com.aliyuncs.profile.IClientProfile; |
||||
|
|
||||
|
import java.util.*; |
||||
|
|
||||
|
/** |
||||
|
* 提交语音异步检测任务 demo |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/8 21:51 |
||||
|
*/ |
||||
|
public class VoiceAsyncScanRequestSample extends BaseSample { |
||||
|
|
||||
|
|
||||
|
|
||||
|
public static void main(String[] args) throws Exception { |
||||
|
|
||||
|
//请替换成您自己的AccessKey ID、AccessKey Secret。
|
||||
|
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", |
||||
|
"LTAI4G6Fv6uTzQbpsayATHq4", //您自己的AccessKey ID
|
||||
|
"QevMw1RYCwQUG3RSMPq1J6EAfmSblo");//您自己的AccessKey Secret
|
||||
|
final IAcsClient client = new DefaultAcsClient(profile); |
||||
|
|
||||
|
VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); //class different vs common
|
||||
|
asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
|
||||
|
asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
|
||||
|
asyncScanRequest.setRegionId("cn-shanghai"); |
||||
|
asyncScanRequest.setConnectTimeout(3000); |
||||
|
asyncScanRequest.setReadTimeout(6000); |
||||
|
|
||||
|
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>(); |
||||
|
Map<String, Object> task1 = new LinkedHashMap<String, Object>(); |
||||
|
// 请将下面的地址修改为要检测的语音文件的地址。
|
||||
|
task1.put("dataId","voice1"); |
||||
|
task1.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/6480bd6be9f14a458162218cea84dfa5.aac"); |
||||
|
|
||||
|
Map<String, Object> task2 = new LinkedHashMap<String, Object>(); |
||||
|
task2.put("dataId","voice2"); |
||||
|
task2.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/b566d94fd7114ffb9a203e80c22ebb96.aac"); |
||||
|
|
||||
|
tasks.add(task1); |
||||
|
tasks.add(task2); |
||||
|
|
||||
|
JSONObject data = new JSONObject(); |
||||
|
|
||||
|
System.out.println("==========Task count:" + tasks.size()); |
||||
|
data.put("scenes", Arrays.asList("antispam")); |
||||
|
data.put("tasks", tasks); |
||||
|
// 如果是语音流检测,则修改为true。
|
||||
|
data.put("live", false); |
||||
|
asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON); |
||||
|
System.out.println("接口入参:"+JSON.toJSONString(data, true)); |
||||
|
|
||||
|
try { |
||||
|
HttpResponse httpResponse = client.doAction(asyncScanRequest); |
||||
|
|
||||
|
if (httpResponse.isSuccess()) { |
||||
|
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); |
||||
|
System.out.println("接口返参:"+JSON.toJSONString(scrResponse, true)); |
||||
|
if (200 == scrResponse.getInteger("code")) { |
||||
|
JSONArray taskResults = scrResponse.getJSONArray("data"); |
||||
|
for (Object taskResult : taskResults) { |
||||
|
Integer code = ((JSONObject) taskResult).getInteger("code"); |
||||
|
if (200 == code) { |
||||
|
final String taskId = ((JSONObject) taskResult).getString("taskId"); |
||||
|
System.out.println("submit async task success, taskId = [" + taskId + "]"); |
||||
|
} else { |
||||
|
System.out.println("task process fail: " + code); |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
System.out.println("detect not success. code: " + scrResponse.getInteger("code")); |
||||
|
} |
||||
|
} else { |
||||
|
System.out.println("response not success. status: " + httpResponse.getStatus()); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,113 @@ |
|||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.aliyuncs.DefaultAcsClient; |
||||
|
import com.aliyuncs.IAcsClient; |
||||
|
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest; |
||||
|
import com.aliyuncs.http.FormatType; |
||||
|
import com.aliyuncs.http.HttpResponse; |
||||
|
import com.aliyuncs.profile.DefaultProfile; |
||||
|
import com.aliyuncs.profile.IClientProfile; |
||||
|
|
||||
|
import java.util.*; |
||||
|
|
||||
|
/** |
||||
|
* 查询异步语音检测结果。 |
||||
|
* |
||||
|
* @author yinzuomei@elink-cn.com |
||||
|
* @date 2020/12/8 22:08 |
||||
|
*/ |
||||
|
public class VoiceAsyncScanResultsRequestSample extends BaseSample { |
||||
|
|
||||
|
|
||||
|
public static void main(String[] args) throws Exception { |
||||
|
// 请替换成您自己的AccessKey ID、AccessKey Secret。
|
||||
|
IClientProfile profile = DefaultProfile |
||||
|
.getProfile("cn-shanghai", |
||||
|
"LTAI4G6Fv6uTzQbpsayATHq4", |
||||
|
"QevMw1RYCwQUG3RSMPq1J6EAfmSblo"); |
||||
|
final IAcsClient client = new DefaultAcsClient(profile); |
||||
|
//提交语音异步检测任务后返回的taskId
|
||||
|
pollingScanResult(client, Arrays.asList("vc_f_7WZZ01BCH0q6ZnM3g1OKGG-1tAaZ7", "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi")); |
||||
|
// pollingScanResult(client, "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi");
|
||||
|
} |
||||
|
|
||||
|
public static void pollingScanResult(IAcsClient client, List<String> taskIdList) throws InterruptedException { |
||||
|
int failCount = 0; |
||||
|
boolean stop = false; |
||||
|
do { |
||||
|
// 设置每10秒查询一次。
|
||||
|
Thread.sleep(10 * 1000); |
||||
|
JSONObject scanResult = getScanResult(client, taskIdList); |
||||
|
System.out.println("接口完整返参:"+JSON.toJSONString(scanResult, true)); |
||||
|
if (scanResult == null || 200 != scanResult.getInteger("code")) { |
||||
|
failCount++; |
||||
|
System.out.println("请求失败,get result fail, failCount=" + failCount); |
||||
|
if (scanResult != null) { |
||||
|
System.out.println("请求失败,错误信息errorMsg:" + scanResult.getString("msg")); |
||||
|
} |
||||
|
if (failCount > 20) { |
||||
|
break; |
||||
|
} |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
JSONArray taskResults = scanResult.getJSONArray("data"); |
||||
|
if (taskResults.isEmpty()) { |
||||
|
System.out.println("请求成功,but data is empty"); |
||||
|
break; |
||||
|
} |
||||
|
System.out.println("data.size=" + taskResults.size()); |
||||
|
for (Object taskResult : taskResults) { |
||||
|
JSONObject result = (JSONObject) taskResult; |
||||
|
Integer code = result.getInteger("code"); |
||||
|
String taskId = result.getString("taskId"); |
||||
|
if (280 == code) { |
||||
|
System.out.println("taskId=" + taskId + ": processing status: " + result.getString("msg")); |
||||
|
} else if (200 == code) { |
||||
|
System.out.println("taskId=" + taskId + "请求成功,返参:" + JSON.toJSONString(taskResult, true)); |
||||
|
stop = true; |
||||
|
} else { |
||||
|
System.out.println("taskId=" + taskId + "请求失败,返参:" + JSON.toJSONString(taskResult, true)); |
||||
|
stop = true; |
||||
|
} |
||||
|
} |
||||
|
} while (!stop); |
||||
|
} |
||||
|
|
||||
|
private static JSONObject getScanResult(IAcsClient client, List<String> taskIdList) { |
||||
|
VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest(); |
||||
|
getResultsRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
|
||||
|
getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
|
||||
|
getResultsRequest.setEncoding("utf-8"); |
||||
|
getResultsRequest.setRegionId("cn-shanghai"); |
||||
|
|
||||
|
|
||||
|
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>(); |
||||
|
for (String taskId : taskIdList) { |
||||
|
Map<String, Object> task1 = new LinkedHashMap<String, Object>(); |
||||
|
task1.put("taskId", taskId); |
||||
|
tasks.add(task1); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 请务必设置超时时间。 |
||||
|
*/ |
||||
|
getResultsRequest.setConnectTimeout(3000); |
||||
|
getResultsRequest.setReadTimeout(6000); |
||||
|
|
||||
|
try { |
||||
|
getResultsRequest.setHttpContent(JSON.toJSONString(tasks).getBytes("UTF-8"), "UTF-8", FormatType.JSON); |
||||
|
|
||||
|
HttpResponse httpResponse = client.doAction(getResultsRequest); |
||||
|
if (httpResponse.isSuccess()) { |
||||
|
return JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); |
||||
|
} else { |
||||
|
System.out.println("response not success. status: " + httpResponse.getStatus()); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue