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
*/
public interface ICommonConfigService extends IService<CommonConfig> {
Map<String, CommonConfig> getAllConfigMap();
Map<String, CommonConfig> getConfigMapByOrgCode(String sysOrgCode);
CommonConfig getConfigMapByCode(String code);

View File

@ -22,16 +22,34 @@ import java.util.Map;
*/
@Service
public class CommonConfigServiceImpl extends ServiceImpl<CommonConfigMapper, CommonConfig> implements ICommonConfigService {
@Autowired
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
*/
public Map<String,CommonConfig> getConfigMapByOrgCode(String sysOrgCode){
QueryWrapper<CommonConfig> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sys_org_code",sysOrgCode);
queryWrapper.eq("status","1");
queryWrapper.lambda().eq(CommonConfig::getSysOrgCode, sysOrgCode);
queryWrapper.lambda().eq(CommonConfig::getParamStatus, "1");
List<CommonConfig> configList = list(queryWrapper);
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 io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -14,214 +18,243 @@ import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
@Component
public class FaceMain {
@Autowired
private RedisUtil redisUtil;
@Autowired
private RedisUtil redisUtil;
//设置APPID/AK/SK
// public static final String APP_ID = "21388583";
// public static final String API_KEY = "K3bfbZRl7ttZp656n7Z8wSQCy";
// public static final String SECRET_KEY = "xEHt1sRGWlVtsNU10DiNhBMpWUqlTHNQ";
@Autowired
private ICommonConfigService qnCommonConfigService;
@Value(value = "${baidu.face.appid}")
// public static final String APP_ID = "37872563";
public String APP_ID;
//设置APPID/AK/SK
// public static final String APP_ID = "21388583";
// public static final String API_KEY = "K3bfbZRl7ttZp656n7Z8wSQCy";
// public static final String SECRET_KEY = "xEHt1sRGWlVtsNU10DiNhBMpWUqlTHNQ";
@Value(value = "${baidu.face.apikey}")
// public static final String API_KEY = "RBOnlD4HVDnRrdUYNOiykwGz";
public String API_KEY;
//是否灵活配置不配置为从配置文件读取配置为true则开启从表读取key模式
@Value(value = "${baidu.face.flexibleConfiguration}")
// public static final String APP_ID = "37872563";
public boolean FLEXIBLE_CONFIGURATION;
@Value(value = "${baidu.face.secretkey}")
// public static final String SECRET_KEY = "PeTEGIgcSRXLILoVjQ2GBah1NN2XrWBK";
public String SECRET_KEY;
@Value(value = "${baidu.face.appid}")
// public static final String APP_ID = "37872563";
public String APP_ID;
@Value(value = "${baidu.face.sleepTime}")
// public static final Long SLEEP_TIME = 1000L;
public Long SLEEP_TIME;
@Value(value = "${baidu.face.apikey}")
// public static final String API_KEY = "RBOnlD4HVDnRrdUYNOiykwGz";
public String API_KEY;
public AipFace creatApiClent(){
// 可选设置网络连接参数
// client.setConnectionTimeoutInMillis(2000);
// client.setSocketTimeoutInMillis(60000);
@Value(value = "${baidu.face.secretkey}")
// public static final String SECRET_KEY = "PeTEGIgcSRXLILoVjQ2GBah1NN2XrWBK";
public String SECRET_KEY;
@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二选一或者均不设置
// //client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
// //client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
// // 可选设置代理服务器地址, http和socket二选一或者均不设置
// //client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
// //client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
//
// // 调用接口
// String image = "取决于image_type参数传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
// String imageType = "BASE64";
// // 调用接口
// String image = "取决于image_type参数传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
// String imageType = "BASE64";
//
// // 人脸检测
// JSONObject res = client.detect(image, imageType, options);
// System.out.println(res.toString(2));
// AipFace main = new AipFace(APP_ID, API_KEY, SECRET_KEY);
// main.setConnectionTimeoutInMillis(90000);
// main.setSocketTimeoutInMillis(90000);
// return main;
return new AipFace(APP_ID, API_KEY, SECRET_KEY);
// // 人脸检测
// JSONObject res = client.detect(image, imageType, options);
// System.out.println(res.toString(2));
// AipFace main = new AipFace(APP_ID, API_KEY, SECRET_KEY);
// main.setConnectionTimeoutInMillis(90000);
// main.setSocketTimeoutInMillis(90000);
// return main;
//加上开关
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(无系统地址)
* @param img1 对比的图片1
* @param img2 对比的图片2
* @return
*/
public JSONObject matchBySystemFileUrl(String img1, String img2) {
return matchBySystemFileUrl(img1, img2, true);
}
/**
* 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1
* @param img2 对比的图片2
* @return
*/
public JSONObject matchBySystemFileUrl(String img1, String img2) {
return matchBySystemFileUrl(img1, img2, true);
}
/**
* 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1
* @param img2 对比的图片2
* @param isSleep 是否打开延时开关true加入延时使用配置文件里的时间单位毫秒false不加延时
* @return
*/
public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep) {
return matchBySystemFileUrl(img1, img2, isSleep, SLEEP_TIME);
}
/**
* 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1
* @param img2 对比的图片2
* @param isSleep 是否打开延时开关true加入延时使用配置文件里的时间单位毫秒false不加延时
* @return
*/
public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep) {
return matchBySystemFileUrl(img1, img2, isSleep, getSleepTime());
}
/**
* 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1
* @param img2 对比的图片2
* @param isSleep 是否打开延时开关true加入延时false不加延时
* @param sleepTime 延时时间单位毫秒
* @return
*/
public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep, Long sleepTime) {
if(StringUtils.isBlank(img1) || StringUtils.isBlank(img2)){
return null;
}
//延迟一秒
if(isSleep){
ThreadUtil.sleep(1000);
// return null;
//ThreadUtil.sleep(sleepTime);
}
return match(img1,img2,"BASE64","BASE64");
}
/**
* 人脸对比需要传入URL(无系统地址)
* @param img1 对比的图片1
* @param img2 对比的图片2
* @param isSleep 是否打开延时开关true加入延时false不加延时
* @param sleepTime 延时时间单位毫秒
* @return
*/
public JSONObject matchBySystemFileUrl(String img1, String img2, boolean isSleep, Long sleepTime) {
if(StringUtils.isBlank(img1) || StringUtils.isBlank(img2)){
return null;
}
//延迟一秒
if(isSleep){
ThreadUtil.sleep(1000);
// return null;
//ThreadUtil.sleep(sleepTime);
}
return match(img1,img2,"BASE64","BASE64");
}
/**
* 人脸对比需要传入URL(无系统地址)
* @param img1
* @param img2
* @return
*/
public JSONObject matchBySystemFileUrl(String fillUrl, String img1, String img2) {
return match(fillUrl + File.separator + img1,fillUrl + File.separator + img2,"URL","URL");
}
/**
* 人脸对比需要传入URL(无系统地址)
* @param img1
* @param img2
* @return
*/
public JSONObject matchBySystemFileUrl(String fillUrl, String img1, String img2) {
return match(fillUrl + File.separator + img1,fillUrl + File.separator + img2,"URL","URL");
}
/**
* 人脸对比需要传入可访问的URL
* @param img1
* @param img2
* @return
*/
public JSONObject match(String img1,String img2) {
return match(img1,img2,"URL","URL");
}
/**
* 人脸对比需要传入可访问的URL
* @param img1
* @param img2
* @return
*/
public JSONObject match(String img1,String img2) {
return match(img1,img2,"URL","URL");
}
public JSONObject match(String img1,String img2,String img1Type,String img2Type) {
// 初始化一个AipFace
AipFace client = creatApiClent();
MatchRequest req1 = new MatchRequest(img1, img1Type);
MatchRequest req2 = new MatchRequest(img2, img2Type);
ArrayList<MatchRequest> requests = new ArrayList<>();
requests.add(req1);
requests.add(req2);
return client.match(requests);
}
public JSONObject match(String img1,String img2,String img1Type,String img2Type) {
// 初始化一个AipFace
AipFace client = creatApiClent();
MatchRequest req1 = new MatchRequest(img1, img1Type);
MatchRequest req2 = new MatchRequest(img2, img2Type);
ArrayList<MatchRequest> requests = new ArrayList<>();
requests.add(req1);
requests.add(req2);
return client.match(requests);
}
public static void main(String[] args) {
// 初始化一个AipFace
FaceMain faceMain = new FaceMain();
faceMain.APP_ID = "";
faceMain.API_KEY = "";
faceMain.SECRET_KEY = "";
AipFace client = faceMain.creatApiClent();
public static void main(String[] args) {
// 初始化一个AipFace
FaceMain faceMain = new FaceMain();
faceMain.APP_ID = "";
faceMain.API_KEY = "";
faceMain.SECRET_KEY = "";
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参数需要与之对应
// MatchRequest req1 = new MatchRequest(image1, "BASE64");
MatchRequest req1 = new MatchRequest(image1, "URL");//
MatchRequest req2 = new MatchRequest(image2, "URL");
ArrayList<MatchRequest> requests = new ArrayList<>();
requests.add(req1);
requests.add(req2);
// image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应
// MatchRequest req1 = new MatchRequest(image1, "BASE64");
MatchRequest req1 = new MatchRequest(image1, "URL");//
MatchRequest req2 = new MatchRequest(image2, "URL");
ArrayList<MatchRequest> requests = new ArrayList<>();
requests.add(req1);
requests.add(req2);
JSONObject res = client.match(requests);
System.out.println(res.toString(2));
}
JSONObject res = client.match(requests);
System.out.println(res.toString(2));
}
public void addRunSize(Integer currentNum, Integer allNum){
addRunSize(currentNum);
addRunAllSize(allNum);
}
//正在执行数
public void addRunSize(Integer num) {
redisUtil.set("calcAllFaceDataIsRunSize",num,SLEEP_TIME * 60 * 5);
}
public void addRunSize(Integer currentNum, Integer allNum){
addRunSize(currentNum);
addRunAllSize(allNum);
}
//正在执行数
public void addRunSize(Integer num) {
redisUtil.set("calcAllFaceDataIsRunSize",num,getSleepTime() * 60 * 5);
}
//执行总数
public void addRunAllSize(Integer num) {
redisUtil.set("calcAllFaceDataIsRunAllSize",num,SLEEP_TIME * 60 * 5);
}
//执行总数
public void addRunAllSize(Integer num) {
redisUtil.set("calcAllFaceDataIsRunAllSize",num,getSleepTime() * 60 * 5);
}
public String getPromptStr(){
Integer calcAllFaceDataIsRunSize = (Integer)redisUtil.get("calcAllFaceDataIsRunSize");
Integer calcAllFaceDataIsRunAllSize = (Integer)redisUtil.get("calcAllFaceDataIsRunAllSize");
calcAllFaceDataIsRunSize = calcAllFaceDataIsRunSize == null?0:calcAllFaceDataIsRunSize;
calcAllFaceDataIsRunAllSize = calcAllFaceDataIsRunAllSize == null?0:calcAllFaceDataIsRunAllSize;
return calcAllFaceDataIsRunSize + "/" + calcAllFaceDataIsRunAllSize;
}
//删除执行数
// public void delAddRunSizes() {
// redisUtil.del("calcAllFaceDataIsRunSize");
// redisUtil.del("calcAllFaceDataIsRunAllSize");
// }
public String getPromptStr(){
Integer calcAllFaceDataIsRunSize = (Integer)redisUtil.get("calcAllFaceDataIsRunSize");
Integer calcAllFaceDataIsRunAllSize = (Integer)redisUtil.get("calcAllFaceDataIsRunAllSize");
calcAllFaceDataIsRunSize = calcAllFaceDataIsRunSize == null?0:calcAllFaceDataIsRunSize;
calcAllFaceDataIsRunAllSize = calcAllFaceDataIsRunAllSize == null?0:calcAllFaceDataIsRunAllSize;
return calcAllFaceDataIsRunSize + "/" + calcAllFaceDataIsRunAllSize;
}
//删除执行数
// public void delAddRunSizes() {
// redisUtil.del("calcAllFaceDataIsRunSize");
// redisUtil.del("calcAllFaceDataIsRunAllSize");
// }
public boolean isRun(){
Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRun");
if(isRun != null && isRun){
//redisUtil.del("calcAllFaceDataIsRun");
return true;
}
return false;
}
public boolean isRun(){
Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRun");
if(isRun != null && isRun){
//redisUtil.del("calcAllFaceDataIsRun");
return true;
}
return false;
}
public void runFlag() {
redisUtil.set("calcAllFaceDataIsRun",true,SLEEP_TIME * 60 * 5);
}
public void runFlag() {
redisUtil.set("calcAllFaceDataIsRun",true,getSleepTime() * 60 * 5);
}
public void delRunFlag() {
redisUtil.del("calcAllFaceDataIsRunSize");
redisUtil.del("calcAllFaceDataIsRunAllSize");
redisUtil.del("calcAllFaceDataIsRun");
}
public void delRunFlag() {
redisUtil.del("calcAllFaceDataIsRunSize");
redisUtil.del("calcAllFaceDataIsRunAllSize");
redisUtil.del("calcAllFaceDataIsRun");
}
public void addStopFlag() {
redisUtil.set("calcAllFaceDataIsRunStop",true,SLEEP_TIME * 60 * 5);
}
public boolean isStop(){
Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRunStop");
public void addStopFlag() {
redisUtil.set("calcAllFaceDataIsRunStop",true,getSleepTime() * 60 * 5);
}
public boolean isStop(){
Boolean isRun = (Boolean)redisUtil.get("calcAllFaceDataIsRunStop");
//redisUtil.del("calcAllFaceDataIsRun");
return isRun != null && isRun;
}
public void delStopFlag(){
redisUtil.del("calcAllFaceDataIsRunStop");
public void delStopFlag(){
redisUtil.del("calcAllFaceDataIsRunStop");
}

View File

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

View File

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