Commit cebcd2ad authored by myhloli's avatar myhloli
Browse files

refactor(ocr): improve ONNX model initialization and resource handling

- Replace deprecated importlib.resources.path with importlib.resources.files
- Simplify code structure and improve readability
- Remove unnecessary comments and empty lines
parent 6a3cdb8d
...@@ -7,7 +7,7 @@ import base64 ...@@ -7,7 +7,7 @@ import base64
from magic_pdf.libs.boxbase import __is_overlaps_y_exceeds_threshold from magic_pdf.libs.boxbase import __is_overlaps_y_exceeds_threshold
from magic_pdf.pre_proc.ocr_dict_merge import merge_spans_to_line from magic_pdf.pre_proc.ocr_dict_merge import merge_spans_to_line
import importlib.resources from importlib.resources import files
from paddleocr import PaddleOCR from paddleocr import PaddleOCR
from ppocr.utils.utility import check_and_read from ppocr.utils.utility import check_and_read
...@@ -328,13 +328,14 @@ class ONNXModelSingleton: ...@@ -328,13 +328,14 @@ class ONNXModelSingleton:
self._models[key] = onnx_model_init(key) self._models[key] = onnx_model_init(key)
return self._models[key] return self._models[key]
def onnx_model_init(key): def onnx_model_init(key):
if len(key) < 4: if len(key) < 4:
logger.error('Invalid key length, expected at least 4 elements') logger.error('Invalid key length, expected at least 4 elements')
exit(1) exit(1)
try: try:
with importlib.resources.path('rapidocr_onnxruntime.models', '') as resource_path: resource_path = files("rapidocr_onnxruntime") / "models"
additional_ocr_params = { additional_ocr_params = {
"use_onnx": True, "use_onnx": True,
"det_model_dir": f'{resource_path}/ch_PP-OCRv4_det_infer.onnx', "det_model_dir": f'{resource_path}/ch_PP-OCRv4_det_infer.onnx',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment