deepjavalibrary / djl

An Engine-Agnostic Deep Learning Framework in Java
https://djl.ai
Apache License 2.0
4.08k stars 650 forks source link

无法读取jar包内的模型文件 #2813

Closed wang2713 closed 11 months ago

wang2713 commented 11 months ago

我尝试将ocr识别模型文件存放至resources文件夹内,当发布jar后直接读取jar内的模型资源文件加载模型, 但是 ai.djl.repository.RepositoryFactoryImpl.JarRepositoryFactory 类在执行时newInstance发生了空指针, 如图

image

p对象是一个Null

frankfliu commented 11 months ago

@wang2713

Can you share your url path?

wang2713 commented 11 months ago

@wang2713

Can you share your url path?

jar:file:/root/ocr/ocr-0.1.jar!/BOOT-INF/classes!/models/ch_PP-OCRv4_det_infer_onnx.zip 附件是项目源码 ocr_v4_sdk.zip

frankfliu commented 11 months ago

Can you try this url:

jar:/BOOT-INF/classes/models/ch_PP-OCRv4_det_infer_onnx.zip
wang2713 commented 11 months ago

Can you try this url:

jar:/BOOT-INF/classes/models/ch_PP-OCRv4_det_infer_onnx.zip

非常感谢您的回复。 通过这个地址是可以正常加载到模型的,您提供的这种表达jar内资源文件的方式是如何生成的,可以提供一下生成方式吗?另外我认为通过this.getClass().getClassLoader().getResource("models/ch_PP-OCRv4_det_infer_onnx.zip").toURI()来获得的url可能更通用一些,希望可以得到改进。

frankfliu commented 11 months ago

You can find the implementation here: https://github.com/deepjavalibrary/djl/blob/master/api/src/main/java/ai/djl/repository/JarRepository.java#L38

ClassLoader.getResource() only work for one specific class loader, if you have multiple .jar file, this may not work. Our implementation scan all jar in the class path.

Most user actually doesn't know how to write jar url correctly, we took simple URI syntax: "jar:" (or "jar://) + file path in the jar file

wang2713 commented 11 months ago

好的,再次感谢。