强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

Tesseract OCR 完整教程 / 第 3 章:基本使用

第 3 章:基本使用

掌握 Tesseract 命令行工具的核心用法。

3.1 命令行基础

3.1.1 基本语法

tesseract <input_image> <output_base> [options...]

参数说明

参数说明示例
input_image输入图片路径image.png
output_base输出文件基本名(不含扩展名)output
stdout直接输出到标准输出stdout
-l lang指定语言-l chi_sim+eng
--psm N页面分割模式--psm 6
--oem NOCR 引擎模式--oem 1

3.1.2 基础示例

# 最简单的用法 — 识别英文,输出到文件
tesseract image.png output
cat output.txt

# 直接输出到终端
tesseract image.png stdout

# 指定语言
tesseract image.png output -l chi_sim

# 多语言
tesseract image.png output -l chi_sim+eng

# 从 URL 识别(先下载再识别)
curl -s https://example.com/image.png -o temp.png
tesseract temp.png stdout
rm temp.png

3.2 页面分割模式(PSM)

PSM(Page Segmentation Mode)告诉 Tesseract 如何分析页面布局。

3.2.1 所有 PSM 模式

# 查看所有 PSM 模式
tesseract --help-psm
PSM模式名称说明适用场景
0OSD only仅检测方向和脚本预处理阶段
1Auto + OSD自动分割 + 方向检测不确定布局
2Auto自动分割,无 OSD一般场景
3Fully auto全自动(默认)通用场景
4Single column假设单列文本单栏文档
5Vertical block假设垂直文本块竖排文本
6Uniform block假设统一文本块单一区域
7Single line假设单行文本单行识别
8Single word假设单个词单词识别
9Single word (circle)圆形中的单词印章、徽标
10Single character单个字符字符识别
11Sparse text稀疏文本图片中零散文字
12Sparse text + OSD稀疏文本 + OSD不规则文本
13Raw line原始行不做预处理

3.2.2 PSM 使用示例

# 默认模式(PSM 3)
tesseract image.png output

# 单行文本
tesseract line.png output --psm 7

# 单个词
tesseract word.png output --psm 8

# 稀疏文本(如照片中的文字)
tesseract photo.png output --psm 11

# 扫描件(单列)
tesseract scan.png output --psm 4

# 表格
tesseract table.png output --psm 6

3.2.3 PSM 选择指南

你的输入是什么?
│
├── 完整文档/扫描件
│   ├── 单栏 → PSM 4
│   ├── 多栏 → PSM 3(默认)
│   └── 不确定 → PSM 1
│
├── 部分区域
│   ├── 文本块 → PSM 6
│   ├── 单行 → PSM 7
│   ├── 单词 → PSM 8
│   └── 单字符 → PSM 10
│
├── 照片/自然场景
│   ├── 稀疏文字 → PSM 11
│   └── 需要 OSD → PSM 12
│
└── 不做预处理
    └── PSM 13

3.3 OCR 引擎模式(OEM)

# 查看可用引擎模式
tesseract --help-oem
OEM模式名称说明
0Legacy only仅使用旧引擎(Tesseract 4.x)
1LSTM only仅使用 LSTM 引擎(推荐)
2Legacy + LSTM同时使用两个引擎
3Default自动选择(通常是 LSTM)
# 使用 LSTM 引擎(推荐)
tesseract image.png output --oem 1

# 使用旧引擎(Tesseract 4.x)
tesseract image.png output --oem 0

# 自动选择
tesseract image.png output --oem 3

3.4 输出格式

3.4.1 文本输出(默认)

# 输出纯文本
tesseract image.png output
cat output.txt

3.4.2 hOCR 输出(HTML + 坐标)

# 输出 hOCR 格式(含位置信息)
tesseract image.png output hocr
cat output.hocr

