文档

视频审核

更新时间:

本文介绍了如何使用Java SDK视频审核接口,检测视频中是否包含风险内容。

功能描述

视频审核接口支持同步检测和异步检测两种方式。

  • 同步检测只支持传递视频的截帧图片序列。关于参数的详细介绍,请参见同步检测

  • (推荐)异步检测支持对原始视频或视频的截帧图片序列进行检测。关于参数的详细介绍,请参见异步检测

前提条件

  • 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖

    说明

    请一定按照安装Java依赖页面中的版本安装,否则会导致调用失败。

  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

(推荐)提交视频异步检测任务

接口

描述

支持的地域

VideoAsyncScanRequest

提交视频异步检测任务,对视频进行多个风险场景的识别,包括色情、暴恐涉政、广告 、不良场景、Logo(商标台标)识别。

  • cn-shanghai:华东2(上海)

  • cn-beijing:华北2(北京)

  • cn-shenzhen:华南1(深圳)

  • ap-southeast-1:新加坡

示例代码

  • 传视频URL进行检测

    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.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            IAcsClient client = new DefaultAcsClient(profile);
    
            VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
            videoAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
            videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", "请填写公网可访问的视频HTTP、HTTPS URL地址");
    
            tasks.add(task);
            /**
             * 设置要检测的场景。计费是依据此处传递的场景计算。
             * 视频默认1秒截取一帧,您可以自行控制截帧频率。收费按照视频的截帧数量以及每一帧的检测场景计算。
             * 举例:1分钟的视频截帧60张,检测色情(对应场景参数porn)和暴恐涉政(对应场景参数terrorism)2个场景,收费按照60张色情+60张暴恐涉政进行计费。
             */
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("tasks", tasks);
            data.put("callback", "http://www.aliyundoc.com/xxx.json");
            data.put("seed", "yourPersonalSeed");
    
            videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            /**
             * 请务必设置超时时间。
             */
            videoAsyncScanRequest.setConnectTimeout(3000);
            videoAsyncScanRequest.setReadTimeout(6000);
            try {
                HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    int requestCode = scrResponse.getIntValue("code");
                    // 每一个视频的检测结果。
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    if (200 == requestCode) {
                        for (Object taskResult : taskResults) {
                            // 单个视频的处理结果。
                            int taskCode = ((JSONObject) taskResult).getIntValue("code");
                            if (200 == taskCode) {
                                // 保存taskId用于轮询结果。
                                System.out.println(((JSONObject) taskResult).getString("taskId"));
                            } else {
                                // 单个视频处理失败,原因视具体的情况详细分析。
                                System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                            }
                        }
                    } else {
                        /**
                         * 表明请求整体处理失败,原因视具体的情况详细分析。
                         */
                        System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                    }
                } else {
                    System.out.println("response not success. status:" + httpResponse.getStatus());
                }
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
  • 传本地视频文件进行检测

    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.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.green.extension.uploader.ClientUploader;
    import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            IAcsClient client = new DefaultAcsClient(profile);
    
            VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
            videoAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
            videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
    
            /**
             * 如果您要检测的文件存储在本地服务器上,可以通过下述代码生成URL作为视频地址传递到服务端进行检测。
             */
            String url = null;
            ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false);
            try {
                url = uploader.uploadFile("您的本地文件的绝对路径");
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", url);
    
            tasks.add(task);
            /**
             * 设置要检测的场景。计费是依据此处传递的场景计算。
             * 视频默认1秒截取一帧,您可以自行控制截帧频率。收费按照视频的截帧数量以及每一帧的检测场景进行计算。
             * 举例:1分钟的视频截帧60张,检测色情(对应场景参数porn)和暴恐涉政(对应场景参数terrorism)2个场景,收费按照60张色情+60张暴恐涉政进行计算。
             */
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("tasks", tasks);
            data.put("callback", "http://www.aliyundoc.com/xxx.json");
            data.put("seed", "yourPersonalSeed");
    
            videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            /**
             * 请务必设置超时时间。
             */
            videoAsyncScanRequest.setConnectTimeout(3000);
            videoAsyncScanRequest.setReadTimeout(10000);
            try {
                HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    int requestCode = scrResponse.getIntValue("code");
                    // 每一个视频的检测结果。
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    if (200 == requestCode) {
                        for (Object taskResult : taskResults) {
                            // 单个视频的处理结果。
                            int taskCode = ((JSONObject) taskResult).getIntValue("code");
                            if (200 == taskCode) {
                                // 保存taskId用于轮询结果。
                                System.out.println(((JSONObject) taskResult).getString("taskId"));
                            } else {
                                // 单个视频处理失败,原因视具体的情况详细分析。
                                System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                            }
                        }
                    } else {
                        /**
                         * 表明请求整体处理失败,原因视具体的情况详细分析。
                         */
                        System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
                    }
                } else {
                    System.out.println("response not success. status:" + httpResponse.getStatus());
                }
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
  • 传视频文件二进制数据进行检测

    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.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.green.extension.uploader.ClientUploader;
    import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            IAcsClient client = new DefaultAcsClient(profile);
    
            VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
            videoAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
            videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
    
            /**
             * 如果您要检测的文件存储在本地服务器上,可以通过下述代码生成URL作为视频地址传递到服务端进行检测。
             */
            ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false);
            byte[] videoBytes = null;
            String url = null;
            try {
                // 读取本地文件作为二进制数据当做输入做为示例。实际使用中请直接替换成您的视频二进制数据。
                videoBytes = FileUtils.readFileToByteArray(new File("您的本地文件路径"));
                // 上传到服务端。
                url = uploader.uploadBytes(videoBytes);
            } catch (Exception e) {
                System.out.println("upload file to server fail." + e.toString());
            }
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", url);
    
            tasks.add(task);
            /**
             * 设置要检测的场景。计费依据此处传递的场景计算。
             * 视频默认1秒截取一帧,您可以自行控制截帧频率。收费按照视频的截帧数量以及每一帧的检测场景进行计算。
             * 举例:1分钟的视频截帧60张,检测色情(对应场景参数porn)和暴恐涉政(对应场景参数terrorism)2个场景,收费按照60张色情+60张暴恐涉政进行计算。
             */
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("tasks", tasks);
            data.put("callback", "http://www.aliyundoc.com/xxx.json");
            data.put("seed", "yourPersonalSeed");
    
            videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            /**
             * 请务必设置超时时间。
             */
            videoAsyncScanRequest.setConnectTimeout(3000);
            videoAsyncScanRequest.setReadTimeout(10000);
            try {
                HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    int requestCode = scrResponse.getIntValue("code");
                    // 每一个视频的检测结果。
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    if (200 == requestCode) {
                        for (Object taskResult : taskResults) {
                            // 单个视频的处理结果。
                            int taskCode = ((JSONObject) taskResult).getIntValue("code");
                            if (200 == taskCode) {
                                // 保存taskId用于轮询结果。
                                System.out.println(((JSONObject) taskResult).getString("taskId"));
                            } else {
                                // 单个视频处理失败,原因视具体的情况详细分析。
                                System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                            }
                        }
                    } else {
                        /**
                         * 表明请求整体处理失败,原因视具体的情况详细分析。
                         */
                        System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                    }
                } else {
                    System.out.println("response not success. status:" + httpResponse.getStatus());
                }
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
  • 传视频直播流进行检测

    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.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            IAcsClient client = new DefaultAcsClient(profile);
    
            VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
            videoAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
            videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", "业务ID");
            // URL填写直播流地址。
            task.put("url", "待检测视频链接地址");
    
            tasks.add(task);
            /**
             * 设置要检测的场景。计费依据此处传递的场景计算。
             * 视频默认1秒截取一帧,您可以自行控制截帧频率。收费按照视频的截帧数量以及每一帧的检测场景进行计算。
             * 举例:1分钟的视频截帧60张,检测色情(对应场景参数porn)和暴恐涉政(对应场景参数terrorism)2个场景,收费按照60张色情+60张暴恐涉政进行计算。
             */
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("live", true);
            data.put("tasks", tasks);
            data.put("callback", "您的回调地址");
            data.put("seed", "yourPersonalSeed");
    
            videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            /**
             * 请务必设置超时时间。
             */
            videoAsyncScanRequest.setConnectTimeout(3000);
            videoAsyncScanRequest.setReadTimeout(10000);
            try {
                HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    int requestCode = scrResponse.getIntValue("code");
                    // 每一个视频的检测结果。
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    if (200 == requestCode) {
                        for (Object taskResult : taskResults) {
                            // 单个视频的处理结果。
                            int taskCode = ((JSONObject) taskResult).getIntValue("code");
                            if (200 == taskCode) {
                                // 保存taskId用于轮询结果。
                                System.out.println(((JSONObject) taskResult).getString("taskId"));
                            } else {
                                // 单个视频处理失败,原因视具体的情况详细分析。
                                System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                            }
                        }
                    } else {
                        /**
                         * 表明请求整体处理失败,原因视具体的情况详细分析。
                         */
                        System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                    }
                } else {
                    System.out.println("response not success. status:" + httpResponse.getStatus());
                }
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
  • 传视频语音进行综合检测

    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.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            IAcsClient client = new DefaultAcsClient(profile);
    
            VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
            videoAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
            videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", "业务数据ID");
            // URL填写直播流地址。
            task.put("url", "待检测的视频链接地址");
    
            tasks.add(task);
            /**
             * 设置要检测的场景。计费依据此处传递的场景计算。
             * 视频默认1秒截取一帧,您可以自行控制截帧频率。收费按照视频的截帧数量以及每一帧的检测场景进行计算。
             * 举例:1分钟的视频截帧60张,检测色情(对应场景参数porn)和暴恐涉政(对应场景参数terrorism)2个场景,收费按照60张色情+60张暴恐涉政进行计费。
             */
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("live", true);
            data.put("tasks", tasks);
            data.put("callback", "您的回调地址");
            data.put("seed", "yourPersonalSeed");
            /**
             * 如果检测视频画面的同时需要检测语音是否有风险内容,传递以下参数。
             * 注意语音的计费是按照时长进行,即该视频的时长*语音反垃圾的单价。
             */
            data.put("audioScenes", Arrays.asList("antispam"));
    
            videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            /**
             * 请务必设置超时时间。
             */
            videoAsyncScanRequest.setConnectTimeout(3000);
            videoAsyncScanRequest.setReadTimeout(10000);
            try {
                HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    int requestCode = scrResponse.getIntValue("code");
                    // 每一个视频的检测结果。
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    if (200 == requestCode) {
                        for (Object taskResult : taskResults) {
                            // 单个视频的处理结果。
                            int taskCode = ((JSONObject) taskResult).getIntValue("code");
                            if (200 == taskCode) {
                                // 保存taskId,用于轮询结果。
                                System.out.println(((JSONObject) taskResult).getString("taskId"));
                            } else {
                                // 单个视频处理失败,原因视具体的情况详细分析。
                                System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                            }
                        }
                    } else {
                        /**
                         * 表明请求整体处理失败,原因视具体的情况详细分析。
                         */
                        System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                    }
                } else {
                    System.out.println("response not success. status:" + httpResponse.getStatus());
                }
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

