__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """
  2. OCR 工具包
  3. 整合了文档处理相关的工具函数,包括:
  4. - PDF 处理工具
  5. - JSON/Markdown/HTML 格式化工具
  6. - 文件处理工具
  7. - 数字标准化工具
  8. """
  9. # PDFUtils 和 extract_pdf_pages 使用延迟导入,避免在 PaddleX 环境中触发 MinerU 导入检查
  10. # from .pdf_utils import PDFUtils # 已移除,改为延迟导入
  11. # from .pdf_extractor import extract_pdf_pages # 已移除,改为延迟导入(因为它依赖 PDFUtils)
  12. from .json_formatters import JSONFormatters
  13. from .markdown_generator import MarkdownGenerator
  14. from .html_generator import HTMLGenerator
  15. from .visualization_utils import VisualizationUtils
  16. from .output_formatter_v2 import OutputFormatterV2, save_mineru_format
  17. from .normalize_financial_numbers import (
  18. normalize_financial_numbers,
  19. normalize_json_table,
  20. normalize_markdown_table,
  21. normalize_json_file
  22. )
  23. from .file_utils import (
  24. get_input_files,
  25. collect_pid_files,
  26. get_image_files_from_dir,
  27. get_image_files_from_list,
  28. get_image_files_from_csv,
  29. convert_pdf_to_images,
  30. split_files,
  31. create_temp_file_list,
  32. parse_page_range
  33. )
  34. from .log_utils import setup_logging
  35. from .device_utils import get_device, get_device_name
  36. from .image_utils import (
  37. img_decode,
  38. check_img,
  39. alpha_to_color,
  40. preprocess_image,
  41. bbox_to_points,
  42. points_to_bbox,
  43. rotate_image_and_coordinates
  44. )
  45. from .html_utils import (
  46. find_image_in_multiple_locations,
  47. process_html_images,
  48. process_markdown_images,
  49. process_all_images_in_content,
  50. convert_html_table_to_markdown,
  51. parse_html_tables
  52. )
  53. from .number_utils import (
  54. parse_number,
  55. normalize_text_number
  56. )
  57. # PDF 分类工具(封装自 MinerU,优先使用 MinerU 原版,MinerU 不可用时退回内置实现)
  58. # PDFUtils 和 extract_pdf_pages 使用延迟导入,避免在 PaddleX 环境中触发 MinerU 导入检查
  59. # from .pdf_classify import classify as pdf_classify # 按需 import,避免强依赖
  60. # 坐标工具使用延迟导入,避免循环依赖
  61. # from .coordinate_utils import CoordinateUtils # 已移除,改为延迟导入
  62. __all__ = [
  63. # PDF 工具
  64. 'PDFUtils',
  65. 'extract_pdf_pages',
  66. # JSON 格式化
  67. 'JSONFormatters',
  68. # Markdown 生成
  69. 'MarkdownGenerator',
  70. # HTML 生成
  71. 'HTMLGenerator',
  72. # 可视化
  73. 'VisualizationUtils',
  74. # 输出格式化
  75. 'OutputFormatterV2',
  76. 'save_mineru_format',
  77. # 数字标准化
  78. 'normalize_financial_numbers',
  79. 'normalize_json_table',
  80. 'normalize_markdown_table',
  81. 'normalize_json_file',
  82. # 文件工具
  83. 'get_input_files',
  84. 'collect_pid_files',
  85. 'get_image_files_from_dir',
  86. 'get_image_files_from_list',
  87. 'get_image_files_from_csv',
  88. 'convert_pdf_to_images',
  89. 'split_files',
  90. 'create_temp_file_list',
  91. 'parse_page_range',
  92. # 日志工具
  93. 'setup_logging',
  94. # bbox 工具
  95. 'BBoxExtractor',
  96. # 设备工具
  97. 'get_device',
  98. 'get_device_name',
  99. # 图像处理工具
  100. 'img_decode',
  101. 'check_img',
  102. 'alpha_to_color',
  103. 'preprocess_image',
  104. 'bbox_to_points',
  105. 'points_to_bbox',
  106. 'rotate_image_and_coordinates',
  107. # HTML/Markdown 处理工具
  108. 'find_image_in_multiple_locations',
  109. 'process_html_images',
  110. 'process_markdown_images',
  111. 'process_all_images_in_content',
  112. 'convert_html_table_to_markdown',
  113. 'parse_html_tables',
  114. # 数字解析工具
  115. 'parse_number',
  116. 'normalize_text_number',
  117. # 坐标工具
  118. 'CoordinateUtils',
  119. ]
  120. def __getattr__(name: str):
  121. """
  122. 延迟导入 PDFUtils 和 extract_pdf_pages,只有在实际使用时才触发 MinerU 导入检查。
  123. 这样可以在 PaddleX 环境中正常导入 ocr_utils,即使 MinerU 不可用。
  124. """
  125. if name == 'PDFUtils':
  126. from .pdf_utils import PDFUtils
  127. return PDFUtils
  128. elif name == 'extract_pdf_pages':
  129. from .pdf_extractor import extract_pdf_pages
  130. return extract_pdf_pages
  131. elif name == 'BBoxExtractor':
  132. """
  133. 延迟导入 BBoxExtractor,只有在实际使用时才导入。
  134. """
  135. from .bbox_utils import BBoxExtractor
  136. return BBoxExtractor
  137. elif name == 'CoordinateUtils':
  138. """
  139. 延迟导入 CoordinateUtils,只有在实际使用时才导入。
  140. """
  141. from .coordinate_utils import CoordinateUtils
  142. return CoordinateUtils
  143. raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
  144. __version__ = "1.0.0"
  145. __author__ = "zhch158"