AiServiceImpl.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.ObjectUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.fdkankan.common.constant.CommonStatus;
  8. import com.fdkankan.common.util.CmdUtils;
  9. import com.fdkankan.model.constants.UploadFilePath;
  10. import com.fdkankan.model.utils.ComputerUtil;
  11. import com.fdkankan.scene.constant.CmdConstant;
  12. import com.fdkankan.scene.constant.DetectType;
  13. import com.fdkankan.scene.entity.SceneMarkShape;
  14. import com.fdkankan.scene.entity.ScenePlus;
  15. import com.fdkankan.scene.entity.ScenePlusExt;
  16. import com.fdkankan.scene.entity.SceneShapeEnum;
  17. import com.fdkankan.scene.oss.OssUtil;
  18. import com.fdkankan.scene.service.*;
  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.File;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.Date;
  26. import java.util.List;
  27. @Slf4j
  28. @Service
  29. public class AiServiceImpl implements IAiService {
  30. @Autowired
  31. private IScenePlusService scenePlusService;
  32. @Autowired
  33. private IScenePlusExtService scenePlusExtService;
  34. @Resource
  35. private OssUtil fYunFileService;
  36. @Autowired
  37. private SceneShapeEnumService sceneShapeEnumService;
  38. @Autowired
  39. private ISceneMarkShapeService sceneMarkShapeService;
  40. @Override
  41. public void detectScenePano(ScenePlus scenePlus, ScenePlusExt scenePlusExt, String path) {
  42. try {
  43. String resultsPath = path + File.separator + "results" + File.separator;
  44. String highPath = resultsPath + "high" + File.separator;
  45. String aiWorkPath = highPath + "ai" + File.separator;
  46. List<File> highImgs = FileUtil.loopFiles(highPath);
  47. if(CollUtil.isEmpty(highImgs)){
  48. return;
  49. }
  50. FileUtil.mkdir(aiWorkPath);
  51. for (File file : highImgs) {
  52. String absolutePath = file.getAbsolutePath();
  53. try {
  54. String name = FileUtil.getName(absolutePath);
  55. String prefix = FileUtil.getPrefix(name);
  56. String outPath = aiWorkPath + prefix + File.separator;
  57. FileUtil.mkdir(outPath);
  58. String detectPath = outPath + "detect.json";
  59. String cmd = CmdConstant.PANO_DETECT.replace("@in",absolutePath).replace("@out", detectPath);
  60. CmdUtils.callLine(cmd, 50);
  61. }catch (Exception e){
  62. log.error("ai识别报错,inPath:{}", absolutePath, e);
  63. }
  64. }
  65. for (File file : highImgs) {
  66. String absolutePath = file.getAbsolutePath();
  67. try {
  68. String name = FileUtil.getName(absolutePath);
  69. String prefix = FileUtil.getPrefix(name);
  70. String outPath = aiWorkPath + prefix + File.separator;
  71. String detectPath = outPath + "detect.json";
  72. String cutImagesPath = outPath + "cut_images";
  73. if(!ComputerUtil.checkComputeCompleted(detectPath, 5, 200)){
  74. continue;
  75. }
  76. SceneMarkShape sceneMarkShape = readDetectJson(detectPath);
  77. if (ObjectUtil.isNotNull(sceneMarkShape)){
  78. sceneMarkShape.setNum(scenePlus.getNum());
  79. SceneMarkShape shape = sceneMarkShapeService.findByNumAndImagePathAndType(scenePlus.getNum(), sceneMarkShape.getImagePath(), DetectType.PANO.getCode());
  80. if (ObjectUtil.isNotNull(shape)){
  81. sceneMarkShape.setId(shape.getId());
  82. sceneMarkShape.setUpdateTime(new Date());
  83. sceneMarkShapeService.updateById(sceneMarkShape);
  84. }else {
  85. sceneMarkShape.setCreateTime(new Date());
  86. sceneMarkShape.setType(DetectType.PANO.getCode());
  87. sceneMarkShapeService.save(sceneMarkShape);
  88. }
  89. }
  90. if (FileUtil.exist(cutImagesPath)){
  91. //上传这个文件夹所有的文件
  92. List<File> files = FileUtil.loopFiles(cutImagesPath);
  93. String keyPath = String.format(UploadFilePath.IMG_VIEW_PATH, scenePlus.getNum()) + "cut_images/";
  94. files.forEach(v -> fYunFileService.uploadFile(scenePlusExt.getYunFileBucket(), keyPath+v.getName(), v.getAbsolutePath(), false));
  95. }
  96. }catch (Exception e){
  97. log.error("ai识别报错,inPath:{}", absolutePath, e);
  98. }
  99. }
  100. //生成ai.json
  101. List<SceneMarkShape> sceneMarkShapes = sceneMarkShapeService.findByNumAndType(scenePlus.getNum(), DetectType.PANO.getCode());
  102. if(CollUtil.isNotEmpty(sceneMarkShapes)){
  103. for (SceneMarkShape sceneMarkShape : sceneMarkShapes) {
  104. if (ObjectUtil.isNotEmpty(sceneMarkShape.getShapes())){
  105. for (JSONObject shape : sceneMarkShape.getShapes()) {
  106. String category = shape.getString("category");
  107. SceneShapeEnum sceneShapeEnum = sceneShapeEnumService.findByClassName(category);
  108. if (ObjectUtil.isNotNull(sceneShapeEnum)){
  109. shape.put("name",sceneShapeEnum.getName());
  110. }
  111. if (category.contains("Tag_")){
  112. shape.put("category","Tag");
  113. }
  114. }
  115. }
  116. }
  117. String ajJsonKey = String.format(UploadFilePath.IMG_VIEW_PATH, scenePlus.getNum()) + "ai.json";
  118. fYunFileService.uploadFileBytes(scenePlusExt.getYunFileBucket(), ajJsonKey, JSON.toJSONString(sceneMarkShapes).getBytes(StandardCharsets.UTF_8));
  119. scenePlusExt.setHasRecognition(CommonStatus.YES.code().intValue());
  120. }
  121. }catch (Exception e){
  122. log.error("ai识别出错,num:{}", scenePlus.getNum());
  123. }
  124. }
  125. @Override
  126. public SceneMarkShape readDetectJson(String jsonPath) {
  127. String strings = FileUtil.readString(jsonPath, "UTF-8");
  128. JSONObject bbbb = JSONObject.parseObject(strings);
  129. SceneMarkShape parse = JSONObject.toJavaObject(bbbb, SceneMarkShape.class);
  130. System.out.println(parse);
  131. if (ObjectUtil.isNull(parse.getShapes())){
  132. return null;
  133. }
  134. List<JSONObject> shapes = parse.getShapes();
  135. for (JSONObject shape : shapes) {
  136. shape.remove("name");
  137. SceneShapeEnum category = sceneShapeEnumService.findByClassName(shape.getString("category"));
  138. if (ObjectUtil.isNull(category)){
  139. SceneShapeEnum sceneShapeEnum = new SceneShapeEnum();
  140. sceneShapeEnum.setName(shape.getString("name"));
  141. sceneShapeEnum.setClassName(shape.getString("category"));
  142. sceneShapeEnumService.save(sceneShapeEnum);
  143. }
  144. }
  145. return parse;
  146. }
  147. }