Browse Source

feat: 图像拼接

28968 8 months ago
parent
commit
abf69a935d

+ 32 - 9
taais-modules/taais-biz/src/main/java/com/taais/biz/controller/DataAugmentationController.java

@@ -99,17 +99,19 @@ public class DataAugmentationController extends BaseController {
             if ("图像拼接_sift".equals(dataAugmentation.getTaskType()) || "图像拼接_coordinate".equals(dataAugmentation.getTaskType())) {
                 String lastDirectoryName = imagePath.getFileName().toString();
                 outputPath = outputPath.resolve(lastDirectoryName);  //得到推理结果目录
-                DirectoryStream<Path> stream = Files.newDirectoryStream(outputPath);
+                Stream<Path> stream = Files.list(outputPath);
                 if (Files.exists(outputPath) && Files.isDirectory(outputPath)) {
-                    if (stream.iterator().hasNext()) {
-                        for (Path entry : stream) {
-                            if (Files.isRegularFile(entry)) {
-                                fileName = entry.getFileName().toString();
-                                break;
-                            }
-                        }
+                    // 检查流中是否有下一个元素
+                    Path firstRegularFile = stream.filter(Files::isRegularFile).findFirst().orElse(null);
+                    if (firstRegularFile != null) {
+                        // 找到第一个常规文件
+                            fileName = firstRegularFile.getFileName().toString();
+                            System.out.println("找到的第一个常规文件: " + fileName);
+
+
                     } else {
                         System.out.println("图像拼接算法未在结果目录下保存结果文件!");
+                        return ResponseEntity.status(500).build();
                     }
                 } else {
                     System.out.println("图像拼接算法未创建结果目录!");
@@ -119,7 +121,7 @@ public class DataAugmentationController extends BaseController {
                 Stream<Path> newPathStream = Files.list(imagePath);
                 List<String> origin = newPathStream
                     .filter(Files::isRegularFile)  // 只选择常规文件(排除子目录)
-                    .map(path -> {
+                    .filter(path -> !path.toString().toLowerCase().endsWith(".txt")).map(path -> {
                         try {
                             return Base64.getEncoder().encodeToString(Files.readAllBytes(path));
                         } catch (IOException e) {
@@ -245,4 +247,25 @@ public class DataAugmentationController extends BaseController {
         }
         return CommonResult.success();
     }
+
+    /**
+     * 获取日志字符串
+     */
+    @SaCheckPermission("demo:dataAugmentation:query")
+    @GetMapping(value = "/log/{id}")
+    public CommonResult<String> getLog(@PathVariable Long id) {
+        DataAugmentation byId = dataAugmentationService.getById(id);
+        String filePath = byId.getAlgorithmPath();
+        try{
+            Path path = Paths.get(filePath);
+            if (!Files.exists(path)) {
+                return CommonResult.fail( "日志文件" + filePath + "不存在!");
+            }
+            // 读取文件内容
+            return CommonResult.success(new String(Files.readAllBytes(path)), "获取日志成功");
+        } catch (Exception e) {
+            return CommonResult.fail("日志文件" + filePath + "读取失败!");
+        }
+
+    }
 }

+ 59 - 80
taais-modules/taais-biz/src/main/java/com/taais/biz/service/impl/DataAugmentationServiceImpl.java

@@ -223,88 +223,67 @@ public class DataAugmentationServiceImpl extends BaseServiceImpl<DataAugmentatio
 
     @Override
     public CommonResult start(Long id) {
-        DataAugmentation dataAugmentation = getById(id);
-
-        SysOssVo inputOssEntity = ossService.getById(dataAugmentation.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 = null;
-        Path inputPath = null;
-        Path outputPath = null;
-        Path logPath = null;
-//        Path path = Paths.get();
-        //String osName = System.getProperty("os.name");
-         //判断是否是Windows环境
-//        if (osName.toLowerCase().contains("windows")) {
-//            path = Paths.get("D:", resourcePath);
-//        } else {
-//            path = Paths.get(resourcePath);
-//
-//        }
-        path = Paths.get(resourcePath);
-        inputPath = path.resolveSibling(fileName_without_suffix + "_input");
-        outputPath = path.resolveSibling(fileName_without_suffix + "_output");
-        logPath = path.resolveSibling(fileName_without_suffix + "_log");
-        makeDir(inputPath.toString());
-        makeDir(outputPath.toString());
-        makeDir(logPath.toString());
-
-        System.out.println("inputPath:" + inputPath.toString());
-        ZipUtils.unzip(resourcePath, inputPath.toString());
-        //获取inputPath的下一级目录
-//        if (Files.exists(inputPath) && Files.isDirectory(inputPath)) {
-//            try (DirectoryStream<Path> stream = Files.newDirectoryStream(inputPath)) {
-//                // 遍历目录流以找到第一个子目录
-//                for (Path entry : stream) {
-//                    if (Files.isDirectory(entry)) {
-//                        // 打印第一个子目录的名称,并跳出循环
-//                        dataAugmentation.setInputPath(entry.toString());
-//                        break;  // 只处理第一个子目录后退出
-//                    }
-//                }
-//            } catch (IOException e) {
-//                return CommonResult.fail(e.toString());
-//            }
-//        } else {
-//            return CommonResult.fail("The provided path is not a valid directory.");
-//        }
-        dataAugmentation.setInputPath(inputPath.toString());
-        dataAugmentation.setAlgorithmPath(logPath.toString());
-        dataAugmentation.setOutputPath(outputPath.toString());
-        dataAugmentation.setStartTime(new Date());
-        dataAugmentation.setStatus(BizConstant.VideoStatus.RUNNING);
-        updateById(dataAugmentation);
-        SysDictDataBo sysDictDataBo = new SysDictDataBo();
-        sysDictDataBo.setDictLabel(dataAugmentation.getTaskType() + "开始url");
-        sysDictDataBo.setDictType("biz_data_augmentation");
-        List<SysDictDataVo> sysDictDataVos = iSysDictDataService.selectDictDataList(sysDictDataBo);
-        if (sysDictDataVos.size() == 0) {
-            return CommonResult.fail("未设置数据增强算法推理的url!请在数据字典中设置该算法的推理url!");
-        }
-        String data_augmentation_start_url = sysDictDataVos.get(0).getDictValue();
-        //设置传输对象
-        TransmissionObject transmissionObject = new TransmissionObject();
-        transmissionObject.setBizId(dataAugmentation.getId());
-        transmissionObject.setBizType(dataAugmentation.getTaskType());
-        transmissionObject.setLogPath(dataAugmentation.getAlgorithmPath());
-        transmissionObject.setSourcePath(dataAugmentation.getInputPath());
-        transmissionObject.setResultPath(dataAugmentation.getOutputPath());
-        transmissionObject.setOtherParams(dataAugmentation.getHyperparameterConfiguration());
-        HttpResponseEntity responseEntity = sendPostMsg(data_augmentation_start_url, transmissionObject);
-
-        if (responseEntity.getStatus() == 200) {
-            dataAugmentation.setStatus(BizConstant.VideoStatus.FAILED);
+        try {
+            DataAugmentation dataAugmentation = getById(id);
+            SysOssVo inputOssEntity = ossService.getById(dataAugmentation.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 = null;
+            Path inputPath = null;
+            Path outputPath = null;
+            Path logPath = null;
+            path = Paths.get(resourcePath);
+            inputPath = path.resolveSibling(fileName_without_suffix + "_input");
+            outputPath = path.resolveSibling(fileName_without_suffix + "_output");
+            logPath = path.resolveSibling(fileName_without_suffix + "_log");
+            makeDir(inputPath.toString());
+            makeDir(outputPath.toString());
+            makeDir(logPath.toString());
+            logPath = Paths.get(logPath.toString(), "log.log");
+            Files.createFile(logPath);
+            System.out.println("inputPath:" + inputPath.toString());
+            ZipUtils.unzip(resourcePath, inputPath.toString());
+            dataAugmentation.setInputPath(inputPath.toString());
+            dataAugmentation.setAlgorithmPath(logPath.toString());
+            dataAugmentation.setOutputPath(outputPath.toString());
+            dataAugmentation.setStartTime(new Date());
+            dataAugmentation.setStatus(BizConstant.VideoStatus.RUNNING);
             updateById(dataAugmentation);
-            return CommonResult.success("任务开始成功,请等待完成");
-        }
-        else {
-            return CommonResult.fail("任务开始失败,请检查!");
+            SysDictDataBo sysDictDataBo = new SysDictDataBo();
+            sysDictDataBo.setDictLabel(dataAugmentation.getTaskType() + "开始url");
+            sysDictDataBo.setDictType("biz_data_augmentation");
+            List<SysDictDataVo> sysDictDataVos = iSysDictDataService.selectDictDataList(sysDictDataBo);
+            if (sysDictDataVos.size() == 0) {
+                return CommonResult.fail("未设置" + dataAugmentation.getTaskType() + "算法推理的url!请在数据字典中设置该算法的推理url!");
+            }
+            String data_augmentation_start_url = sysDictDataVos.get(0).getDictValue();
+            //设置传输对象
+            TransmissionObject transmissionObject = new TransmissionObject();
+            transmissionObject.setBizId(dataAugmentation.getId());
+            transmissionObject.setBizType(dataAugmentation.getTaskType());
+            transmissionObject.setLogPath(dataAugmentation.getAlgorithmPath());
+            transmissionObject.setSourcePath(dataAugmentation.getInputPath());
+            transmissionObject.setResultPath(dataAugmentation.getOutputPath());
+            transmissionObject.setOtherParams(dataAugmentation.getHyperparameterConfiguration());
+            HttpResponseEntity responseEntity = sendPostMsg(data_augmentation_start_url, transmissionObject);
+
+            if (responseEntity.getStatus() == 200) {
+                dataAugmentation.setStatus(BizConstant.VideoStatus.FAILED);
+                updateById(dataAugmentation);
+                return CommonResult.success("任务开始成功,请等待完成");
+            }
+            else {
+                return CommonResult.fail("任务开始失败,请检查算法服务!");
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            return CommonResult.fail("出现异常:" + e.toString());
         }
+
     }
 
     public HttpResponseEntity sendPostMsg(String url, Object obj) {