Files
rocr/README.md
T
ssw aa819b10c8 rocr: 离线中文 OCR CLI (RustO!/MNN, PP-OCRv6-Medium) 可分发包
- bin/rocr: 位置无关封装脚本, 带 daemon 常驻管理 (空闲 600s 自退)
- bin/rusto-rs: macOS x86_64 二进制 (含 --serve 常驻模式, 大输出不截断修复)
- install/uninstall 脚本 (含 macOS 双击 .command)
- models/ 与 *.zip 体积大, 由 .gitignore 排除, 需单独获取
2026-09-11 18:07:56 +08:00

107 lines
5.4 KiB
Markdown
Raw 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.
# rocr — 离线中文 OCR 工具(RustO! / rusto-rs 封装,常驻加速版)
纯 Rust OCR 引擎(RapidOCR + PaddleOCR 模型 + 阿里 MNN 推理后端,**零 OpenCV**),
内置 **PP-OCRv6-Medium(约 94MB 高精度模型)**。纯本地推理,无需联网 / API key / 不耗 token。
官方引擎:https://github.com/byrizki/rusto-rs (v0.2.5,本包在原版基础上新增 `--serve` 常驻模式)
## 安装(分发给别人也只需这两步)
1. 解压 `rocr-macos-x86_64.zip`,得到 `rocr/` 目录。
2. 运行安装脚本:
- **macOS**:双击 `install.command`(首次若被拦截,右键 → 打开;或在终端 `bash install.sh`)。
- **Linux / 终端**:`bash install.sh`
- 安装脚本会把 `rocr/` 复制到 `~/rocr`,并在 `/usr/local/bin` 建立 `rocr` 软链。
卸载:`bash uninstall.sh`(或双击 `uninstall.command`)。
> 内置二进制为 **macOS x86_64**。Apple Silicon / Linux x86_64 需自行编译:在 rusto-rs 源码目录 `cargo build --release`,把产物 `target/release/rusto-rs` 覆盖到 `~/rocr/bin/rusto-rs` 即可。
## 使用
```bash
rocr # 无参数 → 显示帮助与用法
rocr scan.jpg # 文本输出(默认)
rocr scan.jpg markdown # Markdown 输出(直接进 文档→Markdown 流水线)
rocr scan.png json # JSON(含坐标框 + 置信度)
```
输出格式:`text`(默认) | `json` | `tsv` | `markdown` | `textordered`。
## 常驻加速(daemon)
首次调用自动后台拉起 daemon:加载模型(一次,~2s)→ 监听 Unix socket(`~/.cache/rocr/rocr.sock`)。
之后每次调用走 socket,省去重复加载;**空闲 600 秒(10 分钟)无请求自动退出释放内存**,下次调用再自动拉起。
性能实测(Intel Mac,无 SIMD 加速):
| 模式 | 单张耗时 | 说明 |
|------|---------|------|
| 一次性(旧版) | ~12s | 每次加载模型 + 推理 |
| 常驻 daemon 热态 | **~9.5s** | 省掉加载,剩余为纯推理 |
| 首次(拉起 daemon) | ~13s | 加载 + 首次推理 |
> ⚠️ 瓶颈说明:本机 MNN 设备探测 `i8sdot:0, fp16:0, i8mm:0`(无 SIMD 加速路径),推理走慢速标量内核,
> rec 模型按检测行数逐行推理,**每张 ~9s 是推理本身的成本**。要数量级提速需换有 NEON/fp16 的机器(如 Apple Silicon),
> 或在批量场景换小模型档(官方 release 另有 PPOCRv6-Tiny ~5.5MB,精度略降但快数倍)。
> 耗时随文本行数近似线性增长:约 1–2s/行,一页 40 行的图约 50s。
## 输出完整性(大 JSON / TSV 不截断)
- daemon 传输已修复:早期版本大响应会在 **8192 字节**(macOS unix socket 默认发送缓冲 8KB)处被截断,
现强制阻塞写 + 客户端读到对端关闭,**任意大小输出完整**(已用 40 行图验证:19KB JSON 完整、合法)。
- `json` / `tsv` 输出同样完整;`json` 可被 `json.load` 直接解析。
- 已过滤 libMNN 直接打到 stdout 的设备探测行(`The device supports: ...`),one-shot 路径也不污染 payload。
## 模型文件与打包产物(不在本仓库内)
本仓库**只含脚本与二进制**,以下内容被 `.gitignore` 排除,需单独获取/生成:
- `models/`(`det.mnn` ~59MB + `rec.mnn` ~73MB + `dict.txt`)——模型权重,体积大,不进 Git。
获取方式:从官方 release 下载 `RustO-Models-PPOCRv6-Medium.zip` 并解压到 `models/PPOCRv6-Medium/`:
```bash
mkdir -p models/PPOCRv6-Medium
curl -L -o /tmp/m.zip https://github.com/byrizki/rusto-rs/releases/download/v0.2.5/RustO-Models-PPOCRv6-Medium.zip
unzip -o /tmp/m.zip -d models/PPOCRv6-Medium
```
- `rocr-*.zip` 分发包——由本仓库内容 + 模型打包生成,不进 Git。
> 因此 **直接 clone 本仓库得到的是「缺模型」的骨架**:`bin/` 与脚本齐备,补上 `models/PPOCRv6-Medium/` 即可运行;
> 或直接使用完整分发包 `rocr-macos-x86_64.zip`。
## 目录结构(安装后位于 ~/rocr)
```
~/rocr/
├── bin/
│ ├── rocr # 封装脚本(位置无关,可被软链,带 daemon 管理)
│ └── rusto-rs # OCR 二进制(含 --serve 常驻模式)
├── models/
│ └── PPOCRv6-Medium/
│ ├── det.mnn # 检测模型
│ ├── rec.mnn # 识别模型
│ └── dict.txt # 中文字典
├── install.sh / install.command
├── uninstall.sh / uninstall.command
└── README.md
```
## 高级:环境变量
```bash
export ROCR_IDLE=300 # 空闲自退时间(秒),默认 600(10 分钟)
export ROCR_NO_DAEMON=1 # 禁用常驻,每次一次性加载(省内存,慢)
export ROCR_SOCK_DIR=/tmp # socket/pid/日志目录,默认 ~/.cache/rocr
export RUSTO_BIN=/path/to/rusto-rs # 重定向二进制
export RUSTO_MODEL_DIR=/path/to/models # 重定向模型目录
export ROCR_HOME=/opt/rocr # 改变安装目标目录(install.sh 用)
```
daemon 日志:`~/.cache/rocr/daemon.log`。
## 在“文档→Markdown”流水线中的定位
扫描件 PDF / 图片缺 OCR 时,用 rocr 离线补齐:**扫描件 → 转图 → `rocr xxx.png markdown` → 回填 Markdown**。
批量场景 daemon 常驻只加载一次模型;模型选型:仅内置高精度档 PP-OCRv6-Medium——干净文本与各小模型持平,扫描件/小字/倾斜/噪点场景明显更稳。