全部产品
云市场

NodeJs

更新时间:2019-10-14 12:40:39

NodeJS SDK

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问访问密钥(AccessKey)。详情参考创建AccessKey
  2. 安装Node.js SDK核心库。阿里云Node.js SDK支持的版本为8.0及以上,您可以通过以下方式安装Node.js SDK: npm install @alicloud/pop-core -S
  3. 您也可以使用cnpm、yarn等包管理工具来安装@alicloud/pop-core。

完整代码示例

  1. const Core = require('@alicloud/pop-core');
  2. var client = new Core({
  3. accessKeyId: process.env['ACCESS_KEY_ID'],
  4. accessKeySecret: process.env['ACCESS_KEY_SECRET'],
  5. endpoint: 'https://ivpd.cn-shanghai.aliyuncs.com',
  6. apiVersion: '2019-06-25',
  7. codes: new Set([200, '0', 'OK', 'Success'])
  8. });
  9. function execute(action, params) {
  10. var requestOption = {
  11. method: 'POST'
  12. };
  13. console.log("");
  14. client.request(action, params, requestOption).then((result) => {
  15. console.log(action + ".Result: " + JSON.stringify(result));
  16. }, (ex) => {
  17. console.log(action + ".Error, params:" + JSON.stringify(params));
  18. console.log(ex);
  19. })
  20. }
  21. function testSegmentImage() {
  22. var params = {
  23. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/aliyun-doc/pop/images/segment-image-src.jpg"
  24. }
  25. execute('SegmentImage', params);
  26. }
  27. function testMakeSuperResolutionImage() {
  28. var params = {
  29. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/MakeSuperResolution/sup-dog.png"
  30. }
  31. execute('MakeSuperResolutionImage', params);
  32. }
  33. function testChangeImageSize() {
  34. var params = {
  35. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/ChangeImageSize/change-image-size-src.png",
  36. "Width": 1080,
  37. "Height": 1920
  38. }
  39. execute('ChangeImageSize', params);
  40. }
  41. function testDetectImageElements() {
  42. var params = {
  43. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/DetectImageElements/detect-elements-src.png"
  44. }
  45. execute('DetectImageElements', params);
  46. }
  47. function testExtendImageStyle() {
  48. var params = {
  49. "MajorUrl": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/ExtendImageStyle/majorUrl.jpeg",
  50. "StyleUrl": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/ExtendImageStyle/styleUrl.jpeg"
  51. }
  52. execute('ExtendImageStyle', params);
  53. }
  54. function testRecognizeImageColor() {
  55. var params = {
  56. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecognizeImageColor/RecognizeImageColor.png"
  57. }
  58. execute('RecognizeImageColor', params);
  59. }
  60. function testRecognizeImageStyle() {
  61. var params = {
  62. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecognizeImageStyle/technology.png"
  63. }
  64. execute('RecognizeImageStyle', params);
  65. }
  66. function testRecolorImage() {
  67. var paramsAuto = {
  68. "Mode": "AUTO",
  69. "ColorCount": 3,
  70. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-1-src.png"
  71. }
  72. var paramsRefPic = {
  73. "Mode": "REF_PIC",
  74. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-1-src.png",
  75. "RefUrl": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-refurl-src.png"
  76. }
  77. var paramsRefTemplate = {
  78. "Mode": "TEMPLATE",
  79. "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-1-src.png",
  80. "ColorTemplate": [
  81. {"Color": "056A6B"},
  82. {"Color": "FF0000"},
  83. {"Color": "00FF00"},
  84. ]
  85. }
  86. execute('RecolorImage', paramsAuto);
  87. execute('RecolorImage', paramsRefPic);
  88. execute('RecolorImage', paramsRefTemplate);
  89. }
  90. testSegmentImage();//图像分割/抠图
  91. testMakeSuperResolutionImage();//超分辨率
  92. testChangeImageSize();//尺寸变换
  93. testDetectImageElements();//元素检测
  94. testExtendImageStyle();//风格迁移
  95. testRecognizeImageColor();//色板识别
  96. testRecognizeImageStyle();//风格识别
  97. testRecolorImage();//色彩迁移

执行(保存为IVPD.js)

  1. node ./IVPD.js

