---
name: php186-skill-70475
description: "专门用于高棉语（柬埔寨语）PDF 扫描件的 OCR 识别，将高棉语 PDF 转换为可编辑文本。支持单页对照输出，优先提取高棉语，兼容英柬双语混排文档。"
---

# khmer-pdf-ocr

## khmer-ocr — 高棉语 PDF OCR 识别

 

> **适用场景**：高棉语（柬埔寨语）PDF 扫描件 → 可编辑文本
> 
>  
> 
> **不适用**：文字层 PDF（用 ` lit parse --no-ocr ` 更高效）、中文/英文单语言扫描件（用 pdf-ocr-md）

 

## 环境依赖

 

已在当前环境安装，无需额外配置：

 

| 依赖 | 状态 | 安装命令 |
| --- | --- | --- |
| ` tesseract-ocr-khm ` | ✅ 已装 | ` apt install tesseract-ocr-khm ` |
| ` tesseract-ocr-eng ` | ✅ 已装 | ` apt install tesseract-ocr-eng ` |
| ` poppler-utils ` | ✅ 已装 | ` apt install poppler-utils ` |
| ` python3-pdf2image ` | ✅ 已装 | ` pip install pdf2image ` |
| ` pytesseract ` | ✅ 已装 | ` pip install pytesseract ` |
| ` Pillow ` | ✅ 已装 | ` pip install Pillow ` |

 

## 工作流程

 

```
输入 PDF (高棉语扫描件)
    │
    ▼
① PDF → PNG 图片（DPI 300）
    │
    ▼
② Tesseract OCR（语言：khm + eng 混合）
    │
    ▼
③ 按页输出：khmer_text.txt（高棉语原文）
```

 

## 核心脚本

 

### 主脚本：` scripts/khmer_ocr.py `

 

```
python3 scripts/khmer_ocr.py <input.pdf> [output_dir] [--dpi 300] [--lang khm+eng]
```

 

**参数说明：**

 

- ` <input.pdf> ` — 输入 PDF 路径（必填）
- ` output_dir ` — 输出目录（默认：./khmer\_ocr\_output/）
- ` --dpi 300 ` — 图片转 DPI，默认 300
- ` --lang khm+eng ` — OCR 语言，默认 ` khm+eng `（高棉语+英语双语）

 

**输出文件（按页）：**

 

- ` output_dir/page_01_khmer.txt ` — 第1页高棉语原文
- ` output_dir/page_02_khmer.txt ` — 第2页高棉语原文
- ...
- ` output_dir/full_text.txt ` — 合并全文

 

## 使用示例

 

### 基础用法

 

```
# 识别单个 PDF
python3 scripts/khmer_ocr.py /path/to/cambodian_document.pdf

# 指定输出目录
python3 scripts/khmer_ocr.py /path/to/cambodian_document.pdf --dpi 300 -o /path/to/output/

# 指定语言（高棉语+英语双语）
python3 scripts/khmer_ocr.py /path/to/document.pdf --lang khm+eng
```

 

### 管道用法（结合后续翻译）

 

```
# 识别后直接 cat 查看
python3 scripts/khmer_ocr.py document.pdf -o /tmp/ocr_result/ && cat /tmp/ocr_result/full_text.txt
```

 

## 质量说明

 

| 因素 | 影响 | 建议 |
| --- | --- | --- |
| 扫描 DPI | DPI越高识别越准 | 建议 ≥ 200 DPI |
| 页面噪点 | 噪点严重时准确率下降 | 可先做去噪预处理 |
| 双语混排 | 柬英混合时适当 | 使用 ` khm+eng ` 模式 |
| 手写体 | 手写文档不适用 | 本工具仅适用于印刷体 |

 

**实测准确率**：约 70-80%（柬埔寨公司章程扫描件），对正式印刷体效果较好。

 

**关键经验教训**

 

⚠️ **先验证，后结论**：收到高棉语 PDF 时不要凭预判下结论。正确流程：

 

1. 先用 ` lit parse --no-ocr ` 确认有无文字层
2. 若为空，再实际跑 tesseract khm 输出结果验证
3. 不要仅凭 ` apt-cache search ` 判断语言包是否存在——khm 语言包在标准 apt 源中可用，但 ` apt-cache search khm ` 搜不到（关键词差异），直接 ` apt-cache search tesseract-ocr ` 可见

 

⚠️ **apt-cache 搜索盲区**：搜索 Khmer 相关包时，用 ` apt-cache search tesseract-ocr | grep -i kh ` 或直接 ` apt-cache search khm `（包名就是 khm），而不要搜 ` khmer `（关键词是 khm）

 

**故障排除**

 

**Q: ` apt-cache search khm ` 或 ` apt-cache search khmer ` 找不到包**

 

```
A: 语言包名就是 khm，直接安装：
   apt install tesseract-ocr-khm
   # 不要搜 "khmer"，关键词是 "khm"
```

 

**Q: tesseract 报错 "Khmer language is not installed"**

 

```
A: 运行以下命令安装：
   apt install tesseract-ocr-khm
```

 

**Q: 识别结果全为空**

 

```
A: 检查是否为真正的扫描件（无文字层）：
   python3 -c "import fitz; doc=fitz.open('file.pdf'); print(any(page.get_text() for page in doc))"
   如果返回 True，说明有文字层，应该用 lit parse 而不是本工具。
```

 

**Q: 识别率低，有大量乱码字符**

 

```
A: 页面噪点过多，可先预处理去噪：
   convert -density 150 input.pdf -colorspace Gray -blur 0x1 output.png
   tesseract output.png result -l khm
```

 

**Q: 第1页噪点极重无法识别**

 

```
A: 这是扫描件封面常见问题，后续页面通常清晰。处理时：
   1. 分别处理各页，不因第1页失败放弃全稿
   2. DPI 提到 400 可能略微改善
   3. 封面内容（公司名、注册资本等）在后续页面的正文中通常会重复出现
```

 

## 技术说明

 

- **OCR 引擎**：Tesseract 5.x（Leptonica 后端）
- **语言包**：tesseract-ocr-khm（Tessdata 格式，~3MB）
- **图片转换**：pdf2image（Poppler 渲染）
- **输出编码**：UTF-8 Unicode

 

> 📋 完整工作流记录（含 PDF→图片→OCR→HTML 对照输出全流程）：见 ` references/workflow.md `
