Forráskód Böngészése

增加修改标注接口

xiewj 2 éve
szülő
commit
caeea94d0e

+ 51 - 0
src/main/java/com/fdkankan/scene/controller/SceneMarkShapeController.java

@@ -1,9 +1,14 @@
 package com.fdkankan.scene.controller;
 
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.lang.UUID;
 import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.common.constant.CommonOperStatus;
+import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.fyun.face.FYunFileServiceInterface;
+import com.fdkankan.model.constants.ConstantFilePath;
 import com.fdkankan.model.constants.UploadFilePath;
 import com.fdkankan.rabbitmq.util.RabbitMqProducer;
 import com.fdkankan.scene.entity.SceneMarkShape;
@@ -12,12 +17,14 @@ import com.fdkankan.scene.entity.ScenePlusExt;
 import com.fdkankan.scene.service.ISceneMarkShapeService;
 import com.fdkankan.scene.service.IScenePlusExtService;
 import com.fdkankan.scene.service.IScenePlusService;
+import com.fdkankan.scene.util.ConverxyUtil;
 import com.fdkankan.scene.vo.FileParamVO;
 import com.fdkankan.scene.vo.SceneMarkShapeDetectParamVO;
 import com.fdkankan.scene.vo.SceneMarkShapeParamVO;
 import com.fdkankan.web.controller.BaseController;
 import com.fdkankan.web.response.Result;
 import com.fdkankan.web.response.ResultData;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.validation.annotation.Validated;
@@ -25,7 +32,9 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -36,6 +45,7 @@ import java.util.stream.Collectors;
  */
 @RestController
 @RequestMapping("/service/scene/sceneMarkShape")
+@Slf4j
 public class SceneMarkShapeController extends BaseController
 {
     @Value("${queue.scene.yolov5-detect-queue}")
@@ -113,4 +123,45 @@ public class SceneMarkShapeController extends BaseController
             return ResultData.ok(sceneMarkShapeService.save(param));
         }
     }
+    /**
+     * 保存或者修改JSON
+     */
+    @PostMapping("/editLabel")
+    public ResultData editLabel(@RequestParam(value = "num") String num,@RequestParam(value = "imgPath") String imgPath,@RequestParam("file") MultipartFile file) throws IOException {
+        log.info("进入editLabel---num{},imgPath{}",num,imgPath);
+        SceneMarkShape shape = sceneMarkShapeService.findByNumAndImagePath(num, imgPath);
+        if (ObjectUtil.isNotNull(shape)){
+            String uuid = UUID.randomUUID().toString();
+            String fileName = file.getOriginalFilename();
+            String extName = cn.hutool.core.io.FileUtil.extName(fileName);
+            String tempFileName = uuid + "." + extName;
+            String srcPath = ConstantFilePath.SCENE_V4_PATH + num + "/markShapes/" + tempFileName;
+            File tempFile = new File(srcPath);
+            if(!tempFile.getParentFile().exists()){
+                tempFile.getParentFile().mkdirs();
+            }
+            file.transferTo(tempFile);
+
+            List<String> s = FileUtil.readUtf8Lines(tempFile);
+            List<JSONObject> shapeJsons=new ArrayList<>();
+            //转换labelimg标注处理的结果
+            log.info("转换labelimg标注处理的结果开始");
+            for (String s1 : s) {
+                int[] ints = ConverxyUtil.centerWh2xyxy(s1, 4096,2048);
+                String[] s2 = s1.split(" ");
+                JSONObject shapeJson=new JSONObject();
+                shapeJson.put("bbox",ints);
+                shapeJson.put("color",ConverxyUtil.getColor(s2[0]));
+                shapeJson.put("label",s1);
+                shapeJson.put("category",ConverxyUtil.getLabelVal(s2[0]));
+                shapeJson.put("score",0);
+                shapeJsons.add(shapeJson);
+            }
+            log.info("转换labelimg标注处理的结果结束,{}",shapeJsons);
+            shape.setShapes(shapeJsons);
+            return ResultData.ok(sceneMarkShapeService.updateById(shape));
+        }else {
+            return ResultData.error(ErrorCode.NOT_RECORD);
+        }
+    }
 }

+ 5 - 3
src/main/java/com/fdkankan/scene/entity/SceneMarkShape.java

