|
|
|
@ -50,14 +50,14 @@ public class ONNXServiceNew {
|
|
|
|
|
|
|
|
|
|
|
|
System.load(new File(System.getProperty("user.dir")+"\\libs\\opencv\\opencv_java480.dll").getAbsolutePath());
|
|
|
|
System.load(new File(System.getProperty("user.dir")+"\\libs\\opencv\\opencv_java480.dll").getAbsolutePath());
|
|
|
|
ortMap.put("a", new AppConfig.YoloModelConfig());
|
|
|
|
ortMap.put("a", new AppConfig.YoloModelConfig());
|
|
|
|
ortMap.get("a").setImageSize(2048);
|
|
|
|
ortMap.get("a").setImageSize(1024);
|
|
|
|
ortMap.get("a").setConfThreshold(0.5f);
|
|
|
|
ortMap.get("a").setConfThreshold(0.5f);
|
|
|
|
ortMap.get("a").setNames(new String[]{"0"});
|
|
|
|
ortMap.get("a").setNames(new String[]{"0143","0153","0173","0177","0191","0253","0256","0266","0268","0286","0302","0304","0305","0307","0320","0326","0336","0339","0343","0352","0458","0461","0462","0473","0477","0486","0490","0492","0930","1101","1102","1104","1262","1269","1302","1308","1359","1366","1622","1625","1919","1976","20","2165","2188","2210","2224","2445","2476","2611","2730","2731","2910","2914","2943","3027","3028","3029","3212","3226","3344","3501","3509","3538","3725","3741","3751","3754","3763","3766","3808"});
|
|
|
|
|
|
|
|
|
|
|
|
int imageSize = 2048;
|
|
|
|
int imageSize = 1024;
|
|
|
|
String imagePath = "D:\\data\\1776157002220.png";
|
|
|
|
String imagePath = "D:\\PycharmProjects\\yolo\\puer\\train\\2210\\aug_5_1_ed7e63af-f9f5-4c49-baee-12f635aa7b5c.jpg.jpg";
|
|
|
|
|
|
|
|
|
|
|
|
String modelPath = "D:\\data\\best.onnx";
|
|
|
|
String modelPath = "D:\\PycharmProjects\\yolo\\runs\\classify\\train14\\weights\\best.onnx";
|
|
|
|
// List<String> name = Arrays.asList("0143", "0153", "0173", "0177", "0191", "0253", "0256", "0266", "0268", "0286", "0302", "0304", "0305", "0307", "0320", "0326", "0336", "0339", "0343", "0352", "0458", "0461", "0462", "0473", "0477", "0486", "0490", "0492", "0930", "1101", "1102", "1104", "1262", "1269", "1302", "1308", "1359", "1366", "1622", "1625", "1919", "1976", "20", "2165", "2188", "2210", "2224", "2445", "2476", "2611", "2730", "2731", "2910", "2914", "2943", "3027", "3028", "3029", "3212", "3226", "3344", "3501", "3509", "3538", "3725", "3741", "3751", "3754", "3763", "3766", "3808");
|
|
|
|
// List<String> name = Arrays.asList("0143", "0153", "0173", "0177", "0191", "0253", "0256", "0266", "0268", "0286", "0302", "0304", "0305", "0307", "0320", "0326", "0336", "0339", "0343", "0352", "0458", "0461", "0462", "0473", "0477", "0486", "0490", "0492", "0930", "1101", "1102", "1104", "1262", "1269", "1302", "1308", "1359", "1366", "1622", "1625", "1919", "1976", "20", "2165", "2188", "2210", "2224", "2445", "2476", "2611", "2730", "2731", "2910", "2914", "2943", "3027", "3028", "3029", "3212", "3226", "3344", "3501", "3509", "3538", "3725", "3741", "3751", "3754", "3763", "3766", "3808");
|
|
|
|
|
|
|
|
|
|
|
|
OrtSession.SessionOptions sessionOptions = null;
|
|
|
|
OrtSession.SessionOptions sessionOptions = null;
|
|
|
|
@ -72,7 +72,7 @@ public class ONNXServiceNew {
|
|
|
|
sessionOptions = new OrtSession.SessionOptions();
|
|
|
|
sessionOptions = new OrtSession.SessionOptions();
|
|
|
|
session = environment.createSession(modelPath, sessionOptions);
|
|
|
|
session = environment.createSession(modelPath, sessionOptions);
|
|
|
|
ortSessions.put("a", session);
|
|
|
|
ortSessions.put("a", session);
|
|
|
|
List<BoundingBox> boxes = onnxServiceNew.detect(imagePath, "a");
|
|
|
|
ClassifyEntity classifyEntity = onnxServiceNew.classifyOnee(imagePath, "a");
|
|
|
|
|
|
|
|
|
|
|
|
// classifyEntity.setIndex(predictedClassId);
|
|
|
|
// classifyEntity.setIndex(predictedClassId);
|
|
|
|
// classifyEntity.setName(config.getNames()[predictedClassId]);
|
|
|
|
// classifyEntity.setName(config.getNames()[predictedClassId]);
|
|
|
|
@ -84,7 +84,7 @@ public class ONNXServiceNew {
|
|
|
|
|
|
|
|
|
|
|
|
// DrawBoundingBox.drawBoundingBoxesOnImage(imagePath, "D:\\data\\2024-04-24_08-55-42-492data.BMP.jpg", filteredDetections);
|
|
|
|
// DrawBoundingBox.drawBoundingBoxesOnImage(imagePath, "D:\\data\\2024-04-24_08-55-42-492data.BMP.jpg", filteredDetections);
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(boxes);
|
|
|
|
System.out.println(classifyEntity);
|
|
|
|
// System.out.println(name.get(predictedClassId));
|
|
|
|
// System.out.println(name.get(predictedClassId));
|
|
|
|
} catch (OrtException e) {
|
|
|
|
} catch (OrtException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
@ -481,7 +481,9 @@ public class ONNXServiceNew {
|
|
|
|
float[] outputData;
|
|
|
|
float[] outputData;
|
|
|
|
if (output instanceof float[][][]) {
|
|
|
|
if (output instanceof float[][][]) {
|
|
|
|
// 三维数组 float[][][]
|
|
|
|
// 三维数组 float[][][]
|
|
|
|
outputData = ((float[][][]) output)[0];
|
|
|
|
// 三维数组 float[][][],取最后维度的 [0] 得到一维
|
|
|
|
|
|
|
|
float[][][] arr3d = (float[][][]) output;
|
|
|
|
|
|
|
|
outputData = arr3d[arr3d.length - 1][0];
|
|
|
|
} else if (output instanceof float[][]) {
|
|
|
|
} else if (output instanceof float[][]) {
|
|
|
|
// 二维数组 float[][]
|
|
|
|
// 二维数组 float[][]
|
|
|
|
outputData = ((float[][]) output)[0];
|
|
|
|
outputData = ((float[][]) output)[0];
|
|
|
|
@ -556,13 +558,14 @@ public class ONNXServiceNew {
|
|
|
|
Object output = result.get(0).getValue();
|
|
|
|
Object output = result.get(0).getValue();
|
|
|
|
float[] outputData;
|
|
|
|
float[] outputData;
|
|
|
|
if (output instanceof float[][][]) {
|
|
|
|
if (output instanceof float[][][]) {
|
|
|
|
// 三维数组 float[][][]
|
|
|
|
// 三维数组 float[][][],取最后维度的 [0] 得到一维
|
|
|
|
outputData = ((float[][][]) output)[0];
|
|
|
|
float[][][] arr3d = (float[][][]) output;
|
|
|
|
|
|
|
|
outputData = arr3d[arr3d.length - 1][0];
|
|
|
|
} else if (output instanceof float[][]) {
|
|
|
|
} else if (output instanceof float[][]) {
|
|
|
|
// 二维数组 float[][]
|
|
|
|
// 二维数组 float[][],取 [0] 得到一维
|
|
|
|
outputData = ((float[][]) output)[0];
|
|
|
|
outputData = ((float[][]) output)[0];
|
|
|
|
} else if (output instanceof float[]) {
|
|
|
|
} else if (output instanceof float[]) {
|
|
|
|
// 一维数组 float[]
|
|
|
|
// 一维数组 float[],直接使用
|
|
|
|
outputData = (float[]) output;
|
|
|
|
outputData = (float[]) output;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Unexpected output type: " + output.getClass().getName());
|
|
|
|
throw new RuntimeException("Unexpected output type: " + output.getClass().getName());
|
|
|
|
@ -597,6 +600,85 @@ public class ONNXServiceNew {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* yolo11计算图片最佳的模版
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param imagePath 输入的图片位置
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
* @throws OrtException
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public ClassifyEntity classifyOnee(String imagePath, String type) throws OrtException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OrtSession session = getSession(type);
|
|
|
|
|
|
|
|
AppConfig.YoloModelConfig config = getConfig(type);
|
|
|
|
|
|
|
|
// 处理图像
|
|
|
|
|
|
|
|
float[] imageData = new float[0];
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
imageData = processImageFromURL(imagePath, config.getImageSize());
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建输入张量
|
|
|
|
|
|
|
|
long[] shape = new long[]{1, 3, config.getImageSize(), config.getImageSize()}; // batch_size, channels, height, width
|
|
|
|
|
|
|
|
OnnxTensor inputTensor = null;
|
|
|
|
|
|
|
|
OrtSession.Result result = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
inputTensor = OnnxTensor.createTensor(environment, FloatBuffer.wrap(imageData), shape);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashMap<String, OnnxTensor> stringOnnxTensorHashMap = new HashMap<>();
|
|
|
|
|
|
|
|
stringOnnxTensorHashMap.put(session.getInputInfo().keySet().iterator().next(), inputTensor);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行推理
|
|
|
|
|
|
|
|
result = session.run(stringOnnxTensorHashMap);
|
|
|
|
|
|
|
|
// 获取第一个输出(大多数情况下只有一个输出)
|
|
|
|
|
|
|
|
// 动态判断输出形状,避免 ClassCastException
|
|
|
|
|
|
|
|
Object output = result.get(0).getValue();
|
|
|
|
|
|
|
|
float[] outputData;
|
|
|
|
|
|
|
|
if (output instanceof float[][][]) {
|
|
|
|
|
|
|
|
// 三维数组 float[][][],取最后维度的 [0] 得到一维
|
|
|
|
|
|
|
|
float[][][] arr3d = (float[][][]) output;
|
|
|
|
|
|
|
|
outputData = arr3d[arr3d.length - 1][0];
|
|
|
|
|
|
|
|
} else if (output instanceof float[][]) {
|
|
|
|
|
|
|
|
// 二维数组 float[][],取 [0] 得到一维
|
|
|
|
|
|
|
|
outputData = ((float[][]) output)[0];
|
|
|
|
|
|
|
|
} else if (output instanceof float[]) {
|
|
|
|
|
|
|
|
// 一维数组 float[],直接使用
|
|
|
|
|
|
|
|
outputData = (float[]) output;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new RuntimeException("Unexpected output type: " + output.getClass().getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取最大概率对应的类别 ID
|
|
|
|
|
|
|
|
int predictedClassId = argmax(outputData, 0.8);
|
|
|
|
|
|
|
|
ClassifyEntity classifyEntity = new ClassifyEntity();
|
|
|
|
|
|
|
|
classifyEntity.setIndex(predictedClassId);
|
|
|
|
|
|
|
|
if (predictedClassId!=-1){
|
|
|
|
|
|
|
|
log.info("识别模版:{}",config.getNames()[predictedClassId]);
|
|
|
|
|
|
|
|
classifyEntity.setName(config.getNames()[predictedClassId]);
|
|
|
|
|
|
|
|
classifyEntity.setConfidence(outputData[predictedClassId]);
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
|
|
|
log.info("未识别模版");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// classifyEntity.setClassProb(String.format("%.4f", outputData[i]));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// float[] output = (float[]) result.get(0).getValue();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DrawBoundingBox.drawBoundingBoxesOnImage(imagePath, "D:\\data\\2024-04-24_08-55-42-492data.BMP.jpg", filteredDetections);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return classifyEntity;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
if (inputTensor != null) {
|
|
|
|
|
|
|
|
inputTensor.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != null) {
|
|
|
|
|
|
|
|
result.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* yolo11计算图片有多少个识别到的模版
|
|
|
|
* yolo11计算图片有多少个识别到的模版
|
|
|
|
*
|
|
|
|
*
|
|
|
|
|