文档

语音反垃圾检测

更新时间:

本文介绍了如何使用Java SDK语音反垃圾接口,检测实时语音流或语音文件中的垃圾内容。

功能描述

语音流检测和语音文件检测均为异步检测,检测结果需要您以轮询或者回调的方式获取。关于调用请求中的检测场景参数scenes,返回结果中的分类参数label,以及操作建议参数suggestion的说明,请参见语音异步检测

语音检测按照检测的语音文件、语音流的时间长度进行计费,计费粒度为分钟,每天累计检测总时长进行计量统计,每天检测总时长不足一分钟的按照一分钟进行计费。

前提条件

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

    说明

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

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

提交语音异步检测任务

接口

描述

支持的地域

VoiceAsyncScanRequest

异步检测语音流或语音文件中是否包含违规内容。

  • 支持的语音流协议:HTTP、RTMP、RTSP

  • 支持的语音流格式:M3U8、FLV

cn-shanghai:华东2(上海)

示例代码

  • 提交语音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.green.model.v20180509.VoiceAsyncScanRequest;
    import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
    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 {
           /**
             * 阿里云账号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");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。     
            final IAcsClient client = new DefaultAcsClient(profile);
    
            VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); 
            asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
            asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
            asyncScanRequest.setRegionId("cn-shanghai");
            asyncScanRequest.setConnectTimeout(3000);
            asyncScanRequest.setReadTimeout(6000);
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task1 = new LinkedHashMap<String, Object>();
            // 请将下面的地址修改为要检测的语音文件的地址。
            task1.put("url", "https://xxxxxxxxxxxxxx");
            tasks.add(task1);
            JSONObject data = new JSONObject();
    
            System.out.println("==========Task count:" + tasks.size());
            data.put("scenes", Arrays.asList("antispam"));
            data.put("tasks", tasks);
            // 如果是语音流检测,则修改为true。
            data.put("live", false);
            asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
            System.out.println(JSON.toJSONString(data, true));
    
            try {
                HttpResponse httpResponse = client.doAction(asyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    if (200 == scrResponse.getInteger("code")) {
                        JSONArray taskResults = scrResponse.getJSONArray("data");
                        for (Object taskResult : taskResults) {
                            Integer code = ((JSONObject) taskResult).getInteger("code");
                            if (200 == code) {
                                final String taskId = ((JSONObject) taskResult).getString("taskId");
                                System.out.println("submit async task success, taskId = [" + taskId + "]");
                            } else {
                                System.out.println("task process fail: " + code);
                            }
                        }
                    } else {
                        System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
                    }
                } else {
                    System.out.println("response not success. status: " + httpResponse.getStatus());
                }
            } catch (Exception 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.green.model.v20180509.VoiceAsyncScanRequest;
    import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import com.aliyuncs.green.extension.uploader.ClientUploader;
    
    import java.util.*;
    
    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");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            final IAcsClient client = new DefaultAcsClient(profile);
    
            VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); 
            asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
            asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
            asyncScanRequest.setRegionId("cn-shanghai");
            asyncScanRequest.setConnectTimeout(3000);
            asyncScanRequest.setReadTimeout(6000);
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task1 = new LinkedHashMap<String, Object>();
            task1.put("type", "file");
            String url = null;
            ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false);
            try{
                url = uploader.uploadFile("d:/test.mp4");
            }catch (Exception e){
                e.printStackTrace();
            }
            task1.put("url", url);
            tasks.add(task1);
            JSONObject data = new JSONObject();
    
            System.out.println("==========Task count:" + tasks.size());
            data.put("scenes", Arrays.asList("antispam"));
            data.put("tasks", tasks);
            asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
            System.out.println(JSON.toJSONString(data, true));
    
            try {
                HttpResponse httpResponse = client.doAction(asyncScanRequest);
    
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    System.out.println(JSON.toJSONString(scrResponse, true));
                    if (200 == scrResponse.getInteger("code")) {
                        JSONArray taskResults = scrResponse.getJSONArray("data");
                        for (Object taskResult : taskResults) {
                            Integer code = ((JSONObject) taskResult).getInteger("code");
                            if (200 == code) {
                                final String taskId = ((JSONObject) taskResult).getString("taskId");
                                System.out.println("submit async task success, taskId = [" + taskId + "]");
                            } else {
                                System.out.println("task process fail: " + code);
                            }
                        }
                    } else {
                        System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
                    }
                } else {
                    System.out.println("response not success. status: " + httpResponse.getStatus());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

查询语音异步检测结果

接口

描述

支持的地域

VoiceAsyncScanResultsRequest

查询异步语音检测结果。

cn-shanghai:华东2(上海)

示例代码

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.green.model.v20180509.VoiceAsyncScanResultsRequest;
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 {
        /**
         * 阿里云账号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");
        // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
        final IAcsClient client = new DefaultAcsClient(profile);
        pollingScanResult(client, "提交语音异步检测任务后返回的taskId");
    }

    public static void pollingScanResult(IAcsClient client, String taskId) throws InterruptedException {
        int failCount = 0;
        boolean stop = false;
        do {
            // 设置每10秒查询一次。
            Thread.sleep(10 * 1000);
            JSONObject scanResult = getScanResult(client, taskId);
            if (scanResult == null || 200 != scanResult.getInteger("code")) {
                failCount++;
                System.out.println(taskId + ": get result fail, failCount=" + failCount);
                if (scanResult != null) {
                    System.out.println(taskId + ": errorMsg:" + scanResult.getString("msg"));
                }
                if (failCount > 20) {
                    break;
                }
                continue;
            }

            JSONArray taskResults = scanResult.getJSONArray("data");
            if (taskResults.isEmpty()) {
                System.out.println("failed");
                break;
            }

            for (Object taskResult : taskResults) {
                JSONObject result = (JSONObject) taskResult;
                Integer code = result.getInteger("code");
                if (280 == code) {
                    System.out.println(taskId + ": processing status: " + result.getString("msg"));
                } else if (200 == code) {
                    System.out.println(taskId + ": ========== SUCCESS ===========");
                    System.out.println(JSON.toJSONString(scanResult, true));
                    System.out.println(taskId + ": ========== SUCCESS ===========");
                    stop = true;
                } else {
                    System.out.println(taskId + ": ========== FAILED ===========");
                    System.out.println(JSON.toJSONString(scanResult, true));
                    System.out.println(taskId + ": ========== FAILED ===========");
                    stop = true;
                }
            }
        } while (!stop);
    }

    private static JSONObject getScanResult(IAcsClient client, String taskId) {
        VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest();
        getResultsRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
        getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
        getResultsRequest.setEncoding("utf-8");
        getResultsRequest.setRegionId("cn-shanghai");


        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        Map<String, Object> task1 = new LinkedHashMap<String, Object>();
        task1.put("taskId", taskId);
        tasks.add(task1);

        /**
         * 请务必设置超时时间。
         */
        getResultsRequest.setConnectTimeout(3000);
        getResultsRequest.setReadTimeout(6000);

        try {
            getResultsRequest.setHttpContent(JSON.toJSONString(tasks).getBytes("UTF-8"), "UTF-8", FormatType.JSON);

            HttpResponse httpResponse = client.doAction(getResultsRequest);
            if (httpResponse.isSuccess()) {
                return JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
            } else {
                System.out.println("response not success. status: " + httpResponse.getStatus());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

短语音同步检测

示例代码

/**

* 阿里云账号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");

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.green.model.v20180509.VoiceSyncScanRequest;
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 {
       /**
         * 阿里云账号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");
        // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
        final IAcsClient client = new DefaultAcsClient(profile);

        VoiceSyncScanRequest asyncScanRequest = new VoiceSyncScanRequest();
        asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
        asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
        asyncScanRequest.setRegionId("cn-shanghai");
        asyncScanRequest.setConnectTimeout(3000);
        // 由于同步语音检测比较耗时,因此建议将超时时间设置在15秒以上。
        asyncScanRequest.setReadTimeout(15000);

        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        Map<String, Object> task1 = new LinkedHashMap<String, Object>();
        // 请将下面的地址修改为要检测的语音文件的地址。
        task1.put("url", "https://xxxxxxxxxxxxxx");
        tasks.add(task1);
        JSONObject data = new JSONObject();

        System.out.println("==========Task count:" + tasks.size());
        data.put("scenes", Arrays.asList("antispam"));
        data.put("tasks", tasks);
        asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
        System.out.println(JSON.toJSONString(data, true));

        try {
            HttpResponse httpResponse = client.doAction(asyncScanRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    for (Object taskResult : taskResults) {
                        Integer code = ((JSONObject) taskResult).getInteger("code");
                        if (200 == code) {
                            System.out.println("task process success, result:" + JSON.toJSONString(taskResult));
                        } else {
                            System.out.println("task process fail: " + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response fail:" + new String(httpResponse.getHttpContent(), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 本页导读 (0)