首页 > 解决方案 > 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))))

linux截图

但在 Windows 中同样的调用pytesseract.image_to_string返回一个空字符串:

窗口截图

代码在 Python 3 环境中的两台机器上执行。

在我的 Windows 机器上安装 Tesseract 时,我可能错过了一个明显的步骤来解释这种行为吗?

Windows 中的 Tesseract 是使用以下安装程序安装的: https ://github.com/UB-Mannheim/tesseract/wiki

在 Linux 中,我简单地使用了: yum install tesseract

标签: pythonpython-3.xocrtesseractpython-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,因为它有更好的结果。

推荐阅读