本文介绍了如何使用.NET SDK图片审核接口,检测图片中是否包含风险内容。
功能描述
图片审核支持同步检测和异步检测两种方式。
支持传入检测的图片内容媒介包括互联网图片URL、本地图片文件路径和二进制图片文件流。
前提条件
(推荐)图片同步检测
接口 | 描述 | 支持的地域 |
ImageSyncScanRequest | 提交图片同步检测任务,对图片进行多个风险场景的识别,包括色情、暴恐涉政、广告、二维码、不良场景、Logo(商标台标)识别。 |
|
示例代码
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);
ImageSyncScanRequest request = new ImageSyncScanRequest();
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
{
ImageSyncScanResponse 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);
}
}
}
}
提交图片异步检测任务
使用.NET SDK对图片进行风险检测,通过异步请求提交检测任务,结果可以通过提交请求时设置callback回调获取,也可以通过接口轮询获取。
异步检测支持的输入内容与同步检测一致(本地文件、图片URL、图片二进制流),以下仅以图片URL作为输入示例。
接口 | 描述 | 支持的地域 |
ImageAsyncScanRequest | 提交图片异步检测任务,对图片进行多个风险场景的识别,包括色情、暴恐涉政、广告、二维码、不良场景、Logo(商标台标)识别。 |
|
示例代码
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);
ImageAsyncScanRequest request = new ImageAsyncScanRequest();
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
{
ImageAsyncScanResponse 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);
}
}
}
}
查询异步检测结果
接口 | 描述 | 支持的地域 |
ImageAsyncScanResultsRequest | 查询图片异步检测任务的结果。支持同时查询多个检测任务的返回结果。 |
|
示例代码
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);
ImageAsyncScanResultsRequest request = new ImageAsyncScanResultsRequest();
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
{
ImageAsyncScanResultsResponse 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);
}
}
}
}
图片检测结果反馈
如果您认为图片检测结果与您的预期不符,可以通过图片检测结果反馈接口,对检测结果进行纠正(系统会根据您反馈的结果,将图片添加到相似图片的黑名单库或者白名单库)。当您再次提交相似的内容进行检测时,以您反馈的label返回结果。
关于接口的说明,请参见检测结果反馈。
接口 | 描述 | 支持的Region |
ImageScanFeedbackRequest | 提交图片检测结果的反馈,以人工反馈的检测结果纠正算法检测结果。 |
|
示例代码
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);
ImageScanFeedbackRequest request = new ImageScanFeedbackRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
// scenes:检测场景,支持指定多个场景。
// suggestion:期望的检测结果,pass:正常;block:违规。
Dictionary<string, object> httpBody = new Dictionary<string, object>();
httpBody.Add("scenes", new List<string> { "porn", "terrorism" });
httpBody.Add("suggestion", "block");
httpBody.Add("url", "图片链接地址");
request.SetContent(System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)), "utf-8", FormatType.JSON);
try
{
ImageScanFeedbackResponse 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);
}
}
}
}