示例代码
更新时间:2020-05-17
Plain Text
1var AipFaceClient = require("baidu-aip-sdk").face;
2var APP_ID = "XXXXXXX";
3var API_KEY = "YYYYYYYYY";
4var SECRET_KEY = "ZZZZZZZZ";
5//Initialize client
6var client = new AipFaceClient(APP_ID, API_KEY, SECRET_KEY);
7
8//check image quality
9client.detect(face_image, 'BASE64', options).then(function (body) {
10 if (body.error_code != 0) {
11 console.log(JSON.stringify(body));
12 return;
13 }
14 //The thresholds below can be adjusted according to the quality of the photo
15 var face_angle = body.result.face_list[0].angle;
16 var quality = body.result.face_list[0].quality;
17 var baseAngleThreshold = 30;
18 if (Math.abs(face_angle.yaw) > baseAngleThreshold ||
19 Math.abs(face_angle.pitch) > baseAngleThreshold ||
20 Math.abs(face_angle.roll) > baseAngleThreshold ||
21 quality.occlusion.left_eye > 0.7 ||
22 quality.occlusion.right_eye > 0.7 ||
23 quality.occlusion.nose > 0.7 ||
24 quality.occlusion.mouth > 0.7 ||
25 quality.occlusion.left_cheek > 0.7 ||
26 quality.occlusion.right_cheek > 0.7 ||
27 quality.occlusion.chin_contour > 0.7) {
28 console.log('Poor quality.');
29 return;
30 }
31
32 //passed quality check; perform face search
33 var group = 'test';
34 var options = {"match_threshold": "70"};
35 client.search(task.face_image, "BASE64", group, options).then(function (body) {
36 if (body.error_code != 0) {
37 console.log(JSON.stringify(body));
38 return;
39 }
40 //If found, print the matched user (by default returns only the top match)
41 console.log(JSON.stringify(body));
42 }).catch(function(err) {
43 console.log(err);
44 });
45
46}).catch(function(err) {
47 console.log(err);
48});
