使用自定义 OCR 模型进行 RapidOCR
In [ ]
已复制!
import os
import os
In [ ]
已复制!
from huggingface_hub import snapshot_download
from huggingface_hub import snapshot_download
In [ ]
已复制!
from docling.datamodel.pipeline_options import PdfPipelineOptions, RapidOcrOptions
from docling.document_converter import (
ConversionResult,
DocumentConverter,
InputFormat,
PdfFormatOption,
)
from docling.datamodel.pipeline_options import PdfPipelineOptions, RapidOcrOptions from docling.document_converter import ( ConversionResult, DocumentConverter, InputFormat, PdfFormatOption, )
In [ ]
已复制!
def main():
# Source document to convert
source = "https://arxiv.org/pdf/2408.09869v4"
# Download RappidOCR models from HuggingFace
print("Downloading RapidOCR models")
download_path = snapshot_download(repo_id="SWHL/RapidOCR")
# Setup RapidOcrOptions for english detection
det_model_path = os.path.join(
download_path, "PP-OCRv4", "en_PP-OCRv3_det_infer.onnx"
)
rec_model_path = os.path.join(
download_path, "PP-OCRv4", "ch_PP-OCRv4_rec_server_infer.onnx"
)
cls_model_path = os.path.join(
download_path, "PP-OCRv3", "ch_ppocr_mobile_v2.0_cls_train.onnx"
)
ocr_options = RapidOcrOptions(
det_model_path=det_model_path,
rec_model_path=rec_model_path,
cls_model_path=cls_model_path,
)
pipeline_options = PdfPipelineOptions(
ocr_options=ocr_options,
)
# Convert the document
converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_options=pipeline_options,
),
},
)
conversion_result: ConversionResult = converter.convert(source=source)
doc = conversion_result.document
md = doc.export_to_markdown()
print(md)
def main(): # Source document to convert source = "https://arxiv.org/pdf/2408.09869v4" # Download RappidOCR models from HuggingFace print("下载 RapidOCR 模型") download_path = snapshot_download(repo_id="SWHL/RapidOCR") # Setup RapidOcrOptions for english detection det_model_path = os.path.join( download_path, "PP-OCRv4", "en_PP-OCRv3_det_infer.onnx" ) rec_model_path = os.path.join( download_path, "PP-OCRv4", "ch_PP-OCRv4_rec_server_infer.onnx" ) cls_model_path = os.path.join( download_path, "PP-OCRv3", "ch_ppocr_mobile_v2.0_cls_train.onnx" ) ocr_options = RapidOcrOptions( det_model_path=det_model_path, rec_model_path=rec_model_path, cls_model_path=cls_model_path, ) pipeline_options = PdfPipelineOptions( ocr_options=ocr_options, ) # Convert the document converter = DocumentConverter( format_options={ InputFormat.PDF: PdfFormatOption( pipeline_options=pipeline_options, ), }, ) conversion_result: ConversionResult = converter.convert(source=source) doc = conversion_result.document md = doc.export_to_markdown() print(md)
In [ ]
已复制!
if __name__ == "__main__":
main()
if __name__ == "__main__": main()