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, is_debug: bool = False):
  16. self.pdf_bytes = pdf_bytes
  17. self.model_list = model_list
  18. self.image_writer = image_writer
  19. self.pdf_mid_data = None # 未压缩
  20. self.is_debug = is_debug
  21. def get_compress_pdf_mid_data(self):
  22. return JsonCompressor.compress_json(self.pdf_mid_data)
  23. @abstractmethod
  24. def pipe_classify(self):
  25. """
  26. 有状态的分类
  27. """
  28. raise NotImplementedError
  29. @abstractmethod
  30. def pipe_parse(self):
  31. """
  32. 有状态的解析
  33. """
  34. raise NotImplementedError
  35. @abstractmethod
  36. def pipe_mk_uni_format(self):
  37. """
  38. 有状态的组装统一格式
  39. """
  40. raise NotImplementedError
  41. @abstractmethod
  42. def pipe_mk_markdown(self):
  43. """
  44. 有状态的组装markdown
  45. """
  46. raise NotImplementedError
  47. @staticmethod
  48. def classify(pdf_bytes: bytes) -> str:
  49. """
  50. 根据pdf的元数据,判断是否是文本pdf,还是ocr pdf
  51. """
  52. pdf_meta = pdf_meta_scan(pdf_bytes)
  53. if pdf_meta.get("_need_drop", False): # 如果返回了需要丢弃的标志,则抛出异常
  54. raise Exception(f"pdf meta_scan need_drop,reason is {pdf_meta['_drop_reason']}")
  55. else:
  56. is_encrypted = pdf_meta["is_encrypted"]
  57. is_needs_password = pdf_meta["is_needs_password"]
  58. if is_encrypted or is_needs_password: # 加密的,需要密码的,没有页面的,都不处理
  59. raise Exception(f"pdf meta_scan need_drop,reason is {DropReason.ENCRYPTED}")
  60. else:
  61. is_text_pdf, results = classify(
  62. pdf_meta["total_page"],
  63. pdf_meta["page_width_pts"],
  64. pdf_meta["page_height_pts"],
  65. pdf_meta["image_info_per_page"],
  66. pdf_meta["text_len_per_page"],
  67. pdf_meta["imgs_per_page"],
  68. pdf_meta["text_layout_per_page"],
  69. )
  70. if is_text_pdf:
  71. return AbsPipe.PIP_TXT
  72. else:
  73. return AbsPipe.PIP_OCR
  74. @staticmethod
  75. def mk_uni_format(compressed_pdf_mid_data: str, img_buket_path: str) -> list:
  76. """
  77. 根据pdf类型,生成统一格式content_list
  78. """
  79. pdf_mid_data = JsonCompressor.decompress_json(compressed_pdf_mid_data)
  80. parse_type = pdf_mid_data["_parse_type"]
  81. pdf_info_list = pdf_mid_data["pdf_info"]
  82. if parse_type == AbsPipe.PIP_TXT:
  83. # content_list = mk_universal_format(pdf_info_list, img_buket_path)
  84. content_list = make_standard_format_with_para(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