This topic shows you how to use Image Search SDK for Node.js and provides sample code.

Preparations

  • Before you install and use Alibaba Cloud SDK, make sure that you have created an Alibaba Cloud account and obtained an AccessKey pair. For more information, see Create an AccessKey pair.
  • Run the following command to install Image Search SDK for Node.js:
npm install @alicloud/imagesearch20201214@3.1.1 --save

Add

  • Sample code
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch20201214')
    const client = new imagesearch.default({
      accessKeyId: "XXXXXXXXXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'});
    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. The value can be up to 512 characters in length.
         // A product ID can correspond to multiple image names.
        productId: "123455",
          // Required. The image name. The value 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 values 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 4 MB. The transmission timeout period cannot exceed 5 seconds. Only the following image formats are supported: PNG, JPG, JPEG, BMP, GIF, WEBP, TIFF, and PPM.
            
        // For product, brand, and generic image searches, the length and the width of the image must range from 100 pixels to 4,096 pixels.
            
        // For cloth image searches, the length and the width of the image must range from 448 pixels to 4,096 pixels.
            
        // The image cannot contain rotation information.
        picContentObject: picContent
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const addResponse = await client.addImageAdvance(addImageAdvanceRequest, ossRuntime);
      console.log(1, addResponse);
    }
    demo();
  • Sample responses
    AddImageResponse {
      requestId: 'B85D5B4A-92AF-44C1-A3FB-9B554950F8D1',
      success: true,
      message: 'success',
      code: 0,
      picInfo: AddImageResponsePicInfo { categoryId: 0, region: '167,477,221,407' }
    }

SearchImageByPic

  • Sample code
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch20201214')
    const client = new imagesearch.default({
      accessKeyId: "XXXXXXXX",
      accessKeySecret: "XXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    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 4 MB. The transmission timeout period cannot exceed 5 seconds. Only the following image formats are supported: PNG, JPG, JPEG, BMP, GIF, WEBP, TIFF, and PPM.
            
        // For product, brand, and generic image searches, the length and the width of the image must range from 100 pixels to 4,096 pixels.
            
        // For cloth image searches, the length and the width of the image must range from 448 pixels to 4,096 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();
  • Sample responses
    SearchImageByPicResponse {
      requestId: 'A38BFBD4-D7AD-4BE9-9606-72BB765A9001',
      success: true,
      code: 0,
      msg: 'success',
      auctions: [
        SearchImageByPicResponseAuctions {
          categoryId: 0,
          productId: '1000',
          picName: 'vae.jpg',
          score:1.0,
          sortExprValues: '7.33136443711219e+24;0'
        },
      ...............
        SearchImageByPicResponseAuctions {
          categoryId: 0,
          productId: '123455',
          picName: 'this',
          score:1.0,
          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]
        ],
        "multiRegion": [
           [SearchImageByPicResponseBodyPicInfoMultiRegion],
          ........
          [SearchImageByPicResponseBodyPicInfoMultiRegion]
        ]
      }
    }

SearchImageByName

  • Sample code
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch20201214')
    const client = new imagesearch.default({
      accessKeyId: "XXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    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. The value can be up to 512 characters in length.
        // A product ID can correspond to multiple image names.
        productId: "123455",
        // Required. The image name. The value 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"
      });
      const searchImageByNameResponse = await client.searchImageByName(searchImageByNameRequest);
      console.log(2, searchImageByNameResponse);
    }
    demo();
  • Sample responses
    SearchImageByNameResponse {
      requestId: '4B039098-B274-41DE-9A55-32550B6F74E1',
      success: true,
      code: 0,
      msg: 'success',
      auctions: [
        SearchImageByNameResponseAuctions {
          categoryId: 0,
          productId: '1000',
          picName: 'vae.jpg',
          score:1.0,
          sortExprValues: '7.33136443711219e+24;0'
        },
          .........
        SearchImageByNameResponseAuctions {
          categoryId: 0,
          productId: '123455',
          picName: 'this',
          score:1.0,
          sortExprValues: '7.33136443711219e+24;0'
        }
      ],
      head: SearchImageByNameResponseHead {
        docsReturn: 5,
        docsFound: 5,
        searchTime: 11
      },
      picInfo: SearchImageByNameResponsePicInfo {
        categoryId: 0,
          allCategories: [
          [SearchImageByNameResponsePicInfoAllCategories],
          ......
          [SearchImageByNameResponsePicInfoAllCategories]
        ],
         "multiRegion": [
           [SearchImageByPicResponseBodyPicInfoMultiRegion],
          ........
          [SearchImageByPicResponseBodyPicInfoMultiRegion]
        ]
      }
    }

Delete

  • Sample code
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch20201214')
    const client = new imagesearch.default({
      accessKeyId: "XXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    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. The value 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"
      });
      const deleteImageResponse = await client.deleteImage(deleteImageRequest);
      console.log(4, deleteImageResponse);
    }
    demo();
  • Sample responses
    DeleteImageResponse {
      requestId: '3965B44B-0584-41DE-8764-0D8B36857CE1',
      success: true,
      message: 'success',
      code: 0
    }