draw_bbox.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. from magic_pdf.libs.commons import fitz # PyMuPDF
  2. from magic_pdf.libs.ocr_content_type import ContentType, BlockType
  3. def draw_bbox_without_number(i, bbox_list, page, rgb_config, fill_config):
  4. new_rgb = []
  5. for item in rgb_config:
  6. item = float(item) / 255
  7. new_rgb.append(item)
  8. page_data = bbox_list[i]
  9. for bbox in page_data:
  10. x0, y0, x1, y1 = bbox
  11. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  12. if fill_config:
  13. page.draw_rect(
  14. rect_coords,
  15. color=None,
  16. fill=new_rgb,
  17. fill_opacity=0.3,
  18. width=0.5,
  19. overlay=True,
  20. ) # Draw the rectangle
  21. else:
  22. page.draw_rect(
  23. rect_coords,
  24. color=new_rgb,
  25. fill=None,
  26. fill_opacity=1,
  27. width=0.5,
  28. overlay=True,
  29. ) # Draw the rectangle
  30. def draw_bbox_with_number(i, bbox_list, page, rgb_config, fill_config):
  31. new_rgb = []
  32. for item in rgb_config:
  33. item = float(item) / 255
  34. new_rgb.append(item)
  35. page_data = bbox_list[i]
  36. for j, bbox in enumerate(page_data):
  37. x0, y0, x1, y1 = bbox
  38. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  39. if fill_config:
  40. page.draw_rect(
  41. rect_coords,
  42. color=None,
  43. fill=new_rgb,
  44. fill_opacity=0.3,
  45. width=0.5,
  46. overlay=True,
  47. ) # Draw the rectangle
  48. else:
  49. page.draw_rect(
  50. rect_coords,
  51. color=new_rgb,
  52. fill=None,
  53. fill_opacity=1,
  54. width=0.5,
  55. overlay=True,
  56. ) # Draw the rectangle
  57. page.insert_text(
  58. (x0, y0 + 10), str(j + 1), fontsize=10, color=new_rgb
  59. ) # Insert the index at the top left corner of the rectangle
  60. def draw_layout_bbox(pdf_info, pdf_bytes, out_path):
  61. layout_bbox_list = []
  62. dropped_bbox_list = []
  63. tables_list, tables_body_list, tables_caption_list, tables_footnote_list = [], [], [], []
  64. imgs_list, imgs_body_list, imgs_caption_list = [], [], []
  65. titles_list = []
  66. texts_list = []
  67. interequations_list = []
  68. for page in pdf_info:
  69. page_layout_list = []
  70. page_dropped_list = []
  71. tables, tables_body, tables_caption, tables_footnote = [], [], [], []
  72. imgs, imgs_body, imgs_caption = [], [], []
  73. titles = []
  74. texts = []
  75. interequations = []
  76. for layout in page["layout_bboxes"]:
  77. page_layout_list.append(layout["layout_bbox"])
  78. layout_bbox_list.append(page_layout_list)
  79. for dropped_bbox in page["discarded_blocks"]:
  80. page_dropped_list.append(dropped_bbox["bbox"])
  81. dropped_bbox_list.append(page_dropped_list)
  82. for block in page["para_blocks"]:
  83. bbox = block["bbox"]
  84. if block["type"] == BlockType.Table:
  85. tables.append(bbox)
  86. for nested_block in block["blocks"]:
  87. bbox = nested_block["bbox"]
  88. if nested_block["type"] == BlockType.TableBody:
  89. tables_body.append(bbox)
  90. elif nested_block["type"] == BlockType.TableCaption:
  91. tables_caption.append(bbox)
  92. elif nested_block["type"] == BlockType.TableFootnote:
  93. tables_footnote.append(bbox)
  94. elif block["type"] == BlockType.Image:
  95. imgs.append(bbox)
  96. for nested_block in block["blocks"]:
  97. bbox = nested_block["bbox"]
  98. if nested_block["type"] == BlockType.ImageBody:
  99. imgs_body.append(bbox)
  100. elif nested_block["type"] == BlockType.ImageCaption:
  101. imgs_caption.append(bbox)
  102. elif block["type"] == BlockType.Title:
  103. titles.append(bbox)
  104. elif block["type"] == BlockType.Text:
  105. texts.append(bbox)
  106. elif block["type"] == BlockType.InterlineEquation:
  107. interequations.append(bbox)
  108. tables_list.append(tables)
  109. tables_body_list.append(tables_body)
  110. tables_caption_list.append(tables_caption)
  111. tables_footnote_list.append(tables_footnote)
  112. imgs_list.append(imgs)
  113. imgs_body_list.append(imgs_body)
  114. imgs_caption_list.append(imgs_caption)
  115. titles_list.append(titles)
  116. texts_list.append(texts)
  117. interequations_list.append(interequations)
  118. pdf_docs = fitz.open("pdf", pdf_bytes)
  119. for i, page in enumerate(pdf_docs):
  120. draw_bbox_with_number(i, layout_bbox_list, page, [255, 0, 0], False)
  121. draw_bbox_without_number(i, dropped_bbox_list, page, [158, 158, 158], True)
  122. draw_bbox_without_number(i, tables_list, page, [153, 153, 0], True) # color !
  123. draw_bbox_without_number(i, tables_body_list, page, [204, 204, 0], True)
  124. draw_bbox_without_number(i, tables_caption_list, page, [255, 255, 102], True)
  125. draw_bbox_without_number(i, tables_footnote_list, page, [229, 255, 204], True)
  126. draw_bbox_without_number(i, imgs_list, page, [51, 102, 0], True)
  127. draw_bbox_without_number(i, imgs_body_list, page, [153, 255, 51], True)
  128. draw_bbox_without_number(i, imgs_caption_list, page, [102, 178, 255], True)
  129. draw_bbox_without_number(i, titles_list, page, [102, 102, 255], True)
  130. draw_bbox_without_number(i, texts_list, page, [153, 0, 76], True)
  131. draw_bbox_without_number(i, interequations_list, page, [0, 255, 0], True)
  132. # Save the PDF
  133. pdf_docs.save(f"{out_path}/layout.pdf")
  134. def draw_span_bbox(pdf_info, pdf_bytes, out_path):
  135. text_list = []
  136. inline_equation_list = []
  137. interline_equation_list = []
  138. image_list = []
  139. table_list = []
  140. dropped_list = []
  141. for page in pdf_info:
  142. page_text_list = []
  143. page_inline_equation_list = []
  144. page_interline_equation_list = []
  145. page_image_list = []
  146. page_table_list = []
  147. page_dropped_list = []
  148. # 构造dropped_list
  149. for block in page["discarded_blocks"]:
  150. if block["type"] == BlockType.Discarded:
  151. for line in block["lines"]:
  152. for span in line["spans"]:
  153. page_dropped_list.append(span["bbox"])
  154. dropped_list.append(page_dropped_list)
  155. # 构造其余useful_list
  156. for block in page["para_blocks"]:
  157. if block["type"] in [
  158. BlockType.Text,
  159. BlockType.Title,
  160. BlockType.InterlineEquation,
  161. ]:
  162. for line in block["lines"]:
  163. for span in line["spans"]:
  164. if span["type"] == ContentType.Text:
  165. page_text_list.append(span["bbox"])
  166. elif span["type"] == ContentType.InlineEquation:
  167. page_inline_equation_list.append(span["bbox"])
  168. elif span["type"] == ContentType.InterlineEquation:
  169. page_interline_equation_list.append(span["bbox"])
  170. elif span["type"] == ContentType.Image:
  171. page_image_list.append(span["bbox"])
  172. elif span["type"] == ContentType.Table:
  173. page_table_list.append(span["bbox"])
  174. elif block["type"] in [BlockType.Image, BlockType.Table]:
  175. for sub_block in block["blocks"]:
  176. for line in sub_block["lines"]:
  177. for span in line["spans"]:
  178. if span["type"] == ContentType.Text:
  179. page_text_list.append(span["bbox"])
  180. elif span["type"] == ContentType.InlineEquation:
  181. page_inline_equation_list.append(span["bbox"])
  182. elif span["type"] == ContentType.InterlineEquation:
  183. page_interline_equation_list.append(span["bbox"])
  184. elif span["type"] == ContentType.Image:
  185. page_image_list.append(span["bbox"])
  186. elif span["type"] == ContentType.Table:
  187. page_table_list.append(span["bbox"])
  188. text_list.append(page_text_list)
  189. inline_equation_list.append(page_inline_equation_list)
  190. interline_equation_list.append(page_interline_equation_list)
  191. image_list.append(page_image_list)
  192. table_list.append(page_table_list)
  193. pdf_docs = fitz.open("pdf", pdf_bytes)
  194. for i, page in enumerate(pdf_docs):
  195. # 获取当前页面的数据
  196. draw_bbox_without_number(i, text_list, page, [255, 0, 0], False)
  197. draw_bbox_without_number(i, inline_equation_list, page, [0, 255, 0], False)
  198. draw_bbox_without_number(i, interline_equation_list, page, [0, 0, 255], False)
  199. draw_bbox_without_number(i, image_list, page, [255, 204, 0], False)
  200. draw_bbox_without_number(i, table_list, page, [204, 0, 255], False)
  201. draw_bbox_without_number(i, dropped_list, page, [158, 158, 158], False)
  202. # Save the PDF
  203. pdf_docs.save(f"{out_path}/spans.pdf")