比对接口

This commit is contained in:
yangjun 2024-07-09 16:36:55 +08:00
parent fc1aa57b8d
commit 5c750a5b35
1 changed files with 181 additions and 0 deletions

View File

@ -0,0 +1,181 @@
package com.sqx.face;
import cn.hutool.core.thread.ThreadUtil;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.sqx.common.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
@Component
public class FaceMain {
@Autowired
private RedisUtils 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";
public static final Long SLEEP_TIME = 1000L;
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代理
//
// // 调用接口
// String image = "取决于image_type参数传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
// String imageType = "BASE64";
//
// // 人脸检测
// JSONObject res = client.detect(image, imageType, options);
// System.out.println(res.toString(2));
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
* @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不加延时
* @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 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 static void main(String[] args) {
// 初始化一个AipFace
FaceMain faceMain = new FaceMain();
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 image2 = "https://img1.baidu.com/it/u=3388895316,506060607&fm=253&fmt=auto&app=138&f=JPEG?w=608&h=338";
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));
}
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 addRunAllSize(Integer num) {
redisUtil.set("calcAllFaceDataIsRunAllSize",num,SLEEP_TIME * 60 * 5);
}
public String getPromptStr(){
Integer calcAllFaceDataIsRunSize = Integer.parseInt(redisUtil.get("calcAllFaceDataIsRunSize"));
Integer calcAllFaceDataIsRunAllSize = Integer.parseInt(redisUtil.get("calcAllFaceDataIsRunAllSize"));
calcAllFaceDataIsRunSize = calcAllFaceDataIsRunSize == null?0:calcAllFaceDataIsRunSize;
calcAllFaceDataIsRunAllSize = calcAllFaceDataIsRunAllSize == null?0:calcAllFaceDataIsRunAllSize;
return calcAllFaceDataIsRunSize + "/" + calcAllFaceDataIsRunAllSize;
}
public boolean isRun(){
Boolean isRun = Boolean.parseBoolean(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 delRunFlag() {
redisUtil.delete("calcAllFaceDataIsRunSize");
redisUtil.delete("calcAllFaceDataIsRunAllSize");
redisUtil.delete("calcAllFaceDataIsRun");
}
}