> 作者:俏巴
概述
前面分别给出了关于阿里云人脸识别Java调用示例参考、阿里云人脸识别C#调用示例参考、阿里云人脸识别Python3调用示例参考 、阿里云人脸识别PHP调用示例参考。本文主要介绍DodeJS调用阿里云人脸识别服务,参数等的获取参考阿里云人脸识别使用流程简介。
Code Sample
1、使用本地图片
var request = require('request'); var url = require('url'); var crypto = require('crypto'); var date = new Date().toUTCString() // 这里填写AK和请求 var ak_id = '********'; var ak_secret = '********'; // 读取本地图片并转base64编码 var fs = require("fs"); var filePath = "time.jpeg"; var imageData = fs.readFileSync(filePath); // 例:fileUrl="D:\\test\\test.bmp" var imageBase64 = imageData.toString("base64"); var options = { url : 'https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute', method: 'POST', body: '{"type": "1", "content":"'+imageBase64+'"}', headers: { 'accept': 'application/json', 'content-type': 'application/json', 'date': date, 'Authorization': '' } }; md5 = function(buffer) { var hash; hash = crypto.createHash('md5'); hash.update(buffer); return hash.digest('base64'); }; sha1 = function(stringToSign, secret) { var signature; return signature = crypto.createHmac('sha1', secret).update(stringToSign).digest().toString('base64'); }; // step1: 组stringToSign [StringToSign = #{method}\\n#{accept}\\n#{data}\\n#{contentType}\\n#{date}\\n#{action}] var body = options.body || ''; var bodymd5; if(body === void 0 || body === ''){ bodymd5 = body; } else { bodymd5 = md5(new Buffer(body)); } console.log(bodymd5) var stringToSign = options.method + "\n" + options.headers.accept + "\n" + bodymd5 + "\n" + options.headers['content-type'] + "\n" + options.headers.date + "\n" + url.parse(options.url).path; console.log("step1-Sign string:", stringToSign); // step2: 加密 [Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )] var signature = sha1(stringToSign, ak_secret); // console.log("step2-signature:", signature); // step3: 组authorization header [Authorization = Dataplus AccessKeyId + ":" + Signature] var authHeader = "Dataplus " + ak_id + ":" + signature; console.log("step3-authorization Header:", authHeader); options.headers.Authorization = authHeader; console.log('authHeader', authHeader); // step4: send request function callback(error, response, body) { if (error) { console.log("error", error) } console.log("step4-response body:", response.statusCode, body) } request(options, callback);
2、使用网络图片
var request = require('request'); var url = require('url'); var crypto = require('crypto'); var date = new Date().toUTCString() // 这里填写AK和请求 var ak_id = '********'; var ak_secret = '********'; var options = { url : 'https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute', method: 'POST', body: '{"type": "0", "image_url":"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1553926699&di=3e4484731c8897c57e67b3f632801f9a&src=http://b-ssl.duitang.com/uploads/item/201603/28/20160328121906_ErzAB.jpeg"}', headers: { 'accept': 'application/json', 'content-type': 'application/json', 'date': date, 'Authorization': '' } }; md5 = function(buffer) { var hash; hash = crypto.createHash('md5'); hash.update(buffer); return hash.digest('base64'); }; sha1 = function(stringToSign, secret) { var signature; return signature = crypto.createHmac('sha1', secret).update(stringToSign).digest().toString('base64'); }; // step1: 组stringToSign [StringToSign = #{method}\\n#{accept}\\n#{data}\\n#{contentType}\\n#{date}\\n#{action}] var body = options.body || ''; var bodymd5; if(body === void 0 || body === ''){ bodymd5 = body; } else { bodymd5 = md5(new Buffer(body)); } console.log(bodymd5) var stringToSign = options.method + "\n" + options.headers.accept + "\n" + bodymd5 + "\n" + options.headers['content-type'] + "\n" + options.headers.date + "\n" + url.parse(options.url).path; console.log("step1-Sign string:", stringToSign); // step2: 加密 [Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )] var signature = sha1(stringToSign, ak_secret); // console.log("step2-signature:", signature); // step3: 组authorization header [Authorization = Dataplus AccessKeyId + ":" + Signature] var authHeader = "Dataplus " + ak_id + ":" + signature; console.log("step3-authorization Header:", authHeader); options.headers.Authorization = authHeader; console.log('authHeader', authHeader); // step4: send request function callback(error, response, body) { if (error) { console.log("error", error) } console.log("step4-response body:", response.statusCode, body) } request(options, callback);