draw_bbox.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. from magic_pdf.libs.commons import fitz # PyMuPDF
  2. from magic_pdf.libs.Constants import CROSS_PAGE
  3. from magic_pdf.libs.ocr_content_type import BlockType, CategoryId, ContentType
  4. from magic_pdf.model.magic_model import MagicModel
  5. def draw_bbox_without_number(i, bbox_list, page, rgb_config, fill_config):
  6. new_rgb = []
  7. for item in rgb_config:
  8. item = float(item) / 255
  9. new_rgb.append(item)
  10. page_data = bbox_list[i]
  11. for bbox in page_data:
  12. x0, y0, x1, y1 = bbox
  13. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  14. if fill_config:
  15. page.draw_rect(
  16. rect_coords,
  17. color=None,
  18. fill=new_rgb,
  19. fill_opacity=0.3,
  20. width=0.5,
  21. overlay=True,
  22. ) # Draw the rectangle
  23. else:
  24. page.draw_rect(
  25. rect_coords,
  26. color=new_rgb,
  27. fill=None,
  28. fill_opacity=1,
  29. width=0.5,
  30. overlay=True,
  31. ) # Draw the rectangle
  32. def draw_bbox_with_number(i, bbox_list, page, rgb_config, fill_config):
  33. new_rgb = []
  34. for item in rgb_config:
  35. item = float(item) / 255
  36. new_rgb.append(item)
  37. page_data = bbox_list[i]
  38. for j, bbox in enumerate(page_data):
  39. x0, y0, x1, y1 = bbox
  40. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  41. if fill_config:
  42. page.draw_rect(
  43. rect_coords,
  44. color=None,
  45. fill=new_rgb,
  46. fill_opacity=0.3,
  47. width=0.5,
  48. overlay=True,
  49. ) # Draw the rectangle
  50. else:
  51. page.draw_rect(
  52. rect_coords,
  53. color=new_rgb,
  54. fill=None,
  55. fill_opacity=1,
  56. width=0.5,
  57. overlay=True,
  58. ) # Draw the rectangle
  59. page.insert_text(
  60. (x0, y0 + 10), str(j + 1), fontsize=10, color=new_rgb
  61. ) # Insert the index in the top left corner of the rectangle
  62. def draw_layout_bbox(pdf_info, pdf_bytes, out_path, filename):
  63. layout_bbox_list = []
  64. dropped_bbox_list = []
  65. tables_list, tables_body_list = [], []
  66. tables_caption_list, tables_footnote_list = [], []
  67. imgs_list, imgs_body_list, imgs_caption_list = [], [], []
  68. imgs_footnote_list = []
  69. titles_list = []
  70. texts_list = []
  71. interequations_list = []
  72. for page in pdf_info:
  73. page_layout_list = []
  74. page_dropped_list = []
  75. tables, tables_body, tables_caption, tables_footnote = [], [], [], []
  76. imgs, imgs_body, imgs_caption, imgs_footnote = [], [], [], []
  77. titles = []
  78. texts = []
  79. interequations = []
  80. for layout in page['layout_bboxes']:
  81. page_layout_list.append(layout['layout_bbox'])
  82. layout_bbox_list.append(page_layout_list)
  83. for dropped_bbox in page['discarded_blocks']:
  84. page_dropped_list.append(dropped_bbox['bbox'])
  85. dropped_bbox_list.append(page_dropped_list)
  86. for block in page['para_blocks']:
  87. bbox = block['bbox']
  88. if block['type'] == BlockType.Table:
  89. tables.append(bbox)
  90. for nested_block in block['blocks']:
  91. bbox = nested_block['bbox']
  92. if nested_block['type'] == BlockType.TableBody:
  93. tables_body.append(bbox)
  94. elif nested_block['type'] == BlockType.TableCaption:
  95. tables_caption.append(bbox)
  96. elif nested_block['type'] == BlockType.TableFootnote:
  97. tables_footnote.append(bbox)
  98. elif block['type'] == BlockType.Image:
  99. imgs.append(bbox)
  100. for nested_block in block['blocks']:
  101. bbox = nested_block['bbox']
  102. if nested_block['type'] == BlockType.ImageBody:
  103. imgs_body.append(bbox)
  104. elif nested_block['type'] == BlockType.ImageCaption:
  105. imgs_caption.append(bbox)
  106. elif nested_block['type'] == BlockType.ImageFootnote:
  107. imgs_footnote.append(bbox)
  108. elif block['type'] == BlockType.Title:
  109. titles.append(bbox)
  110. elif block['type'] == BlockType.Text:
  111. texts.append(bbox)
  112. elif block['type'] == BlockType.InterlineEquation:
  113. interequations.append(bbox)
  114. tables_list.append(tables)
  115. tables_body_list.append(tables_body)
  116. tables_caption_list.append(tables_caption)
  117. tables_footnote_list.append(tables_footnote)
  118. imgs_list.append(imgs)
  119. imgs_body_list.append(imgs_body)
  120. imgs_caption_list.append(imgs_caption)
  121. imgs_footnote_list.append(imgs_footnote)
  122. titles_list.append(titles)
  123. texts_list.append(texts)
  124. interequations_list.append(interequations)
  125. pdf_docs = fitz.open('pdf', pdf_bytes)
  126. for i, page in enumerate(pdf_docs):
  127. draw_bbox_with_number(i, layout_bbox_list, page, [255, 0, 0], False)
  128. draw_bbox_without_number(i, dropped_bbox_list, page, [158, 158, 158],
  129. True)
  130. draw_bbox_without_number(i, tables_list, page, [153, 153, 0],
  131. True) # color !
  132. draw_bbox_without_number(i, tables_body_list, page, [204, 204, 0],
  133. True)
  134. draw_bbox_without_number(i, tables_caption_list, page, [255, 255, 102],
  135. True)
  136. draw_bbox_without_number(i, tables_footnote_list, page,
  137. [229, 255, 204], True)
  138. draw_bbox_without_number(i, imgs_list, page, [51, 102, 0], True)
  139. draw_bbox_without_number(i, imgs_body_list, page, [153, 255, 51], True)
  140. draw_bbox_without_number(i, imgs_caption_list, page, [102, 178, 255],
  141. True)
  142. draw_bbox_with_number(i, imgs_footnote_list, page, [255, 178, 102],
  143. True),
  144. draw_bbox_without_number(i, titles_list, page, [102, 102, 255], True)
  145. draw_bbox_without_number(i, texts_list, page, [153, 0, 76], True)
  146. draw_bbox_without_number(i, interequations_list, page, [0, 255, 0],
  147. True)
  148. # Save the PDF
  149. pdf_docs.save(f'{out_path}/{filename}_layout.pdf')
  150. def draw_span_bbox(pdf_info, pdf_bytes, out_path, filename):
  151. text_list = []
  152. inline_equation_list = []
  153. interline_equation_list = []
  154. image_list = []
  155. table_list = []
  156. dropped_list = []
  157. next_page_text_list = []
  158. next_page_inline_equation_list = []
  159. def get_span_info(span):
  160. if span['type'] == ContentType.Text:
  161. if span.get(CROSS_PAGE, False):
  162. next_page_text_list.append(span['bbox'])
  163. else:
  164. page_text_list.append(span['bbox'])
  165. elif span['type'] == ContentType.InlineEquation:
  166. if span.get(CROSS_PAGE, False):
  167. next_page_inline_equation_list.append(span['bbox'])
  168. else:
  169. page_inline_equation_list.append(span['bbox'])
  170. elif span['type'] == ContentType.InterlineEquation:
  171. page_interline_equation_list.append(span['bbox'])
  172. elif span['type'] == ContentType.Image:
  173. page_image_list.append(span['bbox'])
  174. elif span['type'] == ContentType.Table:
  175. page_table_list.append(span['bbox'])
  176. for page in pdf_info:
  177. page_text_list = []
  178. page_inline_equation_list = []
  179. page_interline_equation_list = []
  180. page_image_list = []
  181. page_table_list = []
  182. page_dropped_list = []
  183. # 将跨页的span放到移动到下一页的列表中
  184. if len(next_page_text_list) > 0:
  185. page_text_list.extend(next_page_text_list)
  186. next_page_text_list.clear()
  187. if len(next_page_inline_equation_list) > 0:
  188. page_inline_equation_list.extend(next_page_inline_equation_list)
  189. next_page_inline_equation_list.clear()
  190. # 构造dropped_list
  191. for block in page['discarded_blocks']:
  192. if block['type'] == BlockType.Discarded:
  193. for line in block['lines']:
  194. for span in line['spans']:
  195. page_dropped_list.append(span['bbox'])
  196. dropped_list.append(page_dropped_list)
  197. # 构造其余useful_list
  198. for block in page['para_blocks']:
  199. if block['type'] in [
  200. BlockType.Text,
  201. BlockType.Title,
  202. BlockType.InterlineEquation,
  203. ]:
  204. for line in block['lines']:
  205. for span in line['spans']:
  206. get_span_info(span)
  207. elif block['type'] in [BlockType.Image, BlockType.Table]:
  208. for sub_block in block['blocks']:
  209. for line in sub_block['lines']:
  210. for span in line['spans']:
  211. get_span_info(span)
  212. text_list.append(page_text_list)
  213. inline_equation_list.append(page_inline_equation_list)
  214. interline_equation_list.append(page_interline_equation_list)
  215. image_list.append(page_image_list)
  216. table_list.append(page_table_list)
  217. pdf_docs = fitz.open('pdf', pdf_bytes)
  218. for i, page in enumerate(pdf_docs):
  219. # 获取当前页面的数据
  220. draw_bbox_without_number(i, text_list, page, [255, 0, 0], False)
  221. draw_bbox_without_number(i, inline_equation_list, page, [0, 255, 0],
  222. False)
  223. draw_bbox_without_number(i, interline_equation_list, page, [0, 0, 255],
  224. False)
  225. draw_bbox_without_number(i, image_list, page, [255, 204, 0], False)
  226. draw_bbox_without_number(i, table_list, page, [204, 0, 255], False)
  227. draw_bbox_without_number(i, dropped_list, page, [158, 158, 158], False)
  228. # Save the PDF
  229. pdf_docs.save(f'{out_path}/{filename}_spans.pdf')
  230. def drow_model_bbox(model_list: list, pdf_bytes, out_path, filename):
  231. dropped_bbox_list = []
  232. tables_body_list, tables_caption_list, tables_footnote_list = [], [], []
  233. imgs_body_list, imgs_caption_list, imgs_footnote_list = [], [], []
  234. titles_list = []
  235. texts_list = []
  236. interequations_list = []
  237. pdf_docs = fitz.open('pdf', pdf_bytes)
  238. magic_model = MagicModel(model_list, pdf_docs)
  239. for i in range(len(model_list)):
  240. page_dropped_list = []
  241. tables_body, tables_caption, tables_footnote = [], [], []
  242. imgs_body, imgs_caption, imgs_footnote = [], [], []
  243. titles = []
  244. texts = []
  245. interequations = []
  246. page_info = magic_model.get_model_list(i)
  247. layout_dets = page_info['layout_dets']
  248. for layout_det in layout_dets:
  249. bbox = layout_det['bbox']
  250. if layout_det['category_id'] == CategoryId.Text:
  251. texts.append(bbox)
  252. elif layout_det['category_id'] == CategoryId.Title:
  253. titles.append(bbox)
  254. elif layout_det['category_id'] == CategoryId.TableBody:
  255. tables_body.append(bbox)
  256. elif layout_det['category_id'] == CategoryId.TableCaption:
  257. tables_caption.append(bbox)
  258. elif layout_det['category_id'] == CategoryId.TableFootnote:
  259. tables_footnote.append(bbox)
  260. elif layout_det['category_id'] == CategoryId.ImageBody:
  261. imgs_body.append(bbox)
  262. elif layout_det['category_id'] == CategoryId.ImageCaption:
  263. imgs_caption.append(bbox)
  264. elif layout_det[
  265. 'category_id'] == CategoryId.InterlineEquation_YOLO:
  266. interequations.append(bbox)
  267. elif layout_det['category_id'] == CategoryId.Abandon:
  268. page_dropped_list.append(bbox)
  269. elif layout_det['category_id'] == CategoryId.ImageFootnote:
  270. imgs_footnote.append(bbox)
  271. tables_body_list.append(tables_body)
  272. tables_caption_list.append(tables_caption)
  273. tables_footnote_list.append(tables_footnote)
  274. imgs_body_list.append(imgs_body)
  275. imgs_caption_list.append(imgs_caption)
  276. titles_list.append(titles)
  277. texts_list.append(texts)
  278. interequations_list.append(interequations)
  279. dropped_bbox_list.append(page_dropped_list)
  280. imgs_footnote_list.append(imgs_footnote)
  281. for i, page in enumerate(pdf_docs):
  282. draw_bbox_with_number(i, dropped_bbox_list, page, [158, 158, 158],
  283. True) # color !
  284. draw_bbox_with_number(i, tables_body_list, page, [204, 204, 0], True)
  285. draw_bbox_with_number(i, tables_caption_list, page, [255, 255, 102],
  286. True)
  287. draw_bbox_with_number(i, tables_footnote_list, page, [229, 255, 204],
  288. True)
  289. draw_bbox_with_number(i, imgs_body_list, page, [153, 255, 51], True)
  290. draw_bbox_with_number(i, imgs_caption_list, page, [102, 178, 255],
  291. True)
  292. draw_bbox_with_number(i, imgs_footnote_list, page, [255, 178, 102],
  293. True)
  294. draw_bbox_with_number(i, titles_list, page, [102, 102, 255], True)
  295. draw_bbox_with_number(i, texts_list, page, [153, 0, 76], True)
  296. draw_bbox_with_number(i, interequations_list, page, [0, 255, 0], True)
  297. # Save the PDF
  298. pdf_docs.save(f'{out_path}/{filename}_model.pdf')