MonkeyOCR从理论到实践——保姆级教程

论文:https://arxiv.org/pdf/2506.05218
代码:https://github.com/Yuliang-Liu/MonkeyOCR
体验地址:http://vlrlabmonkey.xyz:7685/

一. 理论

1. 算法介绍

本文提出“结构-识别-关系”(SRR)三元组范式,实现了高效且精准的文档解析。其3B参数模型在英文文档解析任务中超越了Gemini 2.5 Pro和Qwen2.5-VL-72B。
在对多页文档解析场景下,处理速度达每秒0.84页,显著优于MinerU(0.65页/秒)和Qwen2.5-VL-7B(0.12页/秒)。
三元组范式包含三个模型:
布局检测模型(DocLayout-YOLO):检测文档中的布局块,包含:文本、表格、图标、公式等;
内容识别模型(Qwen-2.5VL文本提取):将图片中的文本、表格、公式等识别出来;
关系预测(阅读排序):排序模型。
在这里插入图片描述算法流程如上图所示:
实现利用布局检测模型对文档进行检测,然后将每个小块裁剪出来,同时交给内容识别模型和关系预测模型;
内容识别模型识别出每个裁切块的内容,例如:文字、表格、图标、公式;关系预测模型识别出每个裁切块的阅读顺序;最后将前面识别的元素有效的组织成HTML、JSON或Markdown的格式输出。

2. 算法亮点

在这里插入图片描述
轻量高效:3B参数在英文文档解析上超Gemini 2.5 Pro和Qwen2.5-VL-72B。
速度快:多页解析0.84页/秒,超MinerU(0.65页/秒)和Qwen2.5-VL-7B(0.12页/秒)。
结构化输出:覆盖10+文档类型(如论文、发票、报表),生成markdown或json结构化数据,便于下游处理。

3. 技术架构分析

整体架构设计:MonkeyOCR 采用了多阶段流水线架构,将文档解析任务分解为多个专门的处理阶段。
输入文档 → 布局检测 → 内容识别 → 关系预测 → 后处理 → 输出结果

核心组件架构:

  1. 数据层 (Data Layer)
    • Dataset 抽象基类: 统一处理 PDF 和图像数据
    • PymuDocDataset: 专门处理 PDF 文档,基于 PyMuPDF
    • ImageDataset: 处理单张图像
    • PageableData: 页面级数据抽象,支持绘制和文本插入
  2. 模型层 (Model Layer)
    • MonkeyOCR 主模型: 集成多个子模型的核心类
    • 布局检测模型: DocLayout_YOLO (基于 YOLO 架构)
    • 阅读顺序模型: LayoutReader (基于 LayoutLMv3)
    • 视觉语言模型: Qwen2.5-VL (支持 LMDeploy 和 Transformers 后端)
  3. 处理流水线 (Processing Pipeline)
    预处理阶段 (Pre-processing):
    • ocr_detect_all_bboxes.py: 检测所有边界框
    • ocr_dict_merge.py: 合并和修复检测结果
    • cut_image.py: 图像裁剪和预处理
    核心处理阶段:
    • pdf_parse_union_core_v2_llm.py: 核心解析逻辑
    • batch_analyze_llm.py: 批量分析处理
    后处理阶段 (Post-processing):
    • para_split_v3.py: 段落分割和结构化

二. 实践

1. 算法核心流程

阶段1: 布局检测与分析

# 1. 使用 DocLayout_YOLO 进行布局检测
layout_results = model.layout_model.batch_predict(images)

# 2. 检测文本、表格、公式、图像等不同类型的区域
# 支持的类别: 文本(0,1,2,4,6,7,101), 表格(5), 公式(8,14)

阶段2: 内容识别

# 3. 使用 VLM 进行内容识别
for region in layout_results:
    if region['category_id'] in [8, 14]:  # 公式
        latex_result = vlm_model.inference(region_image, formula_prompt)
    elif region['category_id'] == 5:      # 表格
        html_result = vlm_model.inference(region_image, table_prompt)
    else:                                 # 文本
        text_result = vlm_model.inference(region_image, text_prompt)

阶段3: 阅读顺序预测

# 4. 使用 LayoutReader 预测阅读顺序
reading_order = layoutreader_model.predict(region_boxes)
sorted_regions = [regions[i] for i in reading_order]

阶段4: 结构化输出

# 5. 生成结构化输出
markdown_content = convert_to_markdown(sorted_regions)
json_structure = generate_structure_info(sorted_regions)

2. 本地部署和测试

1. 环境安装
conda create -n MonkeyOCR python=3.10
conda activate MonkeyOCR

git clone https://github.com/Yuliang-Liu/MonkeyOCR.git
cd MonkeyOCR

pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu124 
pip install .
2. 下载模型(推荐ModelScope)
pip install modelscope
python download_model.py -t modelscope
3. 模型推理
# 确保在 MonkeyOCR 目录中
python parse.py path/to/your.pdf

自己拿了一篇论文测试,效果还是非常惊艳的:
在这里插入图片描述
生成JSON部分内容:

    {
        "type": "text",
        "text": "Holistically-Nested Edge Detection ",
        "text_level": 1,
        "page_idx": 0
    },
    {
        "type": "text",
        "text": "Saining Xie\nDept. of CSE and Dept. of CogSci\nUniversity of California, San Diego\n9500 Gilman Drive, La Jolla, CA 92093\ns9xie@eng.ucsd.edu ",
        "page_idx": 0
    },
    {
        "type": "text",
        "text": "Zhuowen Tu\nDept. of CogSci and Dept. of CSE\nUniversity of California, San Diego\n9500 Gilman Drive, La Jolla, CA 92093\nztu@ucsd.edu ",
        "page_idx": 0
    },
    {
        "type": "text",
        "text": "Abstract ",
        "text_level": 1,
        "page_idx": 0
    },
    {
        "type": "text",
        "text": "We develop a new edge detection algorithm that addresses two important issues in this long-standing vision problem: (1) holistic image training and prediction; and (2) multi-scale and multi-level feature learning. Our proposed method, holistically-nested edge detection (HED), performs image-to-image prediction by means of a deep learning model that leverages fully convolutional neural networks and deeply-supervised nets. HED automatically learns rich hierarchical representations (guided by deep supervision on side responses) that are important in order to resolve the challenging ambiguity in edge and object boundary detection. We significantly advance the state-of-the-art on the BSD500 dataset (ODS F-score of .782) and the NYU Depth dataset (ODS F-score of .746), and do so with an improved speed (0.4As per image) that is orders of magnitude faster than some recent CNN-based edge detection algorithms. ",
        "page_idx": 0
    },

三. 优化方向

论文中也提到,目前在标准文档上效果非常好,但是拍照场景(文档有弯曲变形或者有阴影)效果比较差。没事,我们先用文档矫正算法把图片矫正,然后进行解析就OK啦~
矫正效果:
在这里插入图片描述

感谢关注,欢迎技术交流,WeChat:guopeiAI

部署 **MonkeyOCR** 涉及多个步骤,包括环境配置、依赖安装、模型下载以及推理流程设置。以下是完整的部署步骤和环境要求: ### 环境要求 1. **操作系统**:支持 Linux 和 macOS,Windows 上可能需要额外配置。 2. **Python 版本**:建议使用 Python 3.8 或更高版本。 3. **CUDA 支持(可选)**:如果使用 GPU 进行推理,需安装 CUDA 11.x 和相应的 cuDNN 库。 4. **依赖库**: - `torch` (建议版本 1.10 以上) - `transformers` - `Pillow` - `tqdm` - `numpy` - `opencv-python` - `modelscope` ### 部署步骤 #### 1. 安装依赖 首先确保 Python 环境已正确配置,并通过 pip 安装必要的库: ```bash pip install torch transformers pillow tqdm numpy opencv-python modelscope ``` #### 2. 下载 MonkeyOCR 项目代码 克隆官方仓库或获取源码包(假设使用 GitHub 获取): ```bash git clone https://github.com/your-repo/MonkeyOCR.git cd MonkeyOCR ``` #### 3. 下载模型权重 使用 ModelScope 提供的脚本下载模型权重: ```bash python tools/download_model.py -t modelscope ``` 该命令将自动下载所需的模型权重文件并保存到指定目录。 #### 4. 配置模型路径 根据项目需求,修改 `config.yaml` 文件以指定模型权重路径和其他参数。例如: ```yaml model: recognition_model_path: "model_weight/Recognition" structure_model_path: "model_weight/Structure" ``` #### 5. 执行文档解析任务 完成上述配置后,即可运行解析脚本对 PDF 或图像进行 OCR 处理: - **处理 PDF 文件**: ```bash python parse.py path/to/your.pdf ``` - **处理图像文件**: ```bash python parse.py path/to/your/image.png ``` - **自定义输出路径与配置**: ```bash python parse.py path/to/your.pdf -o ./output -c config.yaml ``` #### 6. 日志与警告处理 在推理过程中,可能会出现如下日志信息: ``` Did not find a chat template matching model_weight/Recognition ``` 这通常是因为系统未能找到与指定模型完全匹配的聊天模板。此警告不影响 OCR 的识别效果,因为 MonkeyOCR 已经针对文档识别任务进行了专门优化[^2]。 --- ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值