Extract models to usable location and prefer embedded models
This commit is contained in:
parent
926fa62798
commit
fb838867d8
|
|
@ -306,16 +306,34 @@ public class ResourceExtractor {
|
|||
|
||||
private void extractOcrModels() throws IOException {
|
||||
Path targetDir = Paths.get(modelsDir);
|
||||
if (!Files.exists(targetDir) || isDirEmpty(targetDir)) {
|
||||
Path altDir = Paths.get(resourceDir, "models");
|
||||
if (!targetDir.toAbsolutePath().equals(altDir.toAbsolutePath())) {
|
||||
targetDir = altDir;
|
||||
}
|
||||
}
|
||||
|
||||
if (Files.exists(targetDir.resolve("pp-ocrv5/det_model/inference.onnx"))) {
|
||||
log.info("[3/4] OCR模型已存在,跳过");
|
||||
Path[] essentialModels = {
|
||||
targetDir.resolve("pp-ocrv5/PP-OCRv5_server_det_onnx/inference.onnx"),
|
||||
targetDir.resolve("pp-ocrv5/PP-OCRv5_server_rec_onnx/inference.onnx"),
|
||||
targetDir.resolve("pp-ocrv5/PP-OCRv5_server_cls_onnx/inference.onnx")
|
||||
};
|
||||
boolean allExist = true;
|
||||
for (Path model : essentialModels) {
|
||||
if (!Files.exists(model)) {
|
||||
allExist = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allExist) {
|
||||
log.info("[3/4] OCR????????");
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("[3/4] 提取OCR模型...");
|
||||
log.info("[3/4] ??OCR??...");
|
||||
Files.createDirectories(targetDir);
|
||||
copyDirectoryFromClasspath("models", targetDir);
|
||||
log.info(" ✓ 完成");
|
||||
log.info(" ??");
|
||||
}
|
||||
|
||||
private void copyDirectoryFromClasspath(String resourcePath, Path targetDir) throws IOException {
|
||||
|
|
@ -470,6 +488,16 @@ public class ResourceExtractor {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean isDirEmpty(Path dir) throws IOException {
|
||||
if (!Files.isDirectory(dir)) {
|
||||
return true;
|
||||
}
|
||||
try (java.nio.file.DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
|
||||
return !stream.iterator().hasNext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void normalizeVenvDir(Path parentDir, Path expectedDir) throws IOException {
|
||||
if (Files.isDirectory(expectedDir)) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -171,6 +171,10 @@ public class FlaskProcessManager implements ApplicationListener<ContextRefreshed
|
|||
|
||||
// Use bundled models
|
||||
Path modelsPath = Paths.get(modelsDir).toAbsolutePath();
|
||||
if ((!Files.exists(modelsPath) || isDirEmpty(modelsPath)) && Files.exists(Paths.get(resourceDir, "models"))) {
|
||||
modelsPath = Paths.get(resourceDir, "models").toAbsolutePath();
|
||||
log.info("Using embedded models from resource dir: {}", modelsPath);
|
||||
}
|
||||
env.put("PADDLEOCR_HOME", modelsPath.toString());
|
||||
env.put("HUB_HOME", modelsPath.toString());
|
||||
if (disableModelSourceCheck) {
|
||||
|
|
@ -438,6 +442,17 @@ public class FlaskProcessManager implements ApplicationListener<ContextRefreshed
|
|||
return null;
|
||||
}
|
||||
|
||||
private boolean isDirEmpty(Path dir) {
|
||||
if (!Files.isDirectory(dir)) {
|
||||
return true;
|
||||
}
|
||||
try (java.nio.file.DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
|
||||
return !stream.iterator().hasNext();
|
||||
} catch (IOException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Path resolveEmbeddedVenvRoot() {
|
||||
Path venv = Paths.get(resourceDir, "python-runtime/venv-offline");
|
||||
if (Files.isDirectory(venv)) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue