From d976d8824b53235d818ca004f3fb7d523e9c7d1c Mon Sep 17 00:00:00 2001 From: coco1986509808 <1986509808@qq.com> Date: Tue, 18 Apr 2023 17:54:28 +0800 Subject: [PATCH] =?UTF-8?q?4.18=20=E5=90=8E=E7=AB=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/modules/vo/CompareFaceList.java | 40 ++++ .../common/modules/vo/face/FaceResEntity.java | 17 ++ .../dkha/server/FaceServerApplication.java | 2 + .../server/controllers/FaceController.java | 155 +++++++++----- .../dkha/server/controllers/IndexContral.java | 1 + .../server/controllers/WebSocketConfig.java | 19 ++ .../server/controllers/WebSocketContral.java | 197 ++++++++++++++++++ .../services/impl/CompareServiceImpl.java | 2 +- .../modules/security/config/ShiroConfig.java | 1 + .../service/impl/SysUserTokenServiceImpl.java | 2 + .../sys/controller/SysMenuController.java | 1 - .../sys/service/impl/SysMenuServiceImpl.java | 1 + .../java/com/dkha/task/service/Datatest.java | 44 ++++ .../dkha/task/service/WebSocketContral.java | 24 +-- 14 files changed, 440 insertions(+), 66 deletions(-) create mode 100644 face-common/src/main/java/com/dkha/common/modules/vo/CompareFaceList.java create mode 100644 face-common/src/main/java/com/dkha/common/modules/vo/face/FaceResEntity.java create mode 100644 face-server/src/main/java/com/dkha/server/controllers/WebSocketConfig.java create mode 100644 face-server/src/main/java/com/dkha/server/controllers/WebSocketContral.java create mode 100644 face-task/src/main/java/com/dkha/task/service/Datatest.java diff --git a/face-common/src/main/java/com/dkha/common/modules/vo/CompareFaceList.java b/face-common/src/main/java/com/dkha/common/modules/vo/CompareFaceList.java new file mode 100644 index 0000000..15211ef --- /dev/null +++ b/face-common/src/main/java/com/dkha/common/modules/vo/CompareFaceList.java @@ -0,0 +1,40 @@ +package com.dkha.common.modules.vo; +import com.dkha.common.modules.vo.position.PositionVO; +import com.fasterxml.jackson.annotation.JsonInclude; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor +@Data +//@JsonInclude(JsonInclude.Include.NON_NULL) +@ApiModel(value="视频页面比对展示的vo", description="前端页面比对人脸库展示数据") +public class CompareFaceList { + + + @ApiModelProperty(value = "时间") + private String time; + + @ApiModelProperty(value = "原始人脸地址") + private String cphoto; + @ApiModelProperty(value = "比对最相似的人脸地址") + private String rphoto; + @ApiModelProperty(value = "比对相似度") + private double xsd; + @ApiModelProperty(value = "姓名") + private String name; +// @ApiModelProperty(value = "性别") +// private String sex; +// @ApiModelProperty(value = "身份证号码") +// private String idCard; +// @ApiModelProperty(value = "年龄") +// private Integer age; + @ApiModelProperty(value = "库名称") + private String libName; +// @ApiModelProperty(value = "库id") +// private String libId; + +} diff --git a/face-common/src/main/java/com/dkha/common/modules/vo/face/FaceResEntity.java b/face-common/src/main/java/com/dkha/common/modules/vo/face/FaceResEntity.java new file mode 100644 index 0000000..990450c --- /dev/null +++ b/face-common/src/main/java/com/dkha/common/modules/vo/face/FaceResEntity.java @@ -0,0 +1,17 @@ +package com.dkha.common.modules.vo.face; +import com.fasterxml.jackson.annotation.JsonInclude; +import io.swagger.annotations.ApiModel; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +@ApiModel(value = "人脸库1:1比对返回", description = "人脸检索") +public class FaceResEntity { + private String cphoto; + //private String rphoto; + private String time; +} diff --git a/face-server/src/main/java/com/dkha/server/FaceServerApplication.java b/face-server/src/main/java/com/dkha/server/FaceServerApplication.java index 12e40ca..a5b3da9 100644 --- a/face-server/src/main/java/com/dkha/server/FaceServerApplication.java +++ b/face-server/src/main/java/com/dkha/server/FaceServerApplication.java @@ -8,6 +8,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; +import com.dkha.server.controllers.WebSocketContral; import java.net.InetSocketAddress; @@ -23,6 +24,7 @@ public class FaceServerApplication { SpringApplication springApplication = new SpringApplication(FaceServerApplication.class); ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args); //解决WebSocket不能注入的问题 + WebSocketContral.setApplicationContext(configurableApplicationContext); } /** diff --git a/face-server/src/main/java/com/dkha/server/controllers/FaceController.java b/face-server/src/main/java/com/dkha/server/controllers/FaceController.java index 2a7ddaf..b98219b 100644 --- a/face-server/src/main/java/com/dkha/server/controllers/FaceController.java +++ b/face-server/src/main/java/com/dkha/server/controllers/FaceController.java @@ -1,45 +1,21 @@ package com.dkha.server.controllers; -import com.alibaba.fastjson.JSONObject; -import com.dkha.common.exception.DkException; import com.dkha.common.fileupload.MinioUtil; -import com.dkha.common.page.PageParam; +import com.dkha.common.http.HttpUtil; +import com.dkha.common.modules.vo.face.FaceResEntity; +import com.dkha.common.modules.vo.CompareFaceList; import com.dkha.common.result.CommonResult; -import com.dkha.common.util.ExcelUtils; -import com.dkha.common.util.UtilValidate; -import com.dkha.server.annotation.NoRepeatSubmitAnnotation; -import com.dkha.server.modules.entities.FaceCameraEntity; -import com.dkha.server.modules.entities.faceCamera.FaceCameraExcel; -import com.dkha.server.modules.entities.faceCamera.FaceCameraInfo; -import com.dkha.server.modules.entities.faceCamera.InsertFaceCamera; -import com.dkha.server.services.FaceCameraService; -import com.dkha.server.system.modules.sys.entity.SysRegionEntity; -import com.dkha.server.system.modules.sys.enums.RegionLevelEnum; -import com.dkha.server.system.modules.sys.service.SysRegionService; -import com.dkha.server.util.RequestUtil; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import lombok.Data; +import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; -import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; -import sun.misc.BASE64Decoder; import javax.annotation.Resource; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.validation.Valid; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.*; -import static com.dkha.common.systemcode.SystemCode.EXCEL_ERROR_DATA; - @RestController @RequestMapping("faces") @@ -47,18 +23,27 @@ import static com.dkha.common.systemcode.SystemCode.EXCEL_ERROR_DATA; public class FaceController extends CommonResult { @Resource private MinioUtil minioUtil; + @Value("${api.server.prefix}") + private String apiServerPrefix; + @Autowired + private HttpUtil httpUtil; + @Autowired + private Gson gson; + private int count ; private Map faceRes = new HashMap<>(); - private List list = new ArrayList<>(); + private List facelist = new ArrayList<>(); + + private List compareFaceLists = new ArrayList<>(); public FaceController(){ - FaceResEntity resEntity = new FaceResEntity(); - resEntity.setName("jhx"); - resEntity.setRphoto("http://192.168.60.52:9000/school/face/jhx.jpg"); - faceRes.put("eYJST7eY2mbnjGZvywPfxg==",resEntity); + //FaceResEntity resEntity = new FaceResEntity(); + //resEntity.setTime("jhx"); + // resEntity.setRphoto("http://192.168.60.52:9000/school/face/jhx.jpg"); + //faceRes.put("eYJST7eY2mbnjGZvywPfxg==",resEntity); } - @RequestMapping("callback") + /*@RequestMapping("callback") public String callback(HttpServletRequest request) throws IOException { try { String streamText = RequestUtil.getStreamText(request); @@ -69,13 +54,14 @@ public class FaceController extends CommonResult { byte[] _byte = new BASE64Decoder().decodeBuffer(str); InputStream inputStream = new ByteArrayInputStream(_byte); - String object1 = "http://192.168.60.52:9000/test/"+minioUtil.uploadImage(inputStream,"").getString("path"); + String object1 = "http://192.168.1.133:9000/middleware/"+minioUtil.uploadImage(inputStream,"").getString("path"); System.out.println(object1); if( faceRes.get(object.getString("ImageID"))!=null){ FaceResEntity resEntity = faceRes.get(object.getString("ImageID")); resEntity.setCphoto(object1); - resEntity.setXsd(xsd.substring(0,5)); + //resEntity.setXsd(xsd.substring(0,5)); list.add(resEntity); + } } catch (Exception e) { e.printStackTrace(); @@ -85,10 +71,88 @@ public class FaceController extends CommonResult { @GetMapping("list") public List getList(){ + list.clear(); + //System.out.println("返回list测试:" + list); + //String testUrl = apiServerPrefix + ApiUrlEnum.FACE_SEARCH.getUrl(); + String testUrl = "http://192.168.1.196:5000/cutimage"; + System.out.println("testurl:"+ testUrl); + ReturnVO returnVO = (ReturnVO) httpUtil.post(testUrl, null, ReturnVO.class); + List resEntity = JsonUtil.string2Obj(JsonUtil.obj2String(returnVO.getData()), new TypeReference>() {}); + //FaceResEntity resEntity = new FaceResEntity(); + //resEntity.setName("ceshi"); + //resEntity.setCphoto("http://192.168.1.133:9000/middleware/103/1028345463.jpg"); + for(FaceResEntity res :resEntity) { + System.out.println("resEntity cphoto:"+ res.getCphoto()); + list.add(res); + } + System.out.println("返回list测试:" + list); return list; + //return (List) successResult(list); + } +*/ + @GetMapping( "returnList") + public List returnList(){ + System.out.println("开始returnlist"); + System.out.println(facelist.size()); + return facelist; } -/* @GetMapping("one") + + //从算法端获取图片列表接口 + @PostMapping("/getList") + public void getList( + @RequestBody List resEntities) { + System.out.println("getlist被请求"); + facelist.clear(); + //System.out.println(resEntities); + for (FaceResEntity res :resEntities){ + count++; + facelist.add(res); + } + System.out.println(facelist); + WebSocketContral webSocketContral =new WebSocketContral(); + webSocketContral.sendMessage(gson.toJson(facelist)); + //webSocketContral.sendMessage("发送websocket信息中"); + } + + public void sendWebsocket1(List onfacelist){ + facelist.clear(); + facelist = onfacelist; + WebSocketContral webSocketContral =new WebSocketContral(); + System.out.println("facelist"+ facelist); + System.out.println("GSON" + gson.toJson(facelist)); + webSocketContral.sendMessage(gson.toJson(facelist)); + //webSocketContral.sendMessage("发送websocket信息中"); + } + + public void sendWebsocket2(List oncompareFacelist){ + compareFaceLists.clear(); + compareFaceLists = oncompareFacelist; + WebSocketContral webSocketContral =new WebSocketContral(); + webSocketContral.sendMessage(gson.toJson(oncompareFacelist)); + //webSocketContral.sendMessage("发送websocket信息中"); + System.out.println("list:"+ oncompareFacelist); + } + + @PostMapping("/compareList") + public void compareList( + @RequestBody List compareFaceLists ){ +// String compareURL = apiServerPrefix + ApiUrlEnum.ALARMS_POSTURL.getUrl(); +// ReturnVO returnVO = (ReturnVO) httpUtil.post(compareURL, list, ApiVO.class); +// CompareFaceList compareFaceList = JsonUtil.string2Obj(JsonUtil.obj2String(returnVO.getData()), new TypeReference() {}); +// compareFaceLists.add(compareFaceList); +// return successResult(compareFaceLists); + compareFaceLists.clear(); + for(CompareFaceList com: compareFaceLists ){ + compareFaceLists.add(com); + } + } + + public List getFacelist() { + return this.facelist; + } + + /* @GetMapping("one") public List webSocketSet=new CopyOnWriteArraySet(); + public static Map myMap=new ConcurrentHashMap(); + + private RedisUtils redisUtils; + //此处是解决无法注入的关键 + private static ApplicationContext applicationContext; + //要注入的service或者dao +// private SysUserTokenDao sysUserTokenDao; + + public static void setApplicationContext(ApplicationContext applicationContext) { + WebSocketContral.applicationContext = applicationContext; + } + + @OnOpen + public void opOpen(Session session) + { + redisUtils =applicationContext.getBean(RedisUtils.class); + this.session=session; + if(myMap.containsKey(session.getId())) + { + try { + this.session.close(); + } catch (IOException e) { + } +// log.error("已经存在"); + return; + } +// sysUserTokenDao=applicationContext.getBean(SysUserTokenDao.class); +// String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()); +// SysUserTokenEntity sysUserTokenEntity= null;//sysUserTokenDao.getByToken(token,time); +// if(UtilValidate.isEmpty(sysUserTokenEntity)) +// { +// log.error("token过期"); +// sessionSendMsg(session,gson.toJson(new ReturnVO(ErrEnum.UNAUTHORIZED))); +// try { +// this.session.close(); +// } catch (Exception e) { +// } +// return; +// } +// //广播使用 +// redisUtils.set("token",token); +// //对应使用 +// redisUtils.set(RedisKeys.getSessionIdKey(session.getId()),token); +// TimerData.tokens.put(session.getId(),token); + webSocketSet.add(this); + myMap.put(session.getId(), this); +// log.info(session.getId()+"【websocket消息】有新的连接,总数:{},{},{}",webSocketSet.size(),myMap.size(),token); + + } + @OnClose + public void onClose() + { +// log.info(this.session.getId()+"【websocket消息】连接断开,总数:{},{}",webSocketSet.size(),myMap.size()); +// if(TimerData.bayonetMap.containsKey(this.session.getId())) { +// TimerData.bayonetMap.remove(this.session.getId()); +// } + myMap.remove(this.session.getId()); + webSocketSet.remove(this); +// log.info("关闭成功{},{},{}",webSocketSet.size(),myMap.size(),TimerData.bayonetMap.size()); + } + @OnMessage + public void onMessage(String message) + { +// log.error("接收数据:{}",message); +// if(UtilValidate.isEmpty(this.session) || this.session.isOpen()==false){return;} + //System.out.println(message); + if(UtilValidate.isNotEmpty(message)) + { + ReturnVO returnVO=gson.fromJson(message,ReturnVO.class); + System.out.println(message); + System.out.println(returnVO.getMessage()); + System.out.println(returnVO.getMessage().getClass()); + if(returnVO.getMessage().equals("okpro")) { + System.out.println("进入第一个方法"); + if (UtilValidate.isNotEmpty(returnVO.getData())) { + // List facelist =gson.fromJson(gson.toJson(returnVO.getData()),List.class); + List onfacelist = JsonUtil.string2Obj(JsonUtil.obj2String(returnVO.getData()), new TypeReference>() {}); + FaceController faceController = new FaceController(); + faceController.sendWebsocket1(onfacelist); + } + } + else if(returnVO.getMessage().equals("okplus")){ + if (UtilValidate.isNotEmpty(returnVO.getData())) { + // List facelist =gson.fromJson(gson.toJson(returnVO.getData()),List.class); + List oncompareFaceLists = JsonUtil.string2Obj(JsonUtil.obj2String(returnVO.getData()), new TypeReference>() {}); + FaceController faceController = new FaceController(); + faceController.sendWebsocket2(oncompareFaceLists); + } + } + + } + log.info("【websocket消息】收到客户端消息:{}",message); + } + + + /** + * 给指定id用户推送信息 + * @param message + */ + public void sendMessage(String id,String message) + { + for(WebSocketContral websocket:webSocketSet) + { + if(websocket.session.getId().equals(id)) + { +// log.info("【websocket消息】给ID={}发送消息,message={}",id,message); + sessionSendMsg(websocket.session,message); + break; + } + + } + } + /** + * 群发消息调用 + * @param message + */ + public void sendMessage(String message) + { + for(WebSocketContral websocket:webSocketSet) + { +// log.info("【websocket消息】广播消息,message={}",message); + sessionSendMsg(websocket.session,message); + System.out.println("后台在发送websocket信息"); + } + } + + /** + * 发送消息 + * @param session + * @param message + */ + private void sessionSendMsg(Session session,String message) + { + try { + session.getBasicRemote().sendText(message); + } catch (Exception e) { + log.error("socket发送消息错误:"+e.getMessage()); + } + } + + +} diff --git a/face-server/src/main/java/com/dkha/server/services/impl/CompareServiceImpl.java b/face-server/src/main/java/com/dkha/server/services/impl/CompareServiceImpl.java index 57d2ff0..1dcea39 100644 --- a/face-server/src/main/java/com/dkha/server/services/impl/CompareServiceImpl.java +++ b/face-server/src/main/java/com/dkha/server/services/impl/CompareServiceImpl.java @@ -169,7 +169,7 @@ public class CompareServiceImpl implements ICompareService { private void validateSingleFaceImage(String image) { //人脸检测url String faceSearchUrl = apiRequestPrefix + ApiUrlEnum.FACE_SEARCH.getUrl(); - //String faceSearchUrltest ="http://192.168.3.135:5000/message/mask"; + //String faceSearchUrltest ="http://192.168.3.123:5001/message/mask"; //组装api请求数据 ReturnVO returnVO = (ReturnVO) httpUtil.post(faceSearchUrl + "?url=" + image, null, ReturnVO.class); System.out.println(returnVO.getCode()+returnVO.getMessage()); diff --git a/face-server/src/main/java/com/dkha/server/system/modules/security/config/ShiroConfig.java b/face-server/src/main/java/com/dkha/server/system/modules/security/config/ShiroConfig.java index 820a53a..0a67c33 100644 --- a/face-server/src/main/java/com/dkha/server/system/modules/security/config/ShiroConfig.java +++ b/face-server/src/main/java/com/dkha/server/system/modules/security/config/ShiroConfig.java @@ -71,6 +71,7 @@ public class ShiroConfig { filterMap.put("/webjars/**", "anon"); filterMap.put("/druid/**", "anon"); filterMap.put("/login", "anon"); + filterMap.put("/webSocketServer", "anon"); filterMap.put("/swagger/**", "anon"); filterMap.put("/v2/api-docs", "anon"); filterMap.put("/swagger-ui.html", "anon"); diff --git a/face-server/src/main/java/com/dkha/server/system/modules/security/service/impl/SysUserTokenServiceImpl.java b/face-server/src/main/java/com/dkha/server/system/modules/security/service/impl/SysUserTokenServiceImpl.java index 4bd1c7b..6025779 100644 --- a/face-server/src/main/java/com/dkha/server/system/modules/security/service/impl/SysUserTokenServiceImpl.java +++ b/face-server/src/main/java/com/dkha/server/system/modules/security/service/impl/SysUserTokenServiceImpl.java @@ -70,6 +70,8 @@ public class SysUserTokenServiceImpl extends BaseServiceImpl> nav(){ UserDetail user = SecurityUser.getUser(); List list = sysMenuService.getUserMenuList(user, MenuTypeEnum.MENU.value()); - return new Result>().ok(list); } diff --git a/face-server/src/main/java/com/dkha/server/system/modules/sys/service/impl/SysMenuServiceImpl.java b/face-server/src/main/java/com/dkha/server/system/modules/sys/service/impl/SysMenuServiceImpl.java index 47f8f08..e329312 100644 --- a/face-server/src/main/java/com/dkha/server/system/modules/sys/service/impl/SysMenuServiceImpl.java +++ b/face-server/src/main/java/com/dkha/server/system/modules/sys/service/impl/SysMenuServiceImpl.java @@ -99,6 +99,7 @@ public class SysMenuServiceImpl extends BaseServiceImpl getFaceList() { + FaceController faceController = new FaceController(); + System.out.println(faceController.getFacelist()); + return faceController.getFacelist(); + } + +} diff --git a/face-task/src/main/java/com/dkha/task/service/WebSocketContral.java b/face-task/src/main/java/com/dkha/task/service/WebSocketContral.java index 509aca4..8e2fa12 100644 --- a/face-task/src/main/java/com/dkha/task/service/WebSocketContral.java +++ b/face-task/src/main/java/com/dkha/task/service/WebSocketContral.java @@ -68,18 +68,18 @@ public class WebSocketContral { return; } sysUserTokenDao=applicationContext.getBean(SysUserTokenDao.class); - String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()); - SysUserTokenEntity sysUserTokenEntity= null;//sysUserTokenDao.getByToken(token,time); - if(UtilValidate.isEmpty(sysUserTokenEntity)) - { - log.error("token过期"); - sessionSendMsg(session,gson.toJson(new ReturnVO(ErrEnum.UNAUTHORIZED))); - try { - this.session.close(); - } catch (Exception e) { - } - return; - } +// String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()); +// SysUserTokenEntity sysUserTokenEntity= null;//sysUserTokenDao.getByToken(token,time); +// if(UtilValidate.isEmpty(sysUserTokenEntity)) +// { +// log.error("token过期"); +// sessionSendMsg(session,gson.toJson(new ReturnVO(ErrEnum.UNAUTHORIZED))); +// try { +// this.session.close(); +// } catch (Exception e) { +// } +// return; +// } // //广播使用 // redisUtils.set("token",token); // //对应使用