這篇文章主要講解了“怎么用Java實(shí)現(xiàn)PC人臉識(shí)別登錄”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用Java實(shí)現(xiàn)PC人臉識(shí)別登錄”吧!
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供豐都網(wǎng)站建設(shè)、豐都做網(wǎng)站、豐都網(wǎng)站設(shè)計(jì)、豐都網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、豐都企業(yè)網(wǎng)站模板建站服務(wù),十年豐都做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
實(shí)現(xiàn)原理
我們看一下實(shí)現(xiàn)人臉識(shí)別登錄的大致流程,三個(gè)主要步驟:
前端登錄頁打開攝像頭,進(jìn)行人臉識(shí)別,注意:只識(shí)別畫面中是不是有人臉
識(shí)別到人臉后,拍照上傳當(dāng)前畫面圖片
后端接受圖片并調(diào)用人臉庫SDK,對(duì)人像進(jìn)行比對(duì),通過則登錄成功,并將人像信息注冊(cè)到人臉庫和本地MySQL。
前端實(shí)現(xiàn)
上邊說過要在前端識(shí)別到人臉,所以這里就不得不借助工具了,我使用的 tracking.js,一款輕量級(jí)的前端人臉識(shí)別框架。
前端 Vue 代碼實(shí)現(xiàn)邏輯比較簡(jiǎn)單,tracking.js 打開攝像頭識(shí)別到人臉信息后,對(duì)視頻圖像拍照,將圖片信息上傳到后臺(tái),等待圖片對(duì)比的結(jié)果就可以了。
data() {
return {
showContainer:
true,
// 顯示
tracker:
null,
tipFlag:
false,
// 提示用戶已經(jīng)檢測(cè)到
flag:
false, // 判斷是否已經(jīng)拍照
context:
null, // canvas上下文
removePhotoID:
null, // 停止轉(zhuǎn)換圖片
scanTip:
'人臉識(shí)別中...',// 提示文字
imgUrl:
'', // base64格式圖片
canvas:
null
}
},
mounted() {
this.playVideo()
},
methods: {
playVideo() {
var video =
document.getElementById('video');
this.canvas =
document.getElementById('canvas');
this.context =
this.canvas.getContext('2d');
this.tracker =
new tracking.ObjectTracker('face');
this.tracker.setInitialScale(4);
this.tracker.setStepSize(2);
this.tracker.setEdgesDensity(0.1);
tracking.track('#video',
this.tracker, {camera:
true});
this.tracker.on('track',
this.handleTracked);
},
handleTracked(event) {
this.context.clearRect(0,
0,
this.canvas.width,
this.canvas.height);
if (event.data.length ===
0) {
this.scanTip =
'未識(shí)別到人臉'
}
else {
if (!this.tipFlag) {
this.scanTip =
'識(shí)別成功,正在拍照,請(qǐng)勿亂動(dòng)~'
}
// 1秒后拍照,僅拍一次
if (!this.flag) {
this.scanTip =
'拍照中...'
this.flag =
true
this.removePhotoID = setTimeout(() => {
this.tackPhoto()
this.tipFlag =
true
},
2000
)
}
event.data.forEach(this.plot);
}
},
plot(rect){
this.context.strokeStyle =
'#eb652e';
this.context.strokeRect(rect.x, rect.y, rect.width, rect.height);
this.context.font =
'11px Helvetica';
this.context.fillStyle =
"#fff";
this.context.fillText('x: ' + rect.x +
'px', rect.x + rect.width +
5, rect.y +
11);
this.context.fillText('y: ' + rect.y +
'px', rect.x + rect.width +
5, rect.y +
22);
},
// 拍照
tackPhoto() {
this.context.drawImage(this.$refs.refVideo,
0,
0,
500,
500)
// 保存為base64格式
this.imgUrl =
this.saveAsPNG(this.$refs.refCanvas)
var formData =
new FormData();
formData.append("file",
this.imgUrl);
this.scanTip =
'登錄中,請(qǐng)稍等~'
axios({
method:
'post',
url:
'/faceDiscern',
data: formData,
}).then(function (response) {
alert(response.data.data);
window.location.href="http://127.0.0.1:8081/home";
}).catch(function (error) {
console.log(error);
});
this.close()
},
// 保存為png,base64格式圖片
saveAsPNG(c) {
return c.toDataURL('image/png',
0.3)
},
// 關(guān)閉并清理資源
close() {
this.flag =
false
this.tipFlag =
false
this.showContainer =
false
this.tracker &&
this.tracker.removeListener('track',
this.handleTracked) && tracking.track('#video',
this.tracker, {camera:
false});
this.tracker =
null
this.context =
null
this.scanTip =
''
clearTimeout(this.removePhotoID)
}
}
人臉識(shí)別
之前也搞過一個(gè)人臉識(shí)別案例,不過調(diào)用SDK的方式太過繁瑣,而且代碼量巨大。所以這次為了簡(jiǎn)化實(shí)現(xiàn),改用了百度的人臉識(shí)別API,沒想到出乎意料的簡(jiǎn)單。
別抬杠問我為啥不自己寫人臉識(shí)別工具,別問,問就是不會(huì)
百度云人臉識(shí)別的API非常友好,各種操作的 demo都寫好了,拿過來簡(jiǎn)單改改就可以。
第一步先獲取token,這是調(diào)用百度人臉識(shí)別API的基礎(chǔ)。
https://aip.baidubce.com/oauth/2.0/token?
grant_type=client_credentials&
client_id=【百度云應(yīng)用的AK】&
client_secret=【百度云應(yīng)用的SK】
接下來我們開始對(duì)圖片進(jìn)行比對(duì),百度云提供了一個(gè)在線的人臉庫,用戶登錄我們先在人臉庫查詢?nèi)讼袷欠翊嬖?,存在則表示登錄成功,如果不存在則注冊(cè)到人臉庫。每個(gè)圖片有一個(gè)唯一標(biāo)識(shí)face_token。
百度人臉識(shí)別 API 實(shí)現(xiàn)比較簡(jiǎn)單,需要特別注意參數(shù)image_type,它有三種類型
BASE64:圖片的base64值,base64編碼后的圖片數(shù)據(jù),編碼后的圖片大小不超過2M;
URL:圖片的 URL地址( 可能由于網(wǎng)絡(luò)等原因?qū)е孪螺d圖片時(shí)間過長(zhǎng));
FACE_TOKEN:人臉圖片的唯一標(biāo)識(shí),調(diào)用人臉檢測(cè)接口時(shí),會(huì)為每個(gè)人臉圖片賦予一個(gè)唯一的
FACE_TOKEN,同一張圖片多次檢測(cè)得到的FACE_TOKEN是同一個(gè)。
而我們這里使用的是圖片BASE64文件,所以image_type要設(shè)置成BASE64。
@Override
public BaiDuFaceSearchResult
faceSearch(String file) {
try {
byte[] decode = Base64.decode(Base64Util.base64Process(file));
String faceFile = Base64Util.encode(decode);
Map<String, Object>
map =
new HashMap<>();
map.put("image", faceFile);
map.put("liveness_control",
"NORMAL");
map.put("group_id_list",
"user");
map.put("image_type",
"BASE64");
map.put("quality_control",
"LOW");
String param = GsonUtils.toJson(map);
String result = HttpUtil.post(faceSearchUrl,
this.getAccessToken(),
"application/json", param);
BaiDuFaceSearchResult searchResult = JSONObject.parseObject(result, BaiDuFaceSearchResult.class);
log.info(" faceSearch: {}", JSON.toJSONString(searchResult));
return searchResult;
}
catch (Exception e) {
log.error("get faceSearch error {}", e.getStackTrace());
e.getStackTrace();
}
return null;
}
@Override
public BaiDuFaceDetectResult
faceDetect(String file) {
try {
byte[] decode = Base64.decode(Base64Util.base64Process(file));
String faceFile = Base64Util.encode(decode);
Map<String, Object>
map =
new HashMap<>();
map.put("image", faceFile);
map.put("face_field",
"faceshape,facetype");
map.put("image_type",
"BASE64");
String param = GsonUtils.toJson(map);
String result = HttpUtil.post(faceDetectUrl,
this.getAccessToken(),
"application/json", param);
BaiDuFaceDetectResult detectResult = JSONObject.parseObject(result, BaiDuFaceDetectResult.class);
log.info(" detectResult: {}", JSON.toJSONString(detectResult));
return detectResult;
}
catch (Exception e) {
log.error("get faceDetect error {}", e.getStackTrace());
e.getStackTrace();
}
return null;
}
@Override
public BaiDuFaceAddResult
addFace(String file, UserFaceInfo userFaceInfo) {
try {
byte[] decode = Base64.decode(Base64Util.base64Process(file));
String faceFile = Base64Util.encode(decode);
Map<String, Object>
map =
new HashMap<>();
map.put("image", faceFile);
map.put("group_id",
"user");
map.put("user_id", userFaceInfo.getUserId());
map.put("user_info", JSON.toJSONString(userFaceInfo));
map.put("liveness_control",
"NORMAL");
map.put("image_type",
"BASE64");
map.put("quality_control",
"LOW");
String param = GsonUtils.toJson(map);
String result = HttpUtil.post(addfaceUrl,
this.getAccessToken(),
"application/json", param);
BaiDuFaceAddResult addResult = JSONObject.parseObject(result, BaiDuFaceAddResult.class);
log.info("addResult: {}", JSON.toJSONString(addResult));
return addResult;
}
catch (Exception e) {
log.error("get addFace error {}", e.getStackTrace());
e.getStackTrace();
}
return null;
}
感謝各位的閱讀,以上就是“怎么用Java實(shí)現(xiàn)PC人臉識(shí)別登錄”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用Java實(shí)現(xiàn)PC人臉識(shí)別登錄這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站題目:怎么用Java實(shí)現(xiàn)PC人臉識(shí)別登錄
當(dāng)前鏈接:http://bm7419.com/article30/phdspo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、服務(wù)器托管、網(wǎng)站維護(hù)、云服務(wù)器、Google、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)