您的位置:首页 > Web前端 > Node.js

nodejs 客户端拍照调用azure face api对比身份证照片进行验证

2017-11-18 20:46 423 查看
1. 注册azure账号获取azure face api key https://azure.microsoft.com/en-us/services/cognitive-services/face/ 2. javascript拍照
3. 使用获取face id api注册人脸,以及身份证照片(从数据库获得)
4. 调用find similar face api来对比两个人脸
 
实例代码:
var getFace = function (img,onsuccess) {
var faceApiUrl =
"https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false";

return $.ajax({
url: faceApiUrl,
type: 'post',
data: JSON.stringify({
url: img
}),
headers: {
"Accept": "application/json",
"Content-Type": 'application/json',
"Ocp-Apim-Subscription-Key": subscribKey
},
dataType: 'text',
success: function (data) {
if (onsuccess != null) {
onsuccess(data);
}
}, fail: function (data) {
error("GOT 1st face failed.");
error(data);
console.error(data);
}
});

};
var compareFace = function(faceId1, faceId2, onsuccess) {
var faceApiUrl =
"https://southeastasia.api.cognitive.microsoft.com/face/v1.0/findsimilars";

return $.ajax({
url: faceApiUrl,
type: 'post',
data: JSON.stringify({
"faceId": faceId1,
"faceIds": [faceId2],
"maxNumOfCandidatesReturned": 1,
"mode": "matchFace"
}),
headers: {
"Accept": "application/json",
"Content-Type": 'application/json',
"Ocp-Apim-Subscription-Key": subscribKey
},
dataType: 'text',
success: function (data) {
if (onsuccess != null) {
onsuccess(data);
}
}, fail: function (data) {
error("compare face failed.");
error(data);
console.error(data);
}
});
}

var snapshot = function () {

var videoElement = document.querySelector(".remotevideo");
if (videoElement == null) {
videoElement = document.querySelector("#localVideo");
}

var canvasVideo = document.getElementById("pnlScreen");

var contextVideo = canvasVideo.getContext('2d');
contextVideo.drawImage(videoElement, 0, 0,600,480);

var base64 = document.getElementById("pnlScreen").toDataURL("image/png");

// console.log(base64);
//
$.ajax({
url: "your url",
type: 'post',
headers: {
"Content-Type": 'application/json'
},
data: JSON.stringify({ imgData: base64}),
success: function (data) {
if (data == "ok") {
getFace(image1,
function(data) {
debug("got 1st face JSON");
debug(data);

debug("got 1st face id");
var face1 = JSON.parse(data)[0].faceId;
debug(face1);

var face2 = "";
getFace(image2,
function(d) {
debug("got 2nd face JSON");
debug(d);

face2 = JSON.parse(d)[0].faceId;
debug("got 2nd face id");
debug(face2);

debug("comparing face");
compareFace(face1,
face2,
function(r) {
debug("compare success.");
var json = JSON.parse(r)[0];
showresult("matching confidence : " + json.confidence);
});
});
});
} else {
alert(data);
}
//alert(data);
}, fail: function (data) {
alert(data);
}
});
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