使用视频审核Java SDK检测视频中是否含有风险内容,支持同时检测视频中的图像和语音内容。
背景信息
视频审核支持同步检测和异步检测两种方式。
检测方式 | 支持的检测对象 | 获取检测结果 |
---|---|---|
同步检测 | 只支持传入视频的截帧图片序列。 | 同步返回检测结果。 |
(推荐)异步检测 | 支持传入原始视频(互联网视频URL、本地视频文件路径、二进制视频文件流)或视频的截帧图片序列。 | 支持通过以下两种方式获取检测结果:
|
准备工作
在进行具体的服务调用之前,请完成以下准备工作:
- 创建阿里云AccessKey。具体操作请参见创建AccessKey。
- 安装Java依赖。具体操作请参见安装Java依赖。
- 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类。
(推荐)提交视频异步检测任务
接口 | 描述 | 支持的地域 |
---|---|---|
VideoAsyncScanRequest | 提交视频异步检测任务,对视频进行多个风险场景的识别,包括色情、暴恐涉政、广告 、不良场景、Logo(商标台标)识别。 |
|
示例代码
- 传视频URL进行检测
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.VideoAsyncScanRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.*; public class Main { public static void main(String[] args) throws Exception { IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret"); DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); 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://xxx.xxx.xx/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 jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); System.out.println(JSON.toJSONString(jsonObject, true)); }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.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.*; public class Main { public static void main(String[] args) throws Exception { IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret"); DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); 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://xxx.xxx.xx/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 jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); System.out.println(JSON.toJSONString(jsonObject, true)); }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.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.*; public class Main { public static void main(String[] args) throws Exception { IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret"); DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); 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("/Users/01fb4ab6420b5f34623e13b82b51ef87.mp4")); //上传到服务端。 url = uploader.uploadBytes(videoBytes); }catch (Exception e){ System.out.println("upload file to server fail.", e); } 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://xxx.xxx.xx/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 jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); System.out.println(JSON.toJSONString(jsonObject, true)); }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.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.*; public class Main { public static void main(String[] args) throws Exception { IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret"); DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); 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()); //url填写直播流地址。 task.put("url", "http://xxxx/test.mp4"); 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", "http://xxx.xxx.xx/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 jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); System.out.println(JSON.toJSONString(jsonObject, true)); }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.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.*; public class Main { public static void main(String[] args) throws Exception { IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret"); DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); 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()); //url填写直播流地址。 task.put("url", "http://xxxx/test.mp4"); 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", "http://xxx.xxx.xx/xxx.json"); 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 jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); System.out.println(JSON.toJSONString(jsonObject, true)); }else{ System.out.println("response not success. status:" + httpResponse.getStatus()); } } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }
查询视频异步检测结果
接口 | 描述 | 支持的地域 |
---|---|---|
VideoAsyncScanResultsRequest | 查询视频异步检测任务的结果。
说明 该方法需要轮询结果,建议使用callback的方式获取结果。
|
|
示例代码
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.VideoAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
VideoAsyncScanResultsRequest videoAsyncScanResultsRequest = new VideoAsyncScanResultsRequest();
videoAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON);
List<String> taskList = new ArrayList<String>();
// 这里添加要查询的taskId。提交任务的时候需要自行保存taskId。
taskList.add("vi3pnWxOlikyx6KJNtY7Naza-1pZ$MN");
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 jsonObject = JSON
.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(jsonObject, true));
}else{
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
提交视频同步检测任务
接口 | 描述 | 支持的地域 |
---|---|---|
VideoSyncScanRequest | 提交视频同步检测任务,同步检测视频中的风险内容。
说明 同步检测只支持传递视频帧序列,不支持检测视频文件,推荐使用异步检测接口。
|
|
示例代码
说明 以下代码中使用帧序列的方式提交待检测的视频。
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.VideoSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
public class Main3 {
public static void main(String[] args) throws Exception {
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
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", "https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg");
Map<String, Object> frame2 = new LinkedHashMap<String, Object>();
frame2.put("offset", 5);
frame2.put("url", "http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg");
Map<String, Object> frame3 = new LinkedHashMap<String, Object>();
frame3.put("offset", 10);
frame3.put("url", "http://rifleman-share.oss-cn-hangzhou.aliyuncs.com/test/%E6%AD%A3%E5%B8%B8/68d5883924c9e8cc88806a73bd7a8995.jpg");
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 jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(jsonObject, true));
}else{
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
在文档使用中是否遇到以下问题
更多建议
匿名提交