Node.js SDK 使用方法及代码示例

Node.js SDK 使用方法及代码示例
镜像下载、域名解析、时间同步请点击 阿里巴巴开源镜像站

一、准备工作

在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见创建AccessKey

  • 安装Node.js SDK核心库。阿里云Node.js SDK支持的版本为8.0及以上,您可以通过npm install @alicloud/pop-core -S方式安装Node.js SDK。
  • 您也可以使用cnpm、yarn等包管理工具来安装@alicloud/pop-core。
  • 执行以下命令,安装相应的包。

    • 人脸人体:npm install @alicloud/facebody-2019-12-30
    • 文字识别:npm install @alicloud/ocr-2019-12-30
    • 商品理解:npm install @alicloud/goodstech-2019-12-30
    • 内容安全:npm install @alicloud/imageaudit-2019-12-30
    • 图像识别:npm install @alicloud/imagerecog-2019-09-30
    • 图像增强:npm install @alicloud/imageenhan-2019-09-30
    • 图像分割:npm install @alicloud/imageseg-2019-12-30
    • 目标检测:npm install @alicloud/objectdet-2019-12-30
    • 图像搜索:npm install @alicloud/imagesearch-2020-03-20
    • 视频生产:npm install @alicloud/videoenhan-2020-03-20
    • 视频理解:npm install @alicloud/videorecog-2020-03-20
    • 视频分割:npm install @alicloud/videoseg-2020-03-20

二 、代码示例

1. 图像增强代码示例

const Client = require('@alicloud/imageenhan-2019-09-30');
// 创建实例
const client = new Client({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  securityToken: '', 
  endpoint: 'https://imageenhan.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
//超分辨率
client.makeSuperResolutionImage({
        "Url": "http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/aliyun-doc/pop/images/segment-image-src.jpg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + data);
}, function (err) {
  console.log('Error:' + err);
});
//尺寸变换
client.changeImageSize({
        "Url": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png",
        "Width": 800,
        "Height": 600
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
//风格迁移
client.extendImageStyle({
        "MajorUrl": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ExtendImageStyle/majorUrl.jpeg",
        "StyleUrl": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ExtendImageStyle/styleUrl.jpeg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
//色彩迁移
client.recolorImage({
        "Mode": "AUTO",
        "ColorCount": 3,
        "Url": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
client.recolorImage({
        "Mode": "REF_PIC",
        "Url": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png",
        "RefUrl": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-2-src.png"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
client.recolorImage({
        "Mode": "TEMPLATE",
        "Url": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png",
        "ColorTemplate": [
           {"Color": "056A6B"},
           {"Color": "FF0000"},
           {"Color": "00FF00"},
        ]
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

2. 图像识别代码示例

const Client = require('@alicloud/imagerecog-2019-09-30');
// 创建实例
const client = new Client({
    accessKeyId: 'accessKeyId',
    accessKeySecret: 'accessKeySecret',
securityToken: '', // 支持STS
endpoint: 'https://imagerecog.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
//元素识别
client.detectImageElements({
        "Url": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/DetectImageElements/detect-elements-src.png"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
//颜色识别
client.recognizeImageColor({
        "Url": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeImageColor/RecognizeImageColor.png"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
//风格识别
client.recognizeImageStyle({
        "Url": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeImageStyle/technology.png"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

3. 人脸识别代码示例

const Client = require('@alicloud/facebody-2019-12-30');
// 创建实例
const client = new Client({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  securityToken: '', // 支持STS
  endpoint: 'https://facebody.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
//人脸检测定位
const data = client.detectFace({
        "ImageURL": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

4. 商品理解代码示例

const Client = require('@alicloud/goodstech-2019-12-30');
// 创建实例
const client = new Client({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  securityToken: '', // 支持STS
  endpoint: 'https://goodstech.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
// 商品分类
const data = client.classifyCommodity({
        "ImageURL": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

5. 内容安全代码示例

const Client = require('@alicloud/imageaudit-2019-12-30');
// 创建实例
const client = new Client({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  securityToken: '', // 支持STS
  endpoint: 'https://imageaudit.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
// 商品分类
const data = client.scanImage({
        "Scene":["porn"],
        "Task":[{"DataId":"7B380256-791D-4245-891A-14958DFFACBE", "ImageURL":"https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png"}]
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

6. 图像分割代码示例

const Client = require('@alicloud/imageseg-2019-12-30');
// 创建实例
const client = new Client({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  securityToken: '', // 支持STS
  endpoint: 'https://imageseg.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
//图像裁剪
const data = client.segmentCommonImage({
        "ImageURL": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

7. 目标检测代码示例

const Client = require('@alicloud/objectdet-2019-12-30');
// 创建实例
const client = new Client({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  securityToken: '', // 支持STS
  endpoint: 'https://objectdet.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
// 主体检测
const data = client.detectMainBody({
        "ImageURL": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

8. 文字识别代码示例

const Client = require('@alicloud/ocr-2019-12-30');
// 创建实例
const client = new Client({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  securityToken: '', // 支持STS
  endpoint: 'https://ocr.cn-shanghai.aliyuncs.com'
});
// 发起调用
// 具体API,返回Promise
// 车牌识别
const data = client.recognizeLicensePlate({
        "ImageURL": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeLicensePlate/licensePlate.jpg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
// 银行卡识别
client.recognizeBankCard({
        "ImageURL": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeBankCard/bankcard.jpg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});
// 身份证识别
client.recognizeIdentityCard({
        "Side":"face",
        "ImageURL": "https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeIdentityCard/identityCard.jpg"
}, {timeout: 10000}).then(function (data) {
  console.log('Result:' + JSON.stringify(data));
}, function (err) {
  console.log('Error:' + err);
});

阿里巴巴开源镜像站 提供全面,高效和稳定的系统镜像、应用软件下载、域名解析和时间同步服务。”

上一篇:设计模式6大原则


下一篇:让外卖小哥15分钟训练出AI模型的深思平台,如何让传统企业轻松拥抱AI?