Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wangsen
MinerU
Commits
58d28c96
Unverified
Commit
58d28c96
authored
Jan 20, 2025
by
Xiaomeng Zhao
Committed by
GitHub
Jan 20, 2025
Browse files
Merge pull request #1582 from myhloli/dev
fix(ocr): improve ONNX model initialization and error handling
parents
f473028b
b3d60b96
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
26 deletions
+33
-26
magic_pdf/model/sub_modules/ocr/paddleocr/ocr_utils.py
magic_pdf/model/sub_modules/ocr/paddleocr/ocr_utils.py
+33
-26
No files found.
magic_pdf/model/sub_modules/ocr/paddleocr/ocr_utils.py
View file @
58d28c96
...
@@ -7,6 +7,8 @@ import base64
...
@@ -7,6 +7,8 @@ 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
paddleocr
import
PaddleOCR
from
ppocr.utils.utility
import
check_and_read
from
ppocr.utils.utility
import
check_and_read
...
@@ -327,11 +329,12 @@ class ONNXModelSingleton:
...
@@ -327,11 +329,12 @@ class ONNXModelSingleton:
return
self
.
_models
[
key
]
return
self
.
_models
[
key
]
def
onnx_model_init
(
key
):
def
onnx_model_init
(
key
):
if
len
(
key
)
<
4
:
logger
.
error
(
'Invalid key length, expected at least 4 elements'
)
exit
(
1
)
import
importlib.resources
try
:
with
importlib
.
resources
.
path
(
'rapidocr_onnxruntime.models'
,
''
)
as
resource_path
:
with
importlib
.
resources
.
path
(
'rapidocr_onnxruntime.models'
,
''
)
as
resource_path
:
onnx_model
=
None
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'
,
...
@@ -341,12 +344,12 @@ def onnx_model_init(key):
...
@@ -341,12 +344,12 @@ def onnx_model_init(key):
"use_dilation"
:
key
[
2
],
"use_dilation"
:
key
[
2
],
"det_db_unclip_ratio"
:
key
[
3
],
"det_db_unclip_ratio"
:
key
[
3
],
}
}
# logger.info(f"additional_ocr_params: {additional_ocr_params}")
if
key
[
0
]
is
not
None
:
if
key
[
0
]
is
not
None
:
additional_ocr_params
[
"lang"
]
=
key
[
0
]
additional_ocr_params
[
"lang"
]
=
key
[
0
]
from
paddleocr
import
PaddleOCR
# logger.info(f"additional_ocr_params: {additional_ocr_params}")
onnx_model
=
PaddleOCR
(
**
additional_ocr_params
)
onnx_model
=
PaddleOCR
(
**
additional_ocr_params
)
if
onnx_model
is
None
:
if
onnx_model
is
None
:
...
@@ -354,3 +357,7 @@ def onnx_model_init(key):
...
@@ -354,3 +357,7 @@ def onnx_model_init(key):
exit
(
1
)
exit
(
1
)
else
:
else
:
return
onnx_model
return
onnx_model
except
Exception
as
e
:
logger
.
exception
(
f
'Error initializing model:
{
e
}
'
)
exit
(
1
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment