Google colaboratory 中的 Tesseract 安装

Tesseract installation in Google colaboratory

我已经使用命令

在 Google colab 中安装了 tesseract
!pip install tesseract

但是当我运行命令

text = pytesseract.image_to_string(Image.open('cropped_img.png'))

我收到以下错误:

TesseractNotFoundError: tesseract is not installed or it's not in your path

您需要安装 pytesseract 而不是 tesseract

这是一个例子:

https://colab.research.google.com/drive/1zduW1Hxv7Z_pwMFGjVauhs1dTlvZByCy

添加pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/pytesseract'

这应该可以解决 TesseractNotFoundError。

在使用 pytesseract 包装器之前,您必须先安装 tesseract 引擎。您可以使用以下方法在 Google colab 上安装引擎:

!sudo apt install tesseract-ocr

您可以在以下位置找到示例:

https://github.com/labdeeman7/document-ocr/blob/master/classification%20via%20NLP%20and%20information%20extraction.ipynb

这可能有多种原因,但通常是因为您没有可用于 tesseract 的 C 库。尽管需要 pytesseract,但这只是解决方案的一半。

您基本上需要为 linux 安装 tesseract 包以及 Python 绑定。

这基本上就是解决方案:

! apt install tesseract-ocr
! apt install libtesseract-dev

以上安装了 pytesseract 所需的依赖项。这一点非常重要,尤其是 ! 没有它你无法直接安装到底层操作系统。

剩下的过程比较简单:

! pip install Pillow
! pip install pytesseract

这会安装 Python 绑定。

余下的部分相当简单,您只需 import!

import pytesseract
from PIL import ImageEnhance, ImageFilter, Image

然后你就可以让奇迹发生了。

希望这对某人有所帮助。

!sudo apt install tesseract-ocr

!pip install pytesseract

运行 在使用 tesseract 之前在您的 colab 单元中执行这两个命令。它对我有用。

起初运行单元格中的这段代码:

!pip3 install pytesseract

在 RESTART RUNTIME 之后 运行 另一个单元格中的代码:

!apt install tesseract-ocr

它对我有用。