I’m trying to set up and test the OCR model from the doctr library provided by Mindee
(https://github.com/mindee/doctr
)
within a VSCode. I installed the package in run.ipynb
using the following command:
!pip install -qe doctr/.
I then tried to run the sample OCR code provided in the repository:
from doctr.io import DocumentFile
from doctr.models import ocr_predictor
However, I encountered the following error:
"name": "ImportError",
"message": "DLL load failed while importing onnx_cpp2py_export: A dynamic link library (DLL) initialization routine failed.",
"stack": "---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[3], line 2
1 from doctr.io import DocumentFile
----> 2 from doctr.models import ocr_predictor
4 model = ocr_predictor(pretrained=True)
5 # PDF
File c:\Users\vishw\Desktop\test\doctr\doctr\__init__.py:1
----> 1 from . import io, models, datasets, contrib, transforms, utils
2 from .file_utils import is_tf_available, is_torch_available
3 from .version import __version__ # noqa: F401
File c:\Users\vishw\Desktop\test\doctr\doctr\models\__init__.py:1
----> 1 from .classification import *
2 from .detection import *
3 from .recognition import *
File c:\Users\vishw\Desktop\test\doctr\doctr\models\classification\__init__.py:1
----> 1 from .mobilenet import *
2 from .resnet import *
3 from .vgg import *
File c:\Users\vishw\Desktop\test\doctr\doctr\models\classification\mobilenet\__init__.py:4
1 from doctr.file_utils import is_tf_available, is_torch_available
3 if is_tf_available():
----> 4 from .tensorflow import *
5 elif is_torch_available():
6 from .pytorch import *
File c:\Users\vishw\Desktop\test\doctr\doctr\models\classification\mobilenet\tensorflow.py:15
12 from tensorflow.keras import layers
13 from tensorflow.keras.models import Sequential
---> 15 from ....datasets import VOCABS
16 from ...utils import conv_sequence, load_pretrained_params
18 __all__ = [
19 "MobileNetV3",
20 "mobilenet_v3_small",
(...)
25 "mobilenet_v3_small_page_orientation",
26 ]
File c:\Users\vishw\Desktop\test\doctr\doctr\datasets\__init__.py:3
1 from doctr.file_utils import is_tf_available
----> 3 from .generator import *
4 from .cord import *
5 from .detection import *
File c:\Users\vishw\Desktop\test\doctr\doctr\datasets\generator\__init__.py:4
1 from doctr.file_utils import is_tf_available, is_torch_available
3 if is_tf_available():
----> 4 from .tensorflow import *
5 elif is_torch_available():
6 from .pytorch import * # type: ignore[assignment]
File c:\Users\vishw\Desktop\test\doctr\doctr\datasets\generator\tensorflow.py:8
1 # Copyright (C) 2021-2024, Mindee.
2
3 # This program is licensed under the Apache License 2.0.
4 # See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.
6 import tensorflow as tf
----> 8 from .base import _CharacterGenerator, _WordGenerator
10 __all__ = ["CharacterGenerator", "WordGenerator"]
13 class CharacterGenerator(_CharacterGenerator):
File c:\Users\vishw\Desktop\test\doctr\doctr\datasets\generator\base.py:14
11 from doctr.io.image import tensor_from_pil
12 from doctr.utils.fonts import get_font
---> 14 from ..datasets import AbstractDataset
17 def synthesize_text_img(
18 text: str,
19 font_size: int = 32,
(...)
22 text_color: Optional[Tuple[int, int, int]] = None,
23 ) -> Image.Image:
24 """Generate a synthetic text image
25
26 Args:
(...)
36 PIL image of the text
37 """
File c:\Users\vishw\Desktop\test\doctr\doctr\datasets\datasets\__init__.py:4
1 from doctr.file_utils import is_tf_available, is_torch_available
3 if is_tf_available():
----> 4 from .tensorflow import *
5 elif is_torch_available():
6 from .pytorch import * # type: ignore[assignment]
File c:\Users\vishw\Desktop\test\doctr\doctr\datasets\datasets\tensorflow.py:15
11 import tensorflow as tf
13 from doctr.io import read_img_as_tensor, tensor_from_numpy
---> 15 from .base import _AbstractDataset, _VisionDataset
17 __all__ = ["AbstractDataset", "VisionDataset"]
20 class AbstractDataset(_AbstractDataset):
File c:\Users\vishw\Desktop\test\doctr\doctr\datasets\datasets\base.py:16
13 from doctr.io.image import get_img_shape
14 from doctr.utils.data import download_from_url
---> 16 from ...models.utils import _copy_tensor
18 __all__ = ["_AbstractDataset", "_VisionDataset"]
21 class _AbstractDataset:
File c:\Users\vishw\Desktop\test\doctr\doctr\models\utils\__init__.py:4
1 from doctr.file_utils import is_tf_available, is_torch_available
3 if is_tf_available():
----> 4 from .tensorflow import *
5 elif is_torch_available():
6 from .pytorch import * # type: ignore[assignment]
File c:\Users\vishw\Desktop\test\doctr\doctr\models\utils\tensorflow.py:12
9 from zipfile import ZipFile
11 import tensorflow as tf
---> 12 import tf2onnx
13 from tensorflow.keras import Model, layers
15 from doctr.utils.data import download_from_url
File c:\Python312\Lib\site-packages\tf2onnx\__init__.py:8
3 """tf2onnx package."""
5 __all__ = ["utils", "graph_matcher", "graph", "graph_builder",
6 "tfonnx", "shape_inference", "schemas", "tf_utils", "tf_loader", "convert"]
----> 8 import onnx
9 from .version import git_version, version as __version__
10 from . import verbose_logging as logging
File c:\Python312\Lib\site-packages\onnx\__init__.py:77
73 from typing import IO, Literal, Union
76 from onnx import serialization
---> 77 from onnx.onnx_cpp2py_export import ONNX_ML
78 from onnx.external_data_helper import (
79 load_external_data_for_model,
80 write_external_data_tensors,
81 convert_model_to_external_data,
82 )
83 from onnx.onnx_pb import (
84 AttributeProto,
85 EXPERIMENTAL,
(...)
111 Version,
112 )
ImportError: DLL load failed while importing onnx_cpp2py_export: A dynamic link library (DLL) initialization routine failed."
Details about my environment:
- I’m working within a Jupyter notebook (run.ipynb).
- I’m testing the docTR model in my local Windows environment.
- My project structure looks like this
Im trying to run the docTR model given in the github repo link which i’ve posted and i want someone to help me fix the DLL issue related to ONNX so that i can run docTR models.
also I want to understand:
- Whether this error is due to ONNX or the OCR model setup.
- How to resolve this DLL loading issue to successfully test docTR models in my VSCode Jupyter notebook.
2
Answers
import the class of onxx implementation where onnx runtime is used before any other imports
this will prevent complect between dependencies
This is what worked for me
Overall, the main solution for me was downgrading onnx, which is represented in the line
And is the recommended solution for this error from onnx, which I found here: https://github.com/onnx/onnx/issues/6267
Note that I’m running Windows 11, Python 3.12.5