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

API 操作

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

準備

govendor を使用して次のコマンドを実行し、Alibaba Cloud Image Search SDK for Go をインストールします。
go get  github.com/aliyun/alibabacloud-sdk/imagesearch-20200212/golang@master

追加

  • コード例
    package main
    import (
      "fmt"
      "os"
      util "github.com/aliyun/tea-util/golang/service"
      imagesearch "github.com/aliyun/alibabacloud-sdk/imagesearch-20200212/golang/client"
    )
    func main() {
      // Initialize config.
      var config = new(imagesearch.Config).SetAccessKeyId("XXXXXXX").
        SetAccessKeySecret("XXXXXXXXXXX").
        SetType("access_key").
        SetEndpoint("imagesearch.cn-shanghai.aliyuncs.com").
        SetRegionId("cn-shanghai")
        // Create a client.
      client, err := imagesearch.NewClient(config)
      if err ! = nil {
        panic(err)
      }
      // Initialize runtimeObject.
      var runtimeObject = new(util.RuntimeOptions)
      b, err := os.Open("D:/123.jpg")
      if err ! = nil {
        panic(err)
      }
      request := new(imagesearch.AddImageAdvanceRequest).
         // Required. The name of the Image Search instance.
        SetInstanceName("XXXXXXXX").
        // 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.
        SetPicName("test").
        // Required. The product ID, which can be up to 512 characters in length.
        // A product ID can correspond to multiple image names.
        SetProductId("test").
        // 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.
        SetPicContentObject(b).
        // 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.
        SetCategoryId(2).
        // 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 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.
         // Optional. The user-defined content. The value can be up to 4,096 characters in length.
         // If you specify this parameter, the response includes this parameter and its value. You can add text, such as an image description.
        SetCustomContent("this is a simple test!").
         // 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.
        SetIntAttr(100).
        // 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.
        SetStrAttr("1")
        // Call the API operation.
      resp, err := client.AddImageAdvance(request, runtimeObject)
      if err ! = nil {
        fmt.Println(err.Error())
      }
      fmt.Println(resp)
    }
  • レスポンス例
    {
       "RequestId": "B48CC953-F495-496C-BC96-B96826A860F1",
       "Success": true,
       "Message": "success",
       "Code": 0,
       "PicInfo": {
          "CategoryId": 2,
          "Region": "167,477,220,407"
       }
    }

SearchImageByPic

  • コード例
    package main
    import (
      "fmt"
       "os"
      util "github.com/aliyun/tea-util/golang/service"
      imagesearch "github.com/aliyun/alibabacloud-sdk/imagesearch-20200212/golang/client"
    )
    func main() {
      // Initialize config.
      var config = new(imagesearch.Config).SetAccessKeyId("xxxxxxxxxx").
        SetAccessKeySecret("xxxxxxxxxxxx").
        SetType("access_key").
        SetEndpoint("imagesearch.cn-shanghai.aliyuncs.com").
        SetRegionId("cn-shanghai")
        // Create a client.
      client, err := imagesearch.NewClient(config)
      if err ! = nil {
        panic(err)
      }
      // Initialize runtimeObject.
      var runtimeObject = new(util.RuntimeOptions)
         b, err := os.Open("D:/123.jpg")
      if err ! = nil {
        panic(err)
      }
      request := new(imagesearch.SearchImageByPicAdvanceRequest).
        // Required. The name of the Image Search instance.
         SetInstanceName("xxxxxxxxxx").
         // 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.
         SetPicContentObject(b).
         // 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.
         SetCategoryId(2).
        // Optional. The number of images to be returned. Valid values: 1 to 100. Default value: 10.
         SetNum(10).
         // Optional. No. of the first image to be displayed. Valid values: 0 to 499. Default value: 0.
         SetStart(0).
         // 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"
         SetFilter("int_attr=101 OR str_attr=\"2\"").
        // 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 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.
        // Call the API operation.
      resp, err := client.SearchImageByPicAdvance(request, runtimeObject)
      if err ! = nil {
        fmt.Println(err.Error())
      }
      fmt.Println(resp)
    }
  • レスポンス例
    {
       "RequestId": "F468F52E-963A-452B-88E7-1C43F92C9D81",
       "Success": true,
       "Code": 0,
       "Msg": "success",
       "Auctions": [
          {
             "CategoryId": 2,
             "ProductId": "php",
             "PicName": "test",
             "CustomContent": "this is a simple test!",
             "SortExprValues": "292.509948730469;16",
             "IntAttr": 100,
             "StrAttr": "1"
          }
       ],
       "Head": {
          "DocsReturn": 1,
          "DocsFound": 1,
          "SearchTime": 121
       },
       "PicInfo": {
          "CategoryId": 2,
          "Region": "167,476,220,407",
          "AllCategories": [
             {
                "Id": 0,
                "Name": "Tops"
             },
             .......
             {
                "Id": 88888888,
                "Name": "Other"
             }
          ]
       }
    }

