FloorplanAiServiceImpl.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.ObjUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  10. import com.fdkankan.model.constants.UploadFilePath;
  11. import com.fdkankan.model.utils.ComputerUtil;
  12. import com.fdkankan.scene.constant.CmdConstant;
  13. import com.fdkankan.scene.constant.DetectType;
  14. import com.fdkankan.scene.entity.SceneMarkShape;
  15. import com.fdkankan.scene.oss.OssUtil;
  16. import com.fdkankan.scene.service.FloorplanAiService;
  17. import com.fdkankan.scene.service.ISceneMarkShapeService;
  18. import com.fdkankan.scene.util.CmdUtils;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import javax.annotation.Resource;
  23. import java.io.IOException;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.*;
  26. @Slf4j
  27. @Service
  28. public class FloorplanAiServiceImpl implements FloorplanAiService {
  29. @Autowired
  30. private ISceneMarkShapeService sceneMarkShapeService;
  31. @Resource
  32. private OssUtil ossUtil;
  33. @Override
  34. public LinkedHashMap<Integer, Boolean> detFloorplan(String path) throws Exception {
  35. LinkedHashMap<Integer, Boolean> result = new LinkedHashMap<>();
  36. String workDir = path + "/detFloorplan/";
  37. if(FileUtil.exist(workDir)){
  38. FileUtil.del(workDir);
  39. }
  40. String infoJsonPath = path + "/results/floorplan/info.json";
  41. if(!FileUtil.exist(infoJsonPath)){
  42. return result;
  43. }
  44. JSONObject infoObj = JSON.parseObject(FileUtil.readUtf8String(infoJsonPath));
  45. JSONArray floors = infoObj.getJSONArray("floors");
  46. if(CollUtil.isEmpty(floors)){
  47. return result;
  48. }
  49. for (Object o : floors) {
  50. JSONObject floor = (JSONObject) o;
  51. Integer subgroup = floor.getInteger("subgroup");
  52. String detectPath = workDir + subgroup + "/detect.json";
  53. String floorKeyPath = path + "/results/floorplan/floor_" + subgroup + ".png";
  54. String parent = FileUtil.getParent(detectPath, 1);
  55. FileUtil.mkdir(parent);
  56. result.put(subgroup, false);
  57. if(!FileUtil.exist(floorKeyPath)){
  58. continue;
  59. }
  60. String cmd = CmdConstant.LAYOUT_DETECT;
  61. cmd = cmd.replace("@in", floorKeyPath);
  62. cmd = cmd.replace("@out", detectPath);
  63. CmdUtils.callLineWin(cmd, 50);
  64. if (ComputerUtil.checkComputeCompleted(detectPath,5, 500)) {
  65. result.put(subgroup, true);
  66. }
  67. }
  68. return result;
  69. }
  70. @Override
  71. public boolean detFloorPlanAi(String num, String path, LinkedHashMap<Integer, Boolean> detFloorplan, String bucket) throws IOException {
  72. try {
  73. String aiJsonKey = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "floorplan/ai.json";
  74. //先清空历史数据,因为数据格式改动很多个版本,已经无法按照规律进行过滤删除,所以这里先删除历史数据,而后再根据算法生成去插入数据
  75. sceneMarkShapeService.remove(new LambdaQueryWrapper<SceneMarkShape>().eq(SceneMarkShape::getNum, num).eq(SceneMarkShape::getType, DetectType.PLAN.getCode()));
  76. if(ossUtil.doesObjectExist(bucket, aiJsonKey)){
  77. ossUtil.deleteObject(bucket, aiJsonKey);
  78. }
  79. if(CollUtil.isEmpty(detFloorplan)){
  80. return false;
  81. }
  82. boolean hasFloorplanAi = false;
  83. String workDir = path + "/detFloorplan/";
  84. for (Integer subgroup : detFloorplan.keySet()) {
  85. Boolean yes = detFloorplan.get(subgroup);
  86. SceneMarkShape sceneMarkShape = null;
  87. if(yes){
  88. String detectPath = workDir + subgroup + "/detect.json";
  89. sceneMarkShape = sceneMarkShapeService.readDetectJson(detectPath);
  90. }
  91. if (ObjUtil.isNull(sceneMarkShape)){
  92. sceneMarkShape = new SceneMarkShape();
  93. String infoJsonPath = path + "/results/floorplan/info.json";
  94. JSONObject infoObj = JSON.parseObject(FileUtil.readUtf8String(infoJsonPath));
  95. JSONArray floors = infoObj.getJSONArray("floors");
  96. for (Object floor : floors) {
  97. JSONObject floorObj = (JSONObject)floor;
  98. Integer group = floorObj.getInteger("subgroup");
  99. if(!subgroup.equals(group)){
  100. continue;
  101. }
  102. JSONObject resolution = floorObj.getJSONObject("resolution");
  103. sceneMarkShape.setImageWidth(resolution.getInteger("width"));
  104. sceneMarkShape.setImageHeight(resolution.getInteger("height"));
  105. }
  106. }
  107. if(Objects.isNull(sceneMarkShape.getShapes())){
  108. sceneMarkShape.setShapes(new ArrayList<>());
  109. }
  110. sceneMarkShape.setNum(num);
  111. sceneMarkShape.setCreateTime(new Date());
  112. sceneMarkShape.setType(DetectType.PLAN.getCode());
  113. sceneMarkShapeService.save(sceneMarkShape);
  114. if(CollUtil.isNotEmpty(sceneMarkShape.getShapes())){
  115. hasFloorplanAi = true;
  116. }
  117. }
  118. List<SceneMarkShape> sceneMarkShapes = sceneMarkShapeService.findByNumAndType(num, DetectType.PLAN.getCode());
  119. for (SceneMarkShape ms : sceneMarkShapes) {
  120. if (ObjectUtil.isNotEmpty(ms.getShapes())){
  121. for (JSONObject s : ms.getShapes()) {
  122. String category = s.getString("category");
  123. if (category.contains("Tag_")){
  124. s.put("category","Tag");
  125. }
  126. }
  127. }
  128. }
  129. ossUtil.uploadFileBytes(bucket, aiJsonKey, JSON.toJSONString(sceneMarkShapes).getBytes(StandardCharsets.UTF_8));
  130. return hasFloorplanAi;
  131. }catch (Exception e){
  132. log.error("平面图ai识别处理报错", e);
  133. return false;
  134. }finally {
  135. // FileUtil.del(workDir);
  136. }
  137. }
  138. }