2024年9月8日 新增从数据库读取配置(人脸识别)功能

This commit is contained in:
bai 2024-09-08 11:16:24 +08:00
parent 2026c4e06e
commit c36903bcd2
5 changed files with 230 additions and 172 deletions

View File

@ -12,6 +12,9 @@ import java.util.Map;
* @Version: V1.0 * @Version: V1.0
*/ */
public interface ICommonConfigService extends IService<CommonConfig> { public interface ICommonConfigService extends IService<CommonConfig> {
Map<String, CommonConfig> getAllConfigMap();
Map<String, CommonConfig> getConfigMapByOrgCode(String sysOrgCode); Map<String, CommonConfig> getConfigMapByOrgCode(String sysOrgCode);
CommonConfig getConfigMapByCode(String code); CommonConfig getConfigMapByCode(String code);

View File

@ -22,16 +22,34 @@ import java.util.Map;
*/ */
@Service @Service
public class CommonConfigServiceImpl extends ServiceImpl<CommonConfigMapper, CommonConfig> implements ICommonConfigService { public class CommonConfigServiceImpl extends ServiceImpl<CommonConfigMapper, CommonConfig> implements ICommonConfigService {
@Autowired @Autowired
private CommonConfigMapper commonConfigMapper; private CommonConfigMapper commonConfigMapper;
/**
* 根据机构查询配置信息
* @return
*/
public Map<String,CommonConfig> getAllConfigMap(){
QueryWrapper<CommonConfig> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(CommonConfig::getParamStatus, "1");
List<CommonConfig> configList = list(queryWrapper);
Map<String,CommonConfig> returnMap = new HashMap<>();
if(configList!=null && !configList.isEmpty()){
configList.forEach(x -> returnMap.put(x.getParamCode(),x));
}
return returnMap;
}
/** /**
* 根据机构查询配置信息 * 根据机构查询配置信息
* @return * @return
*/ */
public Map<String,CommonConfig> getConfigMapByOrgCode(String sysOrgCode){ public Map<String,CommonConfig> getConfigMapByOrgCode(String sysOrgCode){
QueryWrapper<CommonConfig> queryWrapper = new QueryWrapper<>(); QueryWrapper<CommonConfig> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sys_org_code",sysOrgCode); queryWrapper.lambda().eq(CommonConfig::getSysOrgCode, sysOrgCode);
queryWrapper.eq("status","1"); queryWrapper.lambda().eq(CommonConfig::getParamStatus, "1");
List<CommonConfig> configList = list(queryWrapper); List<CommonConfig> configList = list(queryWrapper);
Map<String,CommonConfig> returnMap = new HashMap<>(); Map<String,CommonConfig> returnMap = new HashMap<>();

View File

@ -5,8 +5,12 @@ import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest; import com.baidu.aip.face.MatchRequest;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.common.commonConfig.entity.CommonConfig;
import org.jeecg.modules.common.commonConfig.service.ICommonConfigService;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -14,214 +18,243 @@ import org.springframework.stereotype.Component;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
@Component @Component
public class FaceMain { public class FaceMain {
@Autowired @Autowired
private RedisUtil redisUtil; private RedisUtil redisUtil;
//设置APPID/AK/SK @Autowired
// public static final String APP_ID = "21388583"; private ICommonConfigService qnCommonConfigService;
// public static final String API_KEY = "K3bfbZRl7ttZp656n7Z8wSQCy";
// public static final String SECRET_KEY = "xEHt1sRGWlVtsNU10DiNhBMpWUqlTHNQ";
@Value(value = "${baidu.face.appid}") //设置APPID/AK/SK
// public static final String APP_ID = "37872563"; // public static final String APP_ID = "21388583";
public String APP_ID; // public static final String API_KEY = "K3bfbZRl7ttZp656n7Z8wSQCy";
// public static final String SECRET_KEY = "xEHt1sRGWlVtsNU10DiNhBMpWUqlTHNQ";
@Value(value = "${baidu.face.apikey}") //是否灵活配置不配置为从配置文件读取配置为true则开启从表读取key模式
// public static final String API_KEY = "RBOnlD4HVDnRrdUYNOiykwGz"; @Value(value = "${baidu.face.flexibleConfiguration}")
public String API_KEY; // public static final String APP_ID = "37872563";
public boolean FLEXIBLE_CONFIGURATION;
@Value(value = "${baidu.face.secretkey}") @Value(value = "${baidu.face.appid}")
// public static final String SECRET_KEY = "PeTEGIgcSRXLILoVjQ2GBah1NN2XrWBK"; // public static final String APP_ID = "37872563";
public String SECRET_KEY; public String APP_ID;
@Value(value = "${baidu.face.sleepTime}") @Value(value = "${baidu.face.apikey}")
// public static final Long SLEEP_TIME = 1000L; // public static final String API_KEY = "RBOnlD4HVDnRrdUYNOiykwGz";
public Long SLEEP_TIME; public String API_KEY;
public AipFace creatApiClent(){ @Value(value = "${baidu.face.secretkey}")
// 可选设置网络连接参数 // public static final String SECRET_KEY = "PeTEGIgcSRXLILoVjQ2GBah1NN2XrWBK";
// client.setConnectionTimeoutInMillis(2000); public String SECRET_KEY;
// client.setSocketTimeoutInMillis(60000);
@Value(value = "${baidu.face.sleepTime}")
// public static final Long SLEEP_TIME = 1000L;
public Long FACE_SLEEP_TIME;
public Long SLEEP_TIME = FACE_SLEEP_TIME;
public Long getSleepTime(){
creatApiClent();
return SLEEP_TIME;
}
public AipFace creatApiClent(){
// 可选设置网络连接参数
// client.setConnectionTimeoutInMillis(2000);
// client.setSocketTimeoutInMillis(60000);
// //
// // 可选设置代理服务器地址, http和socket二选一或者均不设置 // // 可选设置代理服务器地址, http和socket二选一或者均不设置
// //client.setHttpProxy("proxy_host", proxy_port); // 设置http代理 // //client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
// //client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理 // //client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
// //
// // 调用接口 // // 调用接口
// String image = "取决于image_type参数传入BASE64字符串或URL字符串或FACE_TOKEN字符串"; // String image = "取决于image_type参数传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
// String imageType = "BASE64"; // String imageType = "BASE64";
// //
// // 人脸检测 // // 人脸检测
// JSONObject res = client.detect(image, imageType, options); // JSONObject res = client.detect(image, imageType, options);
// System.out.println(res.toString(2)); // System.out.println(res.toString(2));
// AipFace main = new AipFace(APP_ID, API_KEY, SECRET_KEY); // AipFace main = new AipFace(APP_ID, API_KEY, SECRET_KEY);
// main.setConnectionTimeoutInMillis(90000); // main.setConnectionTimeoutInMillis(90000);
// main.setSocketTimeoutInMillis(90000); // main.setSocketTimeoutInMillis(90000);
// return main; // return main;
return new AipFace(APP_ID, API_KEY, SECRET_KEY); //加上开关
if(FLEXIBLE_CONFIGURATION){
//查找数据库里的key
Map<String, CommonConfig> configMap = qnCommonConfigService.getAllConfigMap();
if(!configMap.isEmpty()){
CommonConfig baiduFaceAppIdConfig = configMap.get("baiduFaceAppId");
CommonConfig baiduFaceApiKeyConfig = configMap.get("baiduFaceApiKey");
CommonConfig baiduFaceSecretKeyConfig = configMap.get("baiduFaceSecretKey");
CommonConfig baiduFaceSleepTimeConfig = configMap.get("baiduFaceSleepTime");
SLEEP_TIME = Long.parseLong(baiduFaceSleepTimeConfig.getParamValue());
return new AipFace(baiduFaceAppIdConfig.getParamValue(), baiduFaceApiKeyConfig.getParamValue(), baiduFaceSecretKeyConfig.getParamValue());
}
}
return new AipFace(APP_ID, API_KEY, SECRET_KEY);
} }
/** /**
* 人脸对比需要传入URL(无系统地址) * 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1 * @param img1 对比的图片1
* @param img2 对比的图片2 * @param img2 对比的图片2
* @return * @return
*/ */
public JSONObject matchBySystemFileUrl(String img1, String img2) { public JSONObject matchBySystemFileUrl(String img1, String img2) {
return matchBySystemFileUrl(img1, img2, true); return matchBySystemFileUrl(img1, img2, true);
} }
/** /**
* 人脸对比需要传入URL(无系统地址) * 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1 * @param img1 对比的图片1
* @param img2 对比的图片2 * @param img2 对比的图片2
* @param isSleep 是否打开延时开关true加入延时使用配置文件里的时间单位毫秒false不加延时 * @param isSleep 是否打开延时开关true加入延时使用配置文件里的时间单位毫秒false不加延时
* @return * @return
*/ */
public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep) { public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep) {
return matchBySystemFileUrl(img1, img2, isSleep, SLEEP_TIME); return matchBySystemFileUrl(img1, img2, isSleep, getSleepTime());
} }
/** /**
* 人脸对比需要传入URL(无系统地址) * 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1 * @param img1 对比的图片1
* @param img2 对比的图片2 * @param img2 对比的图片2
* @param isSleep 是否打开延时开关true加入延时false不加延时 * @param isSleep 是否打开延时开关true加入延时false不加延时
* @param sleepTime 延时时间单位毫秒 * @param sleepTime 延时时间单位毫秒
* @return * @return
*/ */
public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep, Long sleepTime) { public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep, Long sleepTime) {
if(StringUtils.isBlank(img1) || StringUtils.isBlank(img2)){ if(StringUtils.isBlank(img1) || StringUtils.isBlank(img2)){
return null; return null;
} }
//延迟一秒 //延迟一秒
if(isSleep){ if(isSleep){
ThreadUtil.sleep(1000); ThreadUtil.sleep(1000);
// return null; // return null;
//ThreadUtil.sleep(sleepTime); //ThreadUtil.sleep(sleepTime);
} }
return match(img1,img2,"BASE64","BASE64"); return match(img1,img2,"BASE64","BASE64");
} }
/** /**
* 人脸对比需要传入URL(无系统地址) * 人脸对比需要传入URL(无系统地址)
* @param img1 * @param img1
* @param img2 * @param img2
* @return * @return
*/ */
public JSONObject matchBySystemFileUrl(String fillUrl, String img1, String img2) { public JSONObject matchBySystemFileUrl(String fillUrl, String img1, String img2) {
return match(fillUrl + File.separator + img1,fillUrl + File.separator + img2,"URL","URL"); return match(fillUrl + File.separator + img1,fillUrl + File.separator + img2,"URL","URL");
} }
/** /**
* 人脸对比需要传入可访问的URL * 人脸对比需要传入可访问的URL
* @param img1 * @param img1
* @param img2 * @param img2
* @return * @return
*/ */
public JSONObject match(String img1,String img2) { public JSONObject match(String img1,String img2) {
return match(img1,img2,"URL","URL"); return match(img1,img2,"URL","URL");
} }
public JSONObject match(String img1,String img2,String img1Type,String img2Type) { public JSONObject match(String img1,String img2,String img1Type,String img2Type) {
// 初始化一个AipFace // 初始化一个AipFace
AipFace client = creatApiClent(); AipFace client = creatApiClent();
MatchRequest req1 = new MatchRequest(img1, img1Type); MatchRequest req1 = new MatchRequest(img1, img1Type);
MatchRequest req2 = new MatchRequest(img2, img2Type); MatchRequest req2 = new MatchRequest(img2, img2Type);
ArrayList<MatchRequest> requests = new ArrayList<>(); ArrayList<MatchRequest> requests = new ArrayList<>();
requests.add(req1); requests.add(req1);
requests.add(req2); requests.add(req2);
return client.match(requests); return client.match(requests);
} }
public static void main(String[] args) { public static void main(String[] args) {
// 初始化一个AipFace // 初始化一个AipFace
FaceMain faceMain = new FaceMain(); FaceMain faceMain = new FaceMain();
faceMain.APP_ID = ""; faceMain.APP_ID = "";
faceMain.API_KEY = ""; faceMain.API_KEY = "";
faceMain.SECRET_KEY = ""; faceMain.SECRET_KEY = "";
AipFace client = faceMain.creatApiClent(); AipFace client = faceMain.creatApiClent();
String image1 = "https://img0.baidu.com/it/u=2620353834,3334301303&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=670"; String image1 = "https://img0.baidu.com/it/u=2620353834,3334301303&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=670";
String image2 = "https://img1.baidu.com/it/u=3388895316,506060607&fm=253&fmt=auto&app=138&f=JPEG?w=608&h=338"; String image2 = "https://img1.baidu.com/it/u=3388895316,506060607&fm=253&fmt=auto&app=138&f=JPEG?w=608&h=338";
// image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应 // image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应
// MatchRequest req1 = new MatchRequest(image1, "BASE64"); // MatchRequest req1 = new MatchRequest(image1, "BASE64");
MatchRequest req1 = new MatchRequest(image1, "URL");// MatchRequest req1 = new MatchRequest(image1, "URL");//
MatchRequest req2 = new MatchRequest(image2, "URL"); MatchRequest req2 = new MatchRequest(image2, "URL");
ArrayList<MatchRequest> requests = new ArrayList<>(); ArrayList<MatchRequest> requests = new ArrayList<>();
requests.add(req1); requests.add(req1);
requests.add(req2); requests.add(req2);
JSONObject res = client.match(requests); JSONObject res = client.match(requests);
System.out.println(res.toString(2)); System.out.println(res.toString(2));
} }
public void addRunSize(Integer currentNum, Integer allNum){ public void addRunSize(Integer currentNum, Integer allNum){
addRunSize(currentNum); addRunSize(currentNum);
addRunAllSize(allNum); addRunAllSize(allNum);
} }
//正在执行数 //正在执行数
public void addRunSize(Integer num) { public void addRunSize(Integer num) {
redisUtil.set("calcAllFaceDataIsRunSize",num,SLEEP_TIME * 60 * 5); redisUtil.set("calcAllFaceDataIsRunSize",num,getSleepTime() * 60 * 5);
} }
//执行总数 //执行总数
public void addRunAllSize(Integer num) { public void addRunAllSize(Integer num) {
redisUtil.set("calcAllFaceDataIsRunAllSize",num,SLEEP_TIME * 60 * 5); redisUtil.set("calcAllFaceDataIsRunAllSize",num,getSleepTime() * 60 * 5);
} }
public String getPromptStr(){ public String getPromptStr(){
Integer calcAllFaceDataIsRunSize = (Integer)redisUtil.get("calcAllFaceDataIsRunSize"); Integer calcAllFaceDataIsRunSize = (Integer)redisUtil.get("calcAllFaceDataIsRunSize");
Integer calcAllFaceDataIsRunAllSize = (Integer)redisUtil.get("calcAllFaceDataIsRunAllSize"); Integer calcAllFaceDataIsRunAllSize = (Integer)redisUtil.get("calcAllFaceDataIsRunAllSize");
calcAllFaceDataIsRunSize = calcAllFaceDataIsRunSize == null?0:calcAllFaceDataIsRunSize; calcAllFaceDataIsRunSize = calcAllFaceDataIsRunSize == null?0:calcAllFaceDataIsRunSize;
calcAllFaceDataIsRunAllSize = calcAllFaceDataIsRunAllSize == null?0:calcAllFaceDataIsRunAllSize; calcAllFaceDataIsRunAllSize = calcAllFaceDataIsRunAllSize == null?0:calcAllFaceDataIsRunAllSize;
return calcAllFaceDataIsRunSize + "/" + calcAllFaceDataIsRunAllSize; return calcAllFaceDataIsRunSize + "/" + calcAllFaceDataIsRunAllSize;
} }
//删除执行数 //删除执行数
// public void delAddRunSizes() { // public void delAddRunSizes() {
// redisUtil.del("calcAllFaceDataIsRunSize"); // redisUtil.del("calcAllFaceDataIsRunSize");
// redisUtil.del("calcAllFaceDataIsRunAllSize"); // redisUtil.del("calcAllFaceDataIsRunAllSize");
// } // }
public boolean isRun(){ public boolean isRun(){
Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRun"); Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRun");
if(isRun != null && isRun){ if(isRun != null && isRun){
//redisUtil.del("calcAllFaceDataIsRun"); //redisUtil.del("calcAllFaceDataIsRun");
return true; return true;
} }
return false; return false;
} }
public void runFlag() { public void runFlag() {
redisUtil.set("calcAllFaceDataIsRun",true,SLEEP_TIME * 60 * 5); redisUtil.set("calcAllFaceDataIsRun",true,getSleepTime() * 60 * 5);
} }
public void delRunFlag() { public void delRunFlag() {
redisUtil.del("calcAllFaceDataIsRunSize"); redisUtil.del("calcAllFaceDataIsRunSize");
redisUtil.del("calcAllFaceDataIsRunAllSize"); redisUtil.del("calcAllFaceDataIsRunAllSize");
redisUtil.del("calcAllFaceDataIsRun"); redisUtil.del("calcAllFaceDataIsRun");
} }
public void addStopFlag() { public void addStopFlag() {
redisUtil.set("calcAllFaceDataIsRunStop",true,SLEEP_TIME * 60 * 5); redisUtil.set("calcAllFaceDataIsRunStop",true,getSleepTime() * 60 * 5);
} }
public boolean isStop(){ public boolean isStop(){
Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRunStop"); Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRunStop");
//redisUtil.del("calcAllFaceDataIsRun"); //redisUtil.del("calcAllFaceDataIsRun");
return isRun != null && isRun; return isRun != null && isRun;
} }
public void delStopFlag(){ public void delStopFlag(){
redisUtil.del("calcAllFaceDataIsRunStop"); redisUtil.del("calcAllFaceDataIsRunStop");
} }

View File

@ -325,6 +325,8 @@ ffmpeg:
extensionFilter: png,jpg,jpeg extensionFilter: png,jpg,jpeg
baidu: baidu:
face: face:
#是否灵活配置不配置为从配置文件读取配置为true则开启从表读取key模式
flexibleConfiguration: true
# 测试账号 1千/月 # 测试账号 1千/月
# 批量比对时延时多少毫秒 # 批量比对时延时多少毫秒
sleepTime: 1000 sleepTime: 1000

View File

@ -334,6 +334,8 @@ ffmpeg:
extensionFilter: png,jpg,jpeg extensionFilter: png,jpg,jpeg
baidu: baidu:
face: face:
#是否灵活配置不配置为从配置文件读取配置为true则开启从表读取key模式
flexibleConfiguration: true
# 测试账号 1千/月 # 测试账号 1千/月
# 批量比对时延时多少毫秒 # 批量比对时延时多少毫秒
# sleepTime: 1000 # sleepTime: 1000