阿里云人脸识别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);

测试结果

_

相关文章
|
8月前
|
机器学习/深度学习 搜索推荐 计算机视觉
【阿里云OpenVI-人脸感知理解系列之人脸识别】基于Transformer的人脸识别新框架TransFace ICCV-2023论文深入解读
本文介绍 阿里云开放视觉智能团队 被计算机视觉顶级国际会议ICCV 2023接收的论文 "TransFace: Calibrating Transformer Training for Face Recognition from a Data-Centric Perspective"。TransFace旨在探索ViT在人脸识别任务上表现不佳的原因,并从data-centric的角度去提升ViT在人脸识别任务上的性能。
1689 341
|
10月前
|
存储 前端开发 Serverless
阿里云视觉智能平台提供了人脸识别和图像搜索的API接口
阿里云视觉智能平台提供了人脸识别和图像搜索的API接口
1274 0
|
11月前
|
人工智能 达摩院 计算机视觉
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(上)
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(上)
659 0
|
11月前
|
人工智能 计算机视觉
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(下)
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(下)
601 0
|
人工智能 计算机视觉
阿里云产品体系分为6大分类——人工智能——分为10种模块——人脸识别
阿里云产品体系分为6大分类——人工智能——分为10种模块——人脸识别自制脑图
95 1
|
人工智能 文字识别 开发工具
阿里云视觉智能开放平台离线人脸识别SDK开启邀测啦
阿里云视觉智能开放平台离线人脸识别SDK开启邀测,端侧SDK是一套完整的包含高性能检测、鲁棒性识别、多规格活体以及高速比对等功能的人脸识别全链路SDK。应用了多项顶会论文以及核心专利技术,在受限端侧计算资源下具备速度快、精度高等特点,典型应用场景有门禁、考勤等,注册好后可在无网条件下使用。端侧SDK支持Android、IOS终端系统。
456 0
阿里云视觉智能开放平台离线人脸识别SDK开启邀测啦
|
人工智能 算法 Java
阿里云视觉智能开放平台--人脸识别使用教程
人脸人体识别技术是基于阿里云深度学习算法,结合图像或视频的人脸检测、分析、比对以及人体检测等技术,为您提供人脸人体的检测定位、人脸属性识别和人脸比对等能力。可以为开发者和企业提供高性能的在线API服务,应用于人脸AR、人脸识别和认证、大规模人脸检索、照片管理等各种场景。
阿里云视觉智能开放平台--人脸识别使用教程
|
API 开发工具 对象存储
阿里云视觉智能开放平台--人脸识别使用教程(使用本地图片)
前面在博客:阿里云视觉智能开放平台--人脸识别使用教程 介绍了如何在智能视觉开放平台使用人脸识别的接口,示例主要演示了1:N人脸查找的使用流程,使用的是OSS的图片,发现很多同学对本地图片的使用疑问较多,这里以人脸属性识别API为例演示如何使用本地图片。
2623 0
|
开发工具 计算机视觉
阿里云视觉智能开放平台--人脸识别使用教程(使用本地图片)
前面在博客:阿里云视觉智能开放平台--人脸识别使用教程 介绍了如何在智能视觉开放平台使用人脸识别的接口,示例主要演示了1:N人脸查找的使用流程,使用的是OSS的图片,发现很多同学对本地图片的使用疑问较多,这里以人脸属性识别API为例演示如何使用本地图片。
1091 0
阿里云视觉智能开放平台--人脸识别使用教程(使用本地图片)
|
机器学习/深度学习 算法 Java
阿里云视觉智能开放平台--人脸识别使用教程
人脸人体识别技术是基于阿里云深度学习算法,结合图像或视频的人脸检测、分析、比对以及人体检测等技术,为您提供人脸人体的检测定位、人脸属性识别和人脸比对等能力。可以为开发者和企业提供高性能的在线API服务,应用于人脸AR、人脸识别和认证、大规模人脸检索、照片管理等各种场景。
2877 0

热门文章

最新文章