本示例介绍如何使用Alibaba Cloud SDK for Java查询视频异步检测结果。

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-green -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-green</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>
    </dependencies>

代码示例

  • 文本内容检测代码示例如下:
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.profile.DefaultProfile;
    import java.util.*;
    
    public class Scan {
        public static void main(String[] args) throws Exception {
            DefaultProfile profile = DefaultProfile.getProfile(
                    "<your-region-id>", // 您的可用区ID
                    "<your-access-key-id>", // 您的AccessKey ID
                    "<your-access-key-secret>"); // 您的AccessKey Secret
            IAcsClient client = new DefaultAcsClient(profile);
    
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("green.cn-hangzhou.aliyuncs.com");
            request.setSysVersion("2018-05-09");
            request.setSysUriPattern("/green/text/scan");
    
            List<Map<String, String>> tasks = new ArrayList<Map<String, String>>();
            Map<String, String> task1 = new HashMap<String, String>();
            task1.put("dataId",UUID.randomUUID().toString());
            // 待检测的文本,长度不超过10000个字符
            task1.put("content","张三,你妈妈身体怎么样?");
    
            Map<String, String> task2 = new HashMap<String, String>();
            task2.put("dataId",UUID.randomUUID().toString());
            // 待检测的文本,长度不超过10000个字符
            task2.put("content","test content");
            tasks.add(task1);
            tasks.add(task2);
    
            HashMap<String, Object> body = new HashMap<String, Object>();
            body.put("tasks",tasks);
            // 检测场景,文本垃圾检测传递:antispam
            body.put("scenes","antispam");
    
            request.setHttpContent(JSON.toJSONString(body).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
            // 请务必设置超时时间
            request.setSysConnectTimeout(3000);
            request.setSysReadTimeout(6000);
            try {
                CommonResponse response = client.getCommonResponse(request);
                JSONObject dataJson = JSON.parseObject(response.getData());
                System.out.println(JSON.toJSONString(dataJson, true));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }
  • 如果您认为我们的文本检测结果与您的期望不符,您可以通过文本垃圾结果反馈接口纠正算法检测结果,代码示例如下:
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    public class TextFeedback {
        public static void main(String[] args) throws Exception {
            IClientProfile profile = DefaultProfile.getProfile(
                    "<your-region-id>", // 您的可用区ID
                    "<your-access-key-id>", // 您的AccessKey ID
                    "<your-access-key-secret>"); // 您的AccessKey Secret
            IAcsClient client = new DefaultAcsClient(profile);
    
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("green.cn-hangzhou.aliyuncs.com");
            request.setSysVersion("2018-05-09");
            request.setSysUriPattern("/green/text/feedback");
    
            JSONObject data = new JSONObject();
            data.put("taskId", "txt5********1rGrSC");
            data.put("label", "abuse");
    System.out.println(JSON.toJSONString(data, true));
            request.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            // 请务必设置超时时间
            request.setSysConnectTimeout(3000);
            request.setSysReadTimeout(6000);
            try {
                CommonResponse response = client.getCommonResponse(request);
                JSONObject dataJson = JSON.parseObject(response.getData());
                System.out.println(JSON.toJSONString(dataJson, true));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }

执行结果

  • 文本内容检测正确的返回结果类似如下:
    {
        "msg":"OK",
        "code":200,
        "data":[
            {
                "msg":"OK",
                "code":200,
                "dataId":"d4cc69c1-577a-4a0b-a83e-eeb2705cd767",
                "results":[
                    {
                        "rate":99.91,
                        "suggestion":"block",
                        "details":[
                            {
                                "label":"abuse"
                            }
                        ],
                        "label":"abuse",
                        "scene":"antispam"
                    }
                ],
                "content":"张三,你妈妈身体怎么样?",
                "taskId":"txt5********1rGrSC"
            },
            {
                "msg":"OK",
                "code":200,
                "dataId":"17cd7ab5-73ba-44d1-8138-274d22b5cb5a",
                "results":[
                    {
                        "rate":99.91,
                        "suggestion":"pass",
                        "label":"normal",
                        "scene":"antispam"
                    }
                ],
                "content":"test content",
                "taskId":"txt4$aZ*******Ln-1rGrP1"
            }
        ],
        "requestId":"1B4BC8A4-EAF8-44FB-A7D4-07DCE8A96FBC"
    }
  • 文本检测内容反馈正确的返回结果类似如下:
    {
        "msg":"OK",
        "code":200,
        "requestId":"D43F32C9-D9F3-41C6-9CC8-E22745A426D2"
    }