最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用MinerU的簡(jiǎn)單的示例

 更新時(shí)間:2025年04月25日 11:07:32   作者:MasonYyp  
MinerU是國(guó)產(chǎn)的一款將PDF轉(zhuǎn)化為機(jī)器可讀格式的工具,本文主要介紹了Python使用MinerU的簡(jiǎn)單的示例,具有一定的參考價(jià)值,感興趣的可以了解一下

1 簡(jiǎn)介

MinerU是國(guó)產(chǎn)的一款將PDF轉(zhuǎn)化為機(jī)器可讀格式的工具(如markdown、json),可以很方便地抽取為任意格式。目前支持圖像(.jpg及.png)、PDF、Word(.doc及.docx)、以及PowerPoint(.ppt及.pptx)等。

# 官網(wǎng)地址
https://mineru.readthedocs.io/en/latest/index.html

# Github地址
https://github.com/opendatalab/mineru

# API接口地址
https://mineru.readthedocs.io/en/latest/user_guide/quick_start/convert_pdf.html

# 模型下載腳本地址
# 從ModelScope下載模型:download_models.py
# 從HuggingFace下載模型: download_models_hf.py
https://github.com/opendatalab/MinerU/tree/master/scripts

2 安裝MinerU

安裝Python環(huán)境

# 我的版本是:magic-pdf==1.1.0
pip install -U "magic-pdf[full]" -i https://pypi.tuna.tsinghua.edu.cn/simple

下載權(quán)重

官網(wǎng)提供了HuggingFace和ModelScope兩種方法下載,本文從ModlScope上下載,

# 官網(wǎng)下載方法地址
https://github.com/opendatalab/MinerU/blob/master/docs/how_to_download_models_zh_cn.md

開(kāi)始下載權(quán)重

?? 注意:模型下載完成后,腳本會(huì)自動(dòng)生成用戶(hù)目錄下的magic-pdf.json文件,并自動(dòng)配置默認(rèn)模型路徑。 您可在【用戶(hù)目錄】下找到magic-pdf.json文件。

# 安裝modelscope
pip install modelscope

# 下載文件
wget https://gcore.jsdelivr.net/gh/opendatalab/MinerU@master/scripts/download_models.py -O download_models.py
# 也可以到下面的地址,找到download_models.py下載
https://github.com/opendatalab/MinerU/tree/master/scripts

# 執(zhí)行下載模型
# 為了方便使用模型,我修改了download_models.py,添加了設(shè)置模型的位置。
python download_models.py

修改后的download_models.py

?? 此步可以不做。

文件中的local_dir是我新加的下載模型的位置,如果不設(shè)置會(huì)下載到下面目錄中:windows的用戶(hù)目錄為 “C:\Users\用戶(hù)名”, linux用戶(hù)目錄為 “/home/用戶(hù)名”。

import json
import os

import requests
from modelscope import snapshot_download


