最近在鼓搗人臉識別,最開始使用了百度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對象
- 結束請求