本文介绍了如何使用.NET SDK,对指定的图片进行相似图检索。
功能描述
相似图检索帮助您从图库中检索出与给定的图片相似的若干张图片。关于参数的详细说明,请参见相似图检索API文档。
您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)。
前提条件
提交相似图检索任务
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");
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", "待检测图片链接地址");
task1.Add("similarityLibraries", new List<string> { "相似图样本图库名称_1", "相似图样本图库名称_2" });
Dictionary<string, object> httpBody = new Dictionary<string, object>();
/**
* 设置要检测的场景,计费是按照该处传递的场景进行。
* similarity:表示进行相似图检索。
*/
httpBody.Add("scenes", new List<string> { "similarity" });
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);
}
}
}
}
文档内容是否对您有帮助?