最近在鼓捣人脸识别,最开始使用了百度AI大脑的人脸识别,有nodejs的SDK包,使用起来也比较方便。但是每日有限额。找了找看到旷视(Face++)试用版本是没有限额的,虽然不保证并发,应该也够用了。
Face++接口使用https协议可以参考Nodejs官方文档:
const https = require('https');
const querystring = require('querystring');
var data = querystring.stringify({
api_key:"",
api_secret:"",
image_url:""//图片地址,接口支持base64,图片地址和imagefile
})
const options = {
host:'api-cn.faceplusplus.com',
path:'/facepp/v3/detect',
method:'POST',
headers:{
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'
}
}
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
console.log(''+d);//将buffer转为字符串或者使用d.toString()
let b = JSON.parse(''+d);//将buffer转成JSON
console.log(b.image_id);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(data);
req.end();
返回结果为:
{
"image_id": "SSoY2Bnir0iDPGdnKR6XzA==",
"request_id": "1507806917,f98fb976-73c1-4788-817d-2ba2532b612f",
"time_used": 330,
"faces": [{ "face_rectangle": { "width": 217, "top": 232, "left": 379, "height": 217 }, "face_token": "c5bead2221a6a7d9b7e5134d15d03c5a" }] }
faces数组为图片中检测到的人脸
nodejs发起POST请求步骤
- 引用https
- 定义所要传输参数,比如data对象
- 定义发起请求的参数,host,path,method等,默认443端口
- https.request创建请求,请求内监听response
- 监听请求错误
- 写入请求数据,比如data对象
- 结束请求