SearchImageByName

  • コード例
    package main
    import (
      "fmt"
      util "github.com/aliyun/tea-util/golang/service"
      imagesearch "github.com/aliyun/alibabacloud-sdk/imagesearch-20200212/golang/client"
    )
    func main() {
      // Initialize config.
      var config = new(imagesearch.Config).SetAccessKeyId("xxxxx").
        SetAccessKeySecret("XXXXXXXXXXXXXXX").
        SetType("access_key").
        SetEndpoint("imagesearch.cn-shanghai.aliyuncs.com").
        SetRegionId("cn-shanghai")
        // Create a client.
      client, err := imagesearch.NewClient(config)
      if err ! = nil {
        panic(err)
      }
      // Initialize runtimeObject.
      var runtimeObject = new(util.RuntimeOptions)
      request := new(imagesearch.SearchImageByNameRequest).
        // Required. The name of the Image Search instance.
        SetInstanceName("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.
        SetPicName("test").
         // Required. The product ID, which can be up to 512 characters in length.
         // A product ID can correspond to multiple image names.
        SetProductId("php").
         // 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.
        SetCategoryId(2).
        // Optional. The number of images to be returned. Valid values: 1 to 100. Default value: 10.
        SetNum(10).
        // Optional. No. of the first image to be displayed. Valid values: 0 to 499. Default value: 0.
        SetStart(0).
         // 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"
        SetFilter("str_attr=\"1\"")  //Set STRING type filters.
        // Call the API operation.
      resp, err := client.SearchImageByName(request, runtimeObject)
      if err ! = nil {
        fmt.Println(err.Error())
      }
      fmt.Println(resp)
    }
  • レスポンス例
    {
       "RequestId": "9011A4A3-7642-44C5-AA33-6E38BA2EA3F1",
       "Success": true,
       "Code": 0,
       "Msg": "success",
       "Auctions": [
          {
             "CategoryId": 2,
             "ProductId": "php",
             "PicName": "test",
             "CustomContent": "this is a simple test!",
             "SortExprValues": "7.33136443711219e+24;0",
             "IntAttr": 100,
             "StrAttr": "1"
          }
       ],
       "Head": {
          "DocsReturn": 1,
          "DocsFound": 1,
          "SearchTime": 12
       },
       "PicInfo": {
          "CategoryId": 2,
          "Region": null,
          "AllCategories": [
             {
                "Id": 0,
                "Name": "Tops"
             },
            .......
             {
                "Id": 88888888,
                "Name": "Other"
             }
          ]
       }
    }

Delete

  • コード例
    package main
    import (
      "fmt"
      util "github.com/aliyun/tea-util/golang/service"
      imagesearch "github.com/aliyun/alibabacloud-sdk/imagesearch-20200212/golang/client"
    )
    func main() {
      // Initialize config.
      var config = new(imagesearch.Config).SetAccessKeyId("XXXXXXXX").
        SetAccessKeySecret("XXXXXXXX").
        SetType("access_key").
        SetEndpoint("imagesearch.cn-shanghai.aliyuncs.com").
        SetRegionId("cn-shanghai")
        // Create a client.
      client, err := imagesearch.NewClient(config)
      if err ! = nil {
        panic(err)
      }
      // Initialize runtimeObject.
      var runtimeObject = new(util.RuntimeOptions)
      request := new(imagesearch.DeleteImageRequest).
        // Required. The name of the Image Search instance.
        SetInstanceName("XXXXXXXX").
           // 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.
        SetPicName("test").
         // 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.
        SetProductId("php")    
        // Call the API operation.
      resp, err := client.DeleteImage(request, runtimeObject)
      if err ! = nil {
        fmt.Println(err.Error())
      }
      fmt.Println(resp)
    }
  • レスポンス例
    {
       "RequestId": "C9432066-8918-447F-858B-B4B11E2A6941",
       "Success": true,
       "Message": "success",
       "Code": 0
    }