Retrieve similar images

更新时间:
复制 MD 格式

This topic describes how to use the .NET software development kit (SDK) to retrieve similar images.

Features

The similar image retrieval feature lets you retrieve one or more images from an image library that are similar to a specific image. For more information about the parameters, see Similar image retrieval API.

You must use the AI Guardrails endpoint to call the service using the SDK. For more information about API endpoints, see Endpoints.

Note
  • This SDK supports only image URLs. It does not support local files or binary data.

  • The following types of URLs are supported:

    • Public HTTP and HTTPS URLs. The URLs cannot exceed 2,048 characters in length.

    • Alibaba Cloud Object Storage Service (OSS) URLs in the format of oss://<bucket-name>.<endpoint>/<object-name>. You must grant AI Guardrails the required permissions to access the destination OSS bucket. The bucket and the AI Guardrails service must be in the same region. For more information, see Authorize AI Guardrails to access OSS buckets.

Prerequisites

The .NET dependencies must be installed. For instructions, see Install .NET dependencies.

Note

You must use the required .NET version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Submit a similar image retrieval task

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)
        {
            /**
             * To obtain environment variables:
             *     To obtain the AccessKey ID of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     To obtain the AccessKey secret of a RAM user: Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "<Your AccessKey ID>",
                    "<Your 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", "<your_data_id>");
            task1.Add("url", "<the_image_url>");
            task1.Add("similarityLibraries", new List<string> { "<your_similar_image_library_1>", "<your_similar_image_library_2>" });

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            /**
            * Specify the detection scenarios. You are charged based on the scenarios that you specify.
            * similarity: performs a similar image retrieval.
            */
            httpBody.Add("scenes", new List<string> { "similarity" });
            httpBody.Add("bizType", "<your_business_scenario>");
            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);
            }
        }
    }
}