SpringBoot實(shí)現(xiàn)人臉識別功能
閱讀本文大概需要 6 分鐘。
來自:blog.csdn.net/lw1124052197/article/details/106077837
前言
需求分析
一、人臉注冊
二、人臉登錄
具體實(shí)現(xiàn)
一、人臉注冊
html>
<html>
<head>
<meta?charset="UTF-8">
<title>Insert?title?heretitle>
?<style?type="text/css">
???/**解決瀏覽器兼容性問題**/
??*{margin:?0;padding:?0;}
??html,body{width:?100%;height:?100%;}/**/
??body{background:?url(img/bg03.jpg)?no-repeat?center;}
??h1{color:?#fff;text-align:?center;line-height:?80px;}
??.media{width:?534px;height:?400px;margin:?40px?auto?0;
????}
??#register{width:?200px;height:50px;background-color:?#2196f3;?margin:?60px?auto?0;
??text-align:?center;line-height:?50px;color:?#fff;border-radius:?10px;}
??#canvas{display:?none;}
??#shuru{width:?200px;height:50px;background-color:?#2196f3;?margin:?20px?auto?0;}
?style>
head>
<body>
?<h1>百度云人臉注冊h1>
?<div?id="shuru">
?用戶名:
?<input?type="text"?name="username"?id="username"/>
?div>
?
?<div?class="media">
??<video?id="video"?width="450"?height="300"?src=""?autoplay>video>
??<canvas?id="canvas"?width="450"?height="300">canvas>
??
?div>
?<button?id="register"?>確定注冊button>
?<script?type="text/javascript"?src="js/jquery-3.3.1.js">script>
?<script?type="text/javascript">
?/**調(diào)用攝像頭,獲取媒體視頻流**/
?var?video?=?document.getElementById('video');
?//返回畫布二維畫圖環(huán)境
?var?userContext?=?canvas.getContext("2d");
?var?getUserMedia?=?
??//瀏覽器兼容,表示在火狐、Google、IE等瀏覽器都可正常支持
??(navigator.getUserMedia?||?navigator.webkitGetUserMedia?||?navigator.mozGetUserMedia?||?navigator.msGetUserMedia)
??//getUserMedia.call(要調(diào)用的對象,約束條件,調(diào)用成功的函數(shù),調(diào)用失敗的函數(shù))
??getUserMedia.call(navigator,{video:?true,audio:?false},function(localMediaStream){
???//獲取攝像頭捕捉的視頻流
???video.srcObject=localMediaStream;
??},function(e){
???console.log("獲取攝像頭失敗!!")
??});
?//點(diǎn)擊按鈕注冊事件
??var?btn?=?document.getElementById("register");
?
?btn.onclick?=?function?()?{
??var?username?=?$("#username").val();
??alert($("#username").val());
???if(username?!=?null){
????//點(diǎn)擊按鈕時(shí)拿到登陸者面部信息
?????????????userContext.drawImage(video,0,0,450,300);
?????????????var?userImgSrc?=?document.getElementById("canvas").toDataURL("img/png");
?????????????//拿到bash64格式的照片信息
?????????????var?faceBase?=?userImgSrc.split(",")[1];
?????????????
?????????????//ajax異步請求
?????????????$.ajax({
??????????????url:?"register",
??????????????type:?"post",
??????????????data:?{"faceBase":?faceBase,
???????????????"userName":?username
??????????????},
??????????????success:?function(result){
???????????????if(result?===?'1'){
????????????????alert("注冊成功!!,點(diǎn)擊確認(rèn)跳轉(zhuǎn)至登錄頁面");
????????????????window.location.href="toLogin";
???????????????}else?if(result?===?'2'){
????????????????alert("您已經(jīng)注冊過啦!!");
???????????????}else{
????????????????alert("系統(tǒng)錯(cuò)誤!!");
???????????????}
???????????????
??????????????}
?????????????})
???}else{
????alert("用戶名不能為空");
???}
???????????
???????}?
?script>
body>
html>

????private?static?final?String?APP_ID?=?"****";
?
?private?static?final?String?API_KEY?=?"*******";
?
?private?static?final?String?SECRET_KEY?=?"*******";
?
?@Autowired
?private?IUserService?userService;
?
?
?@RequestMapping(value?=?"register",method?=?RequestMethod.POST)
?public?String?register(String?userName,String?faceBase)?throws?IOException?{
??if(!StringUtils.isEmpty(userName)?&&?!StringUtils.isEmpty(faceBase))?{
????//文件上傳的地址
?????????String?upPath?=?ResourceUtils.getURL("classpath:").getPath()+"static\\photo";
?????????//用于查看路徑是否正確
?????????System.out.println(upPath);
?????????//?圖片名稱
?????????String?fileName?=?userName+System.currentTimeMillis()?+?".png";
?????????System.out.println(upPath+"\\"+fileName);
?????????File?file?=?new?File(upPath+"\\"+fileName);
???//初始化百度云的AipFace
???AipFace?client?=?new?AipFace(APP_ID,?API_KEY,?SECRET_KEY);
???
???//往自己demo數(shù)據(jù)庫里插入一條用戶數(shù)據(jù)
???Users?user?=?new?Users();
???user.setUserName(userName);
???user.setUserPhoto(upPath+"\\"+fileName);
???Users?exitUser?=?userService.selectUserByName(user);
???if(exitUser?!=?null)?{?
????return?"2";
???}
???userService.addUsers(user);
???
???
???//?往自己demo服務(wù)器里面上傳攝像頭捕獲的圖片
???GenerateImage(faceBase,?file);
???//向百度云人臉庫插入一張人臉
???facesetAddUser(client,faceBase,userName);
??}
??return?"1";
??
?}
APP_ID?、API_KEY、SECRET_KEY三個(gè)參數(shù)分別為百度云人臉識別上面的三個(gè)數(shù)據(jù)項(xiàng),如下圖





二、刷臉登錄

@RequestMapping(value?=?"login",method?=?RequestMethod.POST)
?public?String?login(String?faceBase)?{
??String?faceData?=?faceBase;
??//進(jìn)行人像數(shù)據(jù)對比
??AipFace?client?=?new?AipFace(APP_ID,API_KEY,SECRET_KEY);
??Double?num?=?verifyUser(faceData,client);
??if(num>95)?{
???return?"1";
??}else?{
???return?"2";
??}
??
?}
?
?/**
??*?人臉比對
??*?@param?imgBash64?照片轉(zhuǎn)bash64格式
??*?@param?imgType?類型
??*?@param?groupList?百度云人臉識別用戶組
??*?@return
??*/
?public?Double?verifyUser(String?imgBash64,AipFace?client)?{
??//?傳入可選參數(shù)調(diào)用接口
?????HashMap?options?=?new?HashMap ();
?????
?????JSONObject?res?=?client.search(imgBash64,?"BASE64",?"user_01",?options);
?????
?????System.out.println(res.toString(2));
?????System.out.println(res.getJSONObject("result"));
?????System.out.println(res.getJSONObject("result").getJSONArray("user_list"));
?????JSONObject?user?=?(JSONObject)?res.getJSONObject("result").getJSONArray("user_list").get(0);
?????Double?score?=?(Double)?user.get("score");
?????
??return?score;
?}


推薦閱讀:
SpringBoot+Querydsl 框架,大大簡化復(fù)雜查詢操作
內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!
?戳閱讀原文領(lǐng)取!? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??朕已閱?
評論
圖片
表情

