Ionic实战七:实现人脸识别认证方案-爱代码爱编程
项目的需求是实现人脸对比及活体检测功能,花钱越少越好,一开始考虑的是移动端的H5方案,看了下需要对接公安的api接口,这个价格太高了,放弃了,最终是移动端拍照上传,调用百度api实现人脸对比及活体检测功能。
- ionic中接入相机插件后实现拍照方法如下:
ionic cordova plugin add cordova-plugin-camera
npm install --save @ionic-native/camera@4 - 在config.xml中的ios平台中加入权限
<config-file parent="NSCameraUsageDescription" platform="ios" target="*-Info.plist">
<string>Need Camera Access To Take Pictures</string>
</config-file>
- 在app.module.ts中引入:
import { Camera, CameraOptions } from '@ionic-native/camera';
在providers中加入:Camera,
- 在按钮点击主页面中引入:
import { Camera, CameraOptions } from '@ionic-native/camera';
private camera: Camera,
以点击按钮调用弹框为例:
//底部弹窗拍照或者相册选取
presentActionSheet() {
const actionSheet = this.actionSheetCtrl.create({
// title: '选择照片',
buttons: [
{
text: '拍照',
handler: () => {
this.pickPicture(1);
}
}, {
text: '相册选取',
handler: () => {
this.pickPicture(0);
}
}
]
});
actionSheet.present();
}
pickPicture(source: Number) {
debugger;
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.DATA_URL,
correctOrientation: true,
targetWidth: 200,
targetHeight: 200,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
saveToPhotoAlbum:true,
sourceType: Number(source)
}
this.camera.getPicture(options).then((imageData) => {
alert(imageData);
console.log("photo data:"+imageData);
// this.addPicData(imageData);
}, (err) => {
console.log(err);
});
}
- 拍照后拿到图片base64的值,然后访问后端接口,具体案例如下:
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.FaceVerifyRequest;
import com.baidu.aip.face.MatchRequest;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author: yingch
* @date: 2020/8/10 13:51
* @description:集成百度人脸实名认证功能之人脸检测
*
* <!--百度人脸认证-->
* <dependency>
* <groupId>com.baidu.aip</groupId>
* <artifactId>java-sdk</artifactId>
* <version>4.15.1</version>
* </dependency>
*/
public class SampleDemo {
//设置APPID/AK/SK
public static final String APP_ID = "XXX";
public static final String API_KEY = "XXX";
public static final String SECRET_KEY = "XXX";
public static void main(String[] args) {
// 初始化一个AipFace
AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
//尹工戴口罩图片
String image = "XXX";
//二次翻拍照片
String image1 = "XXX";
//钟哥人脸图
String image2="XXX";
String image3 =“XXX”;
String imageType = "BASE64";
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
options.put("face_field", "age");
options.put("max_face_num", "2");
options.put("face_type", "LIVE");
options.put("liveness_control", "LOW");
System.out.println("------------------初始化3张照片:image是戴口罩图片;image1是二次翻拍的图片;image2是钟哥人脸;image3是手机拍摄的未处理的图片--------------------");
// 人脸检测 检测当前照片是否是人脸照片
JSONObject res = client.detect(image, imageType, options);
JSONObject res0 = client.detect(image2, imageType, options);
System.out.println("------------------1.检测image是否是人脸照片--------------------");
System.out.println(res.toString(2));
System.out.println("------------------1.检测image2是否是人脸照片--------------------");
System.out.println(res0.toString(2));
/*人脸比对功能*/
System.out.println("---------------2.人脸对比功能(image与image1)-------------");
MatchRequest req1 = new MatchRequest(image, "BASE64");
MatchRequest req2 = new MatchRequest(image1, "BASE64");
ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
requests.add(req1);
requests.add(req2);
JSONObject res1 = client.match(requests);
System.out.println(res1.toString(2));
System.out.println("------------2.人脸对比功能(image与image2)-----------");
MatchRequest req11 = new MatchRequest(image, "BASE64");
MatchRequest req22 = new MatchRequest(image2, "BASE64");
ArrayList<MatchRequest> requests1 = new ArrayList<MatchRequest>();
requests1.add(req11);
requests1.add(req22);
JSONObject res11 = client.match(requests1);
System.out.println(res11.toString(2));
/*在线活体检测*/
FaceVerifyRequest req3 = new FaceVerifyRequest(image, "BASE64");
ArrayList<FaceVerifyRequest> list3 = new ArrayList<FaceVerifyRequest>();
list3.add(req3);
JSONObject res3 = client.faceverify(list3);
System.out.println("------------------3.是否是活体(image)--------------------");
System.out.println(res3.toString(2));
FaceVerifyRequest req4 = new FaceVerifyRequest(image3, "BASE64");
ArrayList<FaceVerifyRequest> list4 = new ArrayList<FaceVerifyRequest>();
list4.add(req4);
JSONObject res4 = client.faceverify(list4);
System.out.println("------------------4.是否是活体(image3)--------------------");
System.out.println(res4.toString(2));
}
}
Java-SDK参考:https://ai.baidu.com/ai-doc/FACE/8k37c1rqz
AppID、 API Key、 Secret Key值获取如下:
- 下面看下代码运行数据:
------------------初始化3张照片:image是戴口罩图片;image1是二次翻拍的图片;image2是经过处理的钟哥人脸;image3是手机拍照获取的未处理数据--------------------
------------------1.检测image是否是人脸照片--------------------
{
"result": null,
"log_id": 1589651015594,
"error_msg": "pic not has face",
"cached": 0,
"error_code": 222202,
"timestamp": 1597050702
}
------------------1.检测image2是否是人脸照片--------------------
{
"result": {
"face_num": 1,
"face_list": [{
"liveness": {"livemapscore": 1},
"angle": {
"roll": -1.2,
"pitch": 8.01,
"yaw": 0.93
},
"face_token": "cd1ae488cb9e53a57853cdb48b0f429f",
"location": {
"top": 51.65,
"left": -0.4,
"rotation": 1,
"width": 140,
"height": 148
},
"face_probability": 0.99,
"age": 30
}]
},
"log_id": 3589201001101,
"error_msg": "SUCCESS",
"cached": 0,
"error_code": 0,
"timestamp": 1597050703
}
------------------2.人脸对比功能(image与image1)--------------------
{
"result": {
"score": 82.12217712,
"face_list": [
{"face_token": "c3b6388aaa8280fabc6f82bda8a7115f"},
{"face_token": "0d269bf33d2393084ed34d9ba4eb22a4"}
]
},
"log_id": 3515845510199,
"error_msg": "SUCCESS",
"cached": 0,
"error_code": 0,
"timestamp": 1597050703
}
------------------2.人脸对比功能(image与image2)--------------------
{
"result": {
"score": 23.42041397,
"face_list": [
{"face_token": "c3b6388aaa8280fabc6f82bda8a7115f"},
{"face_token": "cd1ae488cb9e53a57853cdb48b0f429f"}
]
},
"log_id": 5520145798445,
"error_msg": "SUCCESS",
"cached": 0,
"error_code": 0,
"timestamp": 1597050703
}
------------------3.是否是活体(image)--------------------
{
"result": {
"thresholds": {
"frr_1e-3": 0.3,
"frr_1e-2": 0.9,
"frr_1e-4": 0.05
},
"face_liveness": 0.03,
"face_list": [{
"liveness": {"livemapscore": 0.03},
"angle": {
"roll": -1.4,
"pitch": 1.48,
"yaw": -5.01
},
"face_token": "c3b6388aaa8280fabc6f82bda8a7115f",
"location": {
"top": 39.92,
"left": 12,
"rotation": 1,
"width": 54,
"height": 59
},
"face_probability": 0.99
}]
},
"log_id": 1510184841010,
"error_msg": "SUCCESS",
"cached": 0,
"error_code": 0,
"timestamp": 1597050703
}
------------------4.是否是活体(image3)--------------------
{
"result": {
"thresholds": {
"frr_1e-3": 0.3,
"frr_1e-2": 0.9,
"frr_1e-4": 0.05
},
"face_liveness": 1,
"face_list": [{
"liveness": {"livemapscore": 1},
"angle": {
"roll": 1.07,
"pitch": 7,
"yaw": -2.85
},
"face_token": "c48f9bd50144972f24c06cf19559b256",
"location": {
"top": 76.99,
"left": 27.6,
"rotation": 4,
"width": 57,
"height": 63
},
"face_probability": 1
}]
},
"log_id": 9975949975001,
"error_msg": "SUCCESS",
"cached": 0,
"error_code": 0,
"timestamp": 1597050703
}
说明:
上述代码中将所有的人脸图片base64数据用XXX代替,拍照打印出照片的人脸数据后代替即可。
1.检测是否是人脸根据error_msg判断
2.人脸对比根据score值判断,越接近100为同一个人的概率越大
3.活体检测根据face_liveness值判断,值越接近1表明是活体的概率越大
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/qq_16497617/article/details/107918893