def download_json(url):
    # 下載JSON文件
    response = requests.get(url)
    response.raise_for_status()  # 檢查請(qǐng)求是否成功
    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.1.1':
            data = download_json(url)
    else:
        data = download_json(url)

    # 修改內(nèi)容
    for key, value in modifications.items():
        data[key] = value

    # 保存修改后的內(nèi)容
    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_2501/*",
        "models/TabRec/TableMaster/*",
        "models/TabRec/StructEqTable/*",
    ]

    # 設(shè)置模型下載的位置
    local_dir="E:/mineru"

    # 下載模型
    model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', allow_patterns=mineru_patterns, local_dir=local_dir)
    layoutreader_model_dir = snapshot_download('ppaanngggg/layoutreader', local_dir=local_dir)
    model_dir = model_dir + '/models'
    print(f'model_dir is: {model_dir}')
    print(f'layoutreader_model_dir is: {layoutreader_model_dir}')

    json_url = 'https://gcore.jsdelivr.net/gh/opendatalab/MinerU@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}')

3 Python使用MinerU

Python安裝完MinerU后,可以直接執(zhí)行下面的代碼,首次執(zhí)行時(shí)會(huì)自動(dòng)下載PaddleOCR模型的權(quán)重和參數(shù),PaddleOCR模型會(huì)自動(dòng)下載到用戶(hù)目錄下的.paddleocr目錄下。

解析PDF文件的Python代碼如下:

import os

from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
from magic_pdf.data.dataset import PymuDocDataset
from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
from magic_pdf.config.enums import SupportedPdfParseMethod

# pdf文件路徑
pdf_file_path = "E:/hello/test-5-2.pdf"
# 獲取沒(méi)有后綴的pdf文件名稱(chēng)
pdf_file_path_without_suff = pdf_file_path.split(".")[0]
print(pdf_file_path_without_suff)

# 文件所在的目錄
pdf_file_path_parent_dir = os.path.dirname(pdf_file_path)
image_dir = os.path.join(pdf_file_path_parent_dir, "images")
print(image_dir)

# Markdown的寫(xiě)入實(shí)例
# markdown_dir = "./output/markdown"
# writer_markdown = FileBasedDataWriter(markdown_dir)
writer_markdown = FileBasedDataWriter()
# 讀取圖片
writer_image = FileBasedDataWriter(image_dir)

# 讀取文件流
reader_pdf = FileBasedDataReader("")
bytes_pdf = reader_pdf.read(pdf_file_path)

# 處理數(shù)據(jù)
dataset_pdf = PymuDocDataset(bytes_pdf)

# 判斷是否支持ocr
if dataset_pdf.classify() == SupportedPdfParseMethod.OCR:
    # 支持OCR
    infer_result = dataset_pdf.apply(doc_analyze, ocr=True)
    pipe_result = infer_result.pipe_ocr_mode(writer_image)
else:
    # 不支持OCR
    infer_result = dataset_pdf.apply(doc_analyze, ocr=False)
    pipe_result = infer_result.pipe_txt_mode(writer_image)

# 在每一頁(yè)上都使用模型解析文本
infer_result.draw_model(pdf_file_path)

# 獲取模型處理后的結(jié)果
model_inference_result = infer_result.get_infer_res()
print(model_inference_result)

# 為pdf生成含有顏色標(biāo)注布局的pdf文件
pipe_result.draw_layout(f"{pdf_file_path_without_suff}_layout.pdf")

# 為pdf生成含有顏色標(biāo)注文本行的pdf文件
pipe_result.draw_span(f"{pdf_file_path_without_suff}_spans.pdf")

# 獲取markdown的內(nèi)容
markdown_content = pipe_result.get_markdown(image_dir)
print(markdown_content)

# 保存markdown文件
# pipe_result.dump_md(writer_markdown, f"{pdf_file_path_without_suff}.md", image_dir)
pipe_result.dump_md(writer_markdown, f"{pdf_file_path_without_suff}.md", image_dir)

# json文本列表
# 數(shù)據(jù)類(lèi)型包括type、text、text_level、page_idx、img_path等
content_list_content = pipe_result.get_content_list(image_dir)
print(content_list_content)

# 保存json文本列表
pipe_result.dump_content_list(writer_markdown, f"{pdf_file_path_without_suff}_content_list.json", image_dir)

# 獲取含有位置信息的json文本
middle_json_content = pipe_result.get_middle_json()

# 保存含有位置信息的json文本
pipe_result.dump_middle_json(writer_markdown, f'{pdf_file_path_without_suff}_middle.json')

到此這篇關(guān)于Python使用MinerU的簡(jiǎn)單的示例的文章就介紹到這了,更多相關(guān)Python使用MinerU內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 在NumPy中創(chuàng)建空數(shù)組/矩陣的方法

    在NumPy中創(chuàng)建空數(shù)組/矩陣的方法

    今天小編就為大家分享一篇在NumPy中創(chuàng)建空數(shù)組/矩陣的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Python基礎(chǔ)教程之錯(cuò)誤和異常的處理方法

    Python基礎(chǔ)教程之錯(cuò)誤和異常的處理方法

    程序在運(yùn)行時(shí),如果python解釋器遇到一個(gè)錯(cuò)誤,會(huì)停止程序的執(zhí)行,并且提示一些錯(cuò)誤信息,這就是異常,下面這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)教程之錯(cuò)誤和異常的處理方法,需要的朋友可以參考下
    2022-05-05
  • 詳解Python實(shí)現(xiàn)多進(jìn)程異步事件驅(qū)動(dòng)引擎

    詳解Python實(shí)現(xiàn)多進(jìn)程異步事件驅(qū)動(dòng)引擎

    本篇文章主要介紹了詳解Python實(shí)現(xiàn)多進(jìn)程異步事件驅(qū)動(dòng)引擎,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Python httplib模塊使用實(shí)例

    Python httplib模塊使用實(shí)例

    這篇文章主要介紹了Python httplib模塊使用實(shí)例,httplib模塊是一個(gè)底層基礎(chǔ)模塊,本文講解了httplib模塊的常用方法及使用實(shí)例,需要的朋友可以參考下
    2015-04-04
  • 使用Python發(fā)送郵件附件以定時(shí)備份MySQL的教程

    使用Python發(fā)送郵件附件以定時(shí)備份MySQL的教程

    這篇文章主要介紹了使用Python發(fā)送郵件附件以定時(shí)備份MySQL的教程,本文的示例基于CentOS,需要的朋友可以參考下
    2015-04-04
  • python編譯安裝參數(shù)方式

    python編譯安裝參數(shù)方式

    這篇文章主要介紹了python編譯安裝參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python OpenCV實(shí)現(xiàn)視頻追蹤

    Python OpenCV實(shí)現(xiàn)視頻追蹤

    這篇文章主要為大家詳細(xì)介紹了Python OpenCV實(shí)現(xiàn)視頻追蹤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • python的三種等待方式及優(yōu)缺點(diǎn)小結(jié)

    python的三種等待方式及優(yōu)缺點(diǎn)小結(jié)

    這篇文章主要介紹了python的三種等待方式及優(yōu)缺點(diǎn)的相關(guān)資料,三種等待元素加載的方法分別是強(qiáng)制等待、隱式等待和顯式等待,并詳細(xì)比較了它們的優(yōu)缺點(diǎn),需要的朋友可以參考下
    2024-12-12
  • 詳解Python進(jìn)行數(shù)據(jù)相關(guān)性分析的三種方式

    詳解Python進(jìn)行數(shù)據(jù)相關(guān)性分析的三種方式

    相關(guān)系數(shù)量化數(shù)據(jù)集的變量或特征之間的關(guān)聯(lián)。這些統(tǒng)計(jì)數(shù)據(jù)對(duì)科學(xué)和技術(shù)非常重要,Python?有很好的工具可以用來(lái)計(jì)算它們。SciPy、NumPy?和Pandas相關(guān)方法以及數(shù)據(jù)可視化功能,感興趣的可以了解一下
    2022-04-04
  • 解決windows下python3使用multiprocessing.Pool出現(xiàn)的問(wèn)題

    解決windows下python3使用multiprocessing.Pool出現(xiàn)的問(wèn)題

    這篇文章主要介紹了解決windows下python3使用multiprocessing.Pool出現(xiàn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04

最新評(píng)論

琼中| 贺州市| 河北区| 南木林县| 巩义市| 乡城县| 宝鸡市| 根河市| 卓资县| 拉萨市| 兴和县| 宝鸡市| 枝江市| 上饶市| 罗源县| 鄂尔多斯市| 共和县| 寿光市| 元氏县| 筠连县| 正蓝旗| 临澧县| 山阴县| 利津县| 宜君县| 中超| 平南县| 崇礼县| 肥西县| 读书| 无为县| 景宁| 孟州市| 凤山县| 林周县| 平罗县| 梁山县| 简阳市| 信丰县| 唐河县| 肥城市|