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",
相关推荐
爱吃泡芙的小白白3 小时前
环境数据多维关系探索利器:Pairs Plot 完全指南
python·信息可视化·数据分析·环境领域·pairs plot
莽撞的大地瓜3 小时前
洞察,始于一目了然——让舆情数据自己“说话”
大数据·网络·数据分析
AI职业加油站4 小时前
职业提升之路:我的大数据分析师学习与备考分享
大数据·人工智能·经验分享·学习·职场和发展·数据分析
Java面试题总结18 小时前
基于 Java 的 PDF 文本水印实现方案(iText7 示例)
java·python·pdf
傻啦嘿哟20 小时前
Python操作PDF页面详解:删除指定页的完整方案
开发语言·python·pdf
爱吃泡芙的小白白1 天前
环境数据可视化利器:Hexbin Chart 全解析与应用实战
信息可视化·数据挖掘·数据分析·环境领域·hexbin chart
爱吃泡芙的小白白1 天前
环境数据可视化利器:气泡图(Bubble Chart)全解析
信息可视化·数据挖掘·数据分析·气泡图·bubble chart·环境领域
沐墨染1 天前
Vue实战:自动化研判报告组件的设计与实现
前端·javascript·信息可视化·数据分析·自动化·vue
sensen_kiss1 天前
Jupter Notebook 使用教程
大数据·人工智能·python·学习·数据分析
deephub1 天前
分类数据 EDA 实战:如何发现隐藏的层次结构
人工智能·python·机器学习·数据分析·数据可视化