杰瑞科技汇

Python Tesseract去哪下载?

  1. 安装 Tesseract OCR 引擎本身:这是一个独立的程序,由 Google 维护,Python 只是调用它。
  2. 安装 Python 的 Tesseract 绑定库:这是一个 Python 包,它让 Python 代码能够与 Tesseract 引擎进行通信。

第 1 步:安装 Tesseract OCR 引擎

这是最关键的一步,如果你跳过它,Python 库将无法工作,你需要根据你的操作系统来选择对应的安装方法。

Python Tesseract去哪下载?-图1
(图片来源网络,侵删)

Windows

  1. 下载安装程序: 访问 Tesseract at UB Mannheim 官方页面,这是目前维护得最好的 Windows 版本下载地址: https://github.com/UB-Mannheim/tesseract/wiki

  2. 运行安装程序

    • 下载 tesseract-ocr-w64-setup-v5.3.3.20251005.exe (版本号可能更新)。
    • 管理员身份运行安装程序。
    • 重要:在安装过程中,请务必勾选 "Additional language data" 选项,否则你将无法识别任何文字,你可以选择所有语言,或者只勾选你需要的语言(如中文 Chinese_Simp)。
  3. 记住安装路径

    • 默认安装路径是 C:\Program Files\Tesseract-OCR
    • 安装程序会自动将此路径添加到系统的环境变量 Path 中,所以你通常不需要手动配置。
  4. 验证安装

    Python Tesseract去哪下载?-图2
    (图片来源网络,侵删)
    • 打开命令提示符 或 PowerShell。
    • 输入 tesseract --version,如果看到版本信息,说明安装成功。
    • 输入 tesseract --list-langs,你会看到刚才安装的语言列表。

macOS

使用 Homebrew 是最简单的方法。

  1. 安装 Tesseract: 打开终端,运行以下命令,这会安装 Tesseract 引擎以及最常用的语言数据包(包括英文)。

    brew install tesseract
  2. 安装其他语言数据: 如果你需要其他语言(如中文),需要单独安装。

    # 安装简体中文
    brew install tesseract-lang
    # 或者安装所有语言
    brew install tesseract-lang
  3. 验证安装

    Python Tesseract去哪下载?-图3
    (图片来源网络,侵删)
    tesseract --version
    tesseract --list-langs

Linux (以 Ubuntu/Debian 为例)

使用 apt 包管理器。

  1. 安装 Tesseract 和语言包: 打开终端,运行以下命令。

    # 更新包列表
    sudo apt update
    # 安装 tesseract-ocr 和英文语言包
    sudo apt install tesseract-ocr
    # 安装简体中文语言包
    sudo apt install tesseract-ocr-chi-sim
  2. 验证安装

    tesseract --version
    tesseract --list-langs

第 2 步:安装 Python 绑定库

Tesseract 引擎已经准备好了,我们安装 Python 的库来调用它,最常用的是 pytesseract

  1. 安装 pytesseract: 打开你的终端或命令提示符,运行 pip 命令。

    pip install pytesseract
  2. 安装 Pillow 库pytesseract 通常需要 Pillow 库来处理图像文件,如果你还没有安装,最好也一并安装。

    pip install Pillow

第 3 步:Python 代码示例与常见问题

安装完成后,你就可以在 Python 中使用 Tesseract 了。

基本示例

准备一张包含文字的图片,image.png

import pytesseract
from PIL import Image
# --- Tesseract 没有添加到系统环境变量 ---
# 你需要手动指定 tesseract.exe 的路径
# Windows 默认路径:
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# 打开图片文件
image_path = 'image.png' # 替换成你的图片路径
try:
    # 使用 pytesseract 进行 OCR 识别
    text = pytesseract.image_to_string(Image.open(image_path))
    # 打印识别出的文本
    print("识别结果:")
    print(text)
except FileNotFoundError:
    print(f"错误:找不到图片文件 '{image_path}',请检查路径是否正确。")
except pytesseract.pytesseract.TesseractNotFoundError:
    print("错误:找不到 Tesseract OCR 引擎。")
    print("请确保:")
    print("1. Tesseract OCR 已正确安装。")
    print("2. Tesseract 的安装目录已添加到系统的 PATH 环境变量中。")
    print("3. 如果不想配置环境变量,请在代码中指定 tesseract_cmd 的路径。")
except Exception as e:
    print(f"发生未知错误: {e}")

常见问题与解决方案

问题 1:TesseractNotFoundError 错误

  • 原因:Python 找不到 Tesseract 引擎。
  • 解决方案
    1. 检查安装:确保第 1 步的 Tesseract 引擎已经成功安装,在命令行运行 tesseract --version 验证。
    2. 检查环境变量:确保 Tesseract 的安装路径(如 C:\Program Files\Tesseract-OCR)在系统的 Path 环境变量中,重启你的 IDE 或命令行后再试。
    3. 在代码中指定路径(推荐用于快速调试):在代码开头加上下面这行,把路径改成你自己的。
      import pytesseract
      # Windows 示例
      pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
      # macOS / Linux 示例 (通常不需要,因为 brew/apt 会自动配置路径)
      # pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'

问题 2:识别不出中文,或者识别出乱码

  • 原因:Tesseract 引擎没有安装对应语言的数据包。
  • 解决方案
    1. 检查语言包:在命令行运行 tesseract --list-langs,确认列表中是否有 chi_sim (简体中文) 或 chi_tra (繁体中文)。
    2. 重新安装语言包:如果列表中没有,请返回第 1 步,根据你的操作系统重新安装对应语言包,Windows 用户需要重新运行安装程序并勾选语言。

问题 3:识别准确率不高

  • 原因:OCR 效果受多种因素影响,如图片分辨率、模糊度、背景复杂度、字体等。
  • 解决方案
    1. 预处理图片:在使用 pytesseract 之前,使用 PillowOpenCV 对图片进行预处理,通常能显著提高准确率。
      • 转为灰度图img = img.convert('L')
      • 二值化:将图片转为黑白两色,去除背景干扰。
      • 降噪:去除图片中的噪点。
      • 提高分辨率:确保图片清晰,DPI 足够高。
    2. 使用更高级的参数image_to_string 函数可以传入参数来优化识别。
      # 示例:指定语言,并使用 LSTM 引擎
      text = pytesseract.image_to_string(
          Image.open('image.png'),
          lang='chi_sim+eng', # 同时识别中文和英文
          config='--psm 6'    # PSM (Page Segmentation Mode) 设置页面分割模式
      )

      常用的 PSM 模式:

      • --psm 6: 假设统一的文本块。
      • --psm 3: 默认,全自动页面分割,但效果不一定最好。
      • --psm 1: 将图片当作单个文本行。
  1. 安装引擎:根据你的操作系统,安装 Tesseract OCR 引擎本身,务必安装所需的语言包
  2. 安装库:使用 pip install pytesseract Pillow 安装 Python 库。
  3. 验证路径:确保 Python 能找到 Tesseract 引擎(通过环境变量或代码中的 tesseract_cmd)。
  4. 编写代码:使用 pytesseract.image_to_string() 进行识别,并对图片进行预处理以提高准确率。

按照以上步骤,你应该就能成功在 Python 中使用 Tesseract OCR 了。

分享:
扫描分享到社交APP
上一篇
下一篇