查询视频异步检测结果

接口

描述

支持的地域

VideoAsyncScanResultsRequest

查询视频异步检测任务的结果。

说明

该方法需要轮询结果,建议使用callback的方式获取结果。

  • cn-shanghai:华东2(上海)

  • cn-beijing:华北2(北京)

  • cn-shenzhen:华南1(深圳)

  • ap-southeast-1:新加坡

示例代码

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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.VideoAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {
        /**
         * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
         * 常见获取环境变量方式:
         * 方式一:
         *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * 方式二:
         *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "建议从环境变量中获取RAM用户AccessKey ID",
                "建议从环境变量中获取RAM用户AccessKey Secret");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
        IAcsClient client = new DefaultAcsClient(profile);

        VideoAsyncScanResultsRequest videoAsyncScanResultsRequest = new VideoAsyncScanResultsRequest();
        videoAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON);

        List<String> taskList = new ArrayList<String>();
        // 这里添加要查询的taskId。提交任务的时候需要自行保存taskId。
        taskList.add("视频检测任务ID");

        videoAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskList).getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间。
         */
        videoAsyncScanResultsRequest.setConnectTimeout(3000);
        videoAsyncScanResultsRequest.setReadTimeout(6000);
        try {
            HttpResponse httpResponse = client.doAction(videoAsyncScanResultsRequest);
            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                int requestCode = scrResponse.getIntValue("code");
                // 每一个视频的检测结果。
                JSONArray taskResults = scrResponse.getJSONArray("data");
                if (200 == requestCode) {
                    for (Object taskResult : taskResults) {
                        // 单个视频的处理结果。
                        int taskCode = ((JSONObject) taskResult).getIntValue("code");
                        if (280 == taskCode) {
                            // 检测中。
                            // taskId。
                            System.out.println(((JSONObject) taskResult).getString("taskId"));
                        } else if (200 == taskCode) {
                            // taskId。
                            System.out.println(((JSONObject) taskResult).getString("taskId"));
                            // 检测结果。
                            JSONArray results = ((JSONObject) taskResult).getJSONArray("results");
                            for (Object result : results) {
                                // 视频检测结果的分类。
                                System.out.println(((JSONObject) result).getString("label"));
                                // 置信度分数,取值范围:0(表示置信度最低)~100(表示置信度最高)。
                                System.out.println(((JSONObject) result).getString("rate"));
                                // 视频检测场景,与调用请求中的检测场景一致。
                                System.out.println(((JSONObject) result).getString("scene"));
                                // 建议您执行的后续操作。取值:
                                // pass:结果正常,无需进行其他操作。
                                // review:结果不确定,需要人工审核。
                                // block:结果违规,建议直接删除或者限制公开。
                                System.out.println(((JSONObject) result).getString("suggestion"));
                            }
                        } else {
                            // 单个视频处理失败,原因视具体的情况详细分析。
                            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    /**
                     * 表明请求整体处理失败,原因视具体的情况详细分析。
                     */
                    System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

提交视频同步检测任务

接口

描述

支持的地域

VideoSyncScanRequest

提交视频同步检测任务,同步检测视频中的风险内容。

说明

同步检测只支持传递视频帧序列,不支持检测视频文件,推荐使用异步检测接口。

  • cn-shanghai:华东2(上海)

  • cn-beijing:华北2(北京)

  • cn-shenzhen:华南1(深圳)

  • ap-southeast-1:新加坡

示例代码

说明

以下代码中使用帧序列的方式提交待检测的视频。

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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.VideoSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class Main {

    public static void main(String[] args) throws Exception {
        /**
         * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
         * 常见获取环境变量方式:
         * 方式一:
         *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * 方式二:
         *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "建议从环境变量中获取RAM用户AccessKey ID",
                "建议从环境变量中获取RAM用户AccessKey Secret");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
        IAcsClient client = new DefaultAcsClient(profile);

        VideoSyncScanRequest videoSyncScanRequest = new VideoSyncScanRequest();
        videoSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
        videoSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。

        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        Map<String, Object> task = new LinkedHashMap<String, Object>();
        task.put("dataId", UUID.randomUUID().toString());

        List<Map<String, Object>> frames = new ArrayList<Map<String, Object>>();
        Map<String, Object> frame1 = new LinkedHashMap<String, Object>();
        frame1.put("offset", 0);
        frame1.put("url", "视频截帧地址1");

        Map<String, Object> frame2 = new LinkedHashMap<String, Object>();
        frame2.put("offset", 5);
        frame2.put("url", "视频截帧地址2");

        Map<String, Object> frame3 = new LinkedHashMap<String, Object>();
        frame3.put("offset", 10);
        frame3.put("url", "视频截帧地址3");
        frames.addAll(Arrays.asList(frame1, frame2, frame3));

        task.put("frames", frames);
        tasks.add(task);
        /**
         * 设置要检测的场景。计费依据此处传递的场景计算。
         * 视频默认1秒截取一帧,您可以自行控制截帧频率,收费按照视频的截帧数量以及每一帧的检测场景进行计算。
         * 举例:1分钟的视频截帧60张,检测色情(对应场景参数porn)和暴恐涉政(对应场景参数terrorism)2个场景,收费按照60张色情+60张暴恐涉政进行计算。
         */
        JSONObject data = new JSONObject();
        data.put("scenes", Arrays.asList("porn", "terrorism"));
        data.put("tasks", tasks);

        videoSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间。
         */
        videoSyncScanRequest.setConnectTimeout(3000);
        videoSyncScanRequest.setReadTimeout(10000);
        try {
            HttpResponse httpResponse = client.doAction(videoSyncScanRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                int requestCode = scrResponse.getIntValue("code");
                // 每一个视频的检测结果。
                JSONArray taskResults = scrResponse.getJSONArray("data");
                if (200 == requestCode) {
                    for (Object taskResult : taskResults) {
                        // 单个视频的处理结果。
                        int taskCode = ((JSONObject) taskResult).getIntValue("code");
                        if (200 == taskCode) {
                            // taskId。
                            System.out.println(((JSONObject) taskResult).getString("taskId"));
                            // 检测结果。
                            JSONArray results = scrResponse.getJSONArray("results");
                            for (Object result : results) {
                                // 视频检测结果的分类。
                                System.out.println(((JSONObject) result).getString("label"));
                                // 置信度分数,取值范围:0(表示置信度最低)~100(表示置信度最高)。
                                System.out.println(((JSONObject) result).getString("rate"));
                                // 视频检测场景,和调用请求中的场景对应。
                                System.out.println(((JSONObject) result).getString("scene"));
                                // 建议您执行的后续操作。取值:
                                // pass:结果正常,无需进行其余操作。
                                // review:结果不确定,需要进行人工审核。
                                // block:结果违规,建议直接删除或者限制公开。
                                System.out.println(((JSONObject) result).getString("suggestion"));
                            }
                        } else {
                            // 单个视频处理失败,原因视具体的情况详细分析。
                            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    /**
                     * 表明请求整体处理失败,原因视具体的情况详细分析。
                     */
                    System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

视频检测结果反馈

如果您认为视频检测结果与您的预期不符,可以通过视频检测结果反馈接口,对检测结果进行纠正(系统会根据您反馈的结果,将视频截帧添加到相似图片的黑名单库或者白名单库)。当您再次提交相似的内容进行检测时,以您反馈的label返回结果。

关于接口的说明,请参见检测结果反馈

接口

描述

支持的Region

VideoFeedbackRequest

提交视频检测结果的反馈,以人工反馈的检测结果纠正算法检测结果。

  • cn-shanghai:华东2(上海)

  • cn-beijing:华北2(北京)

  • cn-shenzhen:华南1(深圳)

  • ap-southeast-1:新加坡

示例代码

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.VideoFeedbackRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class VideoFeedbackSample {

    public static void main(String[] args) throws Exception {
        /**
         * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
         * 常见获取环境变量方式:
         * 方式一:
         *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * 方式二:
         *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "建议从环境变量中获取RAM用户AccessKey ID",
                "建议从环境变量中获取RAM用户AccessKey Secret");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
        IAcsClient client = new DefaultAcsClient(profile);

        VideoFeedbackRequest videoFeedbackRequest = new VideoFeedbackRequest();
        // 指定API返回格式。
        videoFeedbackRequest.setAcceptFormat(FormatType.JSON);
        // 指定请求方法。
        videoFeedbackRequest.setMethod(MethodType.POST);
        videoFeedbackRequest.setEncoding("utf-8");
        // 支持HTTP和HTTPS。
        videoFeedbackRequest.setProtocol(ProtocolType.HTTP);

        List<JSONObject> framesList = new ArrayList();
        JSONObject frame1 = new JSONObject();
        frame1.put("url", "视频截帧链接_1");
        frame1.put("offset", 100);
        framesList.add(frame1);
        // scenes:检测场景,支持指定多个场景。
        // suggestion:期望的检测结果,pass:正常;block:违规。
        JSONObject httpBody = new JSONObject();
        httpBody.put("dataId", "检测数据ID");
        httpBody.put("taskId", "视频审核任务ID");
        httpBody.put("url", "视频链接");
        httpBody.put("suggestion", "block");
        httpBody.put("frames", framesList);
        httpBody.put("scenes", Arrays.asList("ad", "terrorism"));
        httpBody.put("note", "备注信息");

        videoFeedbackRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
                "UTF-8", FormatType.JSON);

        videoFeedbackRequest.setConnectTimeout(3000);
        videoFeedbackRequest.setReadTimeout(10000);
        try {
            HttpResponse httpResponse = client.doAction(videoFeedbackRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                int requestCode = scrResponse.getIntValue("code");
                if (200 == requestCode) {
                    // 调用成功
                } else {
                    /**
                     * 表明请求整体处理失败,原因视具体的情况详细分析。
                     */
                    System.out.println("the whole request failed. response:" + JSON.toJSONString(scrResponse));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
  • 本页导读 (0)
文档反馈