PDF内容提取,MinerU使用

准备环境

bash 复制代码
# python 3.10
python3 -m pip install huggingface_hub
python3 -m pip install modelscope
python3 -m pip install -U magic-pdf[full] --extra-index-url https://wheels.myhloli.com

下载需要的模型

python 复制代码
import json
import os

import requests
from huggingface_hub import snapshot_download
# from modelscope import snapshot_download  # 下载'opendatalab/PDF-Extract-Kit-1.0' 快


def download_json(url):
    # 下载JSON文件
    response = requests.get(url)
    response.raise_for_status()  # 检查请求是否成功
    return response.json()


def download_and_modify_json(url, local_filename, modifications):
    if os.path.exists(local_filename):
        data = json.load(open(local_filename))
        config_version = data.get('config_version', '0.0.0')
        if config_version < '1.0.0':
            data = download_json(url)
    else:
        data = download_json(url)

    # 修改内容
    for key, value in modifications.items():
        data[key] = value

    # 保存修改后的内容
    with open(local_filename, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)


if __name__ == '__main__':

    mineru_patterns = [
        "models/Layout/LayoutLMv3/*",
        "models/Layout/YOLO/*",
        "models/MFD/YOLO/*",
        "models/MFR/unimernet_small/*",
        "models/TabRec/TableMaster/*",
        "models/TabRec/StructEqTable/*",
    ]
    model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', allow_patterns=mineru_patterns)

    layoutreader_pattern = [
        "*.json",
        "*.safetensors",
    ]
    layoutreader_model_dir = snapshot_download('hantian/layoutreader', allow_patterns=layoutreader_pattern)

    model_dir = model_dir + '/models'
    print(f'model_dir is: {model_dir}')
    print(f'layoutreader_model_dir is: {layoutreader_model_dir}')

    json_url = 'https://github.com/opendatalab/MinerU/raw/master/magic-pdf.template.json'
    config_file_name = 'magic-pdf.json'
    home_dir = os.path.expanduser('~')
    config_file = os.path.join(home_dir, config_file_name)

    json_mods = {
        'models-dir': model_dir,
        'layoutreader-model-dir': layoutreader_model_dir,
    }

    download_and_modify_json(json_url, config_file, json_mods)
    print(f'The configuration file has been configured successfully, the path is: {config_file}')

测试

bash 复制代码
wget https://github.com/opendatalab/MinerU/raw/master/demo/small_ocr.pdf -O small_ocr.pdf
magic-pdf -p small_ocr.pdf -o ./output

GPU加速

bash 复制代码
vim yourpath/magic-pdf.json
"device-mode": "cpu" -> "device-mode": "cuda",
相关推荐
Tiandaren16 小时前
大模型应用03 || 函数调用 Function Calling || 概念、思想、流程
人工智能·算法·microsoft·数据分析
人大博士的交易之路1 天前
龙虎榜——20251106
大数据·数学建模·数据分析·缠论·缠中说禅·龙虎榜
YangYang9YangYan1 天前
中专服装设计专业职业发展指南
大数据·人工智能·数据分析
喝凉白开都长肉的大胖子1 天前
比较 main.tex 的两个不同版本(例如旧版和新版),并生成一个带有修改标记(如删除线、高亮、修订注释)的 PDF 或文本输出。
pdf·latex
weixin_441003641 天前
2025教资面试真题电子版|科目试讲+结构化真题解析|完整PDF
面试·职场和发展·pdf
小兜全糖(xdqt)1 天前
python ppt转pdf以及图片提取
python·pdf·powerpoint
番石榴AI1 天前
视频转ppt/pdf V2.0版(新增转为可编辑PPT功能)
人工智能·pdf·powerpoint
ClouGence1 天前
百草味数据架构升级实践:打造 Always Ready 的企业级数据平台
大数据·数据库·数据分析
_深巷的猫1 天前
python爬虫自动库DrissionPage保存网页快照mhtml/pdf/全局截图/打印机另存pdf
pdf
口_天_光健1 天前
制造企业的数据目录编写
大数据·数据库·数据仓库·数据分析