AbsPipe.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from abc import ABC, abstractmethod
  2. from magic_pdf.dict2md.mkcontent import mk_universal_format, mk_mm_markdown
  3. from magic_pdf.dict2md.ocr_mkcontent import make_standard_format_with_para, ocr_mk_mm_markdown_with_para
  4. from magic_pdf.filter.pdf_classify_by_type import classify
  5. from magic_pdf.filter.pdf_meta_scan import pdf_meta_scan
  6. from magic_pdf.rw.AbsReaderWriter import AbsReaderWriter
  7. from magic_pdf.libs.drop_reason import DropReason
  8. from magic_pdf.libs.json_compressor import JsonCompressor
  9. class AbsPipe(ABC):
  10. """
  11. txt和ocr处理的抽象类
  12. """
  13. PIP_OCR = "ocr"
  14. PIP_TXT = "txt"
  15. def __init__(self, pdf_bytes: bytes, model_list: list, image_writer: AbsReaderWriter, img_parent_path:str, is_debug:bool=False):
  16. self.pdf_bytes = pdf_bytes
  17. self.model_list = model_list
  18. self.image_writer = image_writer
  19. self.img_parent_path = img_parent_path
  20. self.pdf_mid_data = None # 未压缩
  21. self.is_debug = is_debug
  22. def get_compress_pdf_mid_data(self):
  23. return JsonCompressor.compress_json(self.pdf_mid_data)
  24. @abstractmethod
  25. def pipe_classify(self):
  26. """
  27. 有状态的分类
  28. """
  29. raise NotImplementedError
  30. @abstractmethod
  31. def pipe_parse(self):
  32. """
  33. 有状态的解析
  34. """
  35. raise NotImplementedError
  36. @abstractmethod
  37. def pipe_mk_uni_format(self):
  38. """
  39. 有状态的组装统一格式
  40. """
  41. raise NotImplementedError
  42. @abstractmethod
  43. def pipe_mk_markdown(self):
  44. """
  45. 有状态的组装markdown
  46. """
  47. raise NotImplementedError
  48. @staticmethod
  49. def classify(pdf_bytes: bytes) -> str:
  50. """
  51. 根据pdf的元数据,判断是否是文本pdf,还是ocr pdf
  52. """
  53. pdf_meta = pdf_meta_scan(pdf_bytes)
  54. if pdf_meta.get("_need_drop", False): # 如果返回了需要丢弃的标志,则抛出异常
  55. raise Exception(f"pdf meta_scan need_drop,reason is {pdf_meta['_drop_reason']}")
  56. else:
  57. is_encrypted = pdf_meta["is_encrypted"]
  58. is_needs_password = pdf_meta["is_needs_password"]
  59. if is_encrypted or is_needs_password: # 加密的,需要密码的,没有页面的,都不处理
  60. raise Exception(f"pdf meta_scan need_drop,reason is {DropReason.ENCRYPTED}")
  61. else:
  62. is_text_pdf, results = classify(
  63. pdf_meta["total_page"],
  64. pdf_meta["page_width_pts"],
  65. pdf_meta["page_height_pts"],
  66. pdf_meta["image_info_per_page"],
  67. pdf_meta["text_len_per_page"],
  68. pdf_meta["imgs_per_page"],
  69. pdf_meta["text_layout_per_page"],
  70. )
  71. if is_text_pdf:
  72. return AbsPipe.PIP_TXT
  73. else:
  74. return AbsPipe.PIP_OCR
  75. @staticmethod
  76. def mk_uni_format(compressed_pdf_mid_data: str, img_buket_path: str) -> list:
  77. """
  78. 根据pdf类型,生成统一格式content_list
  79. """
  80. pdf_mid_data = JsonCompressor.decompress_json(compressed_pdf_mid_data)
  81. parse_type = pdf_mid_data["_parse_type"]
  82. pdf_info_list = pdf_mid_data["pdf_info"]
  83. if parse_type == AbsPipe.PIP_TXT:
  84. content_list = mk_universal_format(pdf_info_list, img_buket_path)
  85. elif parse_type == AbsPipe.PIP_OCR:
  86. content_list = make_standard_format_with_para(pdf_info_list, img_buket_path)
  87. return content_list
  88. @staticmethod
  89. def mk_markdown(compressed_pdf_mid_data: str, img_buket_path: str) -> list:
  90. """
  91. 根据pdf类型,markdown
  92. """
  93. pdf_mid_data = JsonCompressor.decompress_json(compressed_pdf_mid_data)
  94. parse_type = pdf_mid_data["_parse_type"]
  95. pdf_info_list = pdf_mid_data["pdf_info"]
  96. if parse_type == AbsPipe.PIP_TXT:
  97. # content_list = mk_universal_format(pdf_info_list, img_buket_path)
  98. # md_content = mk_mm_markdown(content_list)
  99. md_content = ocr_mk_mm_markdown_with_para(pdf_info_list, img_buket_path)
  100. elif parse_type == AbsPipe.PIP_OCR:
  101. md_content = ocr_mk_mm_markdown_with_para(pdf_info_list, img_buket_path)
  102. return md_content