nodejs发起HTTPS,GET,POST请求Face++人脸识别
最近在鼓捣人脸识别,最开始使用了百度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对象
- 结束请求
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?