From 0126abf313035a2ec737fa97b106c427ab4f0253 Mon Sep 17 00:00:00 2001 From: 86156 <823267011@qq.com> Date: Sat, 27 Sep 2025 20:48:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/video/common/ModelManager.java | 3 +- .../thread/detector/OpenVinoYoloDetector.java | 47 ++++++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/ruoyi-video/src/main/java/com/ruoyi/video/common/ModelManager.java b/ruoyi-video/src/main/java/com/ruoyi/video/common/ModelManager.java index 3a2443d..a9cc59a 100644 --- a/ruoyi-video/src/main/java/com/ruoyi/video/common/ModelManager.java +++ b/ruoyi-video/src/main/java/com/ruoyi/video/common/ModelManager.java @@ -32,7 +32,8 @@ public final class ModelManager implements AutoCloseable { int rgb = palette[i % palette.length]; i++; int bgr = ((rgb & 0xFF) << 16) | (rgb & 0xFF00) | ((rgb >> 16) & 0xFF); - YoloDetector det = new OpenCvYoloDetector(name, dir, w, h, backend, bgr); + // 使用OpenVinoYoloDetector,但强制使用OpenCV后端 + YoloDetector det = new OpenVinoYoloDetector(name, dir, w, h, "opencv", bgr); map.put(name, det); } } diff --git a/ruoyi-video/src/main/java/com/ruoyi/video/thread/detector/OpenVinoYoloDetector.java b/ruoyi-video/src/main/java/com/ruoyi/video/thread/detector/OpenVinoYoloDetector.java index 9f628e0..855e61b 100644 --- a/ruoyi-video/src/main/java/com/ruoyi/video/thread/detector/OpenVinoYoloDetector.java +++ b/ruoyi-video/src/main/java/com/ruoyi/video/thread/detector/OpenVinoYoloDetector.java @@ -25,9 +25,15 @@ public final class OpenVinoYoloDetector implements YoloDetector { this.input = new Size(inW, inH); this.colorBGR = colorBGR; - String xml = dir.resolve("model.xml").toString(); - String bin = dir.resolve("model.bin").toString(); + // 自动查找模型文件 + String xml = findModelFile(dir, ".xml"); + String bin = findModelFile(dir, ".bin"); + if (xml == null || bin == null) { + throw new Exception("找不到模型文件,请确保目录中存在 .xml 和 .bin 文件: " + dir); + } + + // 读取类别文件 Path clsPath = dir.resolve("classes.txt"); if (Files.exists(clsPath)) { this.classes = Files.readAllLines(clsPath).stream().map(String::trim) @@ -36,19 +42,34 @@ public final class OpenVinoYoloDetector implements YoloDetector { this.classes = new String[0]; } - this.net = readNetFromModelOptimizer(xml, bin); - - boolean set = false; - if ("openvino".equalsIgnoreCase(backend)) { - try { - net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE); - net.setPreferableTarget(DNN_TARGET_CPU); - set = true; - } catch (Throwable ignore) { /* 回退 */ } - } - if (!set) { + try { + // 加载模型,但强制使用OpenCV后端 + this.net = readNetFromModelOptimizer(xml, bin); + + // 强制使用OpenCV后端,避免OpenVINO依赖 net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableTarget(DNN_TARGET_CPU); + + System.out.println("模型加载成功: " + name + " (使用OpenCV后端)"); + + } catch (Exception e) { + throw new Exception("模型加载失败: " + e.getMessage() + + "\n请确保模型文件完整且格式正确", e); + } + } + + /** + * 在目录中查找指定扩展名的模型文件 + */ + private String findModelFile(Path dir, String extension) { + try { + return Files.list(dir) + .filter(path -> path.toString().toLowerCase().endsWith(extension.toLowerCase())) + .map(Path::toString) + .findFirst() + .orElse(null); + } catch (Exception e) { + return null; } }