第一步:
需要下载封装好的代码包,若是找不到下载路径,可前往,自行选择噢
第二步:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import ptions.ClientException;
import ptions.ServerException;
import model.v20180509.ImageSyncScanRequest;
import model.v20180509.VideoAsyncScanRequest;
import model.v20180509.VideoAsyncScanResultsRequest;
import 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 AliyunVideoSyncCheck {/*** 异步视频鉴黄* @param imageUrl* @throws Exception*/public static String aliyunVideoAsyncCheck(String imageUrl) throws Exception {IClientProfile profile = Profile("cn-shanghai", "---accessKeyId---", "--accessKeySecret--");DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green-shanghai.aliyuncs");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", imageUrl);tasks.add(task);/*** 设置要检测的场景, 计费是按照该处传递的场景进行* 视频默认1秒截取一帧,您可以自行控制截帧频率,收费按照视频的截帧数量以及每一帧的检测场景进行计费* 举例:1分钟的视频截帧60张,检测色情和暴恐涉政2个场景,收费按照60张暴恐+60张暴恐涉政进行计费* porn: porn表示色情场景检测,terrorism表示暴恐涉政场景检测*/JSONObject data = new JSONObject();data.put("scenes", Arrays.asList("porn", "terrorism"));data.put("tasks", tasks);videoAsyncScanRequest.JSONString().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 HttpContent(), "UTF-8"));
// System.out.JSONString(jsonObject, true));if (200 == Integer("code")) {JSONArray taskResults = JSONArray("data");for (Object taskResult : taskResults) {if (200 == ((JSONObject) taskResult).getInteger("code")) {String taskId = ((JSONObject) taskResult).getString("taskId");return taskId;}}}}else{System.out.println("response not success. status:" + Status());}} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return null;}/*** 查询异步检测结果* @param taskId* @return* @throws Exception*/public static boolean getResult(String taskId) throws Exception {boolean flag = false;IClientProfile profile = Profile("cn-shanghai", "---accessKeyId---", "--accessKeySecret--");DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green-shanghai.aliyuncs");IAcsClient client = new DefaultAcsClient(profile);VideoAsyncScanResultsRequest videoAsyncScanResultsRequest = new VideoAsyncScanResultsRequest();videoAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON);List<String> taskList = new ArrayList<String>();// 这里添加要查询的taskId,提交任务的时候需要自行保存taskIdtaskList.add(taskId);videoAsyncScanResultsRequest.JSONString(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 HttpContent(), "UTF-8"));
// System.out.JSONString(jsonObject, true));if (200 == Integer("code")) {JSONArray taskResults = JSONArray("data");for (Object taskResult : taskResults) {if (200 == ((JSONObject) taskResult).getInteger("code")) {JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");for (Object sceneResult : sceneResults) {String scene = ((JSONObject) sceneResult).getString("scene");String suggestion = ((JSONObject) sceneResult).getString("suggestion");//根据scene和suggetion做相关的处理//do somethingSystem.out.println("scene = [" + scene + "]");System.out.println("suggestion = [" + suggestion + "]");if (!"pass".equals(suggestion)){flag = false;return flag;}else {flag = true;}}} else {System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));}}} else {System.out.println("detect not success. code:" + Integer("code"));}}else{System.out.println("response not success. status:" + Status());}} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return flag;}/*** 同步检测* @param imageUrl* @return* @throws Exception*/public static boolean aliyunVideoSyncCheck(String imageUrl) throws Exception {boolean flag = false;IClientProfile profile = Profile("cn-shanghai", "---accessKeyId---", "--accessKeySecret--");DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green-shanghai.aliyuncs");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", ".jpg");Map<String, Object> frame2 = new LinkedHashMap<String, Object>();frame2.put("offset", 5);frame2.put("url", ".jpg");Map<String, Object> frame3 = new LinkedHashMap<String, Object>();frame3.put("offset", 10);frame3.put("url", "/%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张,检测色情和暴恐涉政2个场景,收费按照60张暴恐+60张暴恐涉政进行计费* porn: porn表示色情场景检测*/JSONObject data = new JSONObject();data.put("scenes", Arrays.asList("porn", "terrorism"));data.put("tasks", tasks);videoSyncScanRequest.JSONString().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 HttpContent(), "UTF-8"));//System.out.JSONString(jsonObject, true));if (200 == Integer("code")) {JSONArray taskResults = JSONArray("data");for (Object taskResult : taskResults) {if (200 == ((JSONObject) taskResult).getInteger("code")) {JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");for (Object sceneResult : sceneResults) {String scene = ((JSONObject) sceneResult).getString("scene");String suggestion = ((JSONObject) sceneResult).getString("suggestion");//根据scene和suggetion做相关的处理//do somethingSystem.out.println("scene = [" + scene + "]");System.out.println("suggestion = [" + suggestion + "]");if (!"pass".equals(suggestion)){flag = false;return flag;}else {flag = true;}}} else {System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));}}} else {System.out.println("detect not success. code:" + Integer("code"));}}else{System.out.println("response not success. status:" + Status());}} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return flag;}public static void main(String[] args) throws Exception {
// boolean b = aliyunVideoSyncCheck(".mp4");boolean b = aliyunVideoSyncCheck(".mp4");if (b){System.out.printf("不是黄视频暴视频");}else{System.out.printf("是黄视频暴视频");}}}
阿里云接口文档地址:.html?spm=a2c4g.11186623.6.705.2b611e7f1gZrHl
本文发布于:2024-01-31 23:12:03,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170671392632078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |