ocr_demo.bak 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import json
  2. import os
  3. import sys
  4. import time
  5. from loguru import logger
  6. from pathlib import Path
  7. from magic_pdf.libs.config_reader import get_s3_config_dict
  8. from magic_pdf.pdf_parse_by_ocr import parse_pdf_by_ocr
  9. from demo.demo_commons import get_json_from_local_or_s3
  10. from magic_pdf.dict2md.ocr_mkcontent import (
  11. ocr_mk_mm_markdown_with_para,
  12. make_standard_format_with_para
  13. )
  14. from magic_pdf.libs.commons import join_path, read_file, formatted_time
  15. def save_markdown(markdown_text, input_filepath):
  16. # 获取输入文件的目录
  17. directory = os.path.dirname(input_filepath)
  18. # 获取输入文件的文件名(不带扩展名)
  19. base_name = os.path.basename(input_filepath)
  20. file_name_without_ext = os.path.splitext(base_name)[0]
  21. # 定义输出文件的路径
  22. output_filepath = os.path.join(directory, f"{file_name_without_ext}.md")
  23. # 将Markdown文本写入.md文件
  24. with open(output_filepath, 'w', encoding='utf-8') as file:
  25. file.write(markdown_text)
  26. def read_json_file(file_path):
  27. with open(file_path, 'r') as f:
  28. data = json.load(f)
  29. return data
  30. def ocr_local_parse(ocr_pdf_path, ocr_json_file_path):
  31. try:
  32. ocr_pdf_model_info = read_json_file(ocr_json_file_path)
  33. pth = Path(ocr_json_file_path)
  34. book_name = pth.name
  35. pdf_bytes = read_file(ocr_pdf_path, None)
  36. ocr_parse_core(book_name, pdf_bytes, ocr_pdf_model_info)
  37. except Exception as e:
  38. logger.exception(e)
  39. def ocr_online_parse(book_name, start_page_id=0, debug_mode=True):
  40. try:
  41. json_object = get_json_from_local_or_s3(book_name)
  42. # logger.info(json_object)
  43. s3_pdf_path = json_object["file_location"]
  44. s3_config = get_s3_config_dict(s3_pdf_path)
  45. pdf_bytes = read_file(s3_pdf_path, s3_config)
  46. ocr_pdf_model_info = json_object.get("doc_layout_result")
  47. ocr_parse_core(book_name, pdf_bytes, ocr_pdf_model_info)
  48. except Exception as e:
  49. logger.exception(e)
  50. def ocr_parse_core(book_name, pdf_bytes, ocr_pdf_model_info, start_page_id=0):
  51. save_tmp_path = os.path.join(os.path.dirname(__file__), "../..", "tmp", "unittest")
  52. save_path = join_path(save_tmp_path, "md")
  53. save_path_with_bookname = os.path.join(save_path, book_name)
  54. text_content_save_path = f"{save_path_with_bookname}/book.md"
  55. 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)
  56. parent_dir = os.path.dirname(text_content_save_path)
  57. if not os.path.exists(parent_dir):
  58. os.makedirs(parent_dir)
  59. # markdown_content = mk_nlp_markdown(pdf_info_dict)
  60. markdown_content = ocr_mk_mm_markdown_with_para(pdf_info_dict)
  61. # markdown_pagination = ocr_mk_mm_markdown_with_para_and_pagination(pdf_info_dict)
  62. with open(text_content_save_path, "w", encoding="utf-8") as f:
  63. f.write(markdown_content)
  64. standard_format = make_standard_format_with_para(pdf_info_dict)
  65. standard_format_save_path = f"{save_path_with_bookname}/standard_format.txt"
  66. with open(standard_format_save_path, "w", encoding="utf-8") as f:
  67. # 将standard_format dump成json文本并保存
  68. f.write(json.dumps(standard_format, ensure_ascii=False))
  69. def ocr_parse_pdf_core(pdf_bytes, model_output_json_list, book_name, start_page_id=0, debug_mode=False):
  70. start_time = time.time() # 记录开始时间
  71. # 先打印一下book_name和解析开始的时间
  72. logger.info(
  73. f"book_name is:{book_name},start_time is:{formatted_time(start_time)}",
  74. file=sys.stderr,
  75. )
  76. pdf_info_dict = parse_pdf_by_ocr(
  77. pdf_bytes,
  78. model_output_json_list,
  79. "",
  80. book_name,
  81. pdf_model_profile=None,
  82. start_page_id=start_page_id,
  83. debug_mode=debug_mode,
  84. )
  85. end_time = time.time() # 记录完成时间
  86. parse_time = int(end_time - start_time) # 计算执行时间
  87. # 解析完成后打印一下book_name和耗时
  88. logger.info(
  89. f"book_name is:{book_name},end_time is:{formatted_time(end_time)},cost_time is:{parse_time}",
  90. file=sys.stderr,
  91. )
  92. return pdf_info_dict, parse_time
  93. if __name__ == '__main__':
  94. pdf_path = r"/home/cxu/workspace/Magic-PDF/ocr_demo/j.1540-627x.2006.00176.x.pdf"
  95. json_file_path = r"/home/cxu/workspace/Magic-PDF/ocr_demo/j.1540-627x.2006.00176.x.json"
  96. # ocr_local_parse(pdf_path, json_file_path)
  97. book_name = "数学新星网/edu_00001236"
  98. ocr_online_parse(book_name)
  99. pass