本文介绍图像搜索服务Node.js SDK的使用方法及示例。

接口列表

接口名称 接口说明
Add 增加图片。
SearchImageByPic 使用图片进行搜索。
SearchImageByName 指定名称,使用已入库的图片进行搜索。
Delete 删除图片。

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见创建AccessKey
  • 安装图像搜索Node.js SDK。
npm install @alicloud/imagesearch20201214@3.1.1 --save

Add接口

  • 代码示例
    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({
        // 必填,图像搜索实例名称。
        instanceName: "XXXXXXXXXX",
         // 必填,商品id,最多支持 512个字符。
         // 一个商品可有多张图片。
        productId: "123455",
          // 必填,图片名称,最多支持 512个字符。
          // 1. ProductId + PicName唯一确定一张图片。
          // 2. 如果多次添加图片具有相同的ProductId + PicName,以最后一次添加为准,前面添加的图片将被覆盖。
        picName: "this",
        // 图片内容,最多支持 4MB大小图片以及5s的传输等待时间。当前仅支持PNG、JPG、JPEG、BMP、GIF、WEBP、TIFF、PPM格式图片;
            
        // 对于商品、商标、通用图片搜索,图片长和宽的像素必须都大于等于100且小于等于4096;
            
        // 对于布料搜索,图片长和宽的像素必须都大于等于448且小于等于4096;
            
        // 图像中不能带有旋转信息
        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/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({
         // 必填,图像搜索实例名称。
        instanceName: "XXXXXXXX",
        // 图片内容,最多支持 4MB大小图片以及5s的传输等待时间。当前仅支持PNG、JPG、JPEG、BMP、GIF、WEBP、TIFF、PPM格式图片;
            
        // 对于商品、商标、通用图片搜索,图片长和宽的像素必须都大于等于100且小于等于4096;
            
        // 对于布料搜索,图片长和宽的像素必须都大于等于448且小于等于4096;
            
        // 图像中不能带有旋转信息
        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',
          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接口

  • 代码示例
    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({
         // 必填,图像搜索实例名称。
        instanceName: "XXXXXXX",
        // 必填,商品id,最多支持 512个字符。
        // 一个商品可有多张图片。
        productId: "123455",
        // 必填,图片名称,最多支持 512个字符。
       // 1. ProductId + PicName唯一确定一张图片。
        picName: "this"
      });
      const searchImageByNameResponse = await client.searchImageByName(searchImageByNameRequest);
      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',
          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接口

  • 代码示例
    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({
        // 必填,图像搜索实例名称。
        instanceName: "XXXXXXXXXXXXXXXXXX",
         // 必填,商品id,最多支持 512个字符。
         //一个商品可有多张图片。
        productId: "123455",
         //  选填,图片名称。若不指定本参数,则删除ProductId下所有图片;若指定本参数,则删除ProductId+PicName指定的图片。
        picName: "this"
      });
      const deleteImageResponse = await client.deleteImage(deleteImageRequest);
      console.log(4, deleteImageResponse);
    }
    demo();
  • 结果示例
    DeleteImageResponse {
      requestId: '3965B44B-0584-41DE-8764-0D8B36857CE1',
      success: true,
      message: 'success',
      code: 0
    }