修复问题
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user