仓库地址:https://github.com/sml2h3/ddddocr
安装命令:
仓库地址:https://github.com/sml2h3/ddddocr
安装命令:
pip install ddddocr
测试代码:
import ddddocr ocr = ddddocr.DdddOcr(True) for i in range(21): if i > 0: with open("yzm/"+str(i) + ".png", 'rb') as f: image = f.read() res = ocr.classification(image) print(str(i) , res)
会报错:
Microsoft Windows [版本 6.3.9600] (c) 2013 Microsoft Corporation。保留所有权利。 E:\Python\App\01_yzmOcr>python main.py Traceback (most recent call last): File "E:\Python\App\01_yzmOcr\main.py", line 1, in <module> import ddddocr File "E:\Python\python3.11.4\Lib\site-packages\ddddocr\__init__.py", line 7, i n <module> import onnxruntime File "E:\Python\python3.11.4\Lib\site-packages\onnxruntime\__init__.py", line 55, in <module> raise import_capi_exception File "E:\Python\python3.11.4\Lib\site-packages\onnxruntime\__init__.py", line 23, in <module> from onnxruntime.capi._pybind_state import ExecutionMode # noqa: F401 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\Python\python3.11.4\Lib\site-packages\onnxruntime\capi\_pybind_state. py", line 33, in <module> from .onnxruntime_pybind11_state import * # noqa ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ImportError: DLL load failed while importing onnxruntime_pybind11_state: 找不到 指定的模块。
解决方案:安装VC++2019环境 https://aka.ms/vs/16/release/VC_redist.x64.exe
然后再次报错:
Traceback (most recent call last): File "E:\Python\App\01_yzmOcr\main.py", line 10, in <module> res = ocr.classification(image) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\Python\python3.11.4\Lib\site-packages\ddddocr\__init__.py", line 466, in classification image = image.resize((int(image.size[0] * (64 / image.size[1])), 64), Image. ANTIALIAS).convert('L') ^^^^^^ ^^^^^^^^^ AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
解决办法:
方案一,修改ddddocr的_init_.py文件,将其中的ANTIALIAS替换为新方法:
# image = image.resize((int(image.size[0] * (64 / image.size[1])), 64), Image.ANTIALIAS).convert('L') image = image.resize((int(image.size[0] * (64 / image.size[1])), 64), Image.LANCZOS).convert('L')
方案二,降级Pillow的版本,比如使用9.5.0版本
先卸载,再重新安装
pip uninstall -y Pillow pip install Pillow==9.5.0 pip uninstall -y opencv-python pip install opencv-python
两种方案都亲测可用