|
|
@@ -26,8 +26,10 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.*;
|
|
|
+import java.util.concurrent.atomic.LongAdder;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @Author: zhoupan
|
|
|
@@ -41,6 +43,11 @@ import java.util.Map;
|
|
|
@RequestMapping("/faceApi")
|
|
|
public class FaceApi {
|
|
|
|
|
|
+ private static final int taskSize = 50;
|
|
|
+
|
|
|
+ private static ExecutorService pool = Executors.newFixedThreadPool(taskSize);
|
|
|
+
|
|
|
+
|
|
|
Logger logger = LoggerFactory.getLogger(FaceApi.class);
|
|
|
|
|
|
@Autowired
|
|
|
@@ -117,6 +124,49 @@ public class FaceApi {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 人脸比较
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation("人脸比较")
|
|
|
+ @PostMapping("/newCompare")
|
|
|
+ public ResultData newCompare(@RequestParam("file") MultipartFile file,FaceCompare faceCompare) throws IOException{
|
|
|
+
|
|
|
+ ImageInfo imageInfo;
|
|
|
+ File file1 = null;
|
|
|
+ try {
|
|
|
+ file1 = FileUtil.multipartFileToFile(file);
|
|
|
+ imageInfo = faceService.getImageInfo(file1);
|
|
|
+ } finally {
|
|
|
+ if (file1 != null) {
|
|
|
+ file1.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<FaceInfo> faceInfos = faceService.faceDetect(imageInfo);
|
|
|
+ if(null==faceInfos || faceInfos.size()==0){
|
|
|
+ return ResultData.fail("识别中");
|
|
|
+ }
|
|
|
+ FaceFeature faceFeature = faceService.faceFeature(imageInfo, faceInfos);
|
|
|
+ if(StringUtils.isNotNull(faceFeature.getFeatureData())){
|
|
|
+ logger.info("====人脸比较====");
|
|
|
+ R<LabStudentsInfo> info = remoteStudentsService.getInfo(faceCompare.getUserId());
|
|
|
+ if (info.getCode() == 200 && info.getData() != null) {
|
|
|
+ logger.info("====人脸data组装====");
|
|
|
+ FaceFeature target = new FaceFeature(info.getData().getFaceFeature());
|
|
|
+ FaceFeature source = new FaceFeature(faceFeature.getFeatureData());
|
|
|
+ Boolean compore = faceService.compore(target, source);
|
|
|
+ logger.info("====compore====" + compore);
|
|
|
+ return ResultData.result(compore, () -> "人脸对比不符!");
|
|
|
+ } else {
|
|
|
+ return ResultData.fail("未获取到对比数据!:" + info.getMsg());
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return ResultData.fail("未获取到特征码!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 人脸比较
|
|
|
@@ -125,57 +175,78 @@ public class FaceApi {
|
|
|
*/
|
|
|
@ApiOperation("人脸比较")
|
|
|
@PostMapping("/multiCompare")
|
|
|
- public ResultData multiFaceDetection(@RequestBody FaceCompare faceCompare) {
|
|
|
+ public ResultData multiFaceDetection(@RequestBody FaceCompare faceCompare) throws ExecutionException, InterruptedException {
|
|
|
logger.info("====人脸比较====");
|
|
|
- Integer isDutyUser=0;
|
|
|
+ long time=System.currentTimeMillis();
|
|
|
ResultData<List<LabSysUserInfo>> infoList = remoteStudentsService.getFaceBySecuritySubjectId(faceCompare.getLabId());
|
|
|
- if (infoList.getCode() == 200 && infoList.getData() != null) {
|
|
|
- logger.info("====人脸data组装====");
|
|
|
- for(LabSysUserInfo info:infoList.getData()){
|
|
|
- FaceFeature target = new FaceFeature(info.getFaceFeature());
|
|
|
- FaceFeature source = new FaceFeature(faceCompare.getData());
|
|
|
- Boolean compore = faceService.compore(target, source);
|
|
|
- if(compore){
|
|
|
- String userType=info.getUserType();
|
|
|
- Integer isWhite=info.getIsWhite();
|
|
|
- if(StringUtils.isNull(userType)){
|
|
|
- return ResultData.fail("用户不存在!");
|
|
|
- }
|
|
|
- //如果是学生 或者白名单的老师 进二类页面
|
|
|
- if(userType.equals("22") || (userType.equals("11") && isWhite==0)){
|
|
|
- info.setPageType(2);
|
|
|
- }
|
|
|
- ResultData resultData =remoteDutyService.isAdminOrSafeUser(faceCompare.getLabId(),info.getUserId());
|
|
|
- //一类首页对应身份为白名单老师、实验室负责人、安全责任人
|
|
|
- if(Integer.parseInt(String.valueOf(resultData.getData()))>0 || (userType.equals("11") && isWhite==1)){
|
|
|
- info.setPageType(1);
|
|
|
- }
|
|
|
- info.setFaceFeature(null);
|
|
|
- String dateTime = DateUtils.getDate();
|
|
|
- ResultData dutyUserData = remoteDutyService.selectDutyUser(faceCompare.getLabId(),dateTime);
|
|
|
- //判断是否当天值班
|
|
|
- if(null==dutyUserData && dutyUserData.getCode()!= HttpStatus.SUCCESS){
|
|
|
- return ResultData.fail("获取值班人员识别!");
|
|
|
- }
|
|
|
- List<Map<String,Object>> dutyList= (List<Map<String, Object>>) dutyUserData.getData();
|
|
|
- for(Map<String,Object> dutyMap:dutyList){
|
|
|
- String userObj=String.valueOf(dutyMap.get("userId"));
|
|
|
- if(StringUtils.isNotEmpty(userObj) && StringUtils.isNotNull(userObj)){
|
|
|
- if(Long.parseLong(userObj)==info.getUserId()){
|
|
|
- isDutyUser=1;
|
|
|
- break;
|
|
|
+ if(infoList.getCode() == 200 && infoList.getData() != null){
|
|
|
+ List<Future> list = Optional.ofNullable(infoList.getData()).orElseGet(Collections::emptyList)
|
|
|
+ .stream()
|
|
|
+ .map(a->{
|
|
|
+ Future f = pool.submit(new Callable<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object call() throws Exception {
|
|
|
+ return getMultiFaceDetection(a,faceCompare);
|
|
|
}
|
|
|
- }
|
|
|
+ });
|
|
|
+ return f;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ for(Future future:list){
|
|
|
+ ResultData r=(ResultData)future.get();
|
|
|
+ if(r.getCode()==200){
|
|
|
+ long time2=System.currentTimeMillis();
|
|
|
+ System.out.println("执行时间:"+(time2-time));
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //pool.shutdown();
|
|
|
+ }
|
|
|
+ return ResultData.fail("未获取实验室安全准入人脸数据!");
|
|
|
+ }
|
|
|
+
|
|
|
+ private ResultData getMultiFaceDetection(LabSysUserInfo info,FaceCompare faceCompare){
|
|
|
+ Integer isDutyUser=0;
|
|
|
+ FaceFeature target = new FaceFeature(info.getFaceFeature());
|
|
|
+ FaceFeature source = new FaceFeature(faceCompare.getData());
|
|
|
+ Boolean compore = faceService.compore(target, source);
|
|
|
+ if(compore){
|
|
|
+ String userType=info.getUserType();
|
|
|
+ Integer isWhite=info.getIsWhite();
|
|
|
+ if(StringUtils.isNull(userType)){
|
|
|
+ return ResultData.fail("用户不存在!");
|
|
|
+ }
|
|
|
+ //如果是学生 或者白名单的老师 进二类页面
|
|
|
+ if(userType.equals("22") || (userType.equals("11") && isWhite==0)){
|
|
|
+ info.setPageType(2);
|
|
|
+ }
|
|
|
+ ResultData resultData =remoteDutyService.isAdminOrSafeUser(faceCompare.getLabId(),info.getUserId());
|
|
|
+ //一类首页对应身份为白名单老师、实验室负责人、安全责任人
|
|
|
+ if(Integer.parseInt(String.valueOf(resultData.getData()))>0 || (userType.equals("11") && isWhite==1)){
|
|
|
+ info.setPageType(1);
|
|
|
+ }
|
|
|
+ info.setFaceFeature(null);
|
|
|
+ String dateTime = DateUtils.getDate();
|
|
|
+ ResultData dutyUserData = remoteDutyService.selectDutyUser(faceCompare.getLabId(),dateTime);
|
|
|
+ //判断是否当天值班
|
|
|
+ if(null==dutyUserData && dutyUserData.getCode()!= HttpStatus.SUCCESS){
|
|
|
+ return ResultData.fail("获取值班人员识别!");
|
|
|
+ }
|
|
|
+ List<Map<String,Object>> dutyList= (List<Map<String, Object>>) dutyUserData.getData();
|
|
|
+ for(Map<String,Object> dutyMap:dutyList){
|
|
|
+ String userObj=String.valueOf(dutyMap.get("userId"));
|
|
|
+ if(StringUtils.isNotEmpty(userObj) && StringUtils.isNotNull(userObj)){
|
|
|
+ if(Long.parseLong(userObj)==info.getUserId()){
|
|
|
+ isDutyUser=1;
|
|
|
+ break;
|
|
|
}
|
|
|
- info.setIsDutyUser(isDutyUser);
|
|
|
- logger.info("====compore====" +"对比成功用户ID:"+info.getUserId());
|
|
|
- return ResultData.success(info);
|
|
|
}
|
|
|
}
|
|
|
- return ResultData.fail("未获取实验室安全准入人脸数据!");
|
|
|
- } else {
|
|
|
- return ResultData.fail("未获取实验室安全准入人脸数据!");
|
|
|
+ info.setIsDutyUser(isDutyUser);
|
|
|
+ logger.info("====compore====" +"对比成功用户ID:"+info.getUserId());
|
|
|
+ return ResultData.success(info);
|
|
|
}
|
|
|
+ return ResultData.fail("未获取实验室安全准入人脸数据!");
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -194,4 +265,32 @@ public class FaceApi {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static void main(String[] args) throws ExecutionException, InterruptedException {
|
|
|
+ long time=System.currentTimeMillis();
|
|
|
+ ExecutorService pool = Executors.newFixedThreadPool(100);
|
|
|
+ List<String> list=new ArrayList();
|
|
|
+ for(int i=0;i<100;i++){
|
|
|
+ list.add("姓名:"+i);
|
|
|
+ }
|
|
|
+ List<Future> lists = Optional.ofNullable(list).orElseGet(Collections::emptyList)
|
|
|
+ .stream()
|
|
|
+ .map(a->{
|
|
|
+ Future f = pool.submit(new Callable<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object call() throws Exception {
|
|
|
+ Thread.sleep(1000);
|
|
|
+ return a;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return f;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ for(Future future:lists){
|
|
|
+ String r=(String)future.get();
|
|
|
+ System.out.println(r);
|
|
|
+ }
|
|
|
+ long time2=System.currentTimeMillis();
|
|
|
+ System.out.println(time2-time);
|
|
|
+ }
|
|
|
}
|