dengsixing 2 months ago
parent
commit
3d419df4fa

+ 13 - 0
src/main/java/com/fdkankan/scene/service/IAiService.java

@@ -0,0 +1,13 @@
+package com.fdkankan.scene.service;
+
+import com.fdkankan.scene.entity.SceneMarkShape;
+import com.fdkankan.scene.entity.ScenePlus;
+import com.fdkankan.scene.entity.ScenePlusExt;
+
+public interface IAiService {
+
+    void detectScenePano(ScenePlus scenePlus, ScenePlusExt scenePlusExt, String path);
+
+    SceneMarkShape readDetectJson(String jsonPath);
+
+}

+ 2 - 0
src/main/java/com/fdkankan/scene/service/ISceneMarkShapeService.java

@@ -33,4 +33,6 @@ public interface ISceneMarkShapeService extends IService<SceneMarkShape> {
     List<SceneMarkShape> findByNum(String num);
 
     SceneMarkShape readDetectJson(String jsonPath);
+
+    SceneMarkShape findByNumAndImagePathAndType(String num, String imagePath,Integer type);
 }

+ 160 - 0
src/main/java/com/fdkankan/scene/service/impl/AiServiceImpl.java

@@ -0,0 +1,160 @@
+package com.fdkankan.scene.service.impl;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.fdkankan.common.constant.CommonStatus;
+import com.fdkankan.common.util.CmdUtils;
+import com.fdkankan.model.constants.UploadFilePath;
+import com.fdkankan.model.utils.ComputerUtil;
+import com.fdkankan.scene.constant.CmdConstant;
+import com.fdkankan.scene.constant.DetectType;
+import com.fdkankan.scene.entity.SceneMarkShape;
+import com.fdkankan.scene.entity.ScenePlus;
+import com.fdkankan.scene.entity.ScenePlusExt;
+import com.fdkankan.scene.entity.SceneShapeEnum;
+import com.fdkankan.scene.oss.OssUtil;
+import com.fdkankan.scene.service.*;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+import java.util.List;
+
+@Slf4j
+@Service
+public class AiServiceImpl implements IAiService {
+
+    @Autowired
+    private IScenePlusService scenePlusService;
+    @Autowired
+    private IScenePlusExtService scenePlusExtService;
+    @Resource
+    private OssUtil fYunFileService;
+    @Autowired
+    private SceneShapeEnumService sceneShapeEnumService;
+    @Autowired
+    private ISceneMarkShapeService sceneMarkShapeService;
+
+    @Override
+    public void detectScenePano(ScenePlus scenePlus, ScenePlusExt scenePlusExt, String path) {
+        try {
+            String resultsPath = path + File.separator + "results" + File.separator;
+            String highPath = resultsPath + "high" + File.separator;
+            String aiWorkPath = highPath + "ai" + File.separator;
+            List<File> highImgs = FileUtil.loopFiles(highPath);
+            if(CollUtil.isEmpty(highImgs)){
+                return;
+            }
+
+            FileUtil.mkdir(aiWorkPath);
+            for (File file : highImgs) {
+                String absolutePath = file.getAbsolutePath();
+                try {
+                    String name = FileUtil.getName(absolutePath);
+                    String prefix = FileUtil.getPrefix(name);
+                    String outPath = aiWorkPath + prefix + File.separator;
+                    FileUtil.mkdir(outPath);
+                    String detectPath = outPath + "detect.json";
+                    String cmd = String.format(CmdConstant.PANO_DETECT, absolutePath, detectPath);
+                    CmdUtils.callLine(cmd, 50);
+                }catch (Exception e){
+                    log.error("ai识别报错,inPath:{}", absolutePath, e);
+                }
+            }
+
+            for (File file : highImgs) {
+                String absolutePath = file.getAbsolutePath();
+                try {
+                    String name = FileUtil.getName(absolutePath);
+                    String prefix = FileUtil.getPrefix(name);
+                    String outPath = aiWorkPath + prefix + File.separator;
+                    String detectPath = outPath + "detect.json";
+                    String cutImagesPath = outPath + "cut_images";
+                    if(!ComputerUtil.checkComputeCompleted(detectPath, 5, 200)){
+                        continue;
+                    }
+                    SceneMarkShape sceneMarkShape = readDetectJson(detectPath);
+                    if (ObjectUtil.isNotNull(sceneMarkShape)){
+                        sceneMarkShape.setNum(scenePlus.getNum());
+                        SceneMarkShape shape = sceneMarkShapeService.findByNumAndImagePathAndType(scenePlus.getNum(), sceneMarkShape.getImagePath(), DetectType.PANO.getCode());
+                        if (ObjectUtil.isNotNull(shape)){
+                            sceneMarkShape.setId(shape.getId());
+                            sceneMarkShape.setUpdateTime(new Date());
+                            sceneMarkShapeService.updateById(sceneMarkShape);
+                        }else {
+                            sceneMarkShape.setCreateTime(new Date());
+                            sceneMarkShape.setType(DetectType.PANO.getCode());
+                            sceneMarkShapeService.save(sceneMarkShape);
+                        }
+                    }
+                    if (FileUtil.exist(cutImagesPath)){
+                        //上传这个文件夹所有的文件
+                        List<File> files = FileUtil.loopFiles(cutImagesPath);
+                        String keyPath =  String.format(UploadFilePath.IMG_VIEW_PATH, scenePlus.getNum()) + "cut_images/";
+                        files.forEach(v -> fYunFileService.uploadFile(scenePlusExt.getYunFileBucket(), keyPath+v.getName(), v.getAbsolutePath(), false));
+                    }
+
+                }catch (Exception e){
+                    log.error("ai识别报错,inPath:{}", absolutePath, e);
+                }
+            }
+
+            //生成ai.json
+            List<SceneMarkShape> sceneMarkShapes = sceneMarkShapeService.findByNumAndType(scenePlus.getNum(), DetectType.PANO.getCode());
+            if(CollUtil.isNotEmpty(sceneMarkShapes)){
+                for (SceneMarkShape sceneMarkShape : sceneMarkShapes) {
+                    if (ObjectUtil.isNotEmpty(sceneMarkShape.getShapes())){
+                        for (JSONObject shape : sceneMarkShape.getShapes()) {
+                            String category = shape.getString("category");
+                            SceneShapeEnum sceneShapeEnum = sceneShapeEnumService.findByClassName(category);
+                            if (ObjectUtil.isNotNull(sceneShapeEnum)){
+                                shape.put("name",sceneShapeEnum.getName());
+                            }
+                            if (category.contains("Tag_")){
+                                shape.put("category","Tag");
+                            }
+                        }
+                    }
+                }
+
+                String ajJsonKey = String.format(UploadFilePath.IMG_VIEW_PATH, scenePlus.getNum()) + "ai.json";
+                fYunFileService.uploadFileBytes(scenePlusExt.getYunFileBucket(), ajJsonKey, JSON.toJSONString(sceneMarkShapes).getBytes(StandardCharsets.UTF_8));
+
+                scenePlusExt.setHasRecognition(CommonStatus.YES.code().intValue());
+            }
+        }catch (Exception e){
+            log.error("ai识别出错,num:{}", scenePlus.getNum());
+        }
+    }
+
+    @Override
+    public SceneMarkShape readDetectJson(String jsonPath) {
+        String strings = FileUtil.readString(jsonPath, "UTF-8");
+        JSONObject bbbb = JSONObject.parseObject(strings);
+        SceneMarkShape parse = JSONObject.toJavaObject(bbbb, SceneMarkShape.class);
+        System.out.println(parse);
+        if (ObjectUtil.isNull(parse.getShapes())){
+            return null;
+        }
+        List<JSONObject> shapes = parse.getShapes();
+        for (JSONObject shape : shapes) {
+            shape.remove("name");
+            SceneShapeEnum category = sceneShapeEnumService.findByClassName(shape.getString("category"));
+            if (ObjectUtil.isNull(category)){
+                SceneShapeEnum sceneShapeEnum = new SceneShapeEnum();
+                sceneShapeEnum.setName(shape.getString("name"));
+                sceneShapeEnum.setClassName(shape.getString("category"));
+                sceneShapeEnumService.save(sceneShapeEnum);
+            }
+        }
+        return parse;
+    }
+
+}

