|
@@ -0,0 +1,144 @@
|
|
|
+package com.fdkankan.contro.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.contro.constant.DetectType;
|
|
|
+import com.fdkankan.contro.entity.SceneMarkShape;
|
|
|
+import com.fdkankan.contro.entity.ScenePlus;
|
|
|
+import com.fdkankan.contro.entity.ScenePlusExt;
|
|
|
+import com.fdkankan.contro.entity.SceneShapeEnum;
|
|
|
+import com.fdkankan.contro.service.*;
|
|
|
+import com.fdkankan.fyun.face.FYunFileServiceInterface;
|
|
|
+import com.fdkankan.model.constants.ConstantFilePath;
|
|
|
+import com.fdkankan.model.constants.UploadFilePath;
|
|
|
+import com.fdkankan.model.utils.ComputerUtil;
|
|
|
+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;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AiServiceImpl implements IAiService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IScenePlusService scenePlusService;
|
|
|
+ @Autowired
|
|
|
+ private IScenePlusExtService scenePlusExtService;
|
|
|
+ @Resource
|
|
|
+ private FYunFileServiceInterface 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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(v.getAbsolutePath(),keyPath+v.getName()));
|
|
|
+ }
|
|
|
+
|
|
|
+ }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.uploadFile(JSON.toJSONString(sceneMarkShapes).getBytes(StandardCharsets.UTF_8), ajJsonKey);
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|