兴安添加我的审核,人脸检测替换宇泛接口

This commit is contained in:
zc
2025-08-21 16:37:08 +08:00
parent d27c24274e
commit f4342c40f8
16 changed files with 276 additions and 156 deletions

View File

@@ -47,6 +47,12 @@
<artifactId>fastdfs-client</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.3</version>
</dependency>
<!-- Minio -->
<dependency>
<groupId>io.minio</groupId>
@@ -65,6 +71,10 @@
<groupId>com.dcsoft</groupId>
<artifactId>dcsoft-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>com.dcsoft</groupId>
<artifactId>dcsoft-common-redis</artifactId>
</dependency>
</dependencies>

View File

@@ -1,49 +1,47 @@
package com.dcsoft.file.controller;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import cn.hutool.http.HttpUtil;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dcsoft.common.core.domain.R;
import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.file.service.IAlgoService;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.common.core.utils.file.FileUtils;
import com.dcsoft.common.redis.service.RedisService;
import com.dcsoft.file.service.IFaceService;
import com.dcsoft.file.utils.*;
import com.dcsoft.system.api.RemoteStudentService;
import com.dcsoft.file.service.ISysFileService;
import com.dcsoft.file.utils.FileUploadUtils;
import com.dcsoft.file.utils.ImageToBase64Utils;
import com.dcsoft.file.utils.MinioUtil;
import com.dcsoft.file.utils.PictureUtils;
import com.dcsoft.system.api.domain.SysFile;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.reflect.MethodSignature;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.dcsoft.common.core.domain.R;
import com.dcsoft.common.core.utils.file.FileUtils;
import com.dcsoft.file.service.ISysFileService;
import com.dcsoft.system.api.domain.SysFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.dcsoft.file.utils.MinioUtil.getBase64String;
import java.util.concurrent.TimeUnit;
/**
* 文件请求处理
@@ -51,11 +49,12 @@ import static com.dcsoft.file.utils.MinioUtil.getBase64String;
* @author dcsoft
*/
@RestController
public class SysFileController
{
public class SysFileController {
private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
/** 获取yml配置类里的桶名称*/
/**
* 获取yml配置类里的桶名称
*/
@Value("${minio.bucket-name}")
private String bucketName;
@@ -69,7 +68,7 @@ public class SysFileController
private String bucketName3;
@Autowired
private IAlgoService algoService;
private RedisService redisService;
@Autowired
private IFaceService faceService;
@@ -98,24 +97,35 @@ public class SysFileController
@Value("${file.gwurl}")
private String filegwurl;
@Value("${yf.url:http://wo-api.uni-ubi.com}")
private String yfUrl;
@Value("${yf.projectGuid:3C1BA78F6FAD489AA0E6C7D285D8CFDC}")
private String yfProjectGuid;
@Value("${yf.appKey:7E28496B107D45D3B683B143E944D64B}")
private String yfAppKey;
@Value("${yf.appSecret:9D397690DAD64CFE9278FED5C3BCA68F}")
private String yfAppSecret;
private final String yfFaceDetectUrl = "/v2/algor/face/detect";
/**
* 文件上传请求
*/
@PostMapping("upload")
public R<SysFile> upload(MultipartFile file)
{
try
{
public R<SysFile> upload(MultipartFile file) {
try {
// 上传并返回访问地址
String url = sysFileService.uploadFile(file);
SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(url));
url=url.replace(fileurl,filegwurl);
url = url.replace(fileurl, filegwurl);
sysFile.setUrl(url);
return R.ok(sysFile);
}
catch (Exception e)
{
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
@@ -125,36 +135,30 @@ public class SysFileController
* 文件上传请求
*/
@PostMapping("avatar")
public R<SysFile> avatar(@RequestParam("avatarfile") MultipartFile file)
{
try
{
public R<SysFile> avatar(@RequestParam("avatarfile") MultipartFile file) {
try {
// 上传并返回访问地址
String url = sysFileService.uploadFile(file);
SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(url));
sysFile.setUrl(url);
return R.ok(sysFile);
}
catch (Exception e)
{
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
}
@PostMapping("uploadMinioCar")
public R<SysFile> uploadMinioCar(@RequestParam("file") MultipartFile file)
{
try
{
String fileNames = FileUploadUtils.uploadMinio(file,this.bucketName2,file.getName());
public R<SysFile> uploadMinioCar(@RequestParam("file") MultipartFile file) {
try {
String fileNames = FileUploadUtils.uploadMinio(file, this.bucketName2, file.getName());
SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(fileNames));
fileNames=fileNames.replace(miniourl,miniogwurl);
fileNames = fileNames.replace(miniourl, miniogwurl);
sysFile.setUrl(fileNames);
return R.ok(sysFile);
}catch (Exception e){
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
@@ -182,13 +186,11 @@ public class SysFileController
}
/**
* 文件上传请求
*/
@PostMapping("uploadMinio")
public R<SysFile> uploadMinio(MultipartFile file, String fileName)
{
public R<SysFile> uploadMinio(MultipartFile file, String fileName) {
/* //判断压缩图片
if((1024 * 1024 * 0.1) <= file.getSize()){
// 小于 1M 的
@@ -213,8 +215,7 @@ public class SysFileController
throw new RuntimeException(e);
}
}*/
try
{
try {
/*initFlowRules();//
// 上传并返回访问地址
//上传前进行人脸检测
@@ -227,15 +228,15 @@ public class SysFileController
}
//提取特征值
String feature=faceService.getFeature(base64String);*/
String fileNames = FileUploadUtils.uploadMinio(file,this.bucketName,fileName);
String fileNames = FileUploadUtils.uploadMinio(file, this.bucketName, fileName);
SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(fileNames));
fileNames=fileNames.replace(miniourl,miniogwurl);
fileNames = fileNames.replace(miniourl, miniogwurl);
sysFile.setUrl(fileNames);
//sysFile.setFeature(feature);
return R.ok(sysFile);
}catch (Exception e){
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
@@ -245,11 +246,10 @@ public class SysFileController
* 文件上传请求
*/
@PostMapping("uploadMinios")
public R<SysFile> uploadMinios(MultipartFile file,String fileName)
{
public R<SysFile> uploadMinios(MultipartFile file, String fileName) {
//判断压缩图片
if((1024 * 1024 * 0.1) <= file.getSize()){
if ((1024 * 1024 * 0.1) <= file.getSize()) {
// 小于 1M 的
try {
String fileNames1 = file.getOriginalFilename();
@@ -274,15 +274,14 @@ public class SysFileController
throw new RuntimeException(e);
}
}
try
{
try {
// 上传并返回访问地址
String fileNames = FileUploadUtils.uploadMinio(file,this.bucketName,fileName);
String fileNames = FileUploadUtils.uploadMinio(file, this.bucketName, fileName);
SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(fileNames));
sysFile.setUrl(fileNames);
return R.ok(sysFile);
}catch (Exception e){
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
@@ -325,6 +324,7 @@ public class SysFileController
// 上传并返回访问地址
String fileNames = FileUploadUtils.uploadMinio(file, this.bucketName1, fileName);
// checkPhoto(fileNames);
SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(fileNames));
fileNames = fileNames.replace(miniourl, miniogwurl);
@@ -361,40 +361,66 @@ public class SysFileController
* @param imgBase64
*/
private void checkPhoto(String imgBase64) {
if (StringUtils.isNotEmpty(imgBase64)) {
Map<String, Object> map = new HashMap<>();
map.put("imgBase64", imgBase64);
String body = HttpUtil.postData(upload, map);
log.info("checkPhoto_body{}", body);
JSONObject object = JSONObject.parseObject(body);
log.info("返回校验结果:{}", JSON.toJSONString(object));
if (501 == object.getInteger("code")) {
throw new ServiceException("系统繁忙,请稍后再试!");
} else if (200 != object.getInteger("code")) {
throw new ServiceException("未检测到人脸信息,请重新上传!");
} else if (null != object.getInteger("data") && 90 > object.getInteger("data")) {
throw new ServiceException("人脸照片校验不合格,请重新上传!");
String token = redisService.getCacheObject("yf-Token");
if (StringUtils.isBlank(token)) {
try {
long l = System.currentTimeMillis();
String sign = DigestUtils.md5Hex(yfAppKey + l + yfAppSecret);
Map<String, Object> map = new HashMap<>();
map.put("projectGuid", yfProjectGuid);
String result = HttpUtil.createGet(yfUrl + "/v1/" + yfProjectGuid + "/auth")
.header("appKey", yfAppKey)
.header("timestamp", String.valueOf(l))
.header("sign", sign)
.form(map)
.timeout(5000).execute().body();
JSONObject jsonObject1 = JSONObject.parseObject(result);
if (jsonObject1.getInteger("result") == 1 && StringUtils.isNotBlank(jsonObject1.getString("data"))) {
token = jsonObject1.getString("data");
redisService.setCacheObject("yf-Token", token, 20 * 60 * 60L, TimeUnit.SECONDS);
}
} catch (Exception e) {
throw new ServiceException("宇泛鉴权失败!");
}
}
//https://api.uni-ubi.com/wo/apis/V2.1/ai/portrait.html#%E4%BA%BA%E5%83%8F%E6%A3%80%E6%B5%8B
Map<String, Object> map = new HashMap<>();
map.put("imageUrl", "");
map.put("imageBase64", imgBase64);
map.put("returnLandmark", "true");
map.put("returnAttributes", "true");
map.put("unitTag", "");
//0检测全部属性是否合格
map.put("validLevel", "0");
//人脸算法校验
String result = HttpUtil.createPost(yfUrl + yfFaceDetectUrl)
.header("token", token)
.header("projectGuid", yfProjectGuid)
.body(JSON.toJSONString(map))
.timeout(5000).execute().body();
log.info("请求宇泛人脸返回:{}", result);
JSONObject jsonObject1 = JSONObject.parseObject(result);
if (jsonObject1.getInteger("result") != 1 || !jsonObject1.getBoolean("success")) {
throw new ServiceException("人脸检测失败,请重新上传");
}
}
@PostMapping("uploadMinio2")
public R<SysFile> uploadMinio2(MultipartFile file,String fileName, String name)
{
try
{
public R<SysFile> uploadMinio2(MultipartFile file, String fileName, String name) {
try {
// 上传并返回访问地址
String fileNames = FileUploadUtils.uploadMinio(file,this.bucketName1,fileName);
String fileNames = FileUploadUtils.uploadMinio(file, this.bucketName1, fileName);
SysFile sysFile = new SysFile();
sysFile.setName(URLEncoder.encode(name, "UTF-8"));
fileNames=fileNames.replace(miniourl,miniogwurl);
fileNames = fileNames.replace(miniourl, miniogwurl);
sysFile.setUrl(fileNames);
return R.ok(sysFile);
}
catch (Exception e)
{
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
@@ -405,17 +431,14 @@ public class SysFileController
* 文件上传请求
*/
@GetMapping("compareTwoPic")
public R<Float> compareTwoPic(@RequestParam("url1") String url1,@RequestParam("url2") String url2){
try
{
public R<Float> compareTwoPic(@RequestParam("url1") String url1, @RequestParam("url2") String url2) {
try {
//进行人脸1v1
byte[] image1= Base64.decodeBase64(ImageToBase64Utils.getImgUrlToBase64(url1));
byte[] image2=Base64.decodeBase64(ImageToBase64Utils.getImgUrlToBase64(url2));
float data=faceService.compareTwoPic(image1,image2);
byte[] image1 = Base64.decodeBase64(ImageToBase64Utils.getImgUrlToBase64(url1));
byte[] image2 = Base64.decodeBase64(ImageToBase64Utils.getImgUrlToBase64(url2));
float data = faceService.compareTwoPic(image1, image2);
return R.ok(data);
}
catch (Exception e)
{
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
@@ -425,21 +448,18 @@ public class SysFileController
* 文件上传请求
*/
@GetMapping("comparePic")
public R<Float> comparePic(@RequestParam("feature1") String feature1,@RequestParam("feature2") String feature2){
try
{
public R<Float> comparePic(@RequestParam("feature1") String feature1, @RequestParam("feature2") String feature2) {
try {
//进行人脸1v1
float data=faceService.compareTwoPic(feature1,feature2);
float data = faceService.compareTwoPic(feature1, feature2);
return R.ok(data);
}
catch (Exception e)
{
} catch (Exception e) {
log.error("上传文件失败", e);
return R.fail(e.getMessage());
}
}
private static void initFlowRules(){
private static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");