前言
上一篇SpringBoot集成百度人臉demo中我使用的是調(diào)用本機(jī)攝像頭完成人臉注冊,本次demo根據(jù)業(yè)務(wù)需求的不同我采用文件上傳的方式實(shí)現(xiàn)人臉注冊。
效果演示
首頁
注冊
后端響應(yīng)數(shù)據(jù):
登錄
后端響應(yīng)數(shù)據(jù):
項(xiàng)目結(jié)構(gòu)
文章來源:http://www.zghlxwxcb.cn/news/detail-630757.html
后端代碼實(shí)現(xiàn)
1、BaiduAiUtils工具類封裝文章來源地址http://www.zghlxwxcb.cn/news/detail-630757.html
package com.jzj.utils;
import com.baidu.aip.face.AipFace;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
/**
* 百度AI工具類封裝
*
* @author 黎明
* @version 1.0
* @date 2023/8/5 9:35
*/
@Component
@Slf4j
public class BaiduAiUtils {
/*
注入百度個(gè)人用戶相關(guān)配置
*/
@Value("${baidu.face.appId}")
private String APP_ID;
@Value("${baidu.face.apiKey}")
private String API_KEY;
@Value("${baidu.face.secretKey}")
private String SECRET_KEY;
@Value("${baidu.face.imageType}")
private String IMAGE_TYPE;
@Value("${baidu.face.groupId}")
private String groupId;
// 聲明私有變量client,AipFace是百度人臉識(shí)別 API 的 Java 客戶端類,用于與人臉識(shí)別服務(wù)進(jìn)行通信。
private AipFace client;
// 用于存儲(chǔ)一些參數(shù)配置(圖片質(zhì)量控制、活體檢測控制)
private HashMap<String, String> map = new HashMap<>();
/*
私有的構(gòu)造函數(shù),表明該類是一個(gè)單例類,只能通過靜態(tài)方法獲取實(shí)例
*/
private BaiduAiUtils() {
// 圖片質(zhì)量控制 NONE: 不進(jìn)行控制 LOW:較低的質(zhì)量要求 NORMAL: 一般的質(zhì)量要求 HIGH: 較高的質(zhì)量要求 默認(rèn) NONE
map.put("quality_control", "NORMAL");
// 活體檢測控制 NONE: 不進(jìn)行控制 LOW:較低的活體要求(高通過率 低拒絕率) NORMAL: 一般的活體要求(平衡的拒絕率, 通過率) HIGH: 較高的活體要求(高拒絕率 低通過率) 默認(rèn)NONE
map.put("liveness_control", "LOW");
}
/*
用于在類初始化時(shí)執(zhí)行client的初始化操作
*/
@PostConstruct
public void init() {
client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
}
/**
* 人臉注冊:用戶照片存入人臉庫中
*/
public Boolean faceRegister(String userId, String image) {
JSONObject res = client.addUser(image, IMAGE_TYPE, groupId, userId, map);
log.info("人臉注冊響應(yīng)數(shù)據(jù) :{}", res);
Integer errorCode = res.getInt("error_code");
return errorCode == 0 ? true : false;
}
/**
* 人臉更新:更新人臉庫中的用戶照片
*/
public Boolean faceUpdate(String userId, String image) {
JSONObject res = client.updateUser(image, IMAGE_TYPE, groupId, userId, map);
log.info("人臉更新響應(yīng)數(shù)據(jù) :{}", res);
Integer errorCode = res.getInt("error_code");
return errorCode == 0 ? true : false;
}
/**
* 人臉檢測:判斷上傳的圖片中是否具有面部信息
*/
public Boolean faceCheck
到了這里,關(guān)于SpringBoot集成百度人臉識(shí)別實(shí)現(xiàn)登陸注冊功能Demo(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!