# hOCR 结构示例
# <div class='ocr_page' id='page_1' ...>
#   <div class='ocr_carea' id='block_1_1' ...>
#     <span class='ocr_line' id='line_1_1' ...>
#       <span class='ocrx_word' id='word_1_1' ...>Hello</span>
#       <span class='ocrx_word' id='word_1_2' ...>World</span>
#     </span>
#   </div>
# </div>

3.4.3 TSV 输出(制表符分隔)

# 输出 TSV 格式(含置信度)
tesseract image.png output tsv

TSV 格式字段:

字段说明
level层级(1=page, 2=block, 3=para, 4=line, 5=word)
page_num页码
block_num块编号
par_num段落编号
line_num行编号
word_num词编号
left左边距(像素)
top上边距(像素)
width宽度(像素)
height高度(像素)
conf置信度(-1 表示失败)
text识别文本
# 查看 TSV 输出
tesseract image.png stdout tsv | head -20

# 过滤高置信度结果
tesseract image.png stdout tsv | awk -F'\t' '$12 > 80 {print $12, $13}'

3.4.4 PDF 输出(可搜索)

# 输出可搜索 PDF
tesseract image.png output pdf

# 多页 PDF
tesseract page1.png page2.png output pdf

# PDF + 文本层
tesseract image.png output pdf --pdf

3.4.5 ALTO XML 输出

# 输出 ALTO XML 格式
tesseract image.png output alto

3.4.6 输出格式对比

格式命令扩展名包含位置信息包含置信度
文本(默认).txt
hOCRhocr.hocr
TSVtsv.tsv
PDFpdf.pdf
ALTOalto.alto

3.5 多语言识别

3.5.1 指定语言

# 单语言
tesseract image.png output -l eng
tesseract image.png output -l chi_sim

# 多语言组合(用 + 连接)
tesseract image.png output -l chi_sim+eng
tesseract image.png output -l chi_sim+chi_tra+eng

3.5.2 语言组合策略

场景推荐语言组合说明
中英文混合chi_sim+eng最常用
中英日混合chi_sim+jpn+eng需确认语言包
纯中文chi_sim简体中文
纯英文eng英文
多欧洲语言eng+deu+fra欧洲语言混合

注意事项

  • 语言越多,识别速度越慢
  • 语言组合可能降低精度
  • 排在前面的语言权重更高

3.5.3 脚本检测

# 使用 OSD 检测脚本类型
tesseract image.png stdout --psm 0

# 输出示例:
# Page number: 0
# Orientation in degrees: 0
# Rotate: 0
# Orientation confidence: 3.94
# Script: Han
# Script confidence: 2.28

3.6 图片格式支持

3.6.1 支持的格式

格式扩展名说明
PNG.png推荐格式
JPEG.jpg/.jpeg常见格式
TIFF.tif/.tiff扫描件常用
BMP.bmp位图格式
PNM.pnm/.pgm/.ppmNetpbm 格式
WebP.webpWeb 格式
# 不同格式示例
tesseract image.png stdout
tesseract photo.jpg stdout
tesseract scan.tiff stdout
tesseract raw.bmp stdout

3.6.2 多页文档

# 多页 TIFF
tesseract multipage.tiff output

# 多个图片文件
tesseract page1.png page2.png page3.png output

3.7 配置文件

3.7.1 使用配置文件

# Tesseract 配置文件位于 tessdata/configs/
ls /usr/share/tesseract-ocr/5/tessdata/configs/

# 使用配置文件
tesseract image.png output hocr

# 自定义配置文件
# 创建 myconfig
echo '--psm 6' > /usr/share/tesseract-ocr/5/tessdata/configs/myconfig
echo '-c tessedit_char_whitelist=0123456789' >> /usr/share/tesseract-ocr/5/tessdata/configs/myconfig
tesseract image.png output myconfig

3.7.2 常用配置参数

# 字符白名单(只识别指定字符)
tesseract image.png output -c tessedit_char_whitelist=0123456789

# 字符黑名单(不识别指定字符)
tesseract image.png output -c tessedit_char_blacklist=|[]