+ 9 - 0
src/main/java/com/fdkankan/scene/service/impl/SceneMarkShapeServiceImpl.java

@@ -219,4 +219,13 @@ public class SceneMarkShapeServiceImpl extends ServiceImpl<MarkShapeMapper, Scen
         }
         return parse;
     }
+
+    @Override
+    public SceneMarkShape findByNumAndImagePathAndType(String num, String imagePath, Integer type) {
+        LambdaQueryWrapper<SceneMarkShape> wrapper = Wrappers.lambdaQuery();
+        wrapper.eq(SceneMarkShape::getNum,num);
+        wrapper.eq(SceneMarkShape::getImagePath,imagePath);
+        wrapper.eq(SceneMarkShape::getType,type);
+        return getOne(wrapper);
+    }
 }

+ 7 - 0
src/main/java/com/fdkankan/scene/service/impl/ScenePlusServiceImpl.java

@@ -108,6 +108,8 @@ public class ScenePlusServiceImpl extends ServiceImpl<IScenePlusMapper, ScenePlu
     private ICommonService commonService;
     @Autowired
     private FloorplanAiService floorplanAiService;
+    @Autowired
+    private IAiService aiService;
 
     @Override
     public ScenePlus getScenePlusByNum(String num) {
@@ -339,6 +341,10 @@ public class ScenePlusServiceImpl extends ServiceImpl<IScenePlusMapper, ScenePlu
             scenePlusExtService.update(new LambdaUpdateWrapper<ScenePlusExt>().eq(ScenePlusExt::getId, scenePlusExt.getId()).setSql("orientation = null"));
         }
 
+        //开始场景图ai识别
+        aiService.detectScenePano(scenePlus, scenePlusExt, dataSource);
+        scenePlusExtService.saveOrUpdate(scenePlusExt);
+
         //生成编辑主表
         String pwd = fdageData.getString("pwd");
         SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
@@ -414,6 +420,7 @@ public class ScenePlusServiceImpl extends ServiceImpl<IScenePlusMapper, ScenePlu
         sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
         sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
         sceneJson.setSceneKind(scenePlusExt.getSceneKind());
+        sceneJson.setHasRecognition(scenePlusExt.getHasRecognition());
         if(StrUtil.isNotEmpty(scenePlusExt.getVideos())){
             sceneJson.setVideos(scenePlusExt.getVideos());
         }