输出结果

  1. SegmentImage.Result: {"Data":{"Url":"http://luban-vgd-invi.oss-cn-hangzhou.aliyuncs.com/upload/result_parser/2019-10-14/invi_parser_015710271158311033823_7sVnQw.png?Expires=1571030715&OSSAccessKeyId=LTAI4Fc5SVvzUQ19K1Cz3Ew9&Signature=ylAAO23uKJnJ8WJmdz4dldtUQd8%3D"},"Message":"success","RequestId":"7DB95048-2258-4351-B245-FB67E4CEC6EF","Code":"0"}
  2. MakeSuperResolutionImage.Result: {"Data":{"Url":"http://ai-gateway.oss-cn-shanghai.aliyuncs.com/upload/ai-gateway_prod/ds%3D20191014/sisrx2_157102711556307517.jpg?Expires=1571631915&OSSAccessKeyId=LTAI4FeJ8qKkYn6SrHhQ1DZQ&Signature=ftQA6BgwteDyofY%2Bwj6hPMcEzEI%3D"},"Message":"Success.","RequestId":"7FA109DD-5112-4715-9D3C-FC9465ED6621","Code":"0"}
  3. ChangeImageSize.Result: {"Data":{"Url":"http://luban-vgd-invi.oss-cn-hangzhou.aliyuncs.com/upload/result_filter/2019-10-14/invi_filter_015710271161781012676_5kWkSC.png?Expires=1571030716&OSSAccessKeyId=LTAI4Fc5SVvzUQ19K1Cz3Ew9&Signature=PhDUv%2FwxcArwzm2jNyS5EIhea8A%3D"},"Message":"success","RequestId":"AB68E125-69BD-4543-83EF-1D93BBADAC69","Code":"0"}
  4. DetectImageElements.Result: {"Data":{"Elements":[{"Type":"majorhuman","Height":354,"Width":285,"Y":78,"Score":"0.997097373008728","X":287}]},"Message":"success","RequestId":"6F358722-AAC9-4BC1-85C4-6FCA39C70FFA","Code":"0"}
  5. ExtendImageStyle.Result: {"Data":{"Url":"http://luban-vgd-invi.oss-cn-hangzhou.aliyuncs.com/upload/result_/2019-10-14/invi__015710271156451012674_LbidJx.jpg?Expires=1571030715&OSSAccessKeyId=LTAI4Fc5SVvzUQ19K1Cz3Ew9&Signature=nXZpF101BqKmIIkqycwwnktY3Ps%3D","MajorUrl":""},"Message":"success","RequestId":"EF71C2E8-1D30-4304-962E-3246E0206BC5","Code":"0"}
  6. RecognizeImageColor.Result: {"Data":{"ColorTemplateList":[{"Percentage":"0.535937","Label":"blue","Color":"6CA2DC"},{"Percentage":"0.305331","Label":"yellow","Color":"B58D22"},{"Percentage":"0.158732","Label":"gray","Color":"323C46"}]},"Message":"success","RequestId":"60FC2287-90C2-403E-B42D-24044D8573D8","Code":"0"}
  7. RecognizeImageStyle.Result: {"Data":{"Styles":["technology","strong"]},"Message":"success","RequestId":"45F3306C-711C-4E65-A868-4C73111B1FEA","Code":"0"}
  8. RecolorImage.Result: {"Data":{"ImageList":["http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-1-src.png"]},"Message":"success","RequestId":"82056BF7-535D-4DF7-8C5C-822FC96E4A49","Code":"0"}
  9. RecolorImage.Result: {"Data":{"ImageList":["http://luban-vgd-invi.oss-cn-hangzhou.aliyuncs.com/upload/result_filter/2019-10-14/invi_filter_015710271159711012675_n9Jj2U.png?Expires=1571113515&OSSAccessKeyId=LTAI4Fc5SVvzUQ19K1Cz3Ew9&Signature=50bx2pY2Y%2FyU%2Bc3EHTymEMi%2BgNY%3D"]},"Message":"success","RequestId":"60FA107F-5B3E-48A9-B771-7F8792643421","Code":"0"}
  10. RecolorImage.Result: {"Data":{"ImageList":["http://luban-vgd-invi.oss-cn-hangzhou.aliyuncs.com/upload/result_filter/2019-10-14/invi_filter_015710271155631012673_oYUVdc.png?Expires=1571113515&OSSAccessKeyId=LTAI4Fc5SVvzUQ19K1Cz3Ew9&Signature=yijcmYE%2BSQPEkpbL1rFaS%2F6Iwng%3D"]},"Message":"success","RequestId":"E664D7D5-057E-4FEE-8490-47161B6622F0","Code":"0"}