ocr_demo.py 2.91 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

赵小蒙's avatar
赵小蒙 committed
7
from magic_pdf.dict2md.ocr_mkcontent import ocr_mk_mm_markdown_with_para, ocr_mk_nlp_markdown, ocr_mk_mm_markdown, ocr_mk_mm_standard_format
8
from magic_pdf.libs.commons import join_path
赵小蒙's avatar
赵小蒙 committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
26
27
28
29
30
31
def read_json_file(file_path):
    with open(file_path, 'r') as f:
        data = json.load(f)
    return data


赵小蒙's avatar
赵小蒙 committed
32
if __name__ == '__main__':
xuchao's avatar
xuchao committed
33
34
    #ocr_pdf_path = r"D:\project\20231108code-clean\ocr\new\双栏\s0043-1354(02)00581-x.pdf"
    #ocr_json_file_path = r"D:\project\20231108code-clean\ocr\new\双栏\s0043-1354(02)00581-x.json"
35
36
    # ocr_pdf_path = r"D:\project\20231108code-clean\ocr\new\双栏\j.1540-627x.2006.00176.x.pdf"
    # ocr_json_file_path = r"D:\project\20231108code-clean\ocr\new\双栏\j.1540-627x.2006.00176.x.json"
xuchao's avatar
xuchao committed
37
    
xuchao's avatar
xuchao committed
38
39
    ocr_pdf_path = r"/home/cxu/workspace/Magic-PDF/ocr_demo/ocr_1.pdf"
    ocr_json_file_path = r"/home/cxu/workspace/Magic-PDF/ocr_demo/ocr_1.json"
40
    try:
41
42
43
44
45
        ocr_pdf_model_info = read_json_file(ocr_json_file_path)
        pth = Path(ocr_json_file_path)
        book_name = pth.name
        save_tmp_path = os.path.join(os.path.dirname(__file__), "../..", "tmp", "unittest")
        save_path = join_path(save_tmp_path, "md")
46
47
        save_path_with_bookname = os.path.join(save_path, book_name)
        text_content_save_path = f"{save_path_with_bookname}/book.md"
48
49
50
51
        pdf_info_dict = parse_pdf_by_ocr(
            ocr_pdf_path,
            None,
            ocr_pdf_model_info,
赵小蒙's avatar
赵小蒙 committed
52
            save_path,
53
54
            book_name,
            debug_mode=True)
55

56
57
58
59
        parent_dir = os.path.dirname(text_content_save_path)
        if not os.path.exists(parent_dir):
            os.makedirs(parent_dir)

xuchao's avatar
xuchao committed
60
        # markdown_content = mk_nlp_markdown(pdf_info_dict)
赵小蒙's avatar
赵小蒙 committed
61
        markdown_content = ocr_mk_mm_markdown_with_para(pdf_info_dict)
62
63
64
65

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

赵小蒙's avatar
赵小蒙 committed
66
67
68
69
70
        standard_format = ocr_mk_mm_standard_format(pdf_info_dict)
        standard_format_save_path = f"{save_path_with_bookname}/standard_format.txt"
        with open(standard_format_save_path, "w", encoding="utf-8") as f:
            f.write(str(standard_format))

71
72
        # logger.info(markdown_content)
        # save_markdown(markdown_text, ocr_json_file_path)
73
    except Exception as e:
74
        logger.exception(e)