convert_to_train_format.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. def convert_to_train_format(jso: dict) -> []:
  2. pages = []
  3. for k, v in jso.items():
  4. page_idx = v["page_idx"]
  5. width, height = v["page_size"]
  6. info = {"page_info": {"page_no": page_idx, "height": height, "width": width}}
  7. bboxes: list[dict] = []
  8. for img_bbox in v["image_bboxes_with_caption"]:
  9. bbox = {"category_id": 1, "bbox": img_bbox["bbox"]}
  10. if "caption" in img_bbox:
  11. bbox["caption_bbox"] = img_bbox["caption"]
  12. bboxes.append(bbox)
  13. for tbl_bbox in v["table_bboxes_with_caption"]:
  14. bbox = {"category_id": 7, "bbox": tbl_bbox["bbox"]}
  15. if "caption" in tbl_bbox:
  16. bbox["caption_bbox"] = tbl_bbox["caption"]
  17. bboxes.append(bbox)
  18. for bbox in v["bak_page_no_bboxes"]:
  19. n_bbox = {"category_id": 4, "bbox": bbox}
  20. bboxes.append(n_bbox)
  21. for bbox in v["bak_header_bboxes"]:
  22. n_bbox = {"category_id": 3, "bbox": bbox}
  23. bboxes.append(n_bbox)
  24. for bbox in v["bak_footer_bboxes"]:
  25. n_bbox = {"category_id": 6, "bbox": bbox}
  26. bboxes.append(n_bbox)
  27. # 脚注, 目前没有看到例子
  28. for para in v["para_blocks"]:
  29. n_bbox = {"category_id": 2, "bbox": para["bbox"]}
  30. bboxes.append(n_bbox)
  31. for inline_equation in v["inline_equations"]:
  32. n_bbox = {"category_id": 13, "bbox": inline_equation["bbox"]}
  33. bboxes.append(n_bbox)
  34. for inter_equation in v["interline_equations"]:
  35. n_bbox = {"category_id": 10, "bbox": inter_equation["bbox"]}
  36. bboxes.append(n_bbox)
  37. info["bboxes"] = bboxes
  38. pages.append(info)
  39. return pages