语音反垃圾检测

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

功能描述

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

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

前提条件

已安装.NET依赖。关于安装.NET依赖的具体操作,请参见安装.NET依赖

说明

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

提交语音异步检测任务

接口

描述

支持的地域

VoiceAsyncScanRequest

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

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

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

cn-shanghai:华东2(上海)

示例代码

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            DefaultAcsClient client = new DefaultAcsClient(profile);

            VoiceAsyncScanRequest request = new VoiceAsyncScanRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("dataId", "检测数据ID");
            task1.Add("url", "待检测音频链接地址");

            // scenes:检测场景,唯一取值:antispam。
            // callback、seed用于回调通知,可选参数。
            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            httpBody.Add("scenes", new List<string> { "antispam" });
            httpBody.Add("bizType", "业务场景");
            httpBody.Add("callback", "回调地址");
            httpBody.Add("seed", "随机字符串");
            httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                VoiceAsyncScanResponse response = client.GetAcsResponse(request);
                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

查询语音异步检测结果

接口

描述

支持的地域

VoiceAsyncScanResultsRequest

查询异步语音检测结果。

cn-shanghai:华东2(上海)

示例代码

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            DefaultAcsClient client = new DefaultAcsClient(profile);

            VoiceAsyncScanResultsRequest request = new VoiceAsyncScanResultsRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            List<string> taskIdList = new List<string> { "音频检测任务ID" };

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(taskIdList)), "utf-8", FormatType.JSON);
            try
            {
                VoiceAsyncScanResultsResponse response = client.GetAcsResponse(request);


                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

短语音同步检测

接口

描述

支持的地域

VoicesyncscanRequest

查询短语音检测结果。

cn-shanghai:华东2(上海)

示例代码

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
            DefaultAcsClient client = new DefaultAcsClient(profile);

            VoiceSyncScanRequest request = new VoiceSyncScanRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            Dictionary<string, object> task1 = new Dictionary<string, object>();
            task1.Add("dataId", "检测数据ID");
            task1.Add("url", "待检测音频链接地址");

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // scenes:检测场景,支持指定多个场景。
            httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
            httpBody.Add("bizType", "业务场景");
            httpBody.Add("tasks", new List<Dictionary<string, object>> { task1 });

            request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
            try
            {
                VoiceSyncScanResponse response = client.GetAcsResponse(request);
                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("the request failed. status:{0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}