Эх сурвалжийг харах

feat: 可见光转红外修改:1. 重构代码,使得可读性提高 2. 修改路径 3. 未测试

WANGKANG 7 сар өмнө
parent
commit
a33e2db2a9

+ 2 - 1
taais-modules/taais-biz/src/main/java/com/taais/biz/constant/BizConstant.java

@@ -10,7 +10,8 @@ public class BizConstant {
     public static final String UPLOAD_DIR = "uploads/";
     public static final String MODEL_PATH = "model";
     public static final String MODEL_SUFFIX = ".pt";
-    public static final String PREDICT_PATH = "kjg";
+    public static final String TO_INFRARED_PREDICT_INPUT_DIR = "kjg";
+    public static final String TO_INFRARED_PREDICT_OUTPUT_DIR = "images";
     public static final String TD_TRAIN_MODEL_SUFFIX = ".ckpt";
     public static final String RESULT_JSON_NAME = "result.json";
 

+ 84 - 140
taais-modules/taais-biz/src/main/java/com/taais/biz/service/impl/ToInfraredServiceImpl.java

@@ -173,32 +173,42 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
             return false;
         }
 
-        ToInfrared toInfrared = new ToInfrared();
+        ToInfrared entity = new ToInfrared();
+        entity.setInputOssId(toInfraredBo.getInputOssId());
+        entity.setUrl(ossEntity.getUrl());
+        entity.setZipFilePath(ossEntity.getFileName());
+        entity.setName(toInfraredBo.getName());
+        entity.setStatus(NOT_START);
+        entity.setRemarks(toInfraredBo.getRemarks());
+        entity.setAlgorithmModelId(toInfraredBo.getAlgorithmModelId());
 
-        toInfrared.setInputOssId(toInfraredBo.getInputOssId());
-        toInfrared.setUrl(ossEntity.getUrl());
-
-        String filePath = ossEntity.getFileName();
-        String localPath = TaaisConfig.getProfile();
-        String resourcePath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
-        toInfrared.setInputPath(resourcePath);
-
-        String fileName = StringUtils.substringAfterLast(filePath, "/");
-        String fileName_without_suffix = removeFileExtension(fileName);
-
-        Path path = Paths.get(resourcePath);
-        Path outPath = path.resolveSibling(fileName_without_suffix + "_images" + System.currentTimeMillis());
-        toInfrared.setOutputPath(outPath.toString());
+        AlgorithmModelTrack algorithmModelTrack = algorithmModelTrackService.getById(entity.getAlgorithmModelId());
+        AlgorithmConfigTrack algorithmConfigTrack = algorithmConfigTrackService.getById(algorithmModelTrack.getAlgorithmId());
 
-        toInfrared.setZipFilePath(path.resolveSibling(fileName_without_suffix + ".zip").toString());
+        if (algorithmConfigTrack.getType().equals(BizConstant.AlgorithmType.TRAIN)) {
+            String inputPath = getTrainInputPath(ossEntity);
+            String outputPath = getTrainOutputPath(entity, ossEntity);
+            entity.setInputPath(inputPath);
+            entity.setOutputPath(outputPath);
+        } else if (algorithmConfigTrack.getType().equals(BizConstant.AlgorithmType.REASONING)) {
+            String inputPath = getPredictInputPath(ossEntity);
+            String outputPath = getPredictOutputPath(entity, ossEntity);
+            entity.setInputPath(inputPath);
+            entity.setOutputPath(outputPath);
+        } else {
+            throw new RuntimeException("算法类型不支持");
+        }
 
-        toInfrared.setName(toInfraredBo.getName());
-        toInfrared.setStatus(NOT_START);
-        toInfrared.setRemarks(toInfraredBo.getRemarks());
+        String resourcePath = getResourcePath(ossService.getById(entity.getInputOssId()));
+        makeDir(entity.getInputPath());
+        makeDir(entity.getOutputPath());
 
