mineru_1.2.2(备份)
mineru模型原始仓库:PDF-Extract-Kit-1.0 模型仓库 (MinerU使用仓库) · 模型库
MinerU是一款将PDF转化为机器可读格式的工具(如markdown、json),可以很方便地抽取为任意格式。
尽管miner已经更新到2.x版本了,但由于手上只有cpu服务器,还是1.2.2版本适合。
-
1.3以上版本由于将paddleocr转为pytorch格式,导致cpu解析速度变慢。
-
2.x直接升级为vlm,更加不适合cpu使用了。
依赖
pip install -U magic-pdf[full]==1.2.2 --extra-index-url https://wheels.myhloli.com -i https://mirrors.aliyun.com/pypi/simple
详细需要下载Release magic_pdf-1.2.2-released · opendatalab/MinerU 源码查看setup.py文件
使用
修改配置文件magic-pdf.json
"models-dir": "MinerU_1.2.2-backup", # 表格公式等模型目录路径
"layoutreader-model-dir": "MinerU_1.2.2-backup/layoutreader", # 文档结构模型目录路径
"device-mode": "cpu", # 运行设备,gpu设置为cuda
demo例子
import os
# 设置配置文件路径
os.environ['MINERU_TOOLS_CONFIG_JSON'] = "model_config.json"
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
# args
pdf_file_name = "abc.pdf" # 替换为测试的pdf文件路径
name_without_suff = pdf_file_name.split(".")[0]
# prepare env
local_image_dir, local_md_dir = "output/images", "output"
image_dir = str(os.path.basename(local_image_dir))
os.makedirs(local_image_dir, exist_ok=True)
image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(
local_md_dir
)
image_dir = str(os.path.basename(local_image_dir))
# read bytes
reader1 = FileBasedDataReader("")
pdf_bytes = reader1.read(pdf_file_name) # read the pdf content
# proc
## Create Dataset Instance
ds = PymuDocDataset(pdf_bytes)
## inference
if ds.classify() == SupportedPdfParseMethod.OCR:
infer_result = ds.apply(doc_analyze, ocr=True)
## pipeline
pipe_result = infer_result.pipe_ocr_mode(image_writer)
else:
infer_result = ds.apply(doc_analyze, ocr=False)
## pipeline
pipe_result = infer_result.pipe_txt_mode(image_writer)
### dump markdown
pipe_result.dump_md(md_writer, f"{name_without_suff}.md", image_dir)
### dump content list
pipe_result.dump_content_list(md_writer, f"{name_without_suff}_content_list.json", image_dir)