SceneMarkShapeServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.fdkankan.openApi.service.www.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import cn.hutool.core.lang.UUID;
  6. import cn.hutool.core.util.ObjectUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.baomidou.dynamic.datasource.annotation.DS;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  13. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.fdkankan.common.constant.ErrorCode;
  16. import com.fdkankan.common.exception.BusinessException;
  17. import com.fdkankan.model.constants.ConstantFilePath;
  18. import com.fdkankan.openApi.dto.SceneJsonDTO;
  19. import com.fdkankan.openApi.dto.SceneMarkShapesDTO;
  20. import com.fdkankan.openApi.entity.www.SceneMarkShape;
  21. import com.fdkankan.openApi.mapper.www.IMarkShapeMapper;
  22. import com.fdkankan.openApi.service.www.ISceneMarkShapeService;
  23. import com.fdkankan.openApi.util.ConverxyUtil;
  24. import com.fdkankan.openApi.vo.www.SceneMarkShapeParamVO;
  25. import com.fdkankan.openApi.vo.www.SceneMarkShapeReDetectParamVO;
  26. import com.fdkankan.openApi.vo.www.SceneMarkShapeVO;
  27. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  28. import com.fdkankan.web.response.ResultData;
  29. import lombok.extern.slf4j.Slf4j;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.beans.factory.annotation.Value;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import java.io.File;
  35. import java.io.IOException;
  36. import java.util.*;
  37. import java.util.stream.Collectors;
  38. /**
  39. * Created by Xiewj on 2021/11/23 0026 10:14
  40. */
  41. @Slf4j
  42. @Service
  43. @DS("www")
  44. public class SceneMarkShapeServiceImpl extends ServiceImpl<IMarkShapeMapper, SceneMarkShape> implements ISceneMarkShapeService {
  45. @Autowired
  46. private RabbitMqProducer rabbitMqProducer;
  47. @Value("${queue.scene.yolov5-train-queue}")
  48. private String yolov5TrainQueue;
  49. @Value("${main.url}")
  50. private String mainUrl;
  51. @Autowired
  52. private ISceneMarkShapeService sceneMarkShapeService;
  53. @Override
  54. public ResultData saveOrEdit(SceneMarkShapeVO param) {
  55. if (Objects.isNull(param.getShapes())) {
  56. throw new BusinessException(ErrorCode.PARAM_REQUIRED,"shapes 为空");
  57. }
  58. log.info("标注数据保存或修改-场景码:{},场景图片:{},",param.getNum(),param.getImagePath());
  59. SceneMarkShape shape = this.findByNumAndImagePath(param.getNum(), param.getImagePath());
  60. SceneMarkShape sceneMarkShape=new SceneMarkShape();
  61. BeanUtil.copyProperties(param,sceneMarkShape);
  62. if (ObjectUtil.isNotNull(shape)){
  63. log.info("修改成功-修改id--{}",shape.getId());
  64. sceneMarkShape.setId(shape.getId());
  65. sceneMarkShape.setUpdateTime(new Date());
  66. return ResultData.ok(this.updateById(sceneMarkShape));
  67. }else {
  68. log.info("保存成功");
  69. sceneMarkShape.setCreateTime(new Date());
  70. return ResultData.ok(this.save(sceneMarkShape));
  71. }
  72. }
  73. @Override
  74. public ResultData saveOrEditBatch(SceneMarkShapesDTO param) {
  75. Set<String> imagePaths = param.getList().stream().map(v -> v.getImagePath()).collect(Collectors.toSet());
  76. List<SceneMarkShape> sceneMarkShapes = this.baseMapper.selectByNumAndImagePaths(param.getNum(), imagePaths);
  77. Map<String, Long> imagePathIdMap = sceneMarkShapes.stream().collect(Collectors.toMap(SceneMarkShape::getImagePath, SceneMarkShape::getId));
  78. List<SceneMarkShape> sceneMarkShapeList = BeanUtil.copyToList(param.getList(), SceneMarkShape.class);
  79. sceneMarkShapeList.stream().forEach(v -> v.setNum(param.getNum()));
  80. if(CollUtil.isNotEmpty(imagePathIdMap)){
  81. sceneMarkShapeList.stream().forEach(v->{
  82. v.setId(imagePathIdMap.get(v.getImagePath()));
  83. });
  84. }
  85. return ResultData.ok(this.saveOrUpdateBatch(sceneMarkShapeList));
  86. }
  87. @Override
  88. public ResultData getShapesInfo(SceneMarkShapeParamVO param) {
  89. SceneMarkShape res= findByNumAndImagePath(param.getNum(),param.getImagePath());
  90. SceneMarkShapeVO vo=new SceneMarkShapeVO();
  91. BeanUtil.copyProperties(res,vo);
  92. return ResultData.ok(vo);
  93. }
  94. @Override
  95. public void editTrainStatus(SceneMarkShapeParamVO param) {
  96. SceneMarkShape byNumAndImagePath = findByNumAndImagePath(param.getNum(), param.getImagePath());
  97. if (ObjectUtil.isNotNull(byNumAndImagePath)){
  98. byNumAndImagePath.setReTrain(0);
  99. byNumAndImagePath.setToTrain(1);
  100. updateById(byNumAndImagePath);
  101. }
  102. }
  103. @Override
  104. public List<SceneMarkShape> findByReTrainStatus(Integer reTrain){
  105. LambdaQueryWrapper<SceneMarkShape> wrapper = Wrappers.lambdaQuery();
  106. wrapper.eq(SceneMarkShape::getReTrain,reTrain);
  107. return list(wrapper);
  108. }
  109. @Override
  110. public List<SceneMarkShape> findByToTrainStatus(Integer toTrain) {
  111. QueryWrapper queryWrapper = new QueryWrapper();
  112. queryWrapper.select("DISTINCT num")
  113. .eq("to_train",toTrain) ;
  114. return getBaseMapper().selectList(queryWrapper);
  115. }
  116. @Override
  117. public void trainScene(SceneMarkShapeReDetectParamVO param) {
  118. //查询需要重新训练的图片
  119. List<SceneMarkShape> reDetectStatuList = findByReTrainStatus(1);
  120. for (SceneMarkShape shape : reDetectStatuList) {
  121. SceneMarkShapeReDetectParamVO paramVO=new SceneMarkShapeReDetectParamVO();
  122. paramVO.setWebSite(mainUrl);
  123. if (StrUtil.isNotEmpty(param.getSaveDir())){
  124. paramVO.setSaveDir(param.getSaveDir());
  125. }
  126. paramVO.setNum(shape.getNum());
  127. paramVO.setImagePath(shape.getImagePath());
  128. paramVO.setDetectType(1);
  129. rabbitMqProducer.sendByWorkQueue(yolov5TrainQueue,paramVO);
  130. }
  131. //查询需要进入训练的场景
  132. List<SceneMarkShape> byToDetectStatus = findByToTrainStatus(0);
  133. for (SceneMarkShape shape : byToDetectStatus) {
  134. SceneMarkShapeReDetectParamVO paramVO=new SceneMarkShapeReDetectParamVO();
  135. paramVO.setWebSite(mainUrl);
  136. if (StrUtil.isNotEmpty(param.getSaveDir())){
  137. paramVO.setSaveDir(param.getSaveDir());
  138. }
  139. paramVO.setNum(shape.getNum());
  140. paramVO.setDetectType(2);
  141. rabbitMqProducer.sendByWorkQueue(yolov5TrainQueue,paramVO);
  142. }
  143. }
  144. @Override
  145. public ResultData editLabelByFile(String num, String imgPath, MultipartFile file) throws IOException {
  146. SceneMarkShape shape = findByNumAndImagePath(num, imgPath);
  147. if (ObjectUtil.isNotNull(shape)){
  148. String uuid = UUID.randomUUID().toString();
  149. String fileName = file.getOriginalFilename();
  150. String extName = FileUtil.extName(fileName);
  151. String tempFileName = uuid + "." + extName;
  152. String srcPath = ConstantFilePath.SCENE_V4_PATH + num + "/markShapes/" + tempFileName;
  153. File tempFile = new File(srcPath);
  154. if(!tempFile.getParentFile().exists()){
  155. tempFile.getParentFile().mkdirs();
  156. }
  157. file.transferTo(tempFile);
  158. List<String> s = FileUtil.readUtf8Lines(tempFile);
  159. List<JSONObject> shapeJsons=new ArrayList<>();
  160. //转换labelimg标注处理的结果
  161. log.info("转换labelimg标注处理的结果开始");
  162. for (String s1 : s) {
  163. int[] ints = ConverxyUtil.centerWh2xyxy(s1, 4096,2048);
  164. String[] s2 = s1.split(" ");
  165. JSONObject shapeJson=new JSONObject();
  166. shapeJson.put("bbox",ints);
  167. shapeJson.put("color",ConverxyUtil.getColor(s2[0]));
  168. shapeJson.put("label",s1);
  169. shapeJson.put("category", ConverxyUtil.getLabelVal(s2[0]));
  170. shapeJson.put("score",0);
  171. shapeJsons.add(shapeJson);
  172. }
  173. log.info("转换labelimg标注处理的结果结束,{}",shapeJsons);
  174. shape.setShapes(shapeJsons);
  175. shape.setReTrain(1);
  176. shape.setUpdateTime(new Date());
  177. updateById(shape);
  178. return ResultData.ok(shape);
  179. }else {
  180. return ResultData.error(ErrorCode.NOT_RECORD);
  181. }
  182. }
  183. @Override
  184. public SceneMarkShape findByNumAndImagePath(String num, String imagePath) {
  185. LambdaQueryWrapper<SceneMarkShape> wrapper = Wrappers.lambdaQuery();
  186. wrapper.eq(SceneMarkShape::getNum,num);
  187. wrapper.eq(SceneMarkShape::getImagePath,imagePath);
  188. return getOne(wrapper);
  189. }
  190. @Override
  191. public void saveFileToDB(MultipartFile file, String num) throws IOException {
  192. String uuid = UUID.randomUUID().toString();
  193. String fileName = file.getOriginalFilename();
  194. String extName = FileUtil.extName(fileName);
  195. String tempFileName = uuid + "." + extName;
  196. String srcPath = ConstantFilePath.SCENE_V4_PATH + num + "/markShapes/" + tempFileName;
  197. File tempFile = new File(srcPath);
  198. if(!tempFile.getParentFile().exists()){
  199. tempFile.getParentFile().mkdirs();
  200. }
  201. file.transferTo(tempFile);
  202. String s = FileUtil.readUtf8String(tempFile);
  203. JSONObject jsonObject = JSONObject.parseObject(s);
  204. tempFile.delete();
  205. SceneMarkShape sceneMarkShape= JSON.toJavaObject(jsonObject,SceneMarkShape.class);
  206. sceneMarkShape.setNum(num);
  207. SceneMarkShape shape = findByNumAndImagePath(sceneMarkShape.getNum(), sceneMarkShape.getImagePath());
  208. if (ObjectUtil.isNotNull(shape)){
  209. log.info("shape-替换id修改---{}",sceneMarkShape);
  210. sceneMarkShape.setId(shape.getId());
  211. updateById(sceneMarkShape);
  212. }else {
  213. log.info("新增-替换id修改---{}",sceneMarkShape);
  214. log.info("MarkShapeMapper---{}",sceneMarkShape);
  215. save(sceneMarkShape);
  216. }
  217. }
  218. @Override
  219. public List<SceneMarkShape> findByNum(String num) {
  220. LambdaQueryWrapper<SceneMarkShape> wrapper = Wrappers.lambdaQuery();
  221. wrapper.eq(SceneMarkShape::getNum,num);
  222. return list(wrapper);
  223. }
  224. }