全部产品
云市场

Node.js SDK

更新时间:2020-04-03 16:52:31

接口列表

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

准备工作

依赖安装

安装图像搜索Node.js SDK

  1. npm install @alicloud/imagesearch@1.0.2

代码示例

Add接口

代码示例
  1. const oss = require('@alicloud/oss-util');
  2. const fs = require('fs');
  3. const imagesearch = require('@alicloud/imagesearch')
  4. const config = new imagesearch.Config({
  5. accessKeyId: "XXXXXXXXXXXXXXXXX",
  6. accessKeySecret: "XXXXXXXXXXXXXXXXXXXX",
  7. type: "access_key",
  8. endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
  9. regionId: "cn-shanghai",
  10. protocol: 'http'
  11. });
  12. const client = new imagesearch.default(config);
  13. var picContent = fs.createReadStream("D:/123.jpg");
  14. async function demo() {
  15. const addImageAdvanceRequest = new imagesearch.AddImageAdvanceRequest({
  16. // 必填,图像搜索实例名称。
  17. instanceName: "XXXXXXXXXX",
  18. // 必填,商品id,最多支持 512个字符。
  19. // 一个商品可有多张图片。
  20. productId: "123455",
  21. // 必填,图片名称,最多支持 512个字符。
  22. // 1. ProductId + PicName唯一确定一张图片。
  23. // 2. 如果多次添加图片具有相同的ProductId + PicName,以最后一次添加为准,前面添加的的图片将被覆盖。
  24. picName: "this",
  25. // 图片内容,最多支持 2MB大小图片以及5s的传输等待时间。当前仅支持jpg和png格式图片;
  26. // 对于商品、商标、通用图片搜索,图片长和宽的像素必须都大于等于200,并且小于等于1024;
  27. // 对于布料搜索,图片长和宽的像素必须都大于等于448,并且小于等于1024;
  28. // 图像中不能带有旋转信息
  29. picContentObject: picContent
  30. });
  31. let ossRuntime = new oss.RuntimeOptions({});
  32. const addResponse = await client.addImageAdvance(addImageAdvanceRequest, ossRuntime);
  33. console.log(1, addResponse);
  34. }
  35. demo();
返回结果示例
  1. 1 AddImageResponse {
  2. requestId: 'B85D5B4A-92AF-44C1-A3FB-9B554950F8D1',
  3. success: true,
  4. message: 'success',
  5. code: 0,
  6. picInfo: AddImageResponsePicInfo { categoryId: 0, region: '167,477,221,407' }
  7. }

SearchImageByPic接口

代码示例
  1. const oss = require('@alicloud/oss-util');
  2. const fs = require('fs');
  3. const imagesearch = require('@alicloud/imagesearch')
  4. const config = new imagesearch.Config({
  5. accessKeyId: "XXXXXXXX",
  6. accessKeySecret: "XXXXXXXXXX",
  7. type: "access_key",
  8. endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
  9. regionId: "cn-shanghai",
  10. protocol: 'http'
  11. });
  12. const client = new imagesearch.default(config);
  13. var picContent = fs.createReadStream("D:/123.jpg");
  14. async function demo() {
  15. const searchImageByPicAdvanceRequest = new imagesearch.SearchImageByPicAdvanceRequest({
  16. // 必填,图像搜索实例名称。
  17. instanceName: "XXXXXXXX",
  18. // 图片内容,最多支持 2MB大小图片以及5s的传输等待时间。当前仅支持jpg和png格式图片;
  19. // 对于商品、商标、通用图片搜索,图片长和宽的像素必须都大于等于200,并且小于等于1024;
  20. // 对于布料搜索,图片长和宽的像素必须都大于等于448,并且小于等于1024;
  21. // 图像中不能带有旋转信息
  22. picContentObject: picContent
  23. });
  24. let ossRuntime = new oss.RuntimeOptions({});
  25. const searchImageByPicResponse = await client.searchImageByPicAdvance(searchImageByPicAdvanceRequest, ossRuntime);
  26. console.log(3, searchImageByPicResponse);
  27. }
  28. demo();
