人工智能,百度AI人脸识别java版
需求:人脸识别登录,人脸就需要有人脸的照片,数据库建一个字段face保存用户人脸的照片,jquery.webcam.js实现调用摄像头拍照,然后后端接受base64图片,然后与数据库的图片对比,用百度的人脸识别api对比,得出一个分数,分数大于80则登录成功。
前端代码:
<div class="login-desk" id="loginDesk3">
<div>
<div id="status">
<!-- 倒计时 -->
</div>
<div id="webcam"></div>
</div>
<span id="err_face"></span><br>
<button id="my-button" type="button" name="login" class="play">登录</button>
</div>
js代码:
<script src="${pageContext.request.contextPath }/js/webcam/jquery.webcam.min.js"></script>
<script>
var w = 320, h = 240;
var pos = 0, ctx = null, saveCB, image = [];
var canvas = document.createElement("canvas");
canvas.setAttribute('width', w);
canvas.setAttribute('height', h);
ctx = canvas.getContext("2d");
image = ctx.getImageData(0, 0, w, h);
$("#webcam").webcam({
width: w,
height: h,
mode: "callback",
swffile: "${pageContext.request.contextPath }/js/webcam/jscam_canvas_only.swf",
onTick: function(remain) {
if (0 == remain) {
$("#status").text("登陆中...");
} else {
$("#status").text(" "+remain + "秒钟后登录...");
}
},
onSave: function(data){
var col = data.split(";");
var img = image;
for(var i = 0; i < w; i++) {
var tmp = parseInt(col[i]);
img.data[pos + 0] = (tmp >> 16) & 0xff;
img.data[pos + 1] = (tmp >> 8) & 0xff;
img.data[pos + 2] = tmp & 0xff;
img.data[pos + 3] = 0xff;
pos+= 4;
}
if (pos >= 4 * w * h) {
ctx.putImageData(img,0,0);
pos = 0;
Imagedata=canvas.toDataURL().substring(22);
$.post("./testFaceLogin3", {type: "data", image: canvas.toDataURL("image/png")}, function(msg){
console.log(msg);
if(msg==1){
location.href="./user_per/index";
}else{
location.href="./login";
}
});
}
},
onCapture: function () {
webcam.save();
},
debug: function (type, string) {
console.log(type + ": " + string);
},
onLoad: function() {
console.log('加载完毕!')
var cams = webcam.getCameraList();
for(var i in cams) {
}
}
});
$(".play").click(function(){
webcam.capture(5);
});
</script>
后端java代码:
//默认传入的参数带类型等参数:data:image/png;base64,
String imgStr = req.getParameter("image");
if (null != imgStr) {
imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
}
Boolean flag = GenerateImage(imgStr, filePath, fileName);
if (flag) {
String image = "img/" + "faceLogin" + "/" + fileName;
boolean faceFlag = userService.testFaceLogin(image, session, req);
if (faceFlag) {
return "1";
}else{
session.setAttribute("login_error", "登录失败或者没有开启人脸识别功能");
return "0";
}
}else{
session.setAttribute("login_error", "登录失败或者没有开启人脸识别功能");
return "0";
}
缺点:1、谷歌浏览器开启摄像头需要https支持,IE浏览器摄像头开启无效,小编用的是360极速浏览器。
2、需要安装最新flash
3、浏览器需开通摄像头权限,不能是灰色的。
识别率很高,把自己的证件照上传,然后用有摄登录都识别出来了。
核心算法代码:
/**
* true 对比成功
* @return
*/
public static Boolean match(String img1, String img2) {
// 请求url
Boolean flag = false;
String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
try {
byte[] bytes1 = FileUtil.readFileByBytes(img1);
byte[] bytes2 = FileUtil.readFileByBytes(img2);
String image1 = Base64Util.encode(bytes1);
String image2 = Base64Util.encode(bytes2);
List<Map<String, Object>> images = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("image", image1);
map1.put("image_type", "BASE64");
map1.put("face_type", "LIVE");
map1.put("quality_control", "LOW");
map1.put("liveness_control", "NORMAL");
Map<String, Object> map2 = new HashMap<>();
map2.put("image", image2);
map2.put("image_type", "BASE64");
map2.put("face_type", "LIVE");
map2.put("quality_control", "LOW");
map2.put("liveness_control", "NORMAL");
images.add(map1);
images.add(map2);
String param = GsonUtils.toJson(images);
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = AuthService.getAuth();
String result = HttpUtil.post(url, accessToken, "application/json", param);
JSONObject resultJson = parseJSONP(result);
JSONObject scoreJson = parseJSONP(resultJson.getString("result"));
String score = scoreJson.getString("score");
Double obj1 = new Double(score);
Double obj2 = new Double(80);
int retval = obj1.compareTo(obj2);
if(retval > 0) {
flag = true;
}
else if(retval < 0) {
flag = false;
}
else {
flag = true;
}
//score 是 float 人脸相似度得分,推荐阈值80分
//{"error_code":0,"error_msg":"SUCCESS","log_id":747956922753388261,"timestamp":1552275338,"cached":0,"result":{"score":12.31431961,"face_list":[{"face_token":"e7c43e08d768787d7a3d5dac8af322f8"},{"face_token":"f7cd6a55d6b61bf643f3417d0b98cfb5"}]}}
return flag;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
系统需要整合人脸登录java版的,请加QQ490647751(需付费)。
需要源码学习的,请加QQ490647751(需付费)。