kocr/README.md

103 lines
3.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# KOCR v4 — 金蝶 K3 凭证 OCR 填表工具
## 特性
- **自动调参**: 自动分析照片亮度/阴影/模糊度,按类型(闪光灯/偏暗/模糊/正常)选择最优参数
- **双管线识别**: 优先 PPStructure TableMaster 表格识别,失败回退 DB+SVTR
- **大小写金额交叉验证**: 中文大写→数字转换,自动比对合计金额
- **交错匹配**: dr1, cr1, dr2, cr2... 完美处理金额比科目多的场景
- **多页凭证合并**: 按 (日期, 凭证号) 自动合并,借贷汇总校验
- **外置配置**: 所有参数写在 `config.yaml`,部署只需改这一个文件
## 快速开始
### 1. 安装依赖
```bash
cd kocr-v4
pip install -r requirements.txt
```
如需 GPU 加速RTX 3060+ 推荐):
```bash
pip install paddlepaddle-gpu==2.6.1 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
```
### 2. 准备模板
将金蝶 K3 导入模板 `.xls` 文件放到项目根目录,或在 `config.yaml` 中配置 `paths.template` 路径。
### 3. 放照片
将凭证照片放到 `input/` 目录。
### 4. 运行
```bash
# 批量处理所有新照片(自动调参 + 自动归档)
python3 kocr.py
# 查看状态
python3 kocr.py --status
# 批量 + 合并 + 校验报告
python3 kocr.py --merge --review
# 指定配置文件
python3 kocr.py --config my-config.yaml
# 单张处理
python3 kocr.py IMG_001.jpg --verbose
# 强制重新处理
python3 kocr.py --force
```
### 5. 输出
- `output/凭证名_K3.xls` — 每张凭证独立 Excel
- `output/合并凭证_按顺序.xls` — 多张合并(需 `--merge`
- `output/校验报告.txt` — 数据校验报告(需 `--review`
## 配置
所有可调参数都在 `config.yaml`,包括:
- **photo_analysis**: 照片类型检测阈值(亮度/模糊/阴影)
- **preprocessing**: 图像预处理参数(缩放/去噪/去阴影/锐化)
- **preprocessing.photo_type_overrides**: 按照片类型覆盖预处理参数
- **ocr**: OCR 检测/识别参数det_db_thresh, unclip_ratio 等)
- **ocr.photo_type_overrides**: 按照片类型覆盖 OCR 参数
- **postprocessing**: 校验参数(借贷平衡容差等)
- **paths**: 输入输出路径
部署时只需调整 `config.yaml` 中的参数,无需改代码。
## 文件结构
```
kocr-v4/
├── kocr.py # CLI 主入口
├── config.yaml # 外置配置中心(所有可调参数)
├── core/
│ ├── preprocessor.py # 图像预处理 + 自动调参analyze_image
│ ├── recognizer.py # OCR 识别TableMaster + DB+SVTR
│ ├── parser.py # K3 凭证解析器
│ ├── postprocessor.py # 后处理(字符纠错 + 大小写校验 + 验证)
│ └── excel_writer.py # Excel 模板填充 + 合并
├── dict/
│ └── finance_dict.txt # 自定义财务字典
├── script/
│ └── deploy.sh # 一键部署脚本
├── requirements.txt # 依赖清单
└── README.md # 本文件
```
## 版本历史
| 版本 | 日期 | 变更 |
|------|------|------|
| v4.0 | 2026-06-22 | 自动调参 + 大小写校验 + 外置配置 + Gitea 版本控制 |
| v3.x | 2026-06 | 交错匹配 + 多页合并 + 智能去重 |