このトピックでは、Alibaba Cloud Image Search SDK for.NET の使用方法について説明します。

API 操作

操作 説明 
Add 画像を追加します。
SearchImageByPic 新しい画像に基づいて類似画像を検索します。
SearchImageByName 画像ギャラリー内の既存の画像の名前に基づいて、類似した画像を検索します。
Delete 画像を削除します。

準備

NuGet パッケージマネージャーを使用して、Alibaba Cloud Image Search SDK for.NET をインストールできます。 [ソリューションエクスプローラー] で、プロジェクトを右クリックし、[NuGet パッケージの管理]を選択します。 表示されたページで、[参照]タブをクリックして、AlibabaCloud.SDK.ImageSearch 1.0.3 を入力し、[インストール] をクリックします。

Add

  • サンプルコード
    using AlibabaCloud.Commons.Models;
    using AlibabaCloud.ImageSearch;
    using AlibabaCloud.ImageSearch.Models;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    using Tea;
    namespace Test
    {
        class Add
        {
            static void Main(string[] args)
            {
                // Console.WriteLine("Hello World!") ;
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXXXXXXXXXXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                FileStream fs = File.OpenRead("D:/111.jpg");
                AddImageAdvanceRequest request = new AddImageAdvanceRequest();
                // Required. The image name, which can be up to 512 characters in length.
                 // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
               // 2. If you add images that have the same ProductId and PicName for multiple times, the image that is added in the last operation overwrites the images that are added in previous operations.
                request.PicName = "xusong.jpg";
                // Required. The product ID, which can be up to 512 characters in length.
                // A product ID can correspond to multiple image names.
                request.ProductId = "1001";
                // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXXXXXXXXXXXXXXXXXXXXX";
                // The image file. The file size of the image cannot exceed 2 MB. The transmission time-out period cannot exceed 5 seconds. Only JPG and PNG images are supported.
                // For product, brand, and generic image searches, the length and the width of the image must range from 200 pixels to 1,024 pixels.
                // For cloth image searches, the length and the width of the image must range from 448 pixels to 1,024 pixels.
                // The image cannot contain rotation information.
                request.PicContentObject = fs;
                // Optional. The integer attribute. The attribute can be used to filter images when you search for images. If you specify this parameter, the response includes this parameter and its value.
                // For example, you can set different IntAttr values for images of different sites or images of different users. This narrows down the search scope when you specify the integer attribute to search for images.
                request.IntAttr = 99;
                 // Optional. The string attribute. The value can be up to 128 characters in length. The attribute can be used to filter images when you search for images. If you specify this parameter, the response includes this parameter and its value.
                request.StrAttr = "aliyun.com";
                // Optional. The category of the image.
                // 1. For product image searches, if you specify a category for an image, the specified category prevails. If you do not specify a category for an image, the system predicts the category, and returns the prediction result in the response.
                // 2. For generic image searches, the system sets the category ID to 88888888, regardless of whether you specify a category for an image.
                request.CategoryId = 5;
                // Optional. Specifies whether to recognize the subject in the image and search for images based on the recognized subject. The default value is true.
               // 1. true: The system recognizes the subject in the image, and searches for images based on the recognized subject. You can obtain the recognition result in the response.
               // 2. false: The system does not recognize the subject of the image, and searches for images based on the entire image.
                request.Crop = true;
                // Optional. The subject area in the image. The subject area is in the format of x1,x2,y1,y2. x1 and y1 represent the upper-left corner pixel. x2 and y2 represent the lower-right pixel.
               // If you specify the Region parameter, the system searches for images based on this parameter setting regardless of the value of the Crop parameter.
                request.Region = "66,333,97,356";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    AddImageResponse response = client.AddImageAdvance(request, runtimeObject);
                    Console.WriteLine("RequestId:" + response.RequestId);
                    Console.WriteLine("Message:" + response.Message);
                    Console.WriteLine("Success:" + response.Success);
                    Console.WriteLine("Code:" + response.Code);
                    Console.WriteLine("PicInfo.CategoryId:" + response.PicInfo.CategoryId);
                    Console.WriteLine("PicInfo.Region:" + response.PicInfo.Region);
                }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • レスポンスの例
    RequestId:247D5DAC-EBDD-49DC-973B-463B36C6BB41
    Message:success
    Success:True
    Code:0
    PicInfo.CategoryId:5
    PicInfo.Region:66,333,97,356

SearchImageByPic

  • サンプルコード
    using AlibabaCloud.ImageSearch;
    using AlibabaCloud.ImageSearch.Models;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    using Tea;
    namespace Test
    {
        class SearchImageByPic
        {
            static void Main(string[] args)
            {
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                FileStream fs = File.OpenRead("D:/123.jpg");
                SearchImageByPicAdvanceRequest request = new SearchImageByPicAdvanceRequest();
                // Optional. The category of the image.
               // 1. For product image searches, if you specify a category for an image, the specified category prevails. If you do not specify a category for an image, the system predicts the category, and returns the prediction result in the response.
               // 2. For generic image searches, the system sets the category ID to 88888888, regardless of whether you specify a category for an image.
                request.CategoryId =5 ;
                // The image file. The file size of the image cannot exceed 2 MB. The transmission time-out period cannot exceed 5 seconds. Only JPG and PNG images are supported.
                // For product, and generic image searches, the length and the width of the image must range from 200 pixels to 1,024 pixels.
               // The image cannot contain rotation information.
                request.PicContentObject = fs;
                // Optional. Specifies whether to recognize the subject in the image and search for images based on the recognized subject. The default value is true.
                // 1. true: The system recognizes the subject in the image, and searches for images based on the recognized subject. You can obtain the recognition result in the response.
                // 2. false: The system does not recognize the subject of the image, and searches for images based on the entire image.
                // Optional. The filter condition.
               // The int_attr field supports the following operators: >, >=, <=, and =. The str_attr field supports the following operators: = and !=. Multiple filter conditions are joined by AND or OR.
              // Examples:
              //1. Filter results based on IntAttr: int_attr>=100
              //2. Filter results based on StrAttr: str_attr! ="value1"
              //3. Filter results based on IntAttr and StrAttr: int_attr=1000 AND str_attr="value1"
                request.Filter = "int_attr=99";
                // Optional. The subject area in the image. The subject area is in the format of x1,x2,y1,y2. x1 and y1 represent the upper-left corner pixel. x2 and y2 represent the lower-right pixel.
                // If you specify the Region parameter, the system searches for images based on this parameter setting regardless of the value of the Crop parameter.
                // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXX";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    SearchImageByPicResponse response = client.SearchImageByPicAdvance(request, runtimeObject);
                    String result = Newtonsoft.Json.JsonConvert.SerializeObject(response);
                    Console.WriteLine("result:" + result);
                    }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • レスポンスの例
    result:
    {
        "RequestId":"632814A0-F34D-46FB-882D-81D74E7C3290",
        "Success":true,
        "Code":0,
        "Msg":"success",
        "Auctions":[
            {
                "CategoryId":0,
                "ProductId":"test",
                "PicName":"test.jpg",
                "CustomContent":null,
                "SortExprValues":"7.33136443711219;0",
                "IntAttr":null,
                "StrAttr":null
            },
            {......}
            ],
        "Head":{
            "DocsReturn":4,
            "DocsFound":4,
            "SearchTime":155
        },
        "PicInfo":{
            "CategoryId":0,
            "Region":"202,596,155,637",
            "AllCategories":[{"Id":0,"Name":"Tops"},{.....}]
             }
    }

SearchImageByName

  • サンプルコード
    using AlibabaCloud.ImageSearch;
    using AlibabaCloud.ImageSearch.Models;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    using Tea;
    namespace Test
    {
        class SearchImageByName
        {
            static void Main(string[] args)
            {
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                SearchImageByNameRequest request = new SearchImageByNameRequest();
                // Optional. The category of the image.
               // 1. For product image searches, if you specify a category for an image, the specified category prevails. If you do not specify a category for an image, the system predicts the category, and returns the prediction result in the response.
               // 2. For generic image searches, the system sets the category ID to 88888888, regardless of whether you specify a category for an image.
                request.CategoryId = 5;
                // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXXXXXX";
                // Required. The image name, which can be up to 512 characters in length.
                // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
                request.PicName = "xusong.jpg";
                 // Required. The product ID, which can be up to 512 characters in length.
                // A product ID can correspond to multiple image names.
                request.ProductId = "1001";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    SearchImageByNameResponse response = client.SearchImageByName(request,runtimeObject);
                    String result = Newtonsoft.Json.JsonConvert.SerializeObject(response);
                    Console.WriteLine("result:" + result);
                }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • レスポンスの例
    result:
    {
        "RequestId":"5B16E5CD-FE8A-4A4E-A90B-02A5C9F8BD81",
        "Success":true,
        "Code":0,
        "Msg":"success",
        "Auctions":[
            {
                "CategoryId":0,
                "ProductId":"test",
                "PicName":"test.jpg",
                "CustomContent":null,
                "SortExprValues":"7.33136443711219;0",
                "IntAttr":null,
                "StrAttr":null
            },
            {....}
        ],
        "Head":{
            "DocsReturn":4,
            "DocsFound":4,
            "SearchTime":50
        },
        "PicInfo":{
            "CategoryId":0,
            "Region":null,
            "AllCategories":[
                {"Id":0,"Name":"Tops"},
                {...}
            ]
        }
    }

Delete

  • サンプルコード
    using AlibabaCloud.ImageSearch;
    using AlibabaCloud.ImageSearch.Models;
    using AlibabaCloud.TeaUtil.Models;
    using System;
    using System.IO;
    using Tea;
    namespace Test
    {
        class Delete
        {
            static void Main(string[] args)
            {
                Config config = new Config();
                config.AccessKeyId = "XXXXXXXXX";
                config.AccessKeySecret = "XXXXXXXXX";
                config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
                config.RegionId = "cn-shanghai";
                config.Type = "access_key";
                Client client = new Client(config);
                DeleteImageRequest request = new DeleteImageRequest();
               // Required. The name of the Image Search instance.
                request.InstanceName = "XXXXXXXXXXX";
                 // Required. The image name, which can be up to 512 characters in length.
                 // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
                request.PicName = "xusong.jpg";
                // Optional. The image name. If this parameter is not specified, the system deletes all the images that correspond to the specified ProductId parameter. If this parameter is specified, the system deletes only the image that is specified by the ProductId and PicName parameters.
                request.ProductId = "1001";
                RuntimeOptions runtimeObject = new RuntimeOptions();
                try
                {
                    DeleteImageResponse response = client.DeleteImage(request,runtimeObject);
                    Console.WriteLine("RequestId:" + response.RequestId);
                    Console.WriteLine("Message:" + response.Message);
                    Console.WriteLine("Code:" + response.Code);
                }
                catch (TeaUnretryableException e)
                {
                    Console.WriteLine("Returned InnerException:" + e.InnerException.Message);
                    Console.WriteLine("Returned data:" + e.Data);
                    Console.WriteLine("Returned message:" + e.Message);
                    Console.WriteLine("Returned string:" + e.ToString());
                }
            }
        }
    }
  • レスポンスの例
    RequestId:D9BDEAE0-674C-4126-9D76-A79F0BE9E881
    Message:success
    Code:0