Tesseract OCR 安装与使用说明(macOS / Debian / 麒麟 Linux)
一、简介
Tesseract OCR 是一个开源的文字识别(OCR,Optical Character Recognition)引擎,可将图片中的文字识别为可编辑文本。
常见应用场景:
图片文字识别
扫描件识别
PDF 文字提取
检测报告识别
发票、表单识别
PHP、Java、Python 等程序调用 OCR
二、macOS 安装
1、安装 Homebrew(已安装可跳过)
|
1 |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
2、安装 Tesseract
|
1 |
brew install tesseract |
3、查看版本
|
1 |
tesseract --version |
4、查看支持语言
|
1 |
tesseract --list-langs |
输出示例:
|
1 2 3 4 |
List of available languages (3): eng chi_sim osd |
三、Debian 安装
更新软件源:
|
1 |
apt update |
安装 OCR 引擎:
|
1 |
apt install tesseract-ocr |
安装简体中文语言包:
|
1 |
apt install tesseract-ocr-chi-sim |
查看版本:
|
1 |
tesseract --version |
查看语言:
|
1 |
tesseract --list-langs |
四、麒麟 Linux(YUM)安装
安装 OCR 引擎:
|
1 |
yum install tesseract |
安装简体中文语言包:
|
1 |
yum install tesseract-langpack-chi_sim |
部分麒麟版本的软件包名称可能有所不同,例如:
|
1 2 |
yum install tesseract-ocr yum install tesseract-ocr-chi-sim |
如提示找不到软件包,查看系统中的实际软件包名称,可执行:
|
1 |
yum search tesseract |
查看版本:
|
1 |
tesseract --version |
查看语言:
|
1 |
tesseract --list-langs |
五、常用语言包
Tesseract 支持 100 多种语言,可根据业务需要安装对应语言包。
Debian
安装指定语言包:
|
1 |
apt install -y tesseract-ocr-<语言包名称> |
例如:
|
1 2 3 4 5 6 7 8 9 |
apt install tesseract-ocr-eng apt install tesseract-ocr-chi-sim apt install tesseract-ocr-chi-tra apt install tesseract-ocr-jpn apt install tesseract-ocr-kor apt install tesseract-ocr-fra apt install tesseract-ocr-deu apt install tesseract-ocr-spa apt install tesseract-ocr-rus |
麒麟 Linux(YUM)
安装指定语言包:
|
1 |
yum install -y tesseract-langpack-<语言包名称> |
例如:
|
1 2 3 4 5 6 7 8 9 |
yum install tesseract-langpack-eng yum install tesseract-langpack-chi_sim yum install tesseract-langpack-chi_tra yum install tesseract-langpack-jpn yum install tesseract-langpack-kor yum install tesseract-langpack-fra yum install tesseract-langpack-deu yum install tesseract-langpack-spa yum install tesseract-langpack-rus |
部分麒麟发行版的软件包名称可能有所不同,可执行:
|
1 |
yum search tesseract |
查询系统中实际的软件包名称。
macOS
Homebrew 安装完成后,大多数情况下已包含常用语言数据。
查看支持语言:
|
1 |
tesseract --list-langs |
如果缺少某种语言,可下载对应的 *.traineddata 文件并放入 tessdata 目录。
查看已安装语言:
|
1 |
tesseract --list-langs |
输出示例:
|
1 2 3 4 5 6 7 |
List of available languages (6): eng chi_sim chi_tra jpn kor osd |
说明:
eng:英文
chi_sim:简体中文
chi_tra:繁体中文
jpn:日文
kor:韩文
osd:页面方向自动检测
常用语言包对照
| 语言 | 语言代码 | Debian | 麒麟(YUM) |
| 英文 | eng | tesseract-ocr-eng | tesseract-langpack-eng |
| 简体中文 | chi_sim | tesseract-ocr-chi-sim | tesseract-langpack-chi_sim |
| 繁体中文 | chi_tra | tesseract-ocr-chi-tra | tesseract-langpack-chi_tra |
| 日文 | jpn | tesseract-ocr-jpn | tesseract-langpack-jpn |
| 韩文 | kor | tesseract-ocr-kor | tesseract-langpack-kor |
| 法文 | fra | tesseract-ocr-fra | tesseract-langpack-fra |
| 德文 | deu | tesseract-ocr-deu | tesseract-langpack-deu |
| 西班牙文 | spa | tesseract-ocr-spa | tesseract-langpack-spa |
| 葡萄牙文 | por | tesseract-ocr-por | tesseract-langpack-por |
| 意大利文 | ita | tesseract-ocr-ita | tesseract-langpack-ita |
| 俄文 | rus | tesseract-ocr-rus | tesseract-langpack-rus |
| 阿拉伯文 | ara | tesseract-ocr-ara | tesseract-langpack-ara |
六、OCR 使用方法
1、识别英文图片
|
1 |
tesseract test.png output |
生成:
|
1 |
output.txt |
2、识别简体中文
|
1 |
tesseract chinese.png output -l chi_sim |
3、直接输出到终端
英文:
|
1 |
tesseract test.png stdout |
中文:
|
1 |
tesseract chinese.png stdout -l chi_sim |
4、指定多个语言
中英文:
|
1 |
tesseract image.png stdout -l chi_sim+eng |
中英日:
|
1 |
tesseract image.png stdout -l chi_sim+eng+jpn |
七、PHP 调用示例
使用 shell_exec():
|
1 2 |
$text = shell_exec("tesseract image.png stdout -l chi_sim"); echo $text; |
使用 exec():
|
1 2 |
exec("tesseract image.png stdout -l chi_sim", $output); echo implode("\n", $output); |
八、常用命令
查看帮助:
|
1 |
tesseract --help |
查看版本:
|
1 |
tesseract --version |
查看支持语言:
|
1 |
tesseract --list-langs |
查看安装位置:
|
1 |
which tesseract |
九、常见问题
1、提示:
|
1 |
tesseract: command not found |
执行:
|
1 |
which tesseract |
2、提示:
|
1 |
Error opening data file chi_sim.traineddata |
表示未安装中文语言包。
Debian:
|
1 |
apt install tesseract-ocr-chi-sim |
麒麟:
|
1 |
yum install tesseract-langpack-chi_sim |
3、查看语言包是否安装成功
|
1 |
tesseract --list-langs |
若输出包含:
|
1 |
chi_sim |
说明中文语言包安装成功。
十、总结
| 操作系统 | 安装命令 |
| macOS | brew install tesseract |
| Debian | apt install tesseract-ocr tesseract-ocr-chi-sim |
| 麒麟(YUM) | yum install tesseract tesseract-langpack-chi_sim |
安装完成后建议执行:
|
1 2 |
tesseract --version tesseract --list-langs |
确认版本正常、语言列表中包含 chi_sim 后,即可开始进行中文 OCR 识别。