对接阿里云内容安全服务(机审视频检测)

阿里云内容安全异步检测文档地址: https://help.aliyun.com/document_detail/70436.htm?spm=a2c4g.11186623.0.0.6da625ecGmk0u6#reference-r3h-cdz-52b

依赖:

>
    >com.aliyun>
    >aliyun-java-sdk-core>
    >4.1.1>
>
>
    >com.aliyun>
    >aliyun-java-sdk-green>
    >3.6.5>
>
>
    >com.alibaba>
    >fastjson>
    >1.2.51>
>
>
    >com.aliyun.oss>
    >aliyun-sdk-oss>
    >2.8.3>
>

代码:


/**
 * @description TODO
 * @Author Administrator
 * @Date 2022/1/19
 * @Version 1.0
 **/

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.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 org.apache.commons.lang.StringUtils;

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

public class AliyunVideoSyncCheck {

    /**
     * 异步视频鉴黄
     * @param imageUrl
     *        otherId: 业务ID
     * @throws Exception
     */
    public static String aliyunVideoAsyncCheck(String imageUrl, String otherId) throws Exception {

        IClientProfile profile = DefaultProfile.getProfile("cn-beijing", "请填写您的AccessKey ID", "请填写您的AccessKey Secret");
        /**
         *  支持的地域:
         *      cn-shanghai:华东2(上海)
         *      cn-beijing:华北2(北京)
         *      cn-shenzhen:华南1(深圳)
         *      ap-southeast-1:新加坡
         **/
        DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", "Green", "green.cn-beijing.aliyuncs.com");// 调用
//        DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", "Green", "green-vpc.cn-beijing.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>();
        if (StringUtils.isNotEmpty(otherId)){
            task.put("dataId", otherId);  // 视频所在业务数据的ID
        }
        task.put("url", imageUrl); // 待检测视频的URL。
        task.put("interval", 1); // 视频截帧间隔,单位为秒,取值范围:1~600。默认值为1秒。
        task.put("maxFrames", 240); // 系统对本次检测的视频进行截帧的张数上限,取值范围:5~3600,默认为200张。

        tasks.add(task);
        /**
         * 设置要检测的场景, 计费是按照该处传递的场景进行
         * 视频默认1秒截取一帧,您可以自行控制截帧频率,
         * 收费按照api请求次数进行计费
         *      传视频url : 算1.8次
         *      传截帧图片: 算1次
         *
         * porn: porn表示色情场景检测,terrorism表示暴恐涉政场景检测
         *
         * 视频截帧存放应该是单独的内容安全系统(不用管)
         */
        JSONObject data = new JSONObject();
        data.put("scenes", Arrays.asList("porn", "terrorism")); // porn:视频智能鉴黄。terrorism:视频暴恐涉政。ad:视频图文违规。live:视频不良场景。logo:视频logo。
        data.put("tasks", tasks);
		/**
         * 指定视频语音检测场景,唯一取值:antispam,表示语音反垃圾。不传入该参数时仅检测视频图像内容;如果传入该参数,则在检测视频中图像的同时,对视频中语音进行检测。
         * 如果需要检测视频语音,则不支持通过上传视频截帧序列的方式(即在task中传入frames)进行检测,您必须传入视频或视频流的URL地址(即在task中传入url)进行检测。
         **/
        data.put("audioScenes", Arrays.asList("antispam")); // 视频语音检测
        
        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));
                if (200 == jsonObject.getInteger("code")) {
                    JSONArray taskResults = jsonObject.getJSONArray("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:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询异步检测结果 ( 查询检测结果api不计费 )   建议查询间隔设置成30秒(即在提交异步检测任务30秒后查询结果),最长不能超过4小时,否则结果将会丢失
     * @param taskId
     * @return
     * @throws Exception
     */
    public static boolean getResult(String taskId) throws Exception {
        boolean flag = false;
        IClientProfile profile = DefaultProfile.getProfile("cn-beijing", "请填写您的AccessKey ID", "请填写您的AccessKey Secret");
        DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", "Green", "green.cn-beijing.aliyuncs.com"); // 调试
//        DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", "Green", "green-vpc.cn-beijing.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(taskId);

        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(jsonObject, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONArray taskResults = scrResponse.getJSONArray("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 something
                                System.out.println("scene = [" + scene + "]");
                                /**
                                  *  pass:结果正常,无需进行其余操作。
                                  *  review:结果不确定,需要进行人工审核。
                                  *  block:结果违规,建议直接删除或者限制公开。
                                 **/
                                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:" + scrResponse.getInteger("code"));
                }
            }else{
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } 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 = 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> tasks = new ArrayList>();
//        Map task = new LinkedHashMap();
//        task.put("dataId", UUID.randomUUID().toString());
//
//        List> frames = new ArrayList>();
//        Map frame1 = new LinkedHashMap();
//        frame1.put("offset", 0);
//        frame1.put("url", "https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg");
//
//        Map frame2 = new LinkedHashMap();
//        frame2.put("offset", 5);
//        frame2.put("url", "http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg");
//
//        Map frame3 = new LinkedHashMap();
//        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);
//        /**
//         * 设置要检测的场景, 计费是按照该处传递的场景进行
//         * porn: porn表示色情场景检测
//         */
//        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(jsonObject, true));
//                if (200 == scrResponse.getInteger("code")) {
//                    JSONArray taskResults = scrResponse.getJSONArray("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 something
//                                System.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:" + scrResponse.getInteger("code"));
//                }
//            }else{
//                System.out.println("response not success. status:" + httpResponse.getStatus());
//            }
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//        return flag;
//    }


}

sql :

  • -查询一分钟前的数据
select  *  from  表名 where  时间字段名 <= (select SUBDATE(now(),interval 60 second));

你可能感兴趣的:(阿里云,阿里云,安全,音视频)