Skip to content
dsh.fish
Bundle

dsh-latex-ocr

公式图片 → LaTeX(pix2tex / LaTeX-OCR):把教材/截图中的数学公式识别为 LaTeX 代码

Source
Yunnalab
License
MIT
Updated
Updated 7 days ago

Readme

# dsh-latex-ocr

**公式图片 → LaTeX**:为 [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) 提供 `latex_ocr` 工具,
把数学公式截图 / 教材公式页 / 手写公式照片识别为 LaTeX 代码(基于开源 [pix2tex / LaTeX-OCR](https://github.com/lukas-blecher/LaTeX-OCR),
本地 CPU 推理,无需联网、无需 API key)。

## 安装

### 1. 安装识别引擎(一次性)

```bash
bash scripts/setup.sh      # 创建 ~/.dsh/latex-ocr-venv,安装 torch(CPU) + pix2tex
```

或手动:

```bash
python3 -m venv ~/.dsh/latex-ocr-venv
~/.dsh/latex-ocr-venv/bin/pip install --index-url https://download.pytorch.org/whl/cpu torch
~/.dsh/latex-ocr-venv/bin/pip install pix2tex
~/.dsh/latex-ocr-venv/bin/pip install --index-url https://download.pytorch.org/whl/cpu torchvision   # 与 torch 同源,避免算子不匹配
```

首次识别会自动下载模型权重(约 120MB,HuggingFace)。

> **NixOS**:pip 安装的二进制缺少 `libstdc++.so.6` / `libz.so.1`,`setup.sh` 会自动生成
> 注入 nix store 运行库的 `pix2tex-dsh` 包装脚本;插件优先使用它。

### 2. 安装插件

```bash
# 任意 DeepSeek Harness 检出目录
dsh plugin --profile web add dsh-latex-ocr        # npm 发布后可这样装
# 或本地源码:dsh plugin --profile web add /path/to/dsh-latex-ocr
```

重启 DSH Web 进程后生效。

## 使用

```text
把这张公式识别成 LaTeX:latex_ocr(image="/path/to/formula.png")
```

返回 LaTeX 表达式,可直接用于讲解、推导与复现。本地 CPU 推理约 1-7 秒/条。

实测(温度 0.1):

| 原公式 | 识别结果 |
|---|---|
| ∫₀^∞ e^{-x²} dx = √π/2 | `\int_{0}^{\infty}e^{-x^{2}}\,d x={\frac{\sqrt{\pi}}{2}}` ✅ |
| lim_{x→0} sinx/x = 1 | `\operatorname*{lim}_{x\to0}{\frac{\sin x}{x}}=1` ✅ |

复杂公式(重积分、矩阵、求和上下限)可能不完美,关键公式请对照原图核对。

## 工作原理

`latex_ocr` 工具 → `execFile` 调用本机 pix2tex CLI(`~/.dsh/latex-ocr-venv/bin/pix2tex[-dsh]`,
找不到则回退到 PATH 上的 `pix2tex`)→ 解析 stdout 中的 LaTeX。

## 兼容性

- Node ≥ 22(DSH Web 要求)
- Python ≥ 3.10(引擎;PyTorch 需支持对应版本)
- 磁盘:约 1.5GB(torch + pix2tex + 权重)
- 平台:Linux / macOS(Windows 未验证;`scripts/setup.sh` 为 bash)

## License

MIT(pix2tex 模型与权重版权归其原作者所有)。

Install

dsh plugin --profile web add github:Yunnalab/dsh-latex-ocr

Profile: web

  • This source has no pinned commit, so a later push upstream changes what installs. Prefer pinning a commit.
Source