draw_bbox.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(rect_coords, color=None, fill=new_rgb, fill_opacity=0.3, width=0.5, overlay=True) # Draw the rectangle
  14. else:
  15. page.draw_rect(rect_coords, color=new_rgb, fill=None, fill_opacity=1, width=0.5, overlay=True) # Draw the rectangle
  16. def draw_bbox_with_number(i, bbox_list, page, rgb_config, fill_config):
  17. new_rgb = []
  18. for item in rgb_config:
  19. item = float(item) / 255
  20. new_rgb.append(item)
  21. page_data = bbox_list[i]
  22. for j, bbox in enumerate(page_data):
  23. x0, y0, x1, y1 = bbox
  24. rect_coords = fitz.Rect(x0, y0, x1, y1) # Define the rectangle
  25. if fill_config:
  26. page.draw_rect(rect_coords, color=None, fill=new_rgb, fill_opacity=0.3, width=0.5, overlay=True) # Draw the rectangle
  27. else:
  28. page.draw_rect(rect_coords, color=new_rgb, fill=None, fill_opacity=1, width=0.5, overlay=True) # Draw the rectangle
  29. page.insert_text((x0, y0+10), str(j + 1), fontsize=10, color=new_rgb) # Insert the index at the top left corner of the rectangle
  30. def draw_layout_bbox(pdf_info, pdf_bytes, out_path):
  31. layout_bbox_list = []
  32. blocks_bbox_list = []
  33. dropped_bbox_list = []
  34. for page in pdf_info:
  35. page_layout_list = []
  36. page_dropped_list = []
  37. page_blocks_bbox_list = []
  38. for layout in page['layout_bboxes']:
  39. page_layout_list.append(layout['layout_bbox'])
  40. layout_bbox_list.append(page_layout_list)
  41. for dropped_bbox in page['discarded_blocks']:
  42. page_dropped_list.append(dropped_bbox['bbox'])
  43. dropped_bbox_list.append(page_dropped_list)
  44. for block in page['para_blocks']:
  45. page_blocks_bbox_list.append(block['bbox'])
  46. blocks_bbox_list.append(page_blocks_bbox_list)
  47. pdf_docs = fitz.open("pdf", pdf_bytes)
  48. for i, page in enumerate(pdf_docs):
  49. draw_bbox_with_number(i, layout_bbox_list, page, [255, 0, 0], False)
  50. draw_bbox_without_number(i, dropped_bbox_list, page, [0, 255, 0], True)
  51. draw_bbox_without_number(i, blocks_bbox_list, page, [0, 0, 255], True)
  52. # Save the PDF
  53. pdf_docs.save(f"{out_path}/layout.pdf")
  54. def draw_span_bbox(pdf_info, pdf_bytes, out_path):
  55. text_list = []
  56. inline_equation_list = []
  57. interline_equation_list = []
  58. image_list = []
  59. table_list = []
  60. for page in pdf_info:
  61. page_text_list = []
  62. page_inline_equation_list = []
  63. page_interline_equation_list = []
  64. page_image_list = []
  65. page_table_list = []
  66. for block in page['para_blocks']:
  67. if block['type'] in [BlockType.Text, BlockType.Title, BlockType.InterlineEquation]:
  68. for line in block['lines']:
  69. for span in line['spans']:
  70. if span['type'] == ContentType.Text:
  71. page_text_list.append(span['bbox'])
  72. elif span['type'] == ContentType.InlineEquation:
  73. page_inline_equation_list.append(span['bbox'])
  74. elif span['type'] == ContentType.InterlineEquation:
  75. page_interline_equation_list.append(span['bbox'])
  76. elif span['type'] == ContentType.Image:
  77. page_image_list.append(span['bbox'])
  78. elif span['type'] == ContentType.Table:
  79. page_table_list.append(span['bbox'])
  80. elif block['type'] in [BlockType.Image, BlockType.Table]:
  81. for sub_block in block["blocks"]:
  82. for line in sub_block['lines']:
  83. for span in line['spans']:
  84. if span['type'] == ContentType.Text:
  85. page_text_list.append(span['bbox'])
  86. elif span['type'] == ContentType.InlineEquation:
  87. page_inline_equation_list.append(span['bbox'])
  88. elif span['type'] == ContentType.InterlineEquation:
  89. page_interline_equation_list.append(span['bbox'])
  90. elif span['type'] == ContentType.Image:
  91. page_image_list.append(span['bbox'])
  92. elif span['type'] == ContentType.Table:
  93. page_table_list.append(span['bbox'])
  94. text_list.append(page_text_list)
  95. inline_equation_list.append(page_inline_equation_list)
  96. interline_equation_list.append(page_interline_equation_list)
  97. image_list.append(page_image_list)
  98. table_list.append(page_table_list)
  99. pdf_docs = fitz.open("pdf", pdf_bytes)
  100. for i, page in enumerate(pdf_docs):
  101. # 获取当前页面的数据
  102. draw_bbox_without_number(i, text_list, page, [255, 0, 0], False)
  103. draw_bbox_without_number(i, inline_equation_list, page, [0, 255, 0], False)
  104. draw_bbox_without_number(i, interline_equation_list, page, [0, 0, 255], False)
  105. draw_bbox_without_number(i, image_list, page, [255, 204, 0], False)
  106. draw_bbox_without_number(i, table_list, page, [204, 0, 255], False)
  107. # Save the PDF
  108. pdf_docs.save(f"{out_path}/spans.pdf")