magic-pdf 0.10.0__py3-none-any.whl → 0.10.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- magic_pdf/data/data_reader_writer/filebase.py +3 -0
- magic_pdf/filter/pdf_meta_scan.py +3 -17
- magic_pdf/libs/commons.py +0 -161
- magic_pdf/libs/draw_bbox.py +2 -3
- magic_pdf/libs/markdown_utils.py +0 -21
- magic_pdf/libs/pdf_image_tools.py +2 -1
- magic_pdf/libs/version.py +1 -1
- magic_pdf/model/doc_analyze_by_custom_model.py +2 -2
- magic_pdf/model/magic_model.py +0 -30
- magic_pdf/model/sub_modules/ocr/paddleocr/ocr_utils.py +3 -28
- magic_pdf/model/sub_modules/ocr/paddleocr/ppocr_273_mod.py +3 -3
- magic_pdf/para/para_split_v3.py +7 -2
- magic_pdf/pdf_parse_union_core_v2.py +97 -124
- magic_pdf/pre_proc/construct_page_dict.py +0 -55
- magic_pdf/pre_proc/cut_image.py +0 -37
- magic_pdf/pre_proc/ocr_detect_all_bboxes.py +5 -178
- magic_pdf/pre_proc/ocr_dict_merge.py +1 -224
- magic_pdf/pre_proc/ocr_span_list_modify.py +2 -252
- magic_pdf/rw/S3ReaderWriter.py +1 -1
- {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/METADATA +3 -77
- {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/RECORD +25 -76
- {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/WHEEL +1 -1
- magic_pdf/dict2md/mkcontent.py +0 -438
- magic_pdf/layout/__init__.py +0 -0
- magic_pdf/layout/bbox_sort.py +0 -681
- magic_pdf/layout/layout_det_utils.py +0 -182
- magic_pdf/layout/layout_sort.py +0 -921
- magic_pdf/layout/layout_spiler_recog.py +0 -101
- magic_pdf/layout/mcol_sort.py +0 -336
- magic_pdf/libs/calc_span_stats.py +0 -239
- magic_pdf/libs/detect_language_from_model.py +0 -21
- magic_pdf/libs/nlp_utils.py +0 -203
- magic_pdf/libs/textbase.py +0 -33
- magic_pdf/libs/vis_utils.py +0 -308
- magic_pdf/para/block_continuation_processor.py +0 -562
- magic_pdf/para/block_termination_processor.py +0 -480
- magic_pdf/para/commons.py +0 -222
- magic_pdf/para/denoise.py +0 -246
- magic_pdf/para/draw.py +0 -121
- magic_pdf/para/exceptions.py +0 -198
- magic_pdf/para/layout_match_processor.py +0 -40
- magic_pdf/para/para_split.py +0 -807
- magic_pdf/para/para_split_v2.py +0 -959
- magic_pdf/para/raw_processor.py +0 -207
- magic_pdf/para/stats.py +0 -268
- magic_pdf/para/title_processor.py +0 -1014
- magic_pdf/pdf_parse_union_core.py +0 -345
- magic_pdf/post_proc/__init__.py +0 -0
- magic_pdf/post_proc/detect_para.py +0 -3472
- magic_pdf/post_proc/pdf_post_filter.py +0 -60
- magic_pdf/post_proc/remove_footnote.py +0 -153
- magic_pdf/pre_proc/citationmarker_remove.py +0 -161
- magic_pdf/pre_proc/detect_equation.py +0 -134
- magic_pdf/pre_proc/detect_footer_by_model.py +0 -64
- magic_pdf/pre_proc/detect_footer_header_by_statistics.py +0 -284
- magic_pdf/pre_proc/detect_footnote.py +0 -170
- magic_pdf/pre_proc/detect_header.py +0 -64
- magic_pdf/pre_proc/detect_images.py +0 -647
- magic_pdf/pre_proc/detect_page_number.py +0 -64
- magic_pdf/pre_proc/detect_tables.py +0 -62
- magic_pdf/pre_proc/equations_replace.py +0 -550
- magic_pdf/pre_proc/fix_image.py +0 -244
- magic_pdf/pre_proc/fix_table.py +0 -270
- magic_pdf/pre_proc/main_text_font.py +0 -23
- magic_pdf/pre_proc/ocr_detect_layout.py +0 -133
- magic_pdf/pre_proc/pdf_pre_filter.py +0 -78
- magic_pdf/pre_proc/post_layout_split.py +0 -0
- magic_pdf/pre_proc/remove_colored_strip_bbox.py +0 -101
- magic_pdf/pre_proc/remove_footer_header.py +0 -114
- magic_pdf/pre_proc/remove_rotate_bbox.py +0 -236
- magic_pdf/pre_proc/resolve_bbox_conflict.py +0 -184
- magic_pdf/pre_proc/solve_line_alien.py +0 -29
- magic_pdf/pre_proc/statistics.py +0 -12
- {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/LICENSE.md +0 -0
- {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/entry_points.txt +0 -0
- {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/top_level.txt +0 -0
@@ -1,345 +0,0 @@
|
|
1
|
-
import time
|
2
|
-
|
3
|
-
from loguru import logger
|
4
|
-
|
5
|
-
from magic_pdf.config.drop_reason import DropReason
|
6
|
-
from magic_pdf.config.ocr_content_type import ContentType
|
7
|
-
from magic_pdf.layout.layout_sort import (LAYOUT_UNPROC, get_bboxes_layout,
|
8
|
-
get_columns_cnt_of_layout)
|
9
|
-
from magic_pdf.libs.commons import fitz, get_delta_time
|
10
|
-
from magic_pdf.libs.convert_utils import dict_to_list
|
11
|
-
from magic_pdf.libs.hash_utils import compute_md5
|
12
|
-
from magic_pdf.libs.local_math import float_equal
|
13
|
-
from magic_pdf.model.magic_model import MagicModel
|
14
|
-
from magic_pdf.para.para_split_v2 import para_split
|
15
|
-
from magic_pdf.pre_proc.citationmarker_remove import remove_citation_marker
|
16
|
-
from magic_pdf.pre_proc.construct_page_dict import \
|
17
|
-
ocr_construct_page_component_v2
|
18
|
-
from magic_pdf.pre_proc.cut_image import ocr_cut_image_and_table
|
19
|
-
from magic_pdf.pre_proc.equations_replace import (
|
20
|
-
combine_chars_to_pymudict, remove_chars_in_text_blocks,
|
21
|
-
replace_equations_in_textblock)
|
22
|
-
from magic_pdf.pre_proc.ocr_detect_all_bboxes import \
|
23
|
-
ocr_prepare_bboxes_for_layout_split
|
24
|
-
from magic_pdf.pre_proc.ocr_dict_merge import (fill_spans_in_blocks,
|
25
|
-
fix_block_spans,
|
26
|
-
fix_discarded_block,
|
27
|
-
sort_blocks_by_layout)
|
28
|
-
from magic_pdf.pre_proc.ocr_span_list_modify import (
|
29
|
-
get_qa_need_list_v2, remove_overlaps_low_confidence_spans,
|
30
|
-
remove_overlaps_min_spans)
|
31
|
-
from magic_pdf.pre_proc.resolve_bbox_conflict import \
|
32
|
-
check_useful_block_horizontal_overlap
|
33
|
-
|
34
|
-
|
35
|
-
def remove_horizontal_overlap_block_which_smaller(all_bboxes):
|
36
|
-
useful_blocks = []
|
37
|
-
for bbox in all_bboxes:
|
38
|
-
useful_blocks.append({'bbox': bbox[:4]})
|
39
|
-
is_useful_block_horz_overlap, smaller_bbox, bigger_bbox = (
|
40
|
-
check_useful_block_horizontal_overlap(useful_blocks)
|
41
|
-
)
|
42
|
-
if is_useful_block_horz_overlap:
|
43
|
-
logger.warning(
|
44
|
-
f'skip this page, reason: {DropReason.USEFUL_BLOCK_HOR_OVERLAP}, smaller bbox is {smaller_bbox}, bigger bbox is {bigger_bbox}'
|
45
|
-
)
|
46
|
-
for bbox in all_bboxes.copy():
|
47
|
-
if smaller_bbox == bbox[:4]:
|
48
|
-
all_bboxes.remove(bbox)
|
49
|
-
|
50
|
-
return is_useful_block_horz_overlap, all_bboxes
|
51
|
-
|
52
|
-
|
53
|
-
def __replace_STX_ETX(text_str: str):
|
54
|
-
"""Replace \u0002 and \u0003, as these characters become garbled when extracted using pymupdf. In fact, they were originally quotation marks.
|
55
|
-
Drawback: This issue is only observed in English text; it has not been found in Chinese text so far.
|
56
|
-
|
57
|
-
Args:
|
58
|
-
text_str (str): raw text
|
59
|
-
|
60
|
-
Returns:
|
61
|
-
_type_: replaced text
|
62
|
-
"""
|
63
|
-
if text_str:
|
64
|
-
s = text_str.replace('\u0002', "'")
|
65
|
-
s = s.replace('\u0003', "'")
|
66
|
-
return s
|
67
|
-
return text_str
|
68
|
-
|
69
|
-
|
70
|
-
def txt_spans_extract(pdf_page, inline_equations, interline_equations):
|
71
|
-
text_raw_blocks = pdf_page.get_text('dict', flags=fitz.TEXTFLAGS_TEXT)['blocks']
|
72
|
-
char_level_text_blocks = pdf_page.get_text('rawdict', flags=fitz.TEXTFLAGS_TEXT)[
|
73
|
-
'blocks'
|
74
|
-
]
|
75
|
-
text_blocks = combine_chars_to_pymudict(text_raw_blocks, char_level_text_blocks)
|
76
|
-
text_blocks = replace_equations_in_textblock(
|
77
|
-
text_blocks, inline_equations, interline_equations
|
78
|
-
)
|
79
|
-
text_blocks = remove_citation_marker(text_blocks)
|
80
|
-
text_blocks = remove_chars_in_text_blocks(text_blocks)
|
81
|
-
spans = []
|
82
|
-
for v in text_blocks:
|
83
|
-
for line in v['lines']:
|
84
|
-
for span in line['spans']:
|
85
|
-
bbox = span['bbox']
|
86
|
-
if float_equal(bbox[0], bbox[2]) or float_equal(bbox[1], bbox[3]):
|
87
|
-
continue
|
88
|
-
if span.get('type') not in (
|
89
|
-
ContentType.InlineEquation,
|
90
|
-
ContentType.InterlineEquation,
|
91
|
-
):
|
92
|
-
spans.append(
|
93
|
-
{
|
94
|
-
'bbox': list(span['bbox']),
|
95
|
-
'content': __replace_STX_ETX(span['text']),
|
96
|
-
'type': ContentType.Text,
|
97
|
-
'score': 1.0,
|
98
|
-
}
|
99
|
-
)
|
100
|
-
return spans
|
101
|
-
|
102
|
-
|
103
|
-
def replace_text_span(pymu_spans, ocr_spans):
|
104
|
-
return list(filter(lambda x: x['type'] != ContentType.Text, ocr_spans)) + pymu_spans
|
105
|
-
|
106
|
-
|
107
|
-
def parse_page_core(
|
108
|
-
pdf_docs, magic_model, page_id, pdf_bytes_md5, imageWriter, parse_mode
|
109
|
-
):
|
110
|
-
need_drop = False
|
111
|
-
drop_reason = []
|
112
|
-
|
113
|
-
"""从magic_model对象中获取后面会用到的区块信息"""
|
114
|
-
img_blocks = magic_model.get_imgs(page_id)
|
115
|
-
table_blocks = magic_model.get_tables(page_id)
|
116
|
-
discarded_blocks = magic_model.get_discarded(page_id)
|
117
|
-
text_blocks = magic_model.get_text_blocks(page_id)
|
118
|
-
title_blocks = magic_model.get_title_blocks(page_id)
|
119
|
-
inline_equations, interline_equations, interline_equation_blocks = (
|
120
|
-
magic_model.get_equations(page_id)
|
121
|
-
)
|
122
|
-
|
123
|
-
page_w, page_h = magic_model.get_page_size(page_id)
|
124
|
-
|
125
|
-
spans = magic_model.get_all_spans(page_id)
|
126
|
-
|
127
|
-
"""根据parse_mode,构造spans"""
|
128
|
-
if parse_mode == 'txt':
|
129
|
-
"""ocr 中文本类的 span 用 pymu spans 替换!"""
|
130
|
-
pymu_spans = txt_spans_extract(
|
131
|
-
pdf_docs[page_id], inline_equations, interline_equations
|
132
|
-
)
|
133
|
-
spans = replace_text_span(pymu_spans, spans)
|
134
|
-
elif parse_mode == 'ocr':
|
135
|
-
pass
|
136
|
-
else:
|
137
|
-
raise Exception('parse_mode must be txt or ocr')
|
138
|
-
|
139
|
-
"""删除重叠spans中置信度较低的那些"""
|
140
|
-
spans, dropped_spans_by_confidence = remove_overlaps_low_confidence_spans(spans)
|
141
|
-
"""删除重叠spans中较小的那些"""
|
142
|
-
spans, dropped_spans_by_span_overlap = remove_overlaps_min_spans(spans)
|
143
|
-
"""对image和table截图"""
|
144
|
-
spans = ocr_cut_image_and_table(
|
145
|
-
spans, pdf_docs[page_id], page_id, pdf_bytes_md5, imageWriter
|
146
|
-
)
|
147
|
-
|
148
|
-
"""将所有区块的bbox整理到一起"""
|
149
|
-
# interline_equation_blocks参数不够准,后面切换到interline_equations上
|
150
|
-
interline_equation_blocks = []
|
151
|
-
if len(interline_equation_blocks) > 0:
|
152
|
-
all_bboxes, all_discarded_blocks, drop_reasons = (
|
153
|
-
ocr_prepare_bboxes_for_layout_split(
|
154
|
-
img_blocks,
|
155
|
-
table_blocks,
|
156
|
-
discarded_blocks,
|
157
|
-
text_blocks,
|
158
|
-
title_blocks,
|
159
|
-
interline_equation_blocks,
|
160
|
-
page_w,
|
161
|
-
page_h,
|
162
|
-
)
|
163
|
-
)
|
164
|
-
else:
|
165
|
-
all_bboxes, all_discarded_blocks, drop_reasons = (
|
166
|
-
ocr_prepare_bboxes_for_layout_split(
|
167
|
-
img_blocks,
|
168
|
-
table_blocks,
|
169
|
-
discarded_blocks,
|
170
|
-
text_blocks,
|
171
|
-
title_blocks,
|
172
|
-
interline_equations,
|
173
|
-
page_w,
|
174
|
-
page_h,
|
175
|
-
)
|
176
|
-
)
|
177
|
-
|
178
|
-
if len(drop_reasons) > 0:
|
179
|
-
need_drop = True
|
180
|
-
drop_reason.append(DropReason.OVERLAP_BLOCKS_CAN_NOT_SEPARATION)
|
181
|
-
|
182
|
-
"""先处理不需要排版的discarded_blocks"""
|
183
|
-
discarded_block_with_spans, spans = fill_spans_in_blocks(
|
184
|
-
all_discarded_blocks, spans, 0.4
|
185
|
-
)
|
186
|
-
fix_discarded_blocks = fix_discarded_block(discarded_block_with_spans)
|
187
|
-
|
188
|
-
"""如果当前页面没有bbox则跳过"""
|
189
|
-
if len(all_bboxes) == 0:
|
190
|
-
logger.warning(f'skip this page, not found useful bbox, page_id: {page_id}')
|
191
|
-
return ocr_construct_page_component_v2(
|
192
|
-
[],
|
193
|
-
[],
|
194
|
-
page_id,
|
195
|
-
page_w,
|
196
|
-
page_h,
|
197
|
-
[],
|
198
|
-
[],
|
199
|
-
[],
|
200
|
-
interline_equations,
|
201
|
-
fix_discarded_blocks,
|
202
|
-
need_drop,
|
203
|
-
drop_reason,
|
204
|
-
)
|
205
|
-
|
206
|
-
"""在切分之前,先检查一下bbox是否有左右重叠的情况,如果有,那么就认为这个pdf暂时没有能力处理好,这种左右重叠的情况大概率是由于pdf里的行间公式、表格没有被正确识别出来造成的 """
|
207
|
-
|
208
|
-
while True: # 循环检查左右重叠的情况,如果存在就删除掉较小的那个bbox,直到不存在左右重叠的情况
|
209
|
-
is_useful_block_horz_overlap, all_bboxes = (
|
210
|
-
remove_horizontal_overlap_block_which_smaller(all_bboxes)
|
211
|
-
)
|
212
|
-
if is_useful_block_horz_overlap:
|
213
|
-
need_drop = True
|
214
|
-
drop_reason.append(DropReason.USEFUL_BLOCK_HOR_OVERLAP)
|
215
|
-
else:
|
216
|
-
break
|
217
|
-
|
218
|
-
"""根据区块信息计算layout"""
|
219
|
-
page_boundry = [0, 0, page_w, page_h]
|
220
|
-
layout_bboxes, layout_tree = get_bboxes_layout(all_bboxes, page_boundry, page_id)
|
221
|
-
|
222
|
-
if len(text_blocks) > 0 and len(all_bboxes) > 0 and len(layout_bboxes) == 0:
|
223
|
-
logger.warning(
|
224
|
-
f'skip this page, page_id: {page_id}, reason: {DropReason.CAN_NOT_DETECT_PAGE_LAYOUT}'
|
225
|
-
)
|
226
|
-
need_drop = True
|
227
|
-
drop_reason.append(DropReason.CAN_NOT_DETECT_PAGE_LAYOUT)
|
228
|
-
|
229
|
-
"""以下去掉复杂的布局和超过2列的布局"""
|
230
|
-
if any(
|
231
|
-
[lay['layout_label'] == LAYOUT_UNPROC for lay in layout_bboxes]
|
232
|
-
): # 复杂的布局
|
233
|
-
logger.warning(
|
234
|
-
f'skip this page, page_id: {page_id}, reason: {DropReason.COMPLICATED_LAYOUT}'
|
235
|
-
)
|
236
|
-
need_drop = True
|
237
|
-
drop_reason.append(DropReason.COMPLICATED_LAYOUT)
|
238
|
-
|
239
|
-
layout_column_width = get_columns_cnt_of_layout(layout_tree)
|
240
|
-
if layout_column_width > 2: # 去掉超过2列的布局pdf
|
241
|
-
logger.warning(
|
242
|
-
f'skip this page, page_id: {page_id}, reason: {DropReason.TOO_MANY_LAYOUT_COLUMNS}'
|
243
|
-
)
|
244
|
-
need_drop = True
|
245
|
-
drop_reason.append(DropReason.TOO_MANY_LAYOUT_COLUMNS)
|
246
|
-
|
247
|
-
"""根据layout顺序,对当前页面所有需要留下的block进行排序"""
|
248
|
-
sorted_blocks = sort_blocks_by_layout(all_bboxes, layout_bboxes)
|
249
|
-
|
250
|
-
"""将span填入排好序的blocks中"""
|
251
|
-
block_with_spans, spans = fill_spans_in_blocks(sorted_blocks, spans, 0.3)
|
252
|
-
|
253
|
-
"""对block进行fix操作"""
|
254
|
-
fix_blocks = fix_block_spans(block_with_spans, img_blocks, table_blocks)
|
255
|
-
|
256
|
-
"""获取QA需要外置的list"""
|
257
|
-
images, tables, interline_equations = get_qa_need_list_v2(fix_blocks)
|
258
|
-
|
259
|
-
"""构造pdf_info_dict"""
|
260
|
-
page_info = ocr_construct_page_component_v2(
|
261
|
-
fix_blocks,
|
262
|
-
layout_bboxes,
|
263
|
-
page_id,
|
264
|
-
page_w,
|
265
|
-
page_h,
|
266
|
-
layout_tree,
|
267
|
-
images,
|
268
|
-
tables,
|
269
|
-
interline_equations,
|
270
|
-
fix_discarded_blocks,
|
271
|
-
need_drop,
|
272
|
-
drop_reason,
|
273
|
-
)
|
274
|
-
return page_info
|
275
|
-
|
276
|
-
|
277
|
-
def pdf_parse_union(
|
278
|
-
pdf_bytes,
|
279
|
-
model_list,
|
280
|
-
imageWriter,
|
281
|
-
parse_mode,
|
282
|
-
start_page_id=0,
|
283
|
-
end_page_id=None,
|
284
|
-
debug_mode=False,
|
285
|
-
):
|
286
|
-
pdf_bytes_md5 = compute_md5(pdf_bytes)
|
287
|
-
pdf_docs = fitz.open('pdf', pdf_bytes)
|
288
|
-
|
289
|
-
"""初始化空的pdf_info_dict"""
|
290
|
-
pdf_info_dict = {}
|
291
|
-
|
292
|
-
"""用model_list和docs对象初始化magic_model"""
|
293
|
-
magic_model = MagicModel(model_list, pdf_docs)
|
294
|
-
|
295
|
-
"""根据输入的起始范围解析pdf"""
|
296
|
-
# end_page_id = end_page_id if end_page_id else len(pdf_docs) - 1
|
297
|
-
end_page_id = (
|
298
|
-
end_page_id
|
299
|
-
if end_page_id is not None and end_page_id >= 0
|
300
|
-
else len(pdf_docs) - 1
|
301
|
-
)
|
302
|
-
|
303
|
-
if end_page_id > len(pdf_docs) - 1:
|
304
|
-
logger.warning('end_page_id is out of range, use pdf_docs length')
|
305
|
-
end_page_id = len(pdf_docs) - 1
|
306
|
-
|
307
|
-
"""初始化启动时间"""
|
308
|
-
start_time = time.time()
|
309
|
-
|
310
|
-
for page_id, page in enumerate(pdf_docs):
|
311
|
-
"""debug时输出每页解析的耗时."""
|
312
|
-
if debug_mode:
|
313
|
-
time_now = time.time()
|
314
|
-
logger.info(
|
315
|
-
f'page_id: {page_id}, last_page_cost_time: {get_delta_time(start_time)}'
|
316
|
-
)
|
317
|
-
start_time = time_now
|
318
|
-
|
319
|
-
"""解析pdf中的每一页"""
|
320
|
-
if start_page_id <= page_id <= end_page_id:
|
321
|
-
page_info = parse_page_core(
|
322
|
-
pdf_docs, magic_model, page_id, pdf_bytes_md5, imageWriter, parse_mode
|
323
|
-
)
|
324
|
-
else:
|
325
|
-
page_w = page.rect.width
|
326
|
-
page_h = page.rect.height
|
327
|
-
page_info = ocr_construct_page_component_v2(
|
328
|
-
[], [], page_id, page_w, page_h, [], [], [], [], [], True, 'skip page'
|
329
|
-
)
|
330
|
-
pdf_info_dict[f'page_{page_id}'] = page_info
|
331
|
-
|
332
|
-
"""分段"""
|
333
|
-
para_split(pdf_info_dict, debug_mode=debug_mode)
|
334
|
-
|
335
|
-
"""dict转list"""
|
336
|
-
pdf_info_list = dict_to_list(pdf_info_dict)
|
337
|
-
new_pdf_info_dict = {
|
338
|
-
'pdf_info': pdf_info_list,
|
339
|
-
}
|
340
|
-
|
341
|
-
return new_pdf_info_dict
|
342
|
-
|
343
|
-
|
344
|
-
if __name__ == '__main__':
|
345
|
-
pass
|
magic_pdf/post_proc/__init__.py
DELETED
File without changes
|