赞
踩
{"face_token":"efb5996c1707850f81e884ef190c984b","user_list":[{"score":91.080505371094,"group_id":"group1","user_id":"1","user_info":""}]}
1、点击人脸识别:跳转到服务
2、其间会有一些验证,创建应用
3、查看人脸库
4、新建组、用户
5、添加人脸照片
- 基于Springboot框架搭建的,前端使用Vue,通过摄像机拍下照片后,请求后端人脸识别登陆服务,后台调用百度API人脸识别接口,进入百度
- 大脑搜索人脸识别即可获取官网的Secret Key,将前端获取的人脸信息的base64信息和你本地数据库里的人脸信息传到百度人脸识别的接口
- 进行人脸比对,返回一个json数据,result参数 带别人脸相似度, result可自己定义,从而实现人脸识别登录。
- <dependency>
- <groupId>com.baidu.aip</groupId>
- <artifactId>java-sdk</artifactId>
- <version>4.15.1</version>
- </dependency>
- package com.ruoyi.common.utils.face.utils;
- import com.baidu.aip.face.AipFace;
- import com.baidu.aip.util.Base64Util;
- /**
- * 人脸识别工具
- */
- public class FaceUtil {
-
- private static final String APP_ID = "25236971";
- private static final String APP_KEY = "zLIZGY15wiEQh2utjwAGGW9C";
- private static final String SECRET_KEY = "Pzi4Ew6eWfZPINBkshlCO22OqADa777I";
-
- private static volatile AipFace client = new AipFace(APP_ID, APP_KEY, SECRET_KEY);
- // 创建单例避免多次获取sdk
- public static AipFace getClient(){
- client.setConnectionTimeoutInMillis(2000);
- client.setSocketTimeoutInMillis(60000);
- return client;
- }
-
- /**
- * 编码
- * @param form
- * @return
- */
- public static String encodeBase64(byte[] form){
- return Base64Util.encode(form);
- }
-
- /**
- * 解码
- * @param data
- * @return
- */
- public static byte[] decodeBase64(String data){
- return Base64Util.decode(data);
- }
- }
-
- package com.ruoyi.common.utils.face.utils;
- import com.ruoyi.common.utils.face.BizException;
- import com.ruoyi.common.utils.face.constant.ErrorEnum;
- import com.ruoyi.common.utils.face.constant.FaceConstant;
- import com.ruoyi.common.utils.face.dto.FaceResult;
- import org.json.JSONObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
-
- /**
- * 结果工具封装
- */
- public class FaceResultUtil {
- private static final Logger logger = LoggerFactory.getLogger("百度API接口请求结果解析");
-
- public static FaceResult isSuccess(JSONObject res){
- FaceResult result = parseJsonObject(res);
- if (!result.isSuccess()){
- // 对错误进行分类
- ErrorEnum errorEnum = ErrorEnum.getInstance(result.getErrorCode());
- if (errorEnum == null){
- throw new BizException("百度接口请求失败" + result.getErrorMsg());
- }else {
- throw new BizException(errorEnum.getCnDesc());
- }
- }
- return result;
- }
-
-
- /**
- * 解析JsonObject
- * @return
- */
- private static FaceResult parseJsonObject(JSONObject res){
- FaceResult faceResult = FaceResult.builder().build();
- try {
- String logId = res.has(FaceConstant.LOG_ID) ? res.get(FaceConstant.LOG_ID).toString() : "0";
- int errorCode = res.has(FaceConstant.ERROR_CODE) ? res.getInt(FaceConstant.ERROR_CODE) : -1;
- String errorMsg = res.has(FaceConstant.ERROR_MSG) ? res.getString(FaceConstant.ERROR_MSG) : "";
- int cached = res.has(FaceConstant.CACHED) ? res.getInt(FaceConstant.CACHED) : 0;
- long timestamp = res.has(FaceConstant.TIMESTAMP) ? res.getLong(FaceConstant.TIMESTAMP) : 0;
- Object dataString = res.has(FaceConstant.RESULT) ? res.get(FaceConstant.RESULT) : "";
- com.alibaba.fastjson.JSONObject data = null;
- if (dataString != null) {
- data = com.alibaba.fastjson.JSONObject.parseObject(dataString.toString());
- }
- faceResult.setLogId(logId);
- faceResult.setErrorCode(errorCode);
- faceResult.setErrorMsg(errorMsg);
- faceResult.setCached(cached);
- faceResult.setTimestamp(timestamp);
- faceResult.setData(data);
- }catch (Exception e){
- logger.error("JSONObject解析失败", e);
- }
- return faceResult;
- }
- }
-
-
- package com.ruoyi.common.utils.face.dto;
-
- import com.alibaba.fastjson.JSONObject;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.io.Serializable;
-
- /**
- *请求百度API接口结果
- */
- @Data
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- public class FaceResult implements Serializable {
- private String logId;
-
- private String errorMsg;
-
- private int cached;
-
- private int errorCode;
-
- private long timestamp;
-
- private JSONObject data;
-
- public boolean isSuccess(){
- return 0 == this.errorCode ? true : false;
- }
- }
-
-
- package com.ruoyi.common.utils.face.dto;
- import com.ruoyi.common.utils.face.constant.ImageTypeEnum;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.io.Serializable;
-
- /**
- * 图像对象
- */
- @Data
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- public class ImageU implements Serializable {
-
- private ImageTypeEnum imageTypeEnum;
-
- private String data;
- }
-
-
- package com.ruoyi.common.utils.face.dto;
-
- import com.ruoyi.common.utils.face.constant.FaceConstant;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.io.Serializable;
-
-
- @Data
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- public class FaceUserDTO<T> implements Serializable {
-
- private String userId;
-
-
- private String groupId = FaceConstant.DEFAULT_GROUP_ID;
-
- private String faceToken;
-
- private T user;
- }
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。