Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

Merge branch 'master' into dev-in-line-bbox

liukaiwenda5091430bbb
diff --cc magic_pdf/pdf_parse_by_ocr.py
index 75e16a4b,946f7367..7937deca
--- a/magic_pdf/pdf_parse_by_ocr.py
+++ b/magic_pdf/pdf_parse_by_ocr.py
@@@ -1,9 -1,23 +1,24 @@@
+ import json
+ import os
+ import time
+
from loguru import logger
+from magic_pdf.libs.ocr_dict_merge import merge_spans_to_line, remove_overlaps_min_spans, modify_y_axis
+ from magic_pdf.libs.commons import read_file, join_path, fitz, get_img_s3_client, get_delta_time, get_docx_model_output
+ from magic_pdf.libs.coordinate_transform import get_scale_ratio
+ from magic_pdf.libs.safe_filename import sanitize_filename
+ from magic_pdf.pre_proc.detect_footer_by_model import parse_footers
+ from magic_pdf.pre_proc.detect_footnote import parse_footnotes_by_model
+ from magic_pdf.pre_proc.detect_header import parse_headers
+ from magic_pdf.pre_proc.detect_page_number import parse_pageNos
+ from magic_pdf.pre_proc.ocr_cut_image import cut_image_and_table
+ from magic_pdf.pre_proc.ocr_detect_layout import layout_detect
+ from magic_pdf.pre_proc.ocr_dict_merge import remove_overlaps_min_spans, merge_spans_to_line_by_layout
+ from magic_pdf.pre_proc.ocr_remove_spans import remove_spans_by_bboxes
- def construct_page_component(page_id, blocks):
+ def construct_page_component(page_id, blocks, layout_bboxes):
return_dict = {
'preproc_blocks': blocks,
'page_idx': page_id,
@@@ -67,14 -148,26 +149,29 @@@ def parse_pdf_by_ocr
# 删除重叠spans中较小的那些
spans = remove_overlaps_min_spans(spans)
+ # 对tpye=["displayed_equation", "image", "table"]进行额外处理,如果左边有字的话,将该span的bbox中y0调整低于文字的y0
- #spans = modify_y_axis(spans)
++ # spans = modify_y_axis(spans)
++
+ # 删除remove_span_block_bboxes中的bbox
+ spans = remove_spans_by_bboxes(spans, need_remove_spans_bboxes)
+
+ # 对image和table截图
+ spans = cut_image_and_table(spans, page, page_id, book_name, save_path)
+
+
+ # 行内公式调整, 高度调整至与同行文字高度一致(优先左侧, 其次右侧)
- # 将spans合并成line(从上到下,从左到右)
- lines = merge_spans_to_line(spans)
- # logger.info(lines)
+ # 模型识别错误的行间公式, type类型转换成行内公式
- # 从ocr_page_info中获取layout信息
+ # bbox去除粘连
+
+ # 对tpye=["displayed_equation", "image", "table"]进行额外处理,如果左边有字的话,将该span的bbox中y0调整至不高于文字的y0
+
+ # 从ocr_page_info中解析layout信息(按自然阅读方向排序,并修复重叠和交错的bad case)
+ layout_bboxes = layout_detect(ocr_page_info['subfield_dets'], page, ocr_page_info)
+
+ # 将spans合并成line(在layout内,从上到下,从左到右)
+ lines = merge_spans_to_line_by_layout(spans, layout_bboxes)
# 目前不做block拼接,先做个结构,每个block中只有一个line,block的bbox就是line的bbox
diff --cc magic_pdf/pre_proc/ocr_dict_merge.py
index 22574752,b7c51962..7f92035c
--- a/magic_pdf/pre_proc/ocr_dict_merge.py
+++ b/magic_pdf/pre_proc/ocr_dict_merge.py
@@@ -41,114 -63,31 +63,125 @@@ def merge_spans_to_line(spans)
if current_line:
lines.append(current_line)
- # 计算每行的边界框,并对每行中的span按照x0进行排序
- line_objects = []
- for line in lines:
- # 按照x0坐标排序
- line.sort(key=lambda span: span['bbox'][0])
- line_bbox = [
- min(span['bbox'][0] for span in line), # x0
- min(span['bbox'][1] for span in line), # y0
- max(span['bbox'][2] for span in line), # x1
- max(span['bbox'][3] for span in line), # y1
- ]
- line_objects.append({
- "bbox": line_bbox,
- "spans": line,
- })
+ return lines
- return line_objects
+ def merge_spans_to_line_by_layout(spans, layout_bboxes):
+ lines = []
+ new_spans = []
+ for item in layout_bboxes:
+ layout_bbox = item['layout_bbox']
+ # 遍历spans,将每个span放入对应的layout中
+ layout_sapns = []
+ for span in spans:
+ if calculate_overlap_area_in_bbox1_area_ratio(span['bbox'], layout_bbox) > 0.8:
+ layout_sapns.append(span)
+ # 如果layout_sapns不为空,则放入new_spans中
+ if len(layout_sapns) > 0:
+ new_spans.append(layout_sapns)
+ # 从spans删除已经放入layout_sapns中的span
+ for layout_sapn in layout_sapns:
+ spans.remove(layout_sapn)
+
+ if len(new_spans) > 0:
+ for layout_sapns in new_spans:
+ layout_lines = merge_spans_to_line(layout_sapns)
+ lines.extend(layout_lines)
+
+ #对line中的span进行排序
+ lines = line_sort_spans_by_left_to_right(lines)
+
+ return lines
+
+
+
+def modify_y_axis(spans: list):
+ inline_list = []
+ displayed_list = []
+ text_list = []
+ image_list = []
+ table_list = []
+
+ spans.sort(key=lambda span: span['bbox'][1])
+
+ lines = []
+ current_line = [spans[0]]
+ if spans[0]["type"] in ["displayed_equation", "image", "table"]:
+ displayed_list.append(spans[0])
+
+ line_first_y0 = spans[0]["bbox"][1]
+ line_first_y = spans[0]["bbox"][3]
+ #用于给行间公式搜索
+ text_inline_lines = []
+ for span in spans[1:]:
+ # 如果当前的span类型为"displayed_equation" 或者 当前行中已经有"displayed_equation"
+ # image和table类型,同上
+ if span['type'] in ["displayed_equation", "image", "table"] or any(
+ s['type'] in ["displayed_equation", "image", "table"] for s in current_line):
+ #传入
+ if spans[0]["type"] in ["displayed_equation", "image", "table"]:
+ displayed_list.append(span)
+ # 则开始新行
+ lines.append(current_line)
+ current_line = [span]
+ line_first_y0 = span["bbox"][1]
+ line_first_y = span["bbox"][3]
+ continue
+
+ # 如果当前的span与当前行的最后一个span在y轴上重叠,则添加到当前行
+ if __is_overlaps_y_exceeds_threshold(span['bbox'], current_line[-1]['bbox']):
+ if span["bbox"][1] < line_first_y0:
+ line_first_y0 = span["bbox"][1]
+ if span["bbox"][3] > line_first_y:
+ line_first_y = span["bbox"][3]
+ current_line.append(span)
+
+ else:
+ # 否则,开始新行
+ lines.append(current_line)
+ text_inline_lines.append((current_line, (line_first_y0, line_first_y)))
+ current_line = [span]
+ line_first_y0 = spans[0]["bbox"][1]
+ line_first_y = spans[0]["bbox"][3]
+
+ # 添加最后一行
+ if current_line:
+ lines.append(current_line)
+ if len(current_line)>1 or current_line[0]["type"] in ["text", "inline_equation"]:
+ text_inline_lines.append((current_line, (line_first_y0, line_first_y)))
+
+ for line in text_inline_lines:
+ # 按照x0坐标排序
+ current_line = line[0]
+ current_line.sort(key=lambda span: span['bbox'][0])
+
+
+ #调整每一个文字行内bbox统一
+ for line in text_inline_lines:
+ current_line, (line_first_y0, line_first_y) = line
+ for span in current_line:
+ span["bbox"][1] = line_first_y0
+ span["bbox"][3] = line_first_y
+ #错误行间公式转行内公式
+ j = 0
+ for i in range(len(displayed_list)):
+ span = displayed_list[i]
+ span_y0, span_y = span["bbox"][1], span["bbox"][3]
+ while j < len(text_inline_lines):
+ text_line = text_inline_lines[j]
+ y0, y1 = text_line[1]
+ if span_y0 < y0 and span_y > y0 or span_y0 < y1 and span_y > y1 or span_y0 < y0 and span_y > y1 and __is_overlaps_y_exceeds_threshold(span['bbox'], (0, y0, 0, y1)):
+ span["bbox"][1] = y0
+ span["bbox"][3] = y1
+ if span["type"] == "displayed_equation":
+ span["type"] = "inline_equation"
+ break
+ elif span_y < y0 or span_y0 < y0 and span_y > y0 and not __is_overlaps_y_exceeds_threshold(span['bbox'], (0, y0, 0, y1)):
+ break
+ else:
+ j += 1
+
+ return spans
+
+
+
+