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

API 操作

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

準備

次のコマンドを実行して、Alibaba Cloud Image Search SDK for Node.js をインストールします。
npm install  @alicloud/imagesearch@1.0.2

Add

  • コード例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXXXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const addImageAdvanceRequest = new imagesearch.AddImageAdvanceRequest({
        // Required. The name of the Image Search instance.
        instanceName: "XXXXXXXXXX",
         // Required. The product ID, which can be up to 512 characters in length.
         // A product ID can correspond to multiple image names.
        productId: "123455",
          // 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.
        picName: "this",
        // 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.
        picContentObject: picContent
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const addResponse = await client.addImageAdvance(addImageAdvanceRequest, ossRuntime);
      console.log(1, addResponse);
    }
    demo();
  • レスポンス例
    AddImageResponse {
      requestId: 'B85D5B4A-92AF-44C1-A3FB-9B554950F8D1',
      success: true,
      message: 'success',
      code: 0,
      picInfo: AddImageResponsePicInfo { categoryId: 0, region: '167,477,221,407' }
    }

SearchImageByPic

  • コード例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXX",
      accessKeySecret: "XXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const searchImageByPicAdvanceRequest = new imagesearch.SearchImageByPicAdvanceRequest({
         // Required. The name of the Image Search instance.
        instanceName: "XXXXXXXX",
             // 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.
        picContentObject: picContent
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const searchImageByPicResponse = await client.searchImageByPicAdvance(searchImageByPicAdvanceRequest, ossRuntime);
      console.log(3, searchImageByPicResponse);
    }
    demo();
  • レスポンス例
    SearchImageByPicResponse {
      requestId: 'A38BFBD4-D7AD-4BE9-9606-72BB765A9001',
      success: true,
      code: 0,
      msg: 'success',
      auctions: [
        SearchImageByPicResponseAuctions {
          categoryId: 0,
          productId: '1000',
          picName: 'vae.jpg',
          sortExprValues: '7.33136443711219e+24;0'
        },
      ...............
        SearchImageByPicResponseAuctions {
          categoryId: 0,
          productId: '123455',
          picName: 'this',
          sortExprValues: '7.33136443711219e+24;0'
        }
      ],
      head: SearchImageByPicResponseHead {
        docsReturn: 5,
        docsFound: 5,
        searchTime: 218
      },
      picInfo: SearchImageByPicResponsePicInfo {
        categoryId: 0,
        region: '167,477,221,407',
        allCategories: [
          [SearchImageByPicResponsePicInfoAllCategories],
          ........
          [SearchImageByPicResponsePicInfoAllCategories]
        ]
      }
    }

SearchImageByName

  • コード例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const searchImageByNameRequest = new imagesearch.SearchImageByNameRequest({
         // Required. The name of the Image Search instance.
        instanceName: "XXXXXXX",
        // Required. The product ID, which can be up to 512 characters in length.
        // A product ID can correspond to multiple image names.
        productId: "123455",
        // 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.
        picName: "this"
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const searchImageByNameResponse = await client.searchImageByName(searchImageByNameRequest, ossRuntime);
      console.log(2, searchImageByNameResponse);
    }
    demo();
  • レスポンス例
    SearchImageByNameResponse {
      requestId: '4B039098-B274-41DE-9A55-32550B6F74E1',
      success: true,
      code: 0,
      msg: 'success',
      auctions: [
        SearchImageByNameResponseAuctions {
          categoryId: 0,
          productId: '1000',
          picName: 'vae.jpg',
          sortExprValues: '7.33136443711219e+24;0'
        },
          .........
        SearchImageByNameResponseAuctions {
          categoryId: 0,
          productId: '123455',
          picName: 'this',
          sortExprValues: '7.33136443711219e+24;0'
        }
      ],
      head: SearchImageByNameResponseHead {
        docsReturn: 5,
        docsFound: 5,
        searchTime: 11
      },
      picInfo: SearchImageByNameResponsePicInfo {
        categoryId: 0,
          allCategories: [
          [SearchImageByNameResponsePicInfoAllCategories],
          ......
          [SearchImageByNameResponsePicInfoAllCategories]
        ]
      }
    }

Delete

  • コード例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const deleteImageRequest = new imagesearch.DeleteImageRequest({
        // Required. The name of the Image Search instance.
        instanceName: "XXXXXXXXXXXXXXXXXX",
         // Required. The product ID, which can be up to 512 characters in length.
         // A product ID can correspond to multiple image names.
        productId: "123455",
         // 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.
        picName: "this"
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const deleteImageResponse = await client.deleteImage(deleteImageRequest, ossRuntime);
      console.log(4, deleteImageResponse);
    }
    demo();
  • レスポンス例
    DeleteImageResponse {
      requestId: '3965B44B-0584-41DE-8764-0D8B36857CE1',
      success: true,
      message: 'success',
      code: 0
    }