python 进行 文字识别,需安装 tesserocr。该模块安装过程坑比较多,本人亲自踩坑,总结如下安装经验。
安装包下载:Tesseract、tesserocr安装包及安装说明资源-CSDN文库
1、 安装Tesseract
双击tesseract安装包,依次点击 Next,至下面界面,勾选“Additional language data(download)”,然后再依次按照默认设置进行安装(路径可以修改)。
安装完成之后,将该软件安装目录加入系统环境变量。
将安装目录下文件夹“tessdata”拷贝至所使用的python.exe 同级目录下(虚拟环境时,需在虚拟环境文件夹下python.exe 同级目录)。
2、 安装 tesserocr
直接使用pip 安装会失败。
Collecting tesserocr
Downloading tesserocr-2.6.1.tar.gz (58 kB)
---------------------------------------- 58.9/58.9 kB 771.7 kB/s eta 0:00:00
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [34 lines of output]
Failed to extract tesseract version from executable: [WinError 2] 系统找不到指定的文件。
Supporting tesseract v3.04.00
Tesseract major version 3
Traceback (most recent call last):
File "<string>", line 243, in get_build_args
File "<string>", line 107, in package_config
File "D:\SoftWare\Anaconda3\envs\RsEnvPy37\lib\subprocess.py", line 769, in __init__
restore_signals, start_new_session)
File "D:\SoftWare\Anaconda3\envs\RsEnvPy37\lib\subprocess.py", line 1172, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\SoftWare\Anaconda3\envs\RsEnvPy37\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 363, in <module>
main()
File "D:\SoftWare\Anaconda3\envs\RsEnvPy37\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 345, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "D:\SoftWare\Anaconda3\envs\RsEnvPy37\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 130, in get_requires_for_build_wheel
return hook(config_settings)
File "C:\Users\LXH\AppData\Local\Temp\pip-build-env-6pl9s1p_\overlay\Lib\site-packages\setuptools\build_meta.py", line 341, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "C:\Users\LXH\AppData\Local\Temp\pip-build-env-6pl9s1p_\overlay\Lib\site-packages\setuptools\build_meta.py", line 323, in _get_build_requires
self.run_setup()
File "C:\Users\LXH\AppData\Local\Temp\pip-build-env-6pl9s1p_\overlay\Lib\site-packages\setuptools\build_meta.py", line 488, in run_setup
self).run_setup(setup_script=setup_script)
File "C:\Users\LXH\AppData\Local\Temp\pip-build-env-6pl9s1p_\overlay\Lib\site-packages\setuptools\build_meta.py", line 338, in run_setup
exec(code, locals())
File "<string>", line 329, in <module>
File "<string>", line 260, in make_extension
File "<string>", line 252, in get_build_args
File "<string>", line 216, in get_tesseract_version
RuntimeError: Tesseract library not found in LIBPATH: []
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
使用 .whl文件进行安装,
pip install tesserocr-2.6.0-cp37-cp37m-win_amd64.whl
3、 测试
安装完成后,使用如下代码进行测试
import tesserocr
from PIL import Image
import pytesseract
image = Image.open("D:/test_ocr.png")
result = tesserocr.image_to_text(image, lang='chi_sim')
print(result)
后续公众号会发布系列教程,更多内容请关注公众号:程序猿学习日记