@@ -44,12 +44,14 @@ public class SceneMarkShape implements Serializable {
       @TableField("image_height")
       private Integer imageHeight;
       @TableField("image_width")
-
       private Integer imageWidth;
       @TableField("num")
       private String num;
-
-
+      /**
+       * 0不需要 1需要
+       */
+      @TableField("re_detect")
+      private Integer reDetect;
       @TableField("create_time")
       private Date createTime;
 

+ 121 - 0
src/main/java/com/fdkankan/scene/util/ConverxyUtil.java

@@ -0,0 +1,121 @@
+package com.fdkankan.scene.util;
+
+import cn.hutool.core.io.FileUtil;
+import com.alibaba.fastjson.JSONObject;
+
+import java.io.File;
+import java.math.BigDecimal;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Xiewj
+ * @date 2023/4/11
+ */
+public class ConverxyUtil {
+   static Map<String,String> label=new HashMap<>();
+   static Map<String,int[]> color=new HashMap<>();
+
+   static void init() {
+      label.put("0","cabinet");
+      label.put("1","air");
+      label.put("2","battery");
+
+      color.put("0",new int[]{56,56,255});
+      color.put("1",new int[]{151,157,255});
+      color.put("2",new int[]{31,112,255});
+   }
+   public static String getLabelVal(String key) {
+      if (label.size()==0){
+         init();
+      }
+      //标签类型初始化
+     return label.get(key);
+   }
+   public static int[] getColor(String key) {
+      if (color.size()==0){
+         init();
+      }
+      //标签类型初始化
+      return color.get(key);
+   }
+      // 将中心宽高格式转换为左上角点和右下角点格式
+   /**
+    * 将xywh格式的目标框转换为xyxy格式
+    * @param xywh xywh格式的目标框,格式为"x,y,w,h"
+    * @return xyxy格式的目标框,格式为"x1,y1,x2,y2"
+    */
+   public static int[] centerWh2xyxy(String xywh,int w1,int h1){
+      String[] values = xywh.split(" ");
+      if (values.length<4){
+         return null;
+      }
+      float x = Float.parseFloat(values[1]);
+      float y = Float.parseFloat(values[2]);
+      float w = Float.parseFloat(values[3]);
+      float h = Float.parseFloat(values[4]);
+      int[] xyxy = new int[4];
+      xyxy[0] = (int) ((x - w / 2) * w1); // x1
+      xyxy[1] = (int) ((y - h / 2)* h1); // y1
+      xyxy[2] = (int) ((x + w / 2)* w1); // x2
+      xyxy[3] = (int) ((y+ h / 2)* h1); // y2
+      return xyxy;
+   }
+
+   // 将左上角点和右下角点格式转换为中心宽高格式
+   public static double[] xyxy2centerWh(int x1, int y1, int x2, int y2,int w1,int h1){
+
+      double[] centerWh = new double[4];
+      BigDecimal dx1=BigDecimal.valueOf(x1);
+      BigDecimal  dy1=BigDecimal.valueOf(y1);
+      BigDecimal  dx2=BigDecimal.valueOf(x2);
+      BigDecimal dy2= BigDecimal.valueOf(y2);
+      BigDecimal  dw1=BigDecimal.valueOf(w1);
+      BigDecimal  dh1=BigDecimal.valueOf(h1);
+
+
+      centerWh[0] =((dx1.add(dx2)).divide(BigDecimal.valueOf(2))).divide(dw1).doubleValue(); // centerX
+      centerWh[1] = ((dy1.add(dy2)).divide(BigDecimal.valueOf(2))).divide(dh1).doubleValue(); // centerX
+      centerWh[2] = dx2.subtract(dx1).divide(dw1).doubleValue(); // width
+      centerWh[3] =  dy2.subtract(dy1).divide(dh1).doubleValue(); // width
+
+      return centerWh;
+   }
+   public static void main(String[] args) {
+      // 中心宽高格式转换为左上角点和右下角点格式
+//      int[] xyxy = ConverxyUtil.centerWh2xyxy("0.215149 0.557373 0.067749 0.329590",8192,4096);
+//
+//      System.out.println("x1=" + xyxy[0] + ", y1=" + xyxy[1] + ", x2=" + xyxy[2] + ", y2=" + xyxy[3]);
+//
+//      // 左上角点和右下角点格式转换为中心宽高格式
+//      double[] centerWh = ConverxyUtil.xyxy2centerWh(xyxy[0], xyxy[1], xyxy[2], xyxy[3],8192,4096);
+//      System.out.println("centerX=" + centerWh[0] + ", centerY=" + centerWh[1] + ", width=" + centerWh[2] + ", height=" + centerWh[3]);
+//      int[] xyxy1 = ConverxyUtil.centerWh2xyxy("0.21514892578125 0.5572509765625 0.0677490234375 0.329833984375",8192,4096);
+//      System.out.println("x1=" + xyxy1[0] + ", y1=" + xyxy1[1] + ", x2=" + xyxy1[2] + ", y2=" + xyxy1[3]);
+//
+////      Object labelVal = ConverxyUtil.getLabelVal("1");
+////      System.out.println(labelVal);
+
+      File tempFile=new File("C:\\Users\\4DAGE\\Downloads\\KK-UR4UGMSHEK3\\images\\0.txt");
+      List<String> s = FileUtil.readUtf8Lines(tempFile);
+
+
+      List<JSONObject> shapeJsons=new ArrayList<>();
+      //转换labelimg标注处理的结果
+      for (String s1 : s) {
+         int[] ints = ConverxyUtil.centerWh2xyxy(s1, 4096,2048);
+         String[] s2 = s1.split(" ");
+         JSONObject shapeJson=new JSONObject();
+         shapeJson.put("bbox",ints);
+         shapeJson.put("color",ConverxyUtil.getColor(s2[0]));
+         shapeJson.put("label",s1);
+         shapeJson.put("category",ConverxyUtil.getLabelVal(s2[0]));
+         shapeJson.put("score",0);
+         shapeJsons.add(shapeJson);
+      }
+      System.out.println(shapeJsons);
+   }
+}