阿里云人脸识别NodeJS调用示例参考-阿里云开发者社区

开发者社区> 开发与运维> 正文

阿里云人脸识别NodeJS调用示例参考

简介: 前面分别给出了关于阿里云人脸识别Java调用示例参考、阿里云人脸识别C#调用示例参考、阿里云人脸识别Python3调用示例参考 、阿里云人脸识别PHP调用示例参考。本文主要介绍DodeJS调用阿里云人脸识别服务,参数等的获取参考阿里云人脸识别使用流程简介。

概述

前面分别给出了关于阿里云人脸识别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);

测试结果

_

版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章