package com.fdkankan.scene.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.oss.OssUtil; import com.fdkankan.scene.service.FloorplanAiService; import com.fdkankan.scene.service.ISceneMarkShapeService; import com.fdkankan.scene.util.CmdUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.*; @Slf4j @Service public class FloorplanAiServiceImpl implements FloorplanAiService { @Autowired private ISceneMarkShapeService sceneMarkShapeService; @Resource private OssUtil ossUtil; @Override public LinkedHashMap detFloorplan(String path) throws Exception { LinkedHashMap result = new LinkedHashMap<>(); String workDir = path + "/detFloorplan/"; if(FileUtil.exist(workDir)){ FileUtil.del(workDir); } String infoJsonPath = path + "/results/floorplan/info.json"; if(!FileUtil.exist(infoJsonPath)){ return result; } JSONObject infoObj = JSON.parseObject(FileUtil.readUtf8String(infoJsonPath)); JSONArray floors = infoObj.getJSONArray("floors"); if(CollUtil.isEmpty(floors)){ return result; } for (Object o : floors) { JSONObject floor = (JSONObject) o; Integer subgroup = floor.getInteger("subgroup"); String detectPath = workDir + subgroup + "/detect.json"; String floorKeyPath = path + "/results/floorplan/floor_" + subgroup + ".png"; String parent = FileUtil.getParent(detectPath, 1); FileUtil.mkdir(parent); result.put(subgroup, false); if(!FileUtil.exist(floorKeyPath)){ continue; } String cmd = CmdConstant.LAYOUT_DETECT; cmd = cmd.replace("@in", floorKeyPath); cmd = cmd.replace("@out", detectPath); CmdUtils.callLineWin(cmd, 50); if (ComputerUtil.checkComputeCompleted(detectPath,5, 500)) { result.put(subgroup, true); } } return result; } @Override public boolean detFloorPlanAi(String num, String path, LinkedHashMap detFloorplan, String bucket) throws IOException { try { String aiJsonKey = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "floorplan/ai.json"; //先清空历史数据,因为数据格式改动很多个版本,已经无法按照规律进行过滤删除,所以这里先删除历史数据,而后再根据算法生成去插入数据 sceneMarkShapeService.remove(new LambdaQueryWrapper().eq(SceneMarkShape::getNum, num).eq(SceneMarkShape::getType, DetectType.PLAN.getCode())); if(ossUtil.doesObjectExist(bucket, aiJsonKey)){ ossUtil.deleteObject(bucket, aiJsonKey); } if(CollUtil.isEmpty(detFloorplan)){ return false; } boolean hasFloorplanAi = false; String workDir = path + "/detFloorplan/"; for (Integer subgroup : detFloorplan.keySet()) { Boolean yes = detFloorplan.get(subgroup); SceneMarkShape sceneMarkShape = null; if(yes){ String detectPath = workDir + subgroup + "/detect.json"; sceneMarkShape = sceneMarkShapeService.readDetectJson(detectPath); } if (ObjUtil.isNull(sceneMarkShape)){ sceneMarkShape = new SceneMarkShape(); String infoJsonPath = path + "/results/floorplan/info.json"; JSONObject infoObj = JSON.parseObject(FileUtil.readUtf8String(infoJsonPath)); JSONArray floors = infoObj.getJSONArray("floors"); for (Object floor : floors) { JSONObject floorObj = (JSONObject)floor; Integer group = floorObj.getInteger("subgroup"); if(!subgroup.equals(group)){ continue; } JSONObject resolution = floorObj.getJSONObject("resolution"); sceneMarkShape.setImageWidth(resolution.getInteger("width")); sceneMarkShape.setImageHeight(resolution.getInteger("height")); } } if(Objects.isNull(sceneMarkShape.getShapes())){ sceneMarkShape.setShapes(new ArrayList<>()); } sceneMarkShape.setNum(num); sceneMarkShape.setCreateTime(new Date()); sceneMarkShape.setType(DetectType.PLAN.getCode()); sceneMarkShapeService.save(sceneMarkShape); if(CollUtil.isNotEmpty(sceneMarkShape.getShapes())){ hasFloorplanAi = true; } } List sceneMarkShapes = sceneMarkShapeService.findByNumAndType(num, DetectType.PLAN.getCode()); for (SceneMarkShape ms : sceneMarkShapes) { if (ObjectUtil.isNotEmpty(ms.getShapes())){ for (JSONObject s : ms.getShapes()) { String category = s.getString("category"); if (category.contains("Tag_")){ s.put("category","Tag"); } } } } ossUtil.uploadFileBytes(bucket, aiJsonKey, JSON.toJSONString(sceneMarkShapes).getBytes(StandardCharsets.UTF_8)); return hasFloorplanAi; }catch (Exception e){ log.error("平面图ai识别处理报错", e); return false; }finally { // FileUtil.del(workDir); } } }