|
@@ -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);
|
|
|
+ }
|
|
|
+}
|