|
@@ -1,11 +1,17 @@
|
|
|
package com.taais.biz.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.mybatisflex.core.paginate.Page;
|
|
|
import com.mybatisflex.core.query.QueryWrapper;
|
|
|
import com.taais.biz.domain.Data;
|
|
|
import com.taais.biz.domain.bo.DataBo;
|
|
|
import com.taais.biz.domain.vo.BatchDataResult;
|
|
|
+import com.taais.biz.domain.dto.DataAmplifyDto;
|
|
|
import com.taais.biz.domain.vo.DataSelectVo;
|
|
|
import com.taais.biz.domain.vo.DataVo;
|
|
|
import com.taais.biz.mapper.DataMapper;
|
|
@@ -18,6 +24,7 @@ import com.taais.common.core.utils.StringUtils;
|
|
|
import com.taais.common.core.utils.file.FileUploadUtils;
|
|
|
import com.taais.common.core.utils.file.FileUtils;
|
|
|
import com.taais.common.core.utils.file.UnPackedUtil;
|
|
|
+import com.taais.common.json.utils.JsonUtils;
|
|
|
import com.taais.common.orm.core.page.PageQuery;
|
|
|
import com.taais.common.orm.core.service.impl.BaseServiceImpl;
|
|
|
import com.taais.common.redis.utils.RedisUtils;
|
|
@@ -28,6 +35,7 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.File;
|
|
@@ -37,11 +45,11 @@ import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
import java.time.Instant;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.taais.biz.domain.table.DataTableDef.DATA;
|
|
|
|
|
@@ -60,6 +68,10 @@ public class DataServiceImpl extends BaseServiceImpl<DataMapper, Data> implement
|
|
|
private static final String RAR = ".rar";
|
|
|
private static final String TXT = ".txt";
|
|
|
private static final String[] VALID_EXTENSIONS = {".jpg", ".jpeg", ".png"};
|
|
|
+ private static final String PYTHON_DATA_AMPLIFY_API = "http://127.0.0.1:9096/api/data/amplify";
|
|
|
+ private static final String RESULT_CODE = "status";
|
|
|
+ private static final String RESULT_STATUS = "200";
|
|
|
+ private static final String AMPLIFY = "/AMPLIFY/";
|
|
|
@Resource
|
|
|
private DataMapper dataMapper;
|
|
|
|
|
@@ -256,6 +268,63 @@ public class DataServiceImpl extends BaseServiceImpl<DataMapper, Data> implement
|
|
|
return CommonResult.success("数据集上传成功!");
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public CommonResult<Boolean> dataAmplify(DataAmplifyDto dataAmplifyDto) {
|
|
|
+ //根据批次号获取该批次的所有文件数据
|
|
|
+ QueryWrapper query = query();
|
|
|
+ query.eq(Data::getBatchNum, dataAmplifyDto.getBatchNum());
|
|
|
+ List<Data> dataList = dataMapper.selectListByQuery(query);
|
|
|
+ if (dataList.isEmpty()) {
|
|
|
+ return CommonResult.fail("该批次下没有文件数据,请重新选择批次!");
|
|
|
+ }
|
|
|
+ //TODO: 此处需要定义任务开始,把相关任务信息添加上(任务名称、任务开始时间、任务类型),然后再处理文件。
|
|
|
+
|
|
|
+ List<Data> dataListInfo = dataList.stream().filter(data -> !StringUtils.isEmpty(data.getUrl())).toList();
|
|
|
+ if (dataListInfo.isEmpty()) {
|
|
|
+ return CommonResult.fail("该批次下没有文件数据,请重新选择批次!");
|
|
|
+ }
|
|
|
+ String filePath = TaaisConfig.getUploadPath();
|
|
|
+ LocalDate currentDate = LocalDate.now();
|
|
|
+ // 定义日期格式器
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
|
|
+ String formattedDate = currentDate.format(formatter);
|
|
|
+ filePath = filePath + File.separator + formattedDate;
|
|
|
+ String finalFilePath = filePath;
|
|
|
+ dataListInfo.forEach(dataInfo -> {
|
|
|
+ try {
|
|
|
+ //循环调用Python扩增接口
|
|
|
+ Map<String, Object> bodyJson = new HashMap<>();
|
|
|
+ bodyJson.put("augmentationType", dataAmplifyDto.getAugmentationType());
|
|
|
+ bodyJson.put("inputImagePath", dataInfo.getUrl());
|
|
|
+ String outputImagePath = finalFilePath + AMPLIFY + System.currentTimeMillis();
|
|
|
+ File desc = new File(outputImagePath);
|
|
|
+ if (!desc.exists()) {
|
|
|
+ log.info("创建文件目录: {}", desc.mkdirs());
|
|
|
+ }
|
|
|
+ bodyJson.put("outputImagePath", outputImagePath);
|
|
|
+ bodyJson.put("otherParams", dataAmplifyDto.getOtherParams());
|
|
|
+ //实际请求接口,接口未提供,暂且注释
|
|
|
+// String response = HttpRequest.post(PYTHON_DATA_AMPLIFY_API)
|
|
|
+// .body(JsonUtils.toJsonString(bodyJson))
|
|
|
+// .execute().body();
|
|
|
+ String response = "{\"status\":200,\"msg\":\"扩增成功\"}";
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ JsonNode rootNode = objectMapper.readTree(response);
|
|
|
+ String resultCode = rootNode.path(RESULT_CODE).asText();
|
|
|
+ //判断接口是否响应成功
|
|
|
+ if (!RESULT_STATUS.equals(resultCode)) {
|
|
|
+ throw new RuntimeException("调用Python接口返回扩增失败");
|
|
|
+ }
|
|
|
+ //处理当前目录文件,并进行入库
|
|
|
+ saveDataInfo(outputImagePath, dataInfo);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return CommonResult.fail("该批次下没有文件数据,请重新选择批次!");
|
|
|
+ }
|
|
|
+
|
|
|
private void initFileInfo(String dest, List<File> extractedImagesFileList, boolean directory, String fileName) {
|
|
|
if (!directory) {
|
|
|
String fileHeaderSuffix = StringUtils.substring(fileName, fileName.lastIndexOf("."), fileName.length());
|
|
@@ -368,4 +437,84 @@ public class DataServiceImpl extends BaseServiceImpl<DataMapper, Data> implement
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 解析目标目录所有文件,进行文件更名并入库
|
|
|
+ *
|
|
|
+ * @param directoryPath:目录地址
|
|
|
+ * @param dataInfo:深拷贝对象
|
|
|
+ */
|
|
|
+ public void saveDataInfo(String directoryPath, Data dataInfo) {
|
|
|
+ try {
|
|
|
+ // 获取指定目录下所有文件
|
|
|
+ File directory = new File(directoryPath);
|
|
|
+ List<File> extractedImagesFileList = new ArrayList<>();
|
|
|
+ if (directory.exists() && directory.isDirectory()) {
|
|
|
+ File[] files = directory.listFiles();
|
|
|
+ if (files != null) {
|
|
|
+ for (File file : files) {
|
|
|
+ initFileInfo(directoryPath, extractedImagesFileList, file.isDirectory(), file.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取ID集合
|
|
|
+ List<Long> ids = dataMapper.getIds(extractedImagesFileList.size());
|
|
|
+ if (ids.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Data> dataList = new ArrayList<>();
|
|
|
+ AtomicInteger countSize = new AtomicInteger();
|
|
|
+ extractedImagesFileList.forEach(fileInfo -> {
|
|
|
+ Long id = ids.get(countSize.get());
|
|
|
+ Data data = new Data();
|
|
|
+ BeanUtils.copyProperties(dataInfo, data);
|
|
|
+
|
|
|
+ if (checkLabeled(fileInfo.getPath())) {
|
|
|
+ data.setLabeled(Boolean.TRUE);
|
|
|
+ } else {
|
|
|
+ data.setLabeled(Boolean.FALSE);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Path path = Paths.get(fileInfo.getAbsolutePath());
|
|
|
+ BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
|
|
|
+ Instant creationTime = attrs.lastModifiedTime().toInstant();
|
|
|
+ Date date = Date.from(creationTime);
|
|
|
+
|
|
|
+ data.setId(id);
|
|
|
+ data.setGatherTime(date);
|
|
|
+ data.setName(fileInfo.getName());
|
|
|
+
|
|
|
+ // 更改图片文件名称
|
|
|
+ String fileHeaderSuffix = StringUtils.substring(fileInfo.getName(), fileInfo.getName().lastIndexOf("."), fileInfo.getName().length());
|
|
|
+ String destInfo = fileInfo.getPath().replaceAll(fileInfo.getName(), "");
|
|
|
+ File newFile = new File(destInfo, id + fileHeaderSuffix);
|
|
|
+ File oldFile = new File(destInfo, fileInfo.getName());
|
|
|
+ log.info("saveDataInfo更改用户上传图片文件名称:{}", oldFile.renameTo(newFile));
|
|
|
+
|
|
|
+ String imagePath = FileUploadUtils.getPathFileName(destInfo, id + fileHeaderSuffix);
|
|
|
+ data.setUrl(imagePath);
|
|
|
+
|
|
|
+ if (data.getLabeled()) {
|
|
|
+ String labeledPath = fileInfo.getPath().replaceFirst("[.][^.]+$", "") + ".txt";
|
|
|
+ File labeledNewFile = new File(destInfo, id + ".txt");
|
|
|
+ File labeledOldFile = new File(labeledPath);
|
|
|
+ log.info("saveDataInfo更改用户上传标注文件名称:{}", labeledOldFile.renameTo(labeledNewFile));
|
|
|
+ String labelUrl = FileUploadUtils.getPathFileName(destInfo, id + ".txt");
|
|
|
+ data.setLabelurl(labelUrl);
|
|
|
+ }
|
|
|
+ dataList.add(data);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ countSize.getAndIncrement();
|
|
|
+ });
|
|
|
+
|
|
|
+ dataMapper.insertBatch(dataList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[saveDataInfo]数据集处理出现未知异常.e:", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|