-        toInfrared.setAlgorithmModelId(toInfraredBo.getAlgorithmModelId());
+        File file = new File(entity.getInputPath());
+        if (!file.exists()) {
+            ZipUtils.unzip(resourcePath, entity.getInputPath());
+        }
 
-        return this.save(toInfrared);// 使用全局配置的雪花算法主键生成器生成ID值
+        return this.save(entity);// 使用全局配置的雪花算法主键生成器生成ID值
     }
 
     /**
@@ -229,34 +239,57 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
         return this.removeByIds(Arrays.asList(ids));
     }
 
-    @Override
-    public CommonResult start(Long id) {
-        ToInfrared entity = getById(id);
-
-        SysOssVo inputOssEntity = ossService.getById(entity.getInputOssId());
-
-        String filePath = inputOssEntity.getFileName();
+    public static String getResourcePath(SysOssVo ossEntity) {
+        String filePath = ossEntity.getFileName();
         String localPath = TaaisConfig.getProfile();
         String resourcePath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
+        return resourcePath;
+    }
 
-        String fileName = StringUtils.substringAfterLast(filePath, "/");
+    public static String getUnZipDirPath(SysOssVo ossEntity) {
+        String resourcePath = getResourcePath(ossEntity);
+        String fileName = StringUtils.substringAfterLast(resourcePath, "/");
         String fileName_without_suffix = removeFileExtension(fileName);
 
         Path path = Paths.get(resourcePath);
-        Path inputPath = path.resolveSibling(fileName_without_suffix + BizConstant.UNZIP_SUFFIX);
+        Path dirPath = path.resolveSibling(fileName_without_suffix + BizConstant.UNZIP_SUFFIX);
+        return dirPath.toString();
+    }
+
+    public String getTrainInputPath(SysOssVo ossEntity) {
+        return getUnZipDirPath(ossEntity);
+    }
+
+    public String getTrainOutputPath(ToInfrared entity, SysOssVo ossEntity) {
+        String resourcePath = getResourcePath(ossEntity);
+        Path path = Paths.get(resourcePath);
         Path outputPath = path.resolveSibling(entity.getId().toString() + BizConstant.TO_INFRARED_SUFFIX);
+        return outputPath.toString();
+    }
 
-//        makeDir(inputPath.toString());
-        makeDir(outputPath.toString());
+    public String getPredictInputPath(SysOssVo ossEntity) {
+        return getUnZipDirPath(ossEntity) + File.separator + BizConstant.TO_INFRARED_PREDICT_INPUT_DIR;
+    }
 
-        File file = new File(inputPath.toString());
-        if (!file.exists()) {
-            ZipUtils.unzip(resourcePath, inputPath.toString());
-        }
+    public String getPredictOutputPath(ToInfrared entity, SysOssVo ossEntity) {
+        return getUnZipDirPath(ossEntity) + File.separator + BizConstant.TO_INFRARED_PREDICT_OUTPUT_DIR;
+    }
 
-        entity.setInputPath(inputPath.toString());
-        entity.setOutputPath(outputPath.toString());
+    private String getLogFileName(ToInfrared entity) {
+        return entity.getId() + BizConstant.TO_INFRARED_SUFFIX + ".log";
+    }
+
+    public String getLogFilePath(ToInfrared entity) {
+        return entity.getOutputPath() + File.separator + getLogFileName(entity);
+    }
 
+    public String getStatisticsResultPath(SysOssVo ossEntity) {
+        return getUnZipDirPath(ossEntity) + File.separator + BizConstant.RESULT_JSON_NAME;
+    }
+
+    @Override
+    public CommonResult start(Long id) {
+        ToInfrared entity = getById(id);
         entity.setStartTime(new Date());
 
         AlgorithmModelTrack algorithmModelTrack = algorithmModelTrackService.getById(entity.getAlgorithmModelId());
@@ -270,15 +303,12 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
 
         startTaskConfig.setSource_dir(entity.getInputPath());
         startTaskConfig.setResult_dir(entity.getOutputPath());
-        startTaskConfig.setLog_path(entity.getOutputPath() + File.separator + getLogFileName(entity));
+        startTaskConfig.setLog_path(getLogFilePath(entity));
 
         if (BizConstant.AlgorithmType.REASONING.equals(algorithmConfigTrack.getType())) {
             startTaskConfig.setModel_path(algorithmModelTrack.getModelAddress());
-            startTaskConfig.setSource_dir(entity.getInputPath() + File.separator + BizConstant.PREDICT_PATH);
-            startTaskConfig.setResult_dir(entity.getOutputPath() + File.separator + BizConstant.PREDICT_PATH);
         }
 
-
         HttpResponseEntity responseEntity = sendPostMsg(algorithmConfigTrack.getAlgorithmAddress(), startTaskConfig);
         if (ObjectUtil.isNotNull(responseEntity) && responseEntity.getStatus() == 200) {
             entity.setStatus(BizConstant.VideoStatus.RUNNING);
@@ -324,28 +354,6 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
             entity.setCostSecond(null);
         }
         updateById(entity);
-        /*
-         * 这里有很多需要做的:1. 保存所有模型到oss 2. 模型文件保存到算法模型配置表中
-         */
-/*
-        Path resultsPath = Paths.get(entity.getInputPath());
-        Path modelPath = resultsPath.resolve(BizConstant.MODEL_PATH);
-
-        File modelDir = new File(modelPath.toString());
-
-        if(!modelDir.exists() || !modelDir.isDirectory()) {
-            return CommonResult.fail("模型文件不存在");
-        }
-
-        for (File f : modelDir.listFiles()) {
-            if (f.isFile() && f.getName().endsWith(BizConstant.MODEL_SUFFIX)) {
-                SysOssVo upload = ossService.upload(f);
-                AlgorithmModelTrack algorithmModelTrack = new AlgorithmModelTrack();
-                algorithmModelTrack.set
-                algorithmModelTrackService.insert();
-            }
-        }
-*/
 
         WebSocketUtils.publishAll("refresh");
         return CommonResult.success();
