nodejs发起HTTPS,GET,POST请求Face++人脸识别

最近在鼓捣人脸识别,最开始使用了百度AI大脑的人脸识别,有nodejs的SDK包,使用起来也比较方便。但是每日有限额。找了找看到旷视(Face++)试用版本是没有限额的,虽然不保证并发,应该也够用了。
Face++接口使用https协议可以参考Nodejs官方文档:

https://nodejs.org/api/https.html#https_class_https_agent

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请求步骤
  1. 引用https
  2. 定义所要传输参数,比如data对象
  3. 定义发起请求的参数,host,path,method等,默认443端口
  4. https.request创建请求,请求内监听response
  5. 监听请求错误
  6. 写入请求数据,比如data对象
  7. 结束请求
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一颗小行星!

恰饭ing

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值