修改为100个点为标志

拍照位置修改
普洱算法
LAPTOP-S9HJSOEB\昊天 3 days ago
parent 8abf68e4f3
commit d404f18751

4
.gitignore vendored

@ -31,3 +31,7 @@ build/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
log/
logs/
*.onnx

@ -12,8 +12,14 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import static com.example.lxcameraapi.controller.HikController.DATE_FORMATTER;
import static com.example.lxcameraapi.controller.HikController.TIME_FORMATTER;
@RestController @RestController
@RequestMapping("/category") @RequestMapping("/category")
public class CategoryController { public class CategoryController {
@ -45,6 +51,10 @@ public class CategoryController {
@GetMapping("/getBoxCountByCategory") @GetMapping("/getBoxCountByCategory")
public int getBoxCountByCategory(String streetId,String category) throws IOException { public int getBoxCountByCategory(String streetId,String category) throws IOException {
String path =streetId+"\\"
+ LocalDate.now().format(DATE_FORMATTER)
+"\\"+category+"_"+ LocalDateTime.now().format(TIME_FORMATTER) +"_";
List<AppConfig.Camera> cameras = appConfig.getLxCamera(); List<AppConfig.Camera> cameras = appConfig.getLxCamera();
int i = 0; int i = 0;
Map<Integer, String> map = new HashMap(); Map<Integer, String> map = new HashMap();
@ -58,9 +68,9 @@ public class CategoryController {
for (AppConfig.Camera c : cameras) { for (AppConfig.Camera c : cameras) {
if (Objects.equals(c.getId(), cameraId)) { if (Objects.equals(c.getId(), cameraId)) {
String sn = UUID.randomUUID().toString(); String sn = UUID.randomUUID().toString();
LxPointCloudSaveImage.saveImage(c.getIp(), "D:\\data\\" + sn + ".pcd"); LxPointCloudSaveImage.saveImage(c.getIp(), path+ sn + ".pcd");
PcdPojo pojo = new PcdPojo(); PcdPojo pojo = new PcdPojo();
pojo.setPcd1("D:\\data\\" + sn + ".pcd"); pojo.setPcd1(path + sn + ".pcd");
pojo.setConfigPath(c.getConvertConfPath()); pojo.setConfigPath(c.getConvertConfPath());
try { try {
pojo.setPCDInfo(pojo); pojo.setPCDInfo(pojo);
@ -77,9 +87,9 @@ public class CategoryController {
for (AppConfig.Camera c : cameras) { for (AppConfig.Camera c : cameras) {
if (streetId.equals(c.getStreetId())) { if (streetId.equals(c.getStreetId())) {
String sn = UUID.randomUUID().toString(); String sn = UUID.randomUUID().toString();
LxPointCloudSaveImage.saveImage(c.getIp(), "D:\\data\\" + sn + ".pcd"); LxPointCloudSaveImage.saveImage(c.getIp(), path + sn + ".pcd");
PcdPojo pojo = new PcdPojo(); PcdPojo pojo = new PcdPojo();
pojo.setPcd1("D:\\data\\" + sn + ".pcd"); pojo.setPcd1(path + sn + ".pcd");
pojo.setConfigPath(c.getConvertConfPath()); pojo.setConfigPath(c.getConvertConfPath());
try { try {
pojo.setPCDInfo(pojo); pojo.setPCDInfo(pojo);
@ -95,4 +105,5 @@ public class CategoryController {
return i; return i;
} }
} }

@ -48,7 +48,7 @@ public class HikController {
} }
// 标定 // 标定
@GetMapping("/calibration") @GetMapping("/calibrationOpencv")
@ResponseBody @ResponseBody
public String calibration(int direction) throws IOException { public String calibration(int direction) throws IOException {
// 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());
@ -182,8 +182,8 @@ public class HikController {
return pojo; return pojo;
} }
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmssSSS"); public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmssSSS");
@Resource @Resource
ONNXServiceNew onnxServiceNew; ONNXServiceNew onnxServiceNew;
@ -221,7 +221,7 @@ public class HikController {
return pojo; return pojo;
} }
// 识别 // 识别
@GetMapping("/distinguishOnnx") @GetMapping("/distinguish")
@ResponseBody @ResponseBody
public Pojo distinguishOnnx(String streetNumber,int direction, String category) throws IOException { public Pojo distinguishOnnx(String streetNumber,int direction, String category) throws IOException {
String picPath = appConfig.getPicPath(); String picPath = appConfig.getPicPath();

@ -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
* *

@ -432,21 +432,21 @@ public class PointCloudProcessor {
public static void main(String[] args) { public static void main(String[] args) {
// LxPointCloudSaveImage.saveImage("192.168.1.82", "E:\\1.pcd", 1); // LxPointCloudSaveImage.saveImage("192.168.1.82", "E:\\1.pcd", 1);
PointCloudProcessor slicing = new PointCloudProcessor();
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());
PcdPojo pojo = new PcdPojo(); PcdPojo pojo = new PcdPojo();
pojo.setPcd1("E:\\1\\1ij.pcd"); pojo.setPcd1("D:\\xwechat_files\\wxid_ttkf0xgmyihv22_2388\\msg\\file\\2026-04\\2巷道\\F83C51127695\\15_14_46_241_camera2.pcd");
pojo.setConfigPath("E:\\工作\\淮阴\\pz6.json"); pojo.setConfigPath("D:\\xwechat_files\\wxid_ttkf0xgmyihv22_2388\\msg\\file\\2026-04\\2巷道\\F83C51127695\\cj.json");
String pz = "D:\\xwechat_files\\wxid_ttkf0xgmyihv22_2388\\msg\\file\\2026-04\\2巷道\\F83C51127695\\pz.json";
try { try {
pojo.setPCDInfo(pojo); pojo.setPCDInfo(pojo);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
try { System.out.println(slicing.slicing(pojo, slicing.getBoxPositionConf(pz)));
saveImage(pojo, "E:\\工作\\淮阴\\03bcfe74-2b4b-41e5-a9f1-09d60090f01b.png"); ;
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
/** /**
@ -647,7 +647,7 @@ public class PointCloudProcessor {
for (int j = 0; j < boxPositionConfs.get(i).getPosition().size(); j++) { for (int j = 0; j < boxPositionConfs.get(i).getPosition().size(); j++) {
int position = j + 1 + (i) * boxPositionConfs.get(i).getPosition().size(); int position = j + 1 + (i) * boxPositionConfs.get(i).getPosition().size();
if (map.getOrDefault(position, 0) > 200) { if (map.getOrDefault(position, 0) > 100) {
count += 1; count += 1;
} }
} }

Loading…
Cancel
Save