@@ -358,11 +366,6 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
             return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
         }
 
-//        String osName = System.getProperty("os.name");
-//        if (osName.toLowerCase().contains("windows")) {
-//            outputPath = outputPath;
-//        }
-
         AlgorithmModelTrackVo model = algorithmModelTrackService.selectById(toInfrared.getAlgorithmModelId());
         AlgorithmConfigTrackVo config = algorithmConfigTrackService.selectById(model.getAlgorithmId());
 
@@ -375,16 +378,11 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
         }
     }
 
-    private String getLogFileName(ToInfrared entity) {
-        return entity.getId() + BizConstant.TO_INFRARED_SUFFIX + ".log";
-    }
-
     @Override
     public CommonResult getLog(Long id) {
         ToInfrared entity = getById(id);
-        String outPutPath = entity.getOutputPath();
-        String logPath = outPutPath + File.separator + getLogFileName(entity);
-        System.out.println(logPath);
+        String logPath = getLogFilePath(entity);
+        log.info("logPath: {}", logPath);
         File file = new File(logPath);
         if (!file.exists()) {
             return CommonResult.fail("日志文件不存在!");
@@ -446,39 +444,20 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
     public CommonResult previewPredictResult(Long id) {
         ToInfrared entity = getById(id);
 
-        SysOssVo inputOssEntity = ossService.getById(entity.getInputOssId());
-
-        String filePath = inputOssEntity.getFileName();
-        String localPath = TaaisConfig.getProfile();
-        String resourcePath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
-
-        String fileName = StringUtils.substringAfterLast(filePath, "/");
-        String fileName_without_suffix = removeFileExtension(fileName);
-
-        Path path = Paths.get(resourcePath);
-        Path inputPath = path.resolveSibling(fileName_without_suffix + BizConstant.UNZIP_SUFFIX + File.separator + BizConstant.PREDICT_PATH);
-        Path outputPath = path.resolveSibling(entity.getId().toString() + BizConstant.TO_INFRARED_SUFFIX + File.separator + BizConstant.PREDICT_PATH);
-
-//        String urlPrefix = inputOssEntity.getUrl().substring(0, inputOssEntity.getUrl().indexOf(Constants.RESOURCE_PREFIX) + Constants.RESOURCE_PREFIX.length());
+        String inputPath = entity.getInputPath();
+        String outputPath = entity.getOutputPath();
         String urlPrefix = Constants.RESOURCE_PREFIX;
-        return getCompareImage(urlPrefix, inputPath.toString(), outputPath.toString());
+
+        return getCompareImage(urlPrefix, inputPath, outputPath);
     }
 
     @Override
     public CommonResult getStatisticsResult(Long id) {
         ToInfrared entity = getById(id);
-        SysOssVo inputOssEntity = ossService.getById(entity.getInputOssId());
+        SysOssVo ossEntity = ossService.getById(entity.getInputOssId());
+        String statisticsResultPath = getStatisticsResultPath(ossEntity);
 
-        String filePath = inputOssEntity.getFileName();
-        String localPath = TaaisConfig.getProfile();
-        String resourcePath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
-
-        Path path = Paths.get(resourcePath);
-        Path outputPath = path.resolveSibling(entity.getId().toString() + BizConstant.TO_INFRARED_SUFFIX + File.separator + BizConstant.PREDICT_PATH);
-
-        Path statisticsResultPath = outputPath.resolve(BizConstant.RESULT_JSON_NAME);
-
-        List<Dict> resultMap = parseJsonMapList(statisticsResultPath);
+        List<Dict> resultMap = parseJsonMapList(Paths.get(statisticsResultPath));
         if (ObjectUtil.isEmpty(resultMap)) {
             return CommonResult.fail("获取结果文件失败");
         }
@@ -541,14 +520,13 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
 
     private ResponseEntity<org.springframework.core.io.Resource> getPredictImages(ToInfrared toInfrared) {
         String outputPath = toInfrared.getOutputPath();
-        String predictDir = outputPath + File.separator + BizConstant.PREDICT_PATH;
-
-        String zipFilePath = outputPath + File.separator + "predict.zip";
+        Path path____ = Paths.get(outputPath);
+        String zipFilePath = path____.resolveSibling("images.zip").toString();
 
         File file = new File(zipFilePath);
         if (!file.exists()) {
             try {
-                ZipUtils.zipFolderFiles(predictDir, zipFilePath);
+                ZipUtils.zipFolderFiles(outputPath, zipFilePath);
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
@@ -585,38 +563,4 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
         org.springframework.core.io.Resource resource = new FileSystemResource(returnFile);
         return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + returnFile.getName() + "\"").header(HttpHeaders.CONTENT_TYPE, "application/octet-stream").body(resource);
     }
-
-//    @Override
-//    public ResponseEntity<org.springframework.core.io.Resource> zipImages(Long id) {
-//        ToInfrared toInfrared = this.getById(id);
-//        if (ObjectUtil.isNull(toInfrared)) {
-//            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
-//        }
-//
-//        String outputPath = toInfrared.getOutputPath();
-//        String zipFilePath = outputPath + ".zip";
-//
-//        String osName = System.getProperty("os.name");
-//        if (osName.toLowerCase().contains("windows")) {
-//            zipFilePath = "C:" + zipFilePath;
-//        }
-//
-//        try {
-//            ZipUtils.zipFolderFiles(outputPath, zipFilePath);
-//        } catch (IOException e) {
-//            throw new RuntimeException(e);
-//        }
-//
-//        File file = new File(zipFilePath);
-//
-//        if (!file.exists() || !file.isFile()) {
-//            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
-//        }
-//
-//        org.springframework.core.io.Resource resource = new FileSystemResource(file);
-//        return ResponseEntity.ok()
-//            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
-//            .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
-//            .body(resource);
-//    }
 }