123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package com.fdkankan.scene.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.common.util.CmdUtils;
- 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.entity.ScenePlus;
- import com.fdkankan.scene.entity.ScenePlusExt;
- import com.fdkankan.scene.entity.SceneShapeEnum;
- import com.fdkankan.scene.oss.OssUtil;
- import com.fdkankan.scene.service.*;
- 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;
- @Slf4j
- @Service
- public class AiServiceImpl implements IAiService {
- @Autowired
- private IScenePlusService scenePlusService;
- @Autowired
- private IScenePlusExtService scenePlusExtService;
- @Resource
- private OssUtil 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;
- }
- FileUtil.mkdir(aiWorkPath);
- 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;
- FileUtil.mkdir(outPath);
- String detectPath = outPath + "detect.json";
- String cmd = CmdConstant.PANO_DETECT.replace("@in",absolutePath).replace("@out", detectPath);
- CmdUtils.callLine(cmd, 50);
- }catch (Exception e){
- log.error("ai识别报错,inPath:{}", absolutePath, e);
- }
- }
- 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(scenePlusExt.getYunFileBucket(), keyPath+v.getName(), v.getAbsolutePath(), false));
- }
- }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.uploadFileBytes(scenePlusExt.getYunFileBucket(), ajJsonKey, JSON.toJSONString(sceneMarkShapes).getBytes(StandardCharsets.UTF_8));
- 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;
- }
- }
|