本文介绍了如何使用阿里云视觉智能开放平台相关服务的Node.js SDK,具体包括SDK的获取和安装方法以及SDK代码示例。

准备工作

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

图像增强代码示例

// 引入SDK
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);
});
            

图像识别代码示例

// 引入SDK
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);
});
            

人脸识别代码示例

// 引入SDK
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);
});
            

商品理解代码示例

// 引入SDK
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);
});
            

内容安全代码示例

// 引入SDK
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);
});
            

图像分割代码示例

// 引入SDK
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);
});
            

目标检测代码示例

// 引入SDK
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);
});
            

文字识别代码示例

// 引入SDK
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);
});