pdf_parse_union_core_v2.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. import copy
  2. import os
  3. import statistics
  4. import time
  5. from typing import List
  6. import torch
  7. from loguru import logger
  8. from magic_pdf.config.enums import SupportedPdfParseMethod
  9. from magic_pdf.data.dataset import Dataset, PageableData
  10. from magic_pdf.libs.boxbase import calculate_overlap_area_in_bbox1_area_ratio
  11. from magic_pdf.libs.clean_memory import clean_memory
  12. from magic_pdf.libs.commons import fitz, get_delta_time
  13. from magic_pdf.libs.config_reader import get_local_layoutreader_model_dir
  14. from magic_pdf.libs.convert_utils import dict_to_list
  15. from magic_pdf.libs.drop_reason import DropReason
  16. from magic_pdf.libs.hash_utils import compute_md5
  17. from magic_pdf.libs.local_math import float_equal
  18. from magic_pdf.libs.ocr_content_type import ContentType, BlockType
  19. from magic_pdf.model.magic_model import MagicModel
  20. from magic_pdf.para.para_split_v3 import para_split
  21. from magic_pdf.pre_proc.citationmarker_remove import remove_citation_marker
  22. from magic_pdf.pre_proc.construct_page_dict import \
  23. ocr_construct_page_component_v2
  24. from magic_pdf.pre_proc.cut_image import ocr_cut_image_and_table
  25. from magic_pdf.pre_proc.equations_replace import (
  26. combine_chars_to_pymudict, remove_chars_in_text_blocks,
  27. replace_equations_in_textblock)
  28. from magic_pdf.pre_proc.ocr_detect_all_bboxes import \
  29. ocr_prepare_bboxes_for_layout_split_v2
  30. from magic_pdf.pre_proc.ocr_dict_merge import (fill_spans_in_blocks,
  31. fix_discarded_block,
  32. fix_block_spans_v2)
  33. from magic_pdf.pre_proc.ocr_span_list_modify import (
  34. get_qa_need_list_v2, remove_overlaps_low_confidence_spans,
  35. remove_overlaps_min_spans)
  36. from magic_pdf.pre_proc.resolve_bbox_conflict import \
  37. check_useful_block_horizontal_overlap
  38. def remove_horizontal_overlap_block_which_smaller(all_bboxes):
  39. useful_blocks = []
  40. for bbox in all_bboxes:
  41. useful_blocks.append({'bbox': bbox[:4]})
  42. is_useful_block_horz_overlap, smaller_bbox, bigger_bbox = (
  43. check_useful_block_horizontal_overlap(useful_blocks)
  44. )
  45. if is_useful_block_horz_overlap:
  46. logger.warning(
  47. f'skip this page, reason: {DropReason.USEFUL_BLOCK_HOR_OVERLAP}, smaller bbox is {smaller_bbox}, bigger bbox is {bigger_bbox}'
  48. ) # noqa: E501
  49. for bbox in all_bboxes.copy():
  50. if smaller_bbox == bbox[:4]:
  51. all_bboxes.remove(bbox)
  52. return is_useful_block_horz_overlap, all_bboxes
  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. Args:
  57. text_str (str): raw text
  58. Returns:
  59. _type_: replaced text
  60. """ # noqa: E501
  61. if text_str:
  62. s = text_str.replace('\u0002', "'")
  63. s = s.replace('\u0003', "'")
  64. return s
  65. return text_str
  66. def txt_spans_extract(pdf_page, inline_equations, interline_equations):
  67. text_raw_blocks = pdf_page.get_text('dict', flags=fitz.TEXTFLAGS_TEXT)['blocks']
  68. char_level_text_blocks = pdf_page.get_text('rawdict', flags=fitz.TEXTFLAGS_TEXT)[
  69. 'blocks'
  70. ]
  71. text_blocks = combine_chars_to_pymudict(text_raw_blocks, char_level_text_blocks)
  72. text_blocks = replace_equations_in_textblock(
  73. text_blocks, inline_equations, interline_equations
  74. )
  75. text_blocks = remove_citation_marker(text_blocks)
  76. text_blocks = remove_chars_in_text_blocks(text_blocks)
  77. spans = []
  78. for v in text_blocks:
  79. for line in v['lines']:
  80. for span in line['spans']:
  81. bbox = span['bbox']
  82. if float_equal(bbox[0], bbox[2]) or float_equal(bbox[1], bbox[3]):
  83. continue
  84. if span.get('type') not in (
  85. ContentType.InlineEquation,
  86. ContentType.InterlineEquation,
  87. ):
  88. spans.append(
  89. {
  90. 'bbox': list(span['bbox']),
  91. 'content': __replace_STX_ETX(span['text']),
  92. 'type': ContentType.Text,
  93. 'score': 1.0,
  94. }
  95. )
  96. return spans
  97. def replace_text_span(pymu_spans, ocr_spans):
  98. return list(filter(lambda x: x['type'] != ContentType.Text, ocr_spans)) + pymu_spans
  99. def model_init(model_name: str):
  100. from transformers import LayoutLMv3ForTokenClassification
  101. if torch.cuda.is_available():
  102. device = torch.device('cuda')
  103. if torch.cuda.is_bf16_supported():
  104. supports_bfloat16 = True
  105. else:
  106. supports_bfloat16 = False
  107. else:
  108. device = torch.device('cpu')
  109. supports_bfloat16 = False
  110. if model_name == 'layoutreader':
  111. # 检测modelscope的缓存目录是否存在
  112. layoutreader_model_dir = get_local_layoutreader_model_dir()
  113. if os.path.exists(layoutreader_model_dir):
  114. model = LayoutLMv3ForTokenClassification.from_pretrained(
  115. layoutreader_model_dir
  116. )
  117. else:
  118. logger.warning(
  119. 'local layoutreader model not exists, use online model from huggingface'
  120. )
  121. model = LayoutLMv3ForTokenClassification.from_pretrained(
  122. 'hantian/layoutreader'
  123. )
  124. # 检查设备是否支持 bfloat16
  125. if supports_bfloat16:
  126. model.bfloat16()
  127. model.to(device).eval()
  128. else:
  129. logger.error('model name not allow')
  130. exit(1)
  131. return model
  132. class ModelSingleton:
  133. _instance = None
  134. _models = {}
  135. def __new__(cls, *args, **kwargs):
  136. if cls._instance is None:
  137. cls._instance = super().__new__(cls)
  138. return cls._instance
  139. def get_model(self, model_name: str):
  140. if model_name not in self._models:
  141. self._models[model_name] = model_init(model_name=model_name)
  142. return self._models[model_name]
  143. def do_predict(boxes: List[List[int]], model) -> List[int]:
  144. from magic_pdf.model.v3.helpers import (boxes2inputs, parse_logits,
  145. prepare_inputs)
  146. inputs = boxes2inputs(boxes)
  147. inputs = prepare_inputs(inputs, model)
  148. logits = model(**inputs).logits.cpu().squeeze(0)
  149. return parse_logits(logits, len(boxes))
  150. def cal_block_index(fix_blocks, sorted_bboxes):
  151. if sorted_bboxes is not None:
  152. # 使用layoutreader排序
  153. for block in fix_blocks:
  154. line_index_list = []
  155. if len(block['lines']) == 0:
  156. block['index'] = sorted_bboxes.index(block['bbox'])
  157. else:
  158. for line in block['lines']:
  159. line['index'] = sorted_bboxes.index(line['bbox'])
  160. line_index_list.append(line['index'])
  161. median_value = statistics.median(line_index_list)
  162. block['index'] = median_value
  163. # 删除图表body block中的虚拟line信息, 并用real_lines信息回填
  164. if block['type'] in [BlockType.ImageBody, BlockType.TableBody]:
  165. block['virtual_lines'] = copy.deepcopy(block['lines'])
  166. block['lines'] = copy.deepcopy(block['real_lines'])
  167. del block['real_lines']
  168. else:
  169. # 使用xycut排序
  170. block_bboxes = []
  171. for block in fix_blocks:
  172. block_bboxes.append(block['bbox'])
  173. # 删除图表body block中的虚拟line信息, 并用real_lines信息回填
  174. if block['type'] in [BlockType.ImageBody, BlockType.TableBody]:
  175. block['virtual_lines'] = copy.deepcopy(block['lines'])
  176. block['lines'] = copy.deepcopy(block['real_lines'])
  177. del block['real_lines']
  178. import numpy as np
  179. from magic_pdf.model.v3.xycut import recursive_xy_cut
  180. random_boxes = np.array(block_bboxes)
  181. np.random.shuffle(random_boxes)
  182. res = []
  183. recursive_xy_cut(np.asarray(random_boxes).astype(int), np.arange(len(block_bboxes)), res)
  184. assert len(res) == len(block_bboxes)
  185. sorted_boxes = random_boxes[np.array(res)].tolist()
  186. for i, block in enumerate(fix_blocks):
  187. block['index'] = sorted_boxes.index(block['bbox'])
  188. # 生成line index
  189. sorted_blocks = sorted(fix_blocks, key=lambda b: b['index'])
  190. line_inedx = 1
  191. for block in sorted_blocks:
  192. for line in block['lines']:
  193. line['index'] = line_inedx
  194. line_inedx += 1
  195. return fix_blocks
  196. def insert_lines_into_block(block_bbox, line_height, page_w, page_h):
  197. # block_bbox是一个元组(x0, y0, x1, y1),其中(x0, y0)是左下角坐标,(x1, y1)是右上角坐标
  198. x0, y0, x1, y1 = block_bbox
  199. block_height = y1 - y0
  200. block_weight = x1 - x0
  201. # 如果block高度小于n行正文,则直接返回block的bbox
  202. if line_height * 3 < block_height:
  203. if (
  204. block_height > page_h * 0.25 and page_w * 0.5 > block_weight > page_w * 0.25
  205. ): # 可能是双列结构,可以切细点
  206. lines = int(block_height / line_height) + 1
  207. else:
  208. # 如果block的宽度超过0.4页面宽度,则将block分成3行(是一种复杂布局,图不能切的太细)
  209. if block_weight > page_w * 0.4:
  210. line_height = (y1 - y0) / 3
  211. lines = 3
  212. elif block_weight > page_w * 0.25: # (可能是三列结构,也切细点)
  213. lines = int(block_height / line_height) + 1
  214. else: # 判断长宽比
  215. if block_height / block_weight > 1.2: # 细长的不分
  216. return [[x0, y0, x1, y1]]
  217. else: # 不细长的还是分成两行
  218. line_height = (y1 - y0) / 2
  219. lines = 2
  220. # 确定从哪个y位置开始绘制线条
  221. current_y = y0
  222. # 用于存储线条的位置信息[(x0, y), ...]
  223. lines_positions = []
  224. for i in range(lines):
  225. lines_positions.append([x0, current_y, x1, current_y + line_height])
  226. current_y += line_height
  227. return lines_positions
  228. else:
  229. return [[x0, y0, x1, y1]]
  230. def sort_lines_by_model(fix_blocks, page_w, page_h, line_height):
  231. page_line_list = []
  232. for block in fix_blocks:
  233. if block['type'] in [
  234. BlockType.Text, BlockType.Title, BlockType.InterlineEquation,
  235. BlockType.ImageCaption, BlockType.ImageFootnote,
  236. BlockType.TableCaption, BlockType.TableFootnote
  237. ]:
  238. if len(block['lines']) == 0:
  239. bbox = block['bbox']
  240. lines = insert_lines_into_block(bbox, line_height, page_w, page_h)
  241. for line in lines:
  242. block['lines'].append({'bbox': line, 'spans': []})
  243. page_line_list.extend(lines)
  244. else:
  245. for line in block['lines']:
  246. bbox = line['bbox']
  247. page_line_list.append(bbox)
  248. elif block['type'] in [BlockType.ImageBody, BlockType.TableBody]:
  249. bbox = block['bbox']
  250. block["real_lines"] = copy.deepcopy(block['lines'])
  251. lines = insert_lines_into_block(bbox, line_height, page_w, page_h)
  252. block['lines'] = []
  253. for line in lines:
  254. block['lines'].append({'bbox': line, 'spans': []})
  255. page_line_list.extend(lines)
  256. if len(page_line_list) > 200: # layoutreader最高支持512line
  257. return None
  258. # 使用layoutreader排序
  259. x_scale = 1000.0 / page_w
  260. y_scale = 1000.0 / page_h
  261. boxes = []
  262. # logger.info(f"Scale: {x_scale}, {y_scale}, Boxes len: {len(page_line_list)}")
  263. for left, top, right, bottom in page_line_list:
  264. if left < 0:
  265. logger.warning(
  266. f'left < 0, left: {left}, right: {right}, top: {top}, bottom: {bottom}, page_w: {page_w}, page_h: {page_h}'
  267. ) # noqa: E501
  268. left = 0
  269. if right > page_w:
  270. logger.warning(
  271. f'right > page_w, left: {left}, right: {right}, top: {top}, bottom: {bottom}, page_w: {page_w}, page_h: {page_h}'
  272. ) # noqa: E501
  273. right = page_w
  274. if top < 0:
  275. logger.warning(
  276. f'top < 0, left: {left}, right: {right}, top: {top}, bottom: {bottom}, page_w: {page_w}, page_h: {page_h}'
  277. ) # noqa: E501
  278. top = 0
  279. if bottom > page_h:
  280. logger.warning(
  281. f'bottom > page_h, left: {left}, right: {right}, top: {top}, bottom: {bottom}, page_w: {page_w}, page_h: {page_h}'
  282. ) # noqa: E501
  283. bottom = page_h
  284. left = round(left * x_scale)
  285. top = round(top * y_scale)
  286. right = round(right * x_scale)
  287. bottom = round(bottom * y_scale)
  288. assert (
  289. 1000 >= right >= left >= 0 and 1000 >= bottom >= top >= 0
  290. ), f'Invalid box. right: {right}, left: {left}, bottom: {bottom}, top: {top}' # noqa: E126, E121
  291. boxes.append([left, top, right, bottom])
  292. model_manager = ModelSingleton()
  293. model = model_manager.get_model('layoutreader')
  294. with torch.no_grad():
  295. orders = do_predict(boxes, model)
  296. sorted_bboxes = [page_line_list[i] for i in orders]
  297. return sorted_bboxes
  298. def get_line_height(blocks):
  299. page_line_height_list = []
  300. for block in blocks:
  301. if block['type'] in [
  302. BlockType.Text, BlockType.Title,
  303. BlockType.ImageCaption, BlockType.ImageFootnote,
  304. BlockType.TableCaption, BlockType.TableFootnote
  305. ]:
  306. for line in block['lines']:
  307. bbox = line['bbox']
  308. page_line_height_list.append(int(bbox[3] - bbox[1]))
  309. if len(page_line_height_list) > 0:
  310. return statistics.median(page_line_height_list)
  311. else:
  312. return 10
  313. def process_groups(groups, body_key, caption_key, footnote_key):
  314. body_blocks = []
  315. caption_blocks = []
  316. footnote_blocks = []
  317. for i, group in enumerate(groups):
  318. group[body_key]['group_id'] = i
  319. body_blocks.append(group[body_key])
  320. for caption_block in group[caption_key]:
  321. caption_block['group_id'] = i
  322. caption_blocks.append(caption_block)
  323. for footnote_block in group[footnote_key]:
  324. footnote_block['group_id'] = i
  325. footnote_blocks.append(footnote_block)
  326. return body_blocks, caption_blocks, footnote_blocks
  327. def process_block_list(blocks, body_type, block_type):
  328. indices = [block['index'] for block in blocks]
  329. median_index = statistics.median(indices)
  330. body_bbox = next((block['bbox'] for block in blocks if block.get('type') == body_type), [])
  331. return {
  332. 'type': block_type,
  333. 'bbox': body_bbox,
  334. 'blocks': blocks,
  335. 'index': median_index,
  336. }
  337. def revert_group_blocks(blocks):
  338. image_groups = {}
  339. table_groups = {}
  340. new_blocks = []
  341. for block in blocks:
  342. if block['type'] in [BlockType.ImageBody, BlockType.ImageCaption, BlockType.ImageFootnote]:
  343. group_id = block['group_id']
  344. if group_id not in image_groups:
  345. image_groups[group_id] = []
  346. image_groups[group_id].append(block)
  347. elif block['type'] in [BlockType.TableBody, BlockType.TableCaption, BlockType.TableFootnote]:
  348. group_id = block['group_id']
  349. if group_id not in table_groups:
  350. table_groups[group_id] = []
  351. table_groups[group_id].append(block)
  352. else:
  353. new_blocks.append(block)
  354. for group_id, blocks in image_groups.items():
  355. new_blocks.append(process_block_list(blocks, BlockType.ImageBody, BlockType.Image))
  356. for group_id, blocks in table_groups.items():
  357. new_blocks.append(process_block_list(blocks, BlockType.TableBody, BlockType.Table))
  358. return new_blocks
  359. def remove_outside_spans(spans, all_bboxes, all_discarded_blocks):
  360. def get_block_bboxes(blocks, block_type_list):
  361. return [block[0:4] for block in blocks if block[7] in block_type_list]
  362. image_bboxes = get_block_bboxes(all_bboxes, [BlockType.ImageBody])
  363. table_bboxes = get_block_bboxes(all_bboxes, [BlockType.TableBody])
  364. other_block_type = []
  365. for block_type in BlockType.__dict__.values():
  366. if not isinstance(block_type, str):
  367. continue
  368. if block_type not in [BlockType.ImageBody, BlockType.TableBody]:
  369. other_block_type.append(block_type)
  370. other_block_bboxes = get_block_bboxes(all_bboxes, other_block_type)
  371. discarded_block_bboxes = get_block_bboxes(all_discarded_blocks, [BlockType.Discarded])
  372. new_spans = []
  373. for span in spans:
  374. span_bbox = span['bbox']
  375. span_type = span['type']
  376. if any(calculate_overlap_area_in_bbox1_area_ratio(span_bbox, block_bbox) > 0.4 for block_bbox in
  377. discarded_block_bboxes):
  378. new_spans.append(span)
  379. continue
  380. if span_type == ContentType.Image:
  381. if any(calculate_overlap_area_in_bbox1_area_ratio(span_bbox, block_bbox) > 0.5 for block_bbox in
  382. image_bboxes):
  383. new_spans.append(span)
  384. elif span_type == ContentType.Table:
  385. if any(calculate_overlap_area_in_bbox1_area_ratio(span_bbox, block_bbox) > 0.5 for block_bbox in
  386. table_bboxes):
  387. new_spans.append(span)
  388. else:
  389. if any(calculate_overlap_area_in_bbox1_area_ratio(span_bbox, block_bbox) > 0.5 for block_bbox in
  390. other_block_bboxes):
  391. new_spans.append(span)
  392. return new_spans
  393. def parse_page_core(
  394. page_doc: PageableData, magic_model, page_id, pdf_bytes_md5, imageWriter, parse_mode
  395. ):
  396. need_drop = False
  397. drop_reason = []
  398. """从magic_model对象中获取后面会用到的区块信息"""
  399. # img_blocks = magic_model.get_imgs(page_id)
  400. # table_blocks = magic_model.get_tables(page_id)
  401. img_groups = magic_model.get_imgs_v2(page_id)
  402. table_groups = magic_model.get_tables_v2(page_id)
  403. img_body_blocks, img_caption_blocks, img_footnote_blocks = process_groups(
  404. img_groups, 'image_body', 'image_caption_list', 'image_footnote_list'
  405. )
  406. table_body_blocks, table_caption_blocks, table_footnote_blocks = process_groups(
  407. table_groups, 'table_body', 'table_caption_list', 'table_footnote_list'
  408. )
  409. discarded_blocks = magic_model.get_discarded(page_id)
  410. text_blocks = magic_model.get_text_blocks(page_id)
  411. title_blocks = magic_model.get_title_blocks(page_id)
  412. inline_equations, interline_equations, interline_equation_blocks = (
  413. magic_model.get_equations(page_id)
  414. )
  415. page_w, page_h = magic_model.get_page_size(page_id)
  416. """将所有区块的bbox整理到一起"""
  417. # interline_equation_blocks参数不够准,后面切换到interline_equations上
  418. interline_equation_blocks = []
  419. if len(interline_equation_blocks) > 0:
  420. all_bboxes, all_discarded_blocks = ocr_prepare_bboxes_for_layout_split_v2(
  421. img_body_blocks, img_caption_blocks, img_footnote_blocks,
  422. table_body_blocks, table_caption_blocks, table_footnote_blocks,
  423. discarded_blocks,
  424. text_blocks,
  425. title_blocks,
  426. interline_equation_blocks,
  427. page_w,
  428. page_h,
  429. )
  430. else:
  431. all_bboxes, all_discarded_blocks = ocr_prepare_bboxes_for_layout_split_v2(
  432. img_body_blocks, img_caption_blocks, img_footnote_blocks,
  433. table_body_blocks, table_caption_blocks, table_footnote_blocks,
  434. discarded_blocks,
  435. text_blocks,
  436. title_blocks,
  437. interline_equations,
  438. page_w,
  439. page_h,
  440. )
  441. spans = magic_model.get_all_spans(page_id)
  442. """根据parse_mode,构造spans"""
  443. if parse_mode == SupportedPdfParseMethod.TXT:
  444. """ocr 中文本类的 span 用 pymu spans 替换!"""
  445. pymu_spans = txt_spans_extract(page_doc, inline_equations, interline_equations)
  446. spans = replace_text_span(pymu_spans, spans)
  447. elif parse_mode == SupportedPdfParseMethod.OCR:
  448. pass
  449. else:
  450. raise Exception('parse_mode must be txt or ocr')
  451. """在删除重复span之前,应该通过image_body和table_body的block过滤一下image和table的span"""
  452. """顺便删除大水印并保留abandon的span"""
  453. spans = remove_outside_spans(spans, all_bboxes, all_discarded_blocks)
  454. """删除重叠spans中置信度较低的那些"""
  455. spans, dropped_spans_by_confidence = remove_overlaps_low_confidence_spans(spans)
  456. """删除重叠spans中较小的那些"""
  457. spans, dropped_spans_by_span_overlap = remove_overlaps_min_spans(spans)
  458. """对image和table截图"""
  459. spans = ocr_cut_image_and_table(
  460. spans, page_doc, page_id, pdf_bytes_md5, imageWriter
  461. )
  462. """先处理不需要排版的discarded_blocks"""
  463. discarded_block_with_spans, spans = fill_spans_in_blocks(
  464. all_discarded_blocks, spans, 0.4
  465. )
  466. fix_discarded_blocks = fix_discarded_block(discarded_block_with_spans)
  467. """如果当前页面没有bbox则跳过"""
  468. if len(all_bboxes) == 0:
  469. logger.warning(f'skip this page, not found useful bbox, page_id: {page_id}')
  470. return ocr_construct_page_component_v2(
  471. [],
  472. [],
  473. page_id,
  474. page_w,
  475. page_h,
  476. [],
  477. [],
  478. [],
  479. interline_equations,
  480. fix_discarded_blocks,
  481. need_drop,
  482. drop_reason,
  483. )
  484. """将span填入blocks中"""
  485. block_with_spans, spans = fill_spans_in_blocks(all_bboxes, spans, 0.5)
  486. """对block进行fix操作"""
  487. fix_blocks = fix_block_spans_v2(block_with_spans)
  488. """获取所有line并计算正文line的高度"""
  489. line_height = get_line_height(fix_blocks)
  490. """获取所有line并对line排序"""
  491. sorted_bboxes = sort_lines_by_model(fix_blocks, page_w, page_h, line_height)
  492. """根据line的中位数算block的序列关系"""
  493. fix_blocks = cal_block_index(fix_blocks, sorted_bboxes)
  494. """将image和table的block还原回group形式参与后续流程"""
  495. fix_blocks = revert_group_blocks(fix_blocks)
  496. """重排block"""
  497. sorted_blocks = sorted(fix_blocks, key=lambda b: b['index'])
  498. """获取QA需要外置的list"""
  499. images, tables, interline_equations = get_qa_need_list_v2(sorted_blocks)
  500. """构造pdf_info_dict"""
  501. page_info = ocr_construct_page_component_v2(
  502. sorted_blocks,
  503. [],
  504. page_id,
  505. page_w,
  506. page_h,
  507. [],
  508. images,
  509. tables,
  510. interline_equations,
  511. fix_discarded_blocks,
  512. need_drop,
  513. drop_reason,
  514. )
  515. return page_info
  516. def pdf_parse_union(
  517. dataset: Dataset,
  518. model_list,
  519. imageWriter,
  520. parse_mode,
  521. start_page_id=0,
  522. end_page_id=None,
  523. debug_mode=False,
  524. ):
  525. pdf_bytes_md5 = compute_md5(dataset.data_bits())
  526. """初始化空的pdf_info_dict"""
  527. pdf_info_dict = {}
  528. """用model_list和docs对象初始化magic_model"""
  529. magic_model = MagicModel(model_list, dataset)
  530. """根据输入的起始范围解析pdf"""
  531. # end_page_id = end_page_id if end_page_id else len(pdf_docs) - 1
  532. end_page_id = (
  533. end_page_id
  534. if end_page_id is not None and end_page_id >= 0
  535. else len(dataset) - 1
  536. )
  537. if end_page_id > len(dataset) - 1:
  538. logger.warning('end_page_id is out of range, use pdf_docs length')
  539. end_page_id = len(dataset) - 1
  540. """初始化启动时间"""
  541. start_time = time.time()
  542. for page_id, page in enumerate(dataset):
  543. """debug时输出每页解析的耗时."""
  544. if debug_mode:
  545. time_now = time.time()
  546. logger.info(
  547. f'page_id: {page_id}, last_page_cost_time: {get_delta_time(start_time)}'
  548. )
  549. start_time = time_now
  550. """解析pdf中的每一页"""
  551. if start_page_id <= page_id <= end_page_id:
  552. page_info = parse_page_core(
  553. page, magic_model, page_id, pdf_bytes_md5, imageWriter, parse_mode
  554. )
  555. else:
  556. page_info = page.get_page_info()
  557. page_w = page_info.w
  558. page_h = page_info.h
  559. page_info = ocr_construct_page_component_v2(
  560. [], [], page_id, page_w, page_h, [], [], [], [], [], True, 'skip page'
  561. )
  562. pdf_info_dict[f'page_{page_id}'] = page_info
  563. """分段"""
  564. para_split(pdf_info_dict, debug_mode=debug_mode)
  565. """dict转list"""
  566. pdf_info_list = dict_to_list(pdf_info_dict)
  567. new_pdf_info_dict = {
  568. 'pdf_info': pdf_info_list,
  569. }
  570. clean_memory()
  571. return new_pdf_info_dict
  572. if __name__ == '__main__':
  573. pass