Node.js SDK 使用方法及代码示例
本文来自于【阿里云官方镜像站:https://developer.aliyun.com/mirror/?utm_content=g_1000307095 】
原文链接:https://developer.aliyun.com/article/754176?spm=a2c6h.12873581.0.0.54c47e46mLfYep
镜像下载、域名解析、时间同步请点击 阿里巴巴开源镜像站
一、准备工作
在安装和使用阿里云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: '', // 支持STSendpoint: '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);
});