From 75cc8af2cb73e603f6c85fef885394215600e13b Mon Sep 17 00:00:00 2001 From: gaoben Date: Wed, 14 Aug 2024 09:32:48 +0800 Subject: [PATCH] =?UTF-8?q?v10:=E7=89=B9=E5=BE=81=E5=BA=93=E5=88=86?= =?UTF-8?q?=E7=BB=84=EF=BC=8C=E6=A0=B9=E6=8D=AE=E5=B0=8F=E5=8C=BAid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/face/FaceEngineAutoRun.java | 81 ++++++++--- .../modules/face/entity/UserCompareInfo.java | 3 +- .../face/service/FaceEngineService.java | 5 +- .../service/impl/FaceEngineServiceImpl.java | 14 +- .../modules/face/util/UserInfo.java | 11 ++ .../modules/face/util/UserRamCache.java | 32 ++-- .../modules/face/util/UserRamGroup.java | 34 +++++ .../httpapi/controler/FaceController.java | 137 +++++++++--------- .../httpapi/controler/HttpApiController.java | 31 ++-- .../modules/job/task/FaceRestartTask.java | 84 ++++++++--- .../modules/job/task/PersonProcessTask.java | 4 +- .../ten/controller/TenCellController.java | 5 + .../ten/controller/TenPersonController.java | 31 +++- .../modules/ten/dao/TenPersonDao.java | 4 +- .../modules/ten/service/TenPersonService.java | 4 +- .../service/impl/TenPersonServiceImpl.java | 61 ++++++-- .../resources/mapper/ten/TenPersonDao.xml | 10 +- 17 files changed, 380 insertions(+), 171 deletions(-) create mode 100644 shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserInfo.java create mode 100644 shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamGroup.java diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/face/FaceEngineAutoRun.java b/shapelight-admin/src/main/java/net/shapelight/modules/face/FaceEngineAutoRun.java index cd67d31..71fb9a9 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/face/FaceEngineAutoRun.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/face/FaceEngineAutoRun.java @@ -1,11 +1,17 @@ package net.shapelight.modules.face; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.extern.slf4j.Slf4j; +import net.shapelight.common.config.GlobalValue; import net.shapelight.modules.face.entity.UserCompareInfo; import net.shapelight.modules.face.service.FaceEngineService; import net.shapelight.modules.face.util.Base64Util; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; +import net.shapelight.modules.face.util.UserRamGroup; +import net.shapelight.modules.ten.entity.TenCellEntity; import net.shapelight.modules.ten.entity.TenPersonEntity; +import net.shapelight.modules.ten.service.TenCellService; import net.shapelight.modules.ten.service.TenPersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; @@ -26,6 +32,10 @@ public class FaceEngineAutoRun implements ApplicationRunner { private FaceEngineService faceEngineService; @Autowired private TenPersonService tenPersonService; + @Autowired + private TenCellService tenCellService; + @Autowired + private GlobalValue globalValue; @Override public void run(ApplicationArguments args) throws Exception { @@ -52,28 +62,59 @@ public class FaceEngineAutoRun implements ApplicationRunner { // } // } - int count = tenPersonService.findCount(); - int pageSize = 1000; - int page = count/pageSize; - if(count%1000!=0){ - page = page+1; - } - int faceCount = 0; - for (int i = 0; i < page; i++) { - int start = i*1000; - List listPage = tenPersonService.listPage(start,1000); - for(TenPersonEntity personEntity: listPage){ - if(personEntity.getFeature()!=null && personEntity.getFeature().length()>0){ - UserRamCache.UserInfo userInfo = new UserCompareInfo(); - userInfo.setFaceId(personEntity.getPersonId()+""); - userInfo.setName(personEntity.getName()); - userInfo.setFaceFeature(Base64Util.base64ToBytes(personEntity.getFeature())); - //这边注册到内存缓存中 - UserRamCache.addUser(userInfo); - faceCount++; +// int count = tenPersonService.findCount(); +// int pageSize = 1000; +// int page = count/pageSize; +// if(count%1000!=0){ +// page = page+1; +// } +// int faceCount = 0; +// for (int i = 0; i < page; i++) { +// int start = i*1000; +// List listPage = tenPersonService.listPage(start,1000); +// for(TenPersonEntity personEntity: listPage){ +// if(personEntity.getFeature()!=null && personEntity.getFeature().length()>0){ +// UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// userInfo.setFaceId(personEntity.getPersonId()+""); +// userInfo.setName(personEntity.getName()); +// userInfo.setFaceFeature(Base64Util.base64ToBytes(personEntity.getFeature())); +// //这边注册到内存缓存中 +// UserRamCache.addUser(userInfo); +// faceCount++; +// } +// } +// } + + + List cellList = tenCellService.list(new QueryWrapper() + .eq("tenant_id",globalValue.getTenantId()) + .eq("delete_flag",0)); + for(TenCellEntity cellEntity: cellList){ + String cellId = cellEntity.getCellId()+""; + UserRamGroup.addCell(cellId); + int count = tenPersonService.findCount(cellId); + int pageSize = 1000; + int page = count/pageSize; + if(count%1000!=0){ + page = page+1; + } + int faceCount = 0; + for (int i = 0; i < page; i++) { + int start = i*1000; + List listPage = tenPersonService.listPage(start,1000, cellId); + for(TenPersonEntity personEntity: listPage){ + if(personEntity.getFeature()!=null && personEntity.getFeature().length()>0){ + UserInfo userInfo = new UserCompareInfo(); + userInfo.setFaceId(personEntity.getPersonId()+""); + userInfo.setName(personEntity.getName()); + userInfo.setFaceFeature(Base64Util.base64ToBytes(personEntity.getFeature())); + //这边注册到内存缓存中 + UserRamGroup.addUser(userInfo,cellId); + faceCount++; + } } } + log.debug(cellEntity.getName()+":初始化人脸库完成,共 "+faceCount+" 人"); } - log.debug("初始化人脸库完成,共 "+faceCount+" 人"); } } diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/face/entity/UserCompareInfo.java b/shapelight-admin/src/main/java/net/shapelight/modules/face/entity/UserCompareInfo.java index eeeffd5..3499821 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/face/entity/UserCompareInfo.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/face/entity/UserCompareInfo.java @@ -2,9 +2,10 @@ package net.shapelight.modules.face.entity; import lombok.Data; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; @Data -public class UserCompareInfo extends UserRamCache.UserInfo { +public class UserCompareInfo extends UserInfo { private Float similar; } diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/face/service/FaceEngineService.java b/shapelight-admin/src/main/java/net/shapelight/modules/face/service/FaceEngineService.java index 556a16d..de14d74 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/face/service/FaceEngineService.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/face/service/FaceEngineService.java @@ -6,6 +6,7 @@ import com.arcsoft.face.enums.ExtractType; import com.arcsoft.face.toolkit.ImageInfo; import net.shapelight.modules.face.entity.ProcessInfo; import net.shapelight.modules.face.entity.UserCompareInfo; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; import java.util.List; @@ -19,12 +20,10 @@ public interface FaceEngineService { byte[] extractFaceFeature(ImageInfo imageInfo, FaceInfo faceInfo, ExtractType extractType); - List faceRecognition(byte[] faceFeature, List userInfoList, float passRate) ; + List faceRecognition(byte[] faceFeature, List userInfoList, float passRate) ; List process(ImageInfo imageInfo, List faceInfoList); - - } diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/face/service/impl/FaceEngineServiceImpl.java b/shapelight-admin/src/main/java/net/shapelight/modules/face/service/impl/FaceEngineServiceImpl.java index 41c7b9f..20dd6b0 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/face/service/impl/FaceEngineServiceImpl.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/face/service/impl/FaceEngineServiceImpl.java @@ -14,6 +14,7 @@ import net.shapelight.modules.face.enums.ErrorCodeEnum; import net.shapelight.modules.face.factory.FaceEngineFactory; import net.shapelight.modules.face.rpc.BusinessException; import net.shapelight.modules.face.service.FaceEngineService; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; @@ -220,15 +221,15 @@ public class FaceEngineServiceImpl implements FaceEngineService { } @Override - public List faceRecognition(byte[] faceFeature, List userInfoList, float passRate) { + public List faceRecognition(byte[] faceFeature, List userInfoList, float passRate) { List resultUserInfoList = Lists.newLinkedList();//识别到的人脸列表 FaceFeature targetFaceFeature = new FaceFeature(); targetFaceFeature.setFeatureData(faceFeature); - List> faceUserInfoPartList = Lists.partition(userInfoList, 1000);//分成1000一组,多线程处理 + List> faceUserInfoPartList = Lists.partition(userInfoList, 1000);//分成1000一组,多线程处理 CompletionService> completionService = new ExecutorCompletionService(compareExecutorService); - for (List part : faceUserInfoPartList) { + for (List part : faceUserInfoPartList) { completionService.submit(new CompareFaceTask(part, targetFaceFeature, passRate)); } for (int i = 0; i < faceUserInfoPartList.size(); i++) { @@ -248,6 +249,7 @@ public class FaceEngineServiceImpl implements FaceEngineService { } + @Override public List process(ImageInfo imageInfo, List faceInfoList) { FaceEngine faceEngine = null; @@ -303,12 +305,12 @@ public class FaceEngineServiceImpl implements FaceEngineService { private class CompareFaceTask implements Callable> { - private List userInfoList; + private List userInfoList; private FaceFeature targetFaceFeature; private float passRate; - public CompareFaceTask(List userInfoList, FaceFeature targetFaceFeature, float passRate) { + public CompareFaceTask(List userInfoList, FaceFeature targetFaceFeature, float passRate) { this.userInfoList = userInfoList; this.targetFaceFeature = targetFaceFeature; this.passRate = passRate; @@ -320,7 +322,7 @@ public class FaceEngineServiceImpl implements FaceEngineService { List resultUserInfoList = Lists.newLinkedList();//识别到的人脸列表 try { faceEngine = faceEngineComparePool.borrowObject(); - for (UserRamCache.UserInfo userInfo : userInfoList) { + for (UserInfo userInfo : userInfoList) { FaceFeature sourceFaceFeature = new FaceFeature(); sourceFaceFeature.setFeatureData(userInfo.getFaceFeature()); FaceSimilar faceSimilar = new FaceSimilar(); diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserInfo.java b/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserInfo.java new file mode 100644 index 0000000..07781f5 --- /dev/null +++ b/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserInfo.java @@ -0,0 +1,11 @@ +package net.shapelight.modules.face.util; + + +import lombok.Data; + +@Data +public class UserInfo { + private String faceId; + private String name; + private byte[] faceFeature; +} diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamCache.java b/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamCache.java index acf2558..1fda6ca 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamCache.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamCache.java @@ -10,51 +10,51 @@ import java.util.concurrent.CopyOnWriteArraySet; public class UserRamCache { - private static final ConcurrentHashMap USER_INFO_MAP = new ConcurrentHashMap<>(); + private final ConcurrentHashMap USER_INFO_MAP = new ConcurrentHashMap<>(); - private static final Set REGISTER = new CopyOnWriteArraySet<>(); + private final Set REGISTER = new CopyOnWriteArraySet<>(); - public static void addUser(UserInfo userInfo) { + public void addUser(UserInfo userInfo) { USER_INFO_MAP.put(userInfo.getFaceId(), userInfo); for (Listener listener : REGISTER) { listener.onAdd(userInfo); } } - public static void removeUser(String faceId) { + public void removeUser(String faceId) { UserInfo userInfo = USER_INFO_MAP.remove(faceId); for (Listener listener : REGISTER) { listener.onRemove(userInfo); } } - public static List getUserList() { + public List getUserList() { List userInfoList = Lists.newLinkedList(); userInfoList.addAll(USER_INFO_MAP.values()); return userInfoList; } - public static void clear(){ + public void clear(){ USER_INFO_MAP.clear(); REGISTER.clear(); } - public static void addListener(Listener listener) { + public void addListener(Listener listener) { REGISTER.add(listener); } - public static void removeListener(Listener listener) { + public void removeListener(Listener listener) { REGISTER.remove(listener); } - @Data - public static class UserInfo { - - private String faceId; - private String name; - private byte[] faceFeature; - - } +// @Data +// public class UserInfo { +// +// private String faceId; +// private String name; +// private byte[] faceFeature; +// +// } public interface Listener { default void onAdd(UserInfo userInfo) { diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamGroup.java b/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamGroup.java new file mode 100644 index 0000000..a54789c --- /dev/null +++ b/shapelight-admin/src/main/java/net/shapelight/modules/face/util/UserRamGroup.java @@ -0,0 +1,34 @@ +package net.shapelight.modules.face.util; + +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; + +public class UserRamGroup { + + private static final ConcurrentHashMap USER_RAM_GROUP_MAP = new ConcurrentHashMap<>(); + + public static void addCell(String cellId){ + UserRamCache cell = new UserRamCache(); + USER_RAM_GROUP_MAP.put(cellId,cell); + } + + public static void removeCell(String cellId){ + USER_RAM_GROUP_MAP.remove(cellId); + } + + public static void addUser(UserInfo userInfo, String cellId) { + USER_RAM_GROUP_MAP.get(cellId).addUser(userInfo); + } + + public static void removeUser(String faceId, String cellId) { + USER_RAM_GROUP_MAP.get(cellId).removeUser(faceId); + } + + public static List getUserList(String cellId) { + return USER_RAM_GROUP_MAP.get(cellId).getUserList(); + } + + public static void clear(){ + USER_RAM_GROUP_MAP.clear(); + } +} diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/FaceController.java b/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/FaceController.java index 3852217..3ec6278 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/FaceController.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/FaceController.java @@ -18,6 +18,7 @@ import net.shapelight.modules.face.entity.UserCompareInfo; import net.shapelight.modules.face.rpc.Response; import net.shapelight.modules.face.service.FaceEngineService; import net.shapelight.modules.face.util.Base64Util; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -71,47 +72,47 @@ public class FaceController { /* 人脸添加 */ - @RequestMapping(value = "/faceAdd", method = RequestMethod.POST) - @ResponseBody - public Response faceAdd(@RequestBody FaceAddReqDTO faceAddReqDTO) { - String image = faceAddReqDTO.getImage(); - - byte[] bytes = Base64Util.base64ToBytes(image); - ImageInfo rgbData = ImageFactory.getRGBData(bytes); - List faceInfoList = faceEngineService.detectFaces(rgbData); - if (CollectionUtil.isNotEmpty(faceInfoList)) { - for (FaceInfo faceInfo : faceInfoList) { - FaceRecognitionResDTO faceRecognitionResDTO = new FaceRecognitionResDTO(); - faceRecognitionResDTO.setRect(faceInfo.getRect()); - byte[] feature = faceEngineService.extractFaceFeature(rgbData, faceInfo,ExtractType.REGISTER); - if (feature != null) { - UserRamCache.UserInfo userInfo = new UserCompareInfo(); - userInfo.setFaceId(faceAddReqDTO.getName()); - userInfo.setName(faceAddReqDTO.getName()); - userInfo.setFaceFeature(feature); - //这边注册到内存缓存中,也可以根据业务,注册到数据库中 - UserRamCache.addUser(userInfo); - } - } - } - return Response.newSuccessResponse(""); - } +// @RequestMapping(value = "/faceAdd", method = RequestMethod.POST) +// @ResponseBody +// public Response faceAdd(@RequestBody FaceAddReqDTO faceAddReqDTO) { +// String image = faceAddReqDTO.getImage(); +// +// byte[] bytes = Base64Util.base64ToBytes(image); +// ImageInfo rgbData = ImageFactory.getRGBData(bytes); +// List faceInfoList = faceEngineService.detectFaces(rgbData); +// if (CollectionUtil.isNotEmpty(faceInfoList)) { +// for (FaceInfo faceInfo : faceInfoList) { +// FaceRecognitionResDTO faceRecognitionResDTO = new FaceRecognitionResDTO(); +// faceRecognitionResDTO.setRect(faceInfo.getRect()); +// byte[] feature = faceEngineService.extractFaceFeature(rgbData, faceInfo,ExtractType.REGISTER); +// if (feature != null) { +// UserInfo userInfo = new UserCompareInfo(); +// userInfo.setFaceId(faceAddReqDTO.getName()); +// userInfo.setName(faceAddReqDTO.getName()); +// userInfo.setFaceFeature(feature); +// //这边注册到内存缓存中,也可以根据业务,注册到数据库中 +// UserRamCache.addUser(userInfo); +// } +// } +// } +// return Response.newSuccessResponse(""); +// } - @RequestMapping(value = "/getFaceList", method = RequestMethod.POST) - @ResponseBody - public Response> getFaceList() { - List userList = UserRamCache.getUserList(); - List resDTOS = new LinkedList<>(); - for (UserRamCache.UserInfo userInfo : userList) { - GetFaceListResDTO face = new GetFaceListResDTO(); - face.setId(userInfo.getFaceId()); - face.setName(userInfo.getName()); - face.setUrl("/images/" + face.getId() + ".jpg"); - resDTOS.add(face); - } - return Response.newSuccessResponse(resDTOS); - } +// @RequestMapping(value = "/getFaceList", method = RequestMethod.POST) +// @ResponseBody +// public Response> getFaceList() { +// List userList = UserRamCache.getUserList(); +// List resDTOS = new LinkedList<>(); +// for (UserInfo userInfo : userList) { +// GetFaceListResDTO face = new GetFaceListResDTO(); +// face.setId(userInfo.getFaceId()); +// face.setName(userInfo.getName()); +// face.setUrl("/images/" + face.getId() + ".jpg"); +// resDTOS.add(face); +// } +// return Response.newSuccessResponse(resDTOS); +// } // @RequestMapping(value = "/recognition", method = RequestMethod.POST) @@ -156,35 +157,35 @@ public class FaceController { /* 人脸识别 */ - @RequestMapping(value = "/faceRecognition", method = RequestMethod.POST) - @ResponseBody - public Response> faceRecognition(@RequestBody FaceRecognitionReqDTO faceRecognitionReqDTO) { - String image = faceRecognitionReqDTO.getImage(); - - List faceRecognitionResDTOList = Lists.newLinkedList(); - byte[] bytes = Base64Util.base64ToBytes(image); - ImageInfo rgbData = ImageFactory.getRGBData(bytes); - List faceInfoList = faceEngineService.detectFaces(rgbData); - if (CollectionUtil.isNotEmpty(faceInfoList)) { - for (FaceInfo faceInfo : faceInfoList) { - FaceRecognitionResDTO faceRecognitionResDTO = new FaceRecognitionResDTO(); - faceRecognitionResDTO.setRect(faceInfo.getRect()); - byte[] feature = faceEngineService.extractFaceFeature(rgbData, faceInfo,ExtractType.RECOGNIZE); - if (feature != null) { - List userCompareInfos = faceEngineService.faceRecognition(feature, UserRamCache.getUserList(), 0.8f); - if (CollectionUtil.isNotEmpty(userCompareInfos)) { - faceRecognitionResDTO.setName(userCompareInfos.get(0).getName()); - faceRecognitionResDTO.setSimilar(userCompareInfos.get(0).getSimilar()); - } - } - faceRecognitionResDTOList.add(faceRecognitionResDTO); - } - - } - - - return Response.newSuccessResponse(faceRecognitionResDTOList); - } +// @RequestMapping(value = "/faceRecognition", method = RequestMethod.POST) +// @ResponseBody +// public Response> faceRecognition(@RequestBody FaceRecognitionReqDTO faceRecognitionReqDTO) { +// String image = faceRecognitionReqDTO.getImage(); +// +// List faceRecognitionResDTOList = Lists.newLinkedList(); +// byte[] bytes = Base64Util.base64ToBytes(image); +// ImageInfo rgbData = ImageFactory.getRGBData(bytes); +// List faceInfoList = faceEngineService.detectFaces(rgbData); +// if (CollectionUtil.isNotEmpty(faceInfoList)) { +// for (FaceInfo faceInfo : faceInfoList) { +// FaceRecognitionResDTO faceRecognitionResDTO = new FaceRecognitionResDTO(); +// faceRecognitionResDTO.setRect(faceInfo.getRect()); +// byte[] feature = faceEngineService.extractFaceFeature(rgbData, faceInfo,ExtractType.RECOGNIZE); +// if (feature != null) { +// List userCompareInfos = faceEngineService.faceRecognition(feature, UserRamCache.getUserList(), 0.8f); +// if (CollectionUtil.isNotEmpty(userCompareInfos)) { +// faceRecognitionResDTO.setName(userCompareInfos.get(0).getName()); +// faceRecognitionResDTO.setSimilar(userCompareInfos.get(0).getSimilar()); +// } +// } +// faceRecognitionResDTOList.add(faceRecognitionResDTO); +// } +// +// } +// +// +// return Response.newSuccessResponse(faceRecognitionResDTOList); +// } @RequestMapping(value = "/detectFaces", method = RequestMethod.POST) @ResponseBody diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/HttpApiController.java b/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/HttpApiController.java index 4a8c9ac..cca79bf 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/HttpApiController.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/httpapi/controler/HttpApiController.java @@ -26,7 +26,10 @@ import net.shapelight.modules.app.service.AppUserService; import net.shapelight.modules.face.dto.FaceRecognitionResDTO; import net.shapelight.modules.face.entity.UserCompareInfo; import net.shapelight.modules.face.service.FaceEngineService; +import net.shapelight.modules.face.util.Base64Util; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; +import net.shapelight.modules.face.util.UserRamGroup; import net.shapelight.modules.httpapi.service.AuthService; import net.shapelight.modules.httpapi.service.RecordSaveSyncService; import net.shapelight.modules.nettyapi.config.CmdConstant; @@ -1474,6 +1477,7 @@ public class HttpApiController { return res; } log.debug("设备端识别人员开始---------------------------------------------:"+sn); + TenDeviceEntity deviceEntity = tenDeviceService.findBySn(sn); // if (faceFile.isEmpty() || faceFile.getSize() == 0) { // return R.error("文件不能为空"); // } @@ -1496,7 +1500,8 @@ public class HttpApiController { // faceRecognitionResDTO.setRect(faceInfo.getRect()); byte[] feature = faceEngineService.extractFaceFeature(rgbData, faceInfo, ExtractType.RECOGNIZE); if (feature != null) { - List userCompareInfos = faceEngineService.faceRecognition(feature, UserRamCache.getUserList(), 0.8f); +// List userCompareInfos = faceEngineService.faceRecognition(feature, UserRamCache.getUserList(), 0.8f); + List userCompareInfos = faceEngineService.faceRecognition(feature, UserRamGroup.getUserList(deviceEntity.getCellId()+""), 0.8f); if (CollectionUtil.isNotEmpty(userCompareInfos)) { faceRecognitionResDTO = new FaceRecognitionResDTO(); faceRecognitionResDTO.setPersonId(userCompareInfos.get(0).getFaceId()); @@ -1523,7 +1528,7 @@ public class HttpApiController { //身份证比对成功,访客 //----------------------------------------以下业务----------------------------------------------------- log.debug("身份证人脸识别成功保存识别记录:"+sn); - TenDeviceEntity deviceEntity = tenDeviceService.findBySn(sn); +// TenDeviceEntity deviceEntity = tenDeviceService.findBySn(sn); TenRecordEntity record = new TenRecordEntity(); record.setDeviceSn(sn); @@ -1605,7 +1610,7 @@ public class HttpApiController { //----------------------------------------以下业务----------------------------------------------------- log.debug("人脸识别成功:"+sn); - TenDeviceEntity deviceEntity = tenDeviceService.findBySn(sn); +// TenDeviceEntity deviceEntity = tenDeviceService.findBySn(sn); boolean passFlag = false; if(tenPersonEntity.getPersonType() == Constant.PERSON_TYPE_GUEST){ //访客,直接判断有效期 @@ -1706,7 +1711,7 @@ public class HttpApiController { } }else{ if(icCard !=null && icCard.length()>0){ - TenDeviceEntity deviceEntity = tenDeviceService.findBySn(sn); +// TenDeviceEntity deviceEntity = tenDeviceService.findBySn(sn); TenPersonEntity tenPersonEntity = tenPersonService.findByIcCardTop(icCard,deviceEntity.getTenantId()+""); if(tenPersonEntity!=null){ @@ -1941,13 +1946,21 @@ public class HttpApiController { tenPersonService.updateByIdCard(personEntity); } //特征保存到内存 - UserRamCache.removeUser(personEntity.getPersonId()+""); - log.debug("内存删除人员:"+personEntity.getPersonId()+" 姓名:"+personEntity.getName()); - UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// UserRamCache.removeUser(personEntity.getPersonId()+""); +// log.debug("内存删除人员:"+personEntity.getPersonId()+" 姓名:"+personEntity.getName()); +// UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// userInfo.setFaceId(personEntity.getPersonId()+""); +// userInfo.setName(personEntity.getName()); +// userInfo.setFaceFeature(Base64.getDecoder().decode(personEntity.getFeature())); +// UserRamCache.addUser(userInfo); + UserRamGroup.removeUser(personEntity.getPersonId()+"",deviceEntity.getCellId()+""); + + UserInfo userInfo = new UserCompareInfo(); userInfo.setFaceId(personEntity.getPersonId()+""); userInfo.setName(personEntity.getName()); - userInfo.setFaceFeature(Base64.getDecoder().decode(personEntity.getFeature())); - UserRamCache.addUser(userInfo); + userInfo.setFaceFeature(Base64Util.base64ToBytes(personEntity.getFeature())); + //这边注册到内存缓存中 + UserRamGroup.addUser(userInfo,deviceEntity.getCellId()+""); log.debug("内存增加人员:"+personEntity.getPersonId()+" 姓名:"+personEntity.getName()); //-------------------------------------------------------------------------- JSONObject personRes = new JSONObject(); diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/job/task/FaceRestartTask.java b/shapelight-admin/src/main/java/net/shapelight/modules/job/task/FaceRestartTask.java index ccdfaf9..8397b07 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/job/task/FaceRestartTask.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/job/task/FaceRestartTask.java @@ -1,5 +1,6 @@ package net.shapelight.modules.job.task; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.minio.MinioClient; import lombok.extern.slf4j.Slf4j; import net.shapelight.common.config.GlobalValue; @@ -8,8 +9,11 @@ import net.shapelight.common.utils.RedisUtils; import net.shapelight.modules.face.entity.UserCompareInfo; import net.shapelight.modules.face.service.FaceEngineService; import net.shapelight.modules.face.util.Base64Util; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; +import net.shapelight.modules.face.util.UserRamGroup; import net.shapelight.modules.sys.service.SysUserService; +import net.shapelight.modules.ten.entity.TenCellEntity; import net.shapelight.modules.ten.entity.TenPersonEntity; import net.shapelight.modules.ten.entity.TenRecordEntity; import net.shapelight.modules.ten.service.TenCellService; @@ -28,37 +32,75 @@ public class FaceRestartTask implements ITask { private FaceEngineService faceEngineService; @Autowired private TenPersonService tenPersonService; + @Autowired + private TenCellService tenCellService; + @Autowired + private GlobalValue globalValue; @Override public void run(String params){ log.debug("faceEngin restart ............................................."); //clear all face - UserRamCache.clear(); +// UserRamCache.clear(); +// +// int count = tenPersonService.findCount(); +// int pageSize = 1000; +// int page = count/pageSize; +// if(count%1000!=0){ +// page = page+1; +// } +// int faceCount = 0; +// for (int i = 0; i < page; i++) { +// int start = i*1000; +// List listPage = tenPersonService.listPage(start,1000); +// for(TenPersonEntity personEntity: listPage){ +// if(personEntity.getFeature()!=null && personEntity.getFeature().length()>0){ +// UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// userInfo.setFaceId(personEntity.getPersonId()+""); +// userInfo.setName(personEntity.getName()); +// userInfo.setFaceFeature(Base64Util.base64ToBytes(personEntity.getFeature())); +// //这边注册到内存缓存中 +// UserRamCache.addUser(userInfo); +// faceCount++; +// } +// } +// } +// log.debug("初始化人脸库完成,共 "+faceCount+" 人"); - int count = tenPersonService.findCount(); - int pageSize = 1000; - int page = count/pageSize; - if(count%1000!=0){ - page = page+1; - } - int faceCount = 0; - for (int i = 0; i < page; i++) { - int start = i*1000; - List listPage = tenPersonService.listPage(start,1000); - for(TenPersonEntity personEntity: listPage){ - if(personEntity.getFeature()!=null && personEntity.getFeature().length()>0){ - UserRamCache.UserInfo userInfo = new UserCompareInfo(); - userInfo.setFaceId(personEntity.getPersonId()+""); - userInfo.setName(personEntity.getName()); - userInfo.setFaceFeature(Base64Util.base64ToBytes(personEntity.getFeature())); - //这边注册到内存缓存中 - UserRamCache.addUser(userInfo); - faceCount++; + + //清除分组 + UserRamGroup.clear(); + List cellList = tenCellService.list(new QueryWrapper() + .eq("tenant_id",globalValue.getTenantId()) + .eq("delete_flag",0)); + for(TenCellEntity cellEntity: cellList){ + String cellId = cellEntity.getCellId()+""; + UserRamGroup.addCell(cellId); + int count = tenPersonService.findCount(cellId); + int pageSize = 1000; + int page = count/pageSize; + if(count%1000!=0){ + page = page+1; + } + int faceCount = 0; + for (int i = 0; i < page; i++) { + int start = i*1000; + List listPage = tenPersonService.listPage(start,1000, cellId); + for(TenPersonEntity personEntity: listPage){ + if(personEntity.getFeature()!=null && personEntity.getFeature().length()>0){ + UserInfo userInfo = new UserCompareInfo(); + userInfo.setFaceId(personEntity.getPersonId()+""); + userInfo.setName(personEntity.getName()); + userInfo.setFaceFeature(Base64Util.base64ToBytes(personEntity.getFeature())); + //这边注册到内存缓存中 + UserRamGroup.addUser(userInfo,cellId); + faceCount++; + } } } + log.debug(cellEntity.getName()+":初始化人脸库完成,共 "+faceCount+" 人"); } - log.debug("初始化人脸库完成,共 "+faceCount+" 人"); log.debug("faceEngin restart end............................................."); } } diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/job/task/PersonProcessTask.java b/shapelight-admin/src/main/java/net/shapelight/modules/job/task/PersonProcessTask.java index 126f8e4..1c22cca 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/job/task/PersonProcessTask.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/job/task/PersonProcessTask.java @@ -8,6 +8,7 @@ import net.shapelight.common.utils.Constant; import net.shapelight.common.utils.DateUtils; import net.shapelight.modules.face.entity.UserCompareInfo; import net.shapelight.modules.face.util.UserRamCache; +import net.shapelight.modules.face.util.UserRamGroup; import net.shapelight.modules.nettyapi.service.ServerApiService; import net.shapelight.modules.sys.entity.SysDbBakEntity; import net.shapelight.modules.sys.service.SysDbBakService; @@ -52,7 +53,8 @@ public class PersonProcessTask implements ITask { //修改人员表 tenPersonService.updateStatusById(guest); //特征保存到内存 - UserRamCache.removeUser(guest.getPersonId()+""); + UserRamGroup.removeUser(guest.getPersonId()+"",guest.getCellId()+""); +// UserRamCache.removeUser(guest.getPersonId()+""); log.debug("内存删除人员:"+guest.getPersonId()+" 姓名:"+guest.getName()); //修改同步表 // List syncEntityList = tenPersonSyncService.findByPersonId(guest.getPersonId(),guest.getTenantId()); diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenCellController.java b/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenCellController.java index d682fec..0b64a1e 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenCellController.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenCellController.java @@ -10,6 +10,7 @@ import io.swagger.annotations.ApiOperation; import net.shapelight.common.annotation.SysLog; import net.shapelight.common.utils.Constant; import net.shapelight.common.utils.SnowflakeIdWorker; +import net.shapelight.modules.face.util.UserRamGroup; import net.shapelight.modules.sys.controller.AbstractController; import net.shapelight.modules.sys.entity.SysUserEntity; import net.shapelight.modules.sys.service.SysUserRoleService; @@ -108,6 +109,8 @@ public class TenCellController extends AbstractController { tenCell.setCreateTime(new Date()); tenCellService.save(tenCell); + UserRamGroup.addCell(id+""); + return R.ok(); } @@ -144,6 +147,8 @@ public class TenCellController extends AbstractController { tenCellService.removeByIds(Arrays.asList(ids)); //删除小区绑定的设备 tenDeviceService.evictRemoveByCellIds(ids); + + UserRamGroup.addCell(ids[0]+""); return R.ok(); } diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenPersonController.java b/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenPersonController.java index b9f4836..277a157 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenPersonController.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/ten/controller/TenPersonController.java @@ -26,7 +26,10 @@ import net.shapelight.modules.app.entity.AppUserEntity; import net.shapelight.modules.app.service.AppUserService; import net.shapelight.modules.excel.model.PersonModel; import net.shapelight.modules.face.entity.UserCompareInfo; +import net.shapelight.modules.face.util.Base64Util; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; +import net.shapelight.modules.face.util.UserRamGroup; import net.shapelight.modules.nettyapi.service.ServerApiService; import net.shapelight.modules.sys.controller.AbstractController; import net.shapelight.modules.sys.entity.SysUserEntity; @@ -168,6 +171,12 @@ public class TenPersonController extends AbstractController { } params.put("personType", Constant.PERSON_TYPE_GUEST); PageUtils page = tenPersonService.queryPage(params); +// Date now = new Date(); +// for (TenPersonEntity guest : (List)page.getList()) { +// if(guest.getLiveEnd().getTime() findByIdCardAndDept(@Param("idCard")String idCard, @Param("deptId")Long deptId); - List listPage(@Param("start")int start, @Param("count")int count); + List listPage(@Param("start")int start, @Param("count")int count, @Param("cellId")String cellId); - int findCount(); + int findCount(@Param("cellId")String cellId); int findByIdCardCount(@Param("idCard")String idCard, @Param("tenantId")String tenantId); diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/TenPersonService.java b/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/TenPersonService.java index 6e4cbc9..360b3e8 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/TenPersonService.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/TenPersonService.java @@ -145,9 +145,9 @@ public interface TenPersonService { List findByIdCardAndDept(String idCard, Long deptId); - List listPage(int start, int count); + List listPage(int start, int count, String cellId); - int findCount(); + int findCount(String cellId); TenLabelEntity findLabelByName(String name, String tenantId); diff --git a/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/impl/TenPersonServiceImpl.java b/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/impl/TenPersonServiceImpl.java index 2a5f5e7..e19619e 100644 --- a/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/impl/TenPersonServiceImpl.java +++ b/shapelight-admin/src/main/java/net/shapelight/modules/ten/service/impl/TenPersonServiceImpl.java @@ -27,7 +27,9 @@ import net.shapelight.modules.face.dto.FaceRecognitionResDTO; import net.shapelight.modules.face.entity.UserCompareInfo; import net.shapelight.modules.face.service.FaceEngineService; import net.shapelight.modules.face.util.Base64Util; +import net.shapelight.modules.face.util.UserInfo; import net.shapelight.modules.face.util.UserRamCache; +import net.shapelight.modules.face.util.UserRamGroup; import net.shapelight.modules.nettyapi.service.ServerApiService; import net.shapelight.modules.ten.entity.*; import net.shapelight.modules.ten.service.*; @@ -454,13 +456,22 @@ public class TenPersonServiceImpl implements TenPersonService { // } int flag = tenPersonDao.insert(entity); //特征保存到内存 - UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// userInfo.setFaceId(entity.getPersonId()+""); +// userInfo.setName(entity.getName()); +// userInfo.setFaceFeature(Base64.getDecoder().decode(entity.getFeature())); +// UserRamCache.addUser(userInfo); + + UserInfo userInfo = new UserCompareInfo(); userInfo.setFaceId(entity.getPersonId()+""); userInfo.setName(entity.getName()); - userInfo.setFaceFeature(Base64.getDecoder().decode(entity.getFeature())); - UserRamCache.addUser(userInfo); + userInfo.setFaceFeature(Base64Util.base64ToBytes(entity.getFeature())); + //这边注册到内存缓存中 + UserRamGroup.addUser(userInfo,entity.getCellId()+""); + log.debug("内存增加人员:"+entity.getPersonId()+" 姓名:"+entity.getName()); + ////访客添加失效redisKey // if(entity.getPersonType() == Constant.PERSON_TYPE_GUEST){ // @@ -893,7 +904,8 @@ public class TenPersonServiceImpl implements TenPersonService { //删除人员 tenPersonDao.logicDeleteById(personId, cellId); //基础人脸库 - UserRamCache.removeUser(personId+""); +// UserRamCache.removeUser(personId+""); + UserRamGroup.removeUser(personId+"",cellId+""); log.debug("内存移除人员:"+personId); //记录所有人员id @@ -1479,13 +1491,23 @@ public class TenPersonServiceImpl implements TenPersonService { //特征保存到内存 if(entity.getFeature()!=null && entity.getFeature().length()>0){ - UserRamCache.removeUser(entity.getPersonId()+""); +// UserRamCache.removeUser(entity.getPersonId()+""); +// log.debug("内存删除人员:"+entity.getPersonId()+" 姓名:"+entity.getName()); +// UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// userInfo.setFaceId(entity.getPersonId()+""); +// userInfo.setName(entity.getName()); +// userInfo.setFaceFeature(Base64.getDecoder().decode(entity.getFeature())); +// UserRamCache.addUser(userInfo); + + UserRamGroup.removeUser(entity.getPersonId()+"",entity.getCellId()+""); log.debug("内存删除人员:"+entity.getPersonId()+" 姓名:"+entity.getName()); - UserRamCache.UserInfo userInfo = new UserCompareInfo(); + UserInfo userInfo = new UserCompareInfo(); userInfo.setFaceId(entity.getPersonId()+""); userInfo.setName(entity.getName()); - userInfo.setFaceFeature(Base64.getDecoder().decode(entity.getFeature())); - UserRamCache.addUser(userInfo); + userInfo.setFaceFeature(Base64Util.base64ToBytes(entity.getFeature())); + //这边注册到内存缓存中 + UserRamGroup.addUser(userInfo,entity.getCellId()+""); + log.debug("内存增加人员:"+entity.getPersonId()+" 姓名:"+entity.getName()); //配置同步数据 List syncEntitys = tenPersonSyncService.findByPersonId(entity.getPersonId(), entity.getTenantId()); @@ -2159,11 +2181,20 @@ public class TenPersonServiceImpl implements TenPersonService { tenPersonDao.insert(tenPerson); //特征保存到内存 if(tenPerson.getFeature()!=null && tenPerson.getFeature().length()>0){ - UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// UserRamCache.UserInfo userInfo = new UserCompareInfo(); +// userInfo.setFaceId(tenPerson.getPersonId()+""); +// userInfo.setName(tenPerson.getName()); +// userInfo.setFaceFeature(Base64.getDecoder().decode(tenPerson.getFeature())); +// UserRamCache.addUser(userInfo); +// log.debug("内存增加人员:"+tenPerson.getPersonId()+" 姓名:"+tenPerson.getName()); + + UserInfo userInfo = new UserCompareInfo(); userInfo.setFaceId(tenPerson.getPersonId()+""); userInfo.setName(tenPerson.getName()); - userInfo.setFaceFeature(Base64.getDecoder().decode(tenPerson.getFeature())); - UserRamCache.addUser(userInfo); + userInfo.setFaceFeature(Base64Util.base64ToBytes(tenPerson.getFeature())); + //这边注册到内存缓存中 + UserRamGroup.addUser(userInfo,tenPerson.getCellId()+""); + log.debug("内存增加人员:"+tenPerson.getPersonId()+" 姓名:"+tenPerson.getName()); } @@ -2487,13 +2518,13 @@ public class TenPersonServiceImpl implements TenPersonService { } @Override - public List listPage(int start, int count) { - return tenPersonDao.listPage(start, count); + public List listPage(int start, int count, String cellId) { + return tenPersonDao.listPage(start, count, cellId); } @Override - public int findCount() { - return tenPersonDao.findCount(); + public int findCount(String cellId) { + return tenPersonDao.findCount(cellId); } @Override diff --git a/shapelight-admin/src/main/resources/mapper/ten/TenPersonDao.xml b/shapelight-admin/src/main/resources/mapper/ten/TenPersonDao.xml index 49c37af..967d5bd 100644 --- a/shapelight-admin/src/main/resources/mapper/ten/TenPersonDao.xml +++ b/shapelight-admin/src/main/resources/mapper/ten/TenPersonDao.xml @@ -1353,11 +1353,19 @@