ocr_demo.py 3.53 KB
Newer Older
赵小蒙's avatar
赵小蒙 committed
1
import json
赵小蒙's avatar
赵小蒙 committed
2
3
4
import os

from loguru import logger
5
from pathlib import Path
赵小蒙's avatar
赵小蒙 committed
6

7
8
from magic_pdf.pipeline_ocr import ocr_parse_pdf_core
from magic_pdf.spark.s3 import get_s3_config
赵小蒙's avatar
赵小蒙 committed
9
from demo.demo_commons import get_json_from_local_or_s3
赵小蒙's avatar
赵小蒙 committed
10
11
from magic_pdf.dict2md.ocr_mkcontent import (
    ocr_mk_mm_markdown_with_para,
赵小蒙's avatar
赵小蒙 committed
12
    make_standard_format_with_para
赵小蒙's avatar
赵小蒙 committed
13
)
赵小蒙's avatar
赵小蒙 committed
14
from magic_pdf.libs.commons import join_path, read_file
赵小蒙's avatar
赵小蒙 committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from magic_pdf.pdf_parse_by_ocr import parse_pdf_by_ocr


def save_markdown(markdown_text, input_filepath):
    # 获取输入文件的目录
    directory = os.path.dirname(input_filepath)
    # 获取输入文件的文件名(不带扩展名)
    base_name = os.path.basename(input_filepath)
    file_name_without_ext = os.path.splitext(base_name)[0]
    # 定义输出文件的路径
    output_filepath = os.path.join(directory, f"{file_name_without_ext}.md")

    # 将Markdown文本写入.md文件
    with open(output_filepath, 'w', encoding='utf-8') as file:
        file.write(markdown_text)


赵小蒙's avatar
赵小蒙 committed
32
33
34
35
36
37
def read_json_file(file_path):
    with open(file_path, 'r') as f:
        data = json.load(f)
    return data


赵小蒙's avatar
赵小蒙 committed
38
def ocr_local_parse(ocr_pdf_path, ocr_json_file_path):
39
    try:
40
41
42
        ocr_pdf_model_info = read_json_file(ocr_json_file_path)
        pth = Path(ocr_json_file_path)
        book_name = pth.name
赵小蒙's avatar
赵小蒙 committed
43
44
        pdf_bytes = read_file(ocr_pdf_path, None)
        ocr_parse_core(book_name, pdf_bytes, ocr_pdf_model_info)
45
    except Exception as e:
46
        logger.exception(e)
赵小蒙's avatar
赵小蒙 committed
47
48
49


def ocr_online_parse(book_name, start_page_id=0, debug_mode=True):
50
51
52
53
54
    try:
        json_object = get_json_from_local_or_s3(book_name)
        # logger.info(json_object)
        s3_pdf_path = json_object["file_location"]
        s3_config = get_s3_config(s3_pdf_path)
赵小蒙's avatar
赵小蒙 committed
55
        pdf_bytes = read_file(s3_pdf_path, s3_config)
赵小蒙's avatar
赵小蒙 committed
56
        ocr_pdf_model_info = json_object.get("doc_layout_result")
赵小蒙's avatar
赵小蒙 committed
57
        ocr_parse_core(book_name, pdf_bytes, ocr_pdf_model_info)
58
59
60
61
    except Exception as e:
        logger.exception(e)


赵小蒙's avatar
赵小蒙 committed
62
def ocr_parse_core(book_name, pdf_bytes, ocr_pdf_model_info, start_page_id=0):
63
64
65
66
    save_tmp_path = os.path.join(os.path.dirname(__file__), "../..", "tmp", "unittest")
    save_path = join_path(save_tmp_path, "md")
    save_path_with_bookname = os.path.join(save_path, book_name)
    text_content_save_path = f"{save_path_with_bookname}/book.md"
67
    pdf_info_dict, parse_time = ocr_parse_pdf_core(pdf_bytes, ocr_pdf_model_info, book_name, start_page_id=start_page_id, debug_mode=True)
68
69
70
71
72
73
74

    parent_dir = os.path.dirname(text_content_save_path)
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir)

    # markdown_content = mk_nlp_markdown(pdf_info_dict)
    markdown_content = ocr_mk_mm_markdown_with_para(pdf_info_dict)
赵小蒙's avatar
赵小蒙 committed
75
    # markdown_pagination = ocr_mk_mm_markdown_with_para_and_pagination(pdf_info_dict)
76
77
78
79

    with open(text_content_save_path, "w", encoding="utf-8") as f:
        f.write(markdown_content)

赵小蒙's avatar
赵小蒙 committed
80
    standard_format = make_standard_format_with_para(pdf_info_dict)
81
82
    standard_format_save_path = f"{save_path_with_bookname}/standard_format.txt"
    with open(standard_format_save_path, "w", encoding="utf-8") as f:
赵小蒙's avatar
赵小蒙 committed
83
84
        # 将standard_format dump成json文本并保存
        f.write(json.dumps(standard_format, ensure_ascii=False))
85

赵小蒙's avatar
赵小蒙 committed
86
87

if __name__ == '__main__':
kernel.h@qq.com's avatar
kernel.h@qq.com committed
88
89
    pdf_path = r"/home/cxu/workspace/Magic-PDF/ocr_demo/j.1540-627x.2006.00176.x.pdf"
    json_file_path = r"/home/cxu/workspace/Magic-PDF/ocr_demo/j.1540-627x.2006.00176.x.json"
kernel.h@qq.com's avatar
kernel.h@qq.com committed
90
91
92
93
    # ocr_local_parse(pdf_path, json_file_path)
    book_name = "科数网/edu_00011318"
    ocr_online_parse(book_name)
    
赵小蒙's avatar
赵小蒙 committed
94
    pass