AbsPipe.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from abc import ABC, abstractmethod
  2. from magic_pdf.dict2md.ocr_mkcontent import union_make
  3. from magic_pdf.filter.pdf_classify_by_type import classify
  4. from magic_pdf.filter.pdf_meta_scan import pdf_meta_scan
  5. from magic_pdf.libs.MakeContentConfig import MakeMode, DropMode
  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_analyze(self):
  31. """
  32. 有状态的跑模型分析
  33. """
  34. raise NotImplementedError
  35. @abstractmethod
  36. def pipe_parse(self):
  37. """
  38. 有状态的解析
  39. """
  40. raise NotImplementedError
  41. @abstractmethod
  42. def pipe_mk_uni_format(self, img_parent_path, drop_mode):
  43. """
  44. 有状态的组装统一格式
  45. """
  46. raise NotImplementedError
  47. @abstractmethod
  48. def pipe_mk_markdown(self, img_parent_path, drop_mode):
  49. """
  50. 有状态的组装markdown
  51. """
  52. raise NotImplementedError
  53. @staticmethod
  54. def classify(pdf_bytes: bytes) -> str:
  55. """
  56. 根据pdf的元数据,判断是文本pdf,还是ocr pdf
  57. """
  58. pdf_meta = pdf_meta_scan(pdf_bytes)
  59. if pdf_meta.get("_need_drop", False): # 如果返回了需要丢弃的标志,则抛出异常
  60. raise Exception(f"pdf meta_scan need_drop,reason is {pdf_meta['_drop_reason']}")
  61. else:
  62. is_encrypted = pdf_meta["is_encrypted"]
  63. is_needs_password = pdf_meta["is_needs_password"]
  64. if is_encrypted or is_needs_password: # 加密的,需要密码的,没有页面的,都不处理
  65. raise Exception(f"pdf meta_scan need_drop,reason is {DropReason.ENCRYPTED}")
  66. else:
  67. is_text_pdf, results = classify(
  68. pdf_meta["total_page"],
  69. pdf_meta["page_width_pts"],
  70. pdf_meta["page_height_pts"],
  71. pdf_meta["image_info_per_page"],
  72. pdf_meta["text_len_per_page"],
  73. pdf_meta["imgs_per_page"],
  74. pdf_meta["text_layout_per_page"],
  75. )
  76. if is_text_pdf:
  77. return AbsPipe.PIP_TXT
  78. else:
  79. return AbsPipe.PIP_OCR
  80. @staticmethod
  81. def mk_uni_format(compressed_pdf_mid_data: str, img_buket_path: str, drop_mode=DropMode.WHOLE_PDF) -> list:
  82. """
  83. 根据pdf类型,生成统一格式content_list
  84. """
  85. pdf_mid_data = JsonCompressor.decompress_json(compressed_pdf_mid_data)
  86. pdf_info_list = pdf_mid_data["pdf_info"]
  87. content_list = union_make(pdf_info_list, MakeMode.STANDARD_FORMAT, drop_mode, img_buket_path)
  88. return content_list
  89. @staticmethod
  90. def mk_markdown(compressed_pdf_mid_data: str, img_buket_path: str, drop_mode=DropMode.WHOLE_PDF) -> list:
  91. """
  92. 根据pdf类型,markdown
  93. """
  94. pdf_mid_data = JsonCompressor.decompress_json(compressed_pdf_mid_data)
  95. pdf_info_list = pdf_mid_data["pdf_info"]
  96. md_content = union_make(pdf_info_list, MakeMode.MM_MD, drop_mode, img_buket_path)
  97. return md_content