# 设置最小置信度
tesseract image.png output -c tessedit_min_confidence=50

# 调试模式
tesseract image.png output -c tessedit_write_images=true

3.8 批量处理

3.8.1 Shell 脚本批量处理

#!/bin/bash
# batch_ocr.sh - 批量处理图片

INPUT_DIR="./images"
OUTPUT_DIR="./output"
LANG="chi_sim+eng"

mkdir -p "$OUTPUT_DIR"

for img in "$INPUT_DIR"/*.{png,jpg,tiff}; do
    if [ -f "$img" ]; then
        filename=$(basename "$img" | sed 's/\.[^.]*$//')
        echo "Processing: $img"
        tesseract "$img" "$OUTPUT_DIR/$filename" -l "$LANG" --psm 6
    fi
done

echo "Done! Results in $OUTPUT_DIR/"

3.8.2 使用 xargs 并行处理

# 并行处理(4 个进程)
find ./images -name "*.png" | xargs -P 4 -I {} bash -c '
    filename=$(basename "{}" .png)
    tesseract "{}" "./output/$filename" -l chi_sim+eng
'

3.8.3 使用 GNU Parallel

# 安装 parallel
sudo apt install parallel

# 并行处理
find ./images -name "*.png" | parallel -j 4 '
    filename=$(basename {.} .png)
    tesseract {} ./output/$filename -l chi_sim+eng
'

3.9 识别精度影响因素

3.9.1 关键因素

因素影响度说明
图片分辨率⭐⭐⭐⭐⭐300 DPI 最佳
图片清晰度⭐⭐⭐⭐⭐模糊严重影响识别
对比度⭐⭐⭐⭐黑白分明效果好
文字大小⭐⭐⭐⭐字体过小识别困难
倾斜角度⭐⭐⭐需要倾斜校正
噪声⭐⭐⭐噪声需要去噪处理
字体⭐⭐标准字体效果好
语言选择⭐⭐选择正确语言

3.9.2 分辨率建议

# 检查图片分辨率
identify image.png    # ImageMagick
file image.png

# 调整分辨率到 300 DPI
convert image.png -density 300 output.png

# 放大小图
convert small.png -resize 200% large.png

推荐分辨率

文档类型推荐 DPI最小 DPI
打印文档300200
手写文档300200
普通照片-长边 1000px+
表格300300
小字体400+300

3.10 常用命令速查

# === 基础识别 ===
tesseract image.png stdout                          # 输出到终端
tesseract image.png output                          # 输出到文件
tesseract image.png output -l chi_sim+eng           # 指定语言

# === 页面分割 ===
tesseract image.png output --psm 3                  # 全自动(默认)
tesseract image.png output --psm 6                  # 统一文本块
tesseract image.png output --psm 7                  # 单行
tesseract image.png output --psm 11                 # 稀疏文本

# === 输出格式 ===
tesseract image.png output hocr                     # hOCR
tesseract image.png output tsv                      # TSV
tesseract image.png output pdf                      # 可搜索 PDF
tesseract image.png output alto                     # ALTO XML

# === 参数调优 ===
tesseract image.png output -c tessedit_char_whitelist=0123456789  # 数字白名单
tesseract image.png output -c tessedit_write_images=true         # 写出调试图像

# === 信息查询 ===
tesseract --version                                 # 版本信息
tesseract --list-langs                              # 可用语言
tesseract --help                                    # 帮助信息
tesseract --help-psm                                # PSM 说明
tesseract --help-oem                                # OEM 说明

3.11 本章小结

要点说明
基本语法tesseract input output [options]
PSM 选择根据文档布局选择合适模式
多语言-l chi_sim+eng,语言越多越慢
输出格式文本、hOCR、TSV、PDF、ALTO
精度关键分辨率 300 DPI、清晰、黑白分明
批量处理使用 Shell 脚本 + parallel

3.12 扩展阅读


上一章: 安装与配置 | 下一章: 图像预处理