返回结果示例
  1. 3 SearchImageByPicResponse {
  2. requestId: 'A38BFBD4-D7AD-4BE9-9606-72BB765A9001',
  3. success: true,
  4. code: 0,
  5. msg: 'success',
  6. auctions: [
  7. SearchImageByPicResponseAuctions {
  8. categoryId: 0,
  9. productId: '1000',
  10. picName: 'vae.jpg',
  11. sortExprValues: '7.33136443711219e+24;0'
  12. },
  13. ...............
  14. SearchImageByPicResponseAuctions {
  15. categoryId: 0,
  16. productId: '123455',
  17. picName: 'this',
  18. sortExprValues: '7.33136443711219e+24;0'
  19. }
  20. ],
  21. head: SearchImageByPicResponseHead {
  22. docsReturn: 5,
  23. docsFound: 5,
  24. searchTime: 218
  25. },
  26. picInfo: SearchImageByPicResponsePicInfo {
  27. categoryId: 0,
  28. region: '167,477,221,407',
  29. allCategories: [
  30. [SearchImageByPicResponsePicInfoAllCategories],
  31. ........
  32. [SearchImageByPicResponsePicInfoAllCategories]
  33. ]
  34. }0

SearchImageByName接口

代码示例
  1. const oss = require('@alicloud/oss-util');
  2. const fs = require('fs');
  3. const imagesearch = require('@alicloud/imagesearch')
  4. const config = new imagesearch.Config({
  5. accessKeyId: "XXXXXXXXXX",
  6. accessKeySecret: "XXXXXXXXXXX",
  7. type: "access_key",
  8. endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
  9. regionId: "cn-shanghai",
  10. protocol: 'http'
  11. });
  12. const client = new imagesearch.default(config);
  13. var picContent = fs.createReadStream("D:/123.jpg");
  14. async function demo() {
  15. const searchImageByNameRequest = new imagesearch.SearchImageByNameRequest({
  16. // 必填,图像搜索实例名称。
  17. instanceName: "XXXXXXX",
  18. // 必填,商品id,最多支持 512个字符。
  19. // 一个商品可有多张图片。
  20. productId: "123455",
  21. // 必填,图片名称,最多支持 512个字符。
  22. // 1. ProductId + PicName唯一确定一张图片。
  23. picName: "this"
  24. });
  25. let ossRuntime = new oss.RuntimeOptions({});
  26. const searchImageByNameResponse = await client.searchImageByName(searchImageByNameRequest, ossRuntime);
  27. console.log(2, searchImageByNameResponse);
  28. }
  29. demo();
返回结果示例
  1. 2 SearchImageByNameResponse {
  2. requestId: '4B039098-B274-41DE-9A55-32550B6F74E1',
  3. success: true,
  4. code: 0,
  5. msg: 'success',
  6. auctions: [
  7. SearchImageByNameResponseAuctions {
  8. categoryId: 0,
  9. productId: '1000',
  10. picName: 'vae.jpg',
  11. sortExprValues: '7.33136443711219e+24;0'
  12. },
  13. .........
  14. SearchImageByNameResponseAuctions {
  15. categoryId: 0,
  16. productId: '123455',
  17. picName: 'this',
  18. sortExprValues: '7.33136443711219e+24;0'
  19. }
  20. ],
  21. head: SearchImageByNameResponseHead {
  22. docsReturn: 5,
  23. docsFound: 5,
  24. searchTime: 11
  25. },
  26. picInfo: SearchImageByNameResponsePicInfo {
  27. categoryId: 0,
  28. allCategories: [
  29. [SearchImageByNameResponsePicInfoAllCategories],
  30. ......
  31. [SearchImageByNameResponsePicInfoAllCategories]
  32. ]
  33. }
  34. }

Delete接口

代码示例
  1. const oss = require('@alicloud/oss-util');
  2. const fs = require('fs');
  3. const imagesearch = require('@alicloud/imagesearch')
  4. const config = new imagesearch.Config({
  5. accessKeyId: "XXXXXXXXXX",
  6. accessKeySecret: "XXXXXXXXXXXXX",
  7. type: "access_key",
  8. endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
  9. regionId: "cn-shanghai",
  10. protocol: 'http'
  11. });
  12. const client = new imagesearch.default(config);
  13. var picContent = fs.createReadStream("D:/123.jpg");
  14. async function demo() {
  15. const deleteImageRequest = new imagesearch.DeleteImageRequest({
  16. // 必填,图像搜索实例名称。
  17. instanceName: "XXXXXXXXXXXXXXXXXX",
  18. // 必填,商品id,最多支持 512个字符。
  19. //一个商品可有多张图片。
  20. productId: "123455",
  21. // 选填,图片名称。若不指定本参数,则删除ProductId下所有图片;若指定本参数,则删除ProductId+PicName指定的图片。
  22. picName: "this"
  23. });
  24. let ossRuntime = new oss.RuntimeOptions({});
  25. const deleteImageResponse = await client.deleteImage(deleteImageRequest, ossRuntime);
  26. console.log(4, deleteImageResponse);
  27. }
  28. demo();
返回结果示例
  1. 4 DeleteImageResponse {
  2. requestId: '3965B44B-0584-41DE-8764-0D8B36857CE1',
  3. success: true,
  4. message: 'success',
  5. code: 0
  6. }