百度 人脸检测api

官方文档:https://ai.baidu.com/docs#/Begin/top

首先获取access-token:

// 设置APPID/AK/SK
var APP_ID = "xxx";
var API_KEY = "xxx";
var SECRET_KEY = "xxx";
    
var options='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+API_KEY+'&client_secret='+SECRET_KEY+'&';

var req=https.request(options,function(res) { 

  res.on('data', function(d)  {
     // process.stdout.write(d);
    access_token+=d;
  });

  res.on("end",function(){
      console.log(access_token);
  }
})
req.end(function(){
     console.log("end");
 });

 

发送携带access-tocken参数的人脸检测请求

方法1:

var AipFaceClient = require("baidu-aip-sdk").face;
var fs = require('fs');
var https=require('https');
var querystring = require('querystring');
var async=require('async');
// var url=require('url');
// var urlencode = require('urlencode');

     access_token='XXX';
    var images = fs.readFileSync('../public/images/r.jpg');

    var base64Img = new Buffer(images).toString('base64');
//console.log(base64Img);
 //base64Img=urlencode(base64Img);
var postData=querystring.stringify({ image:base64Img }); var option={ host:'aip.baidubce.com', path:'/rest/2.0/face/v2/detect?access_token='+access_token, method:'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData) } }; var req=https.request(ur,function(res) { res.on('data', function(d) { process.stdout.write(d); }); res.on('end',function(){ console.log('接收完毕'); }); }); req.on('error', function(e) { console.error(e); }); req.write(postData); req.end(function(){ console.log('2end'); });

方法2:npm install  baidu-aip-sdk

var AipFaceClient = require("baidu-aip-sdk").face;

var fs = require('fs');

//设置APPID/AK/SK
var APP_ID = "xxx";
var API_KEY = "xxx";
var SECRET_KEY = "xxx";

var client = new AipFaceClient(APP_ID, API_KEY, SECRET_KEY);


var image = fs.readFileSync('../public/images/3.jpg');
var base64Img = new Buffer(image).toString('base64');

client.detect(base64Img,{max_face_num: 1,face_fields:'age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities'}).then(function(result) {
    console.log(JSON.stringify(result));
});

 

转载于:https://www.cnblogs.com/BlingSun/p/7809270.html

你可能感兴趣的:(百度 人脸检测api)