python - Pytesseract 在 Windows 和 Linux 中的行为不同
问题描述
我正在尝试利用 Pytesseract 进行一些非常基本的字符识别。当我在 Linux 中运行以下代码时,输出是有意义的:
import matplotlib.pyplot as plt
import pandas as pd
import sys
import pytesseract
# need to add tesseract install location to path in windows.
if sys.platform == 'win32':
tesseract_path = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = tesseract_path
img = pd.read_csv('https://www.dropbox.com/s/fcs5bcmy73j75o0/two.csv?dl=1').values
fig,ax=plt.subplots()
ax.imshow(img.astype(float),cmap='gray')
print('identified as {}'.format(pytesseract.image_to_string(img.astype(float))))
但在 Windows 中同样的调用pytesseract.image_to_string
返回一个空字符串:
代码在 Python 3 环境中的两台机器上执行。
在我的 Windows 机器上安装 Tesseract 时,我可能错过了一个明显的步骤来解释这种行为吗?
Windows 中的 Tesseract 是使用以下安装程序安装的: https ://github.com/UB-Mannheim/tesseract/wiki
在 Linux 中,我简单地使用了:
yum install tesseract
解决方案
我遇到了同样的问题,结果证明如果我将 tesseract_cmd 链接设置为 Tesseract-ocr v5.0 文件夹(我从这里安装),它工作得很好。
pytesseract.pytesseract.tesseract_cmd = 'C:\\Users\\minh.nguyen\\AppData\\Local\\Tesseract-OCR\\tesseract.exe'
- 请注意,我使用 tesseract v5 而不是 v4.1,因为它有更好的结果。
推荐阅读
- java - 应用程序中的本机代码(C++ 库)是否可以访问 Android 文件系统?
- firebase - Firestore 中的两个对象相互引用
- python - 如何在我的模型中实现 MobileNetV2?
- angular - Nginx try_files for SPA (Angular app) 在子目录中
- javascript - 通过带有动态选择的 JavaScript 从表中的一行填充模态表单
- opencl - 使用 OpenCL 处理 3D 数组,以及带有无效操作数错误的程序构建错误
- django - 如何自动设置线程而不是用户必须在 django 中设置它
- c++ - 在 test_package 配方中将柯南生成的 CMake 变量添加到 LD_LIBRARY_PATH
- python - 如何使用 sql 查询填充列表
- angular - 是否可以将当前日期和时间放在 